This is a practice for operator overloading, I am well aware that some of these don't make sense. I am seeking review only on the code/practices, not the purpose.
/* (C) 2017 ZACH HILMAN
* PROPRIETARY/CONFIDENTIAL. ALL RIGHTS RESERVED
*
*
*
*
*
*/
#ifndef COMPUTERSCIENCE_ENHANCED_STRING_H
#define COMPUTERSCIENCE_ENHANCED_STRING_H
#include <string>
/// Remove the first occrance of rhs from lhs.
inline std::string operator-(const std::string& lhs, const std::string& rhs) {
auto loc = lhs.find(rhs);
if (loc != -1) return lhs.substr(0, loc) + lhs.substr(loc + rhs.size());
return std::string();
}
inline std::string& operator-=(std::string& lhs, const std::string& rhs) {
return lhs = operator-(lhs, rhs);
}
/// Repeat lhs rhs times.
inline std::string operator*(const std::string& lhs, unsigned long rhs) {
std::string out;
while (rhs --> 0) out += lhs;
return out;
}
inline std::string& operator*=(std::string& lhs, unsigned long rhs) {
return lhs = operator*(lhs, rhs);
}
/// Find the number of times rhs appears in lhs.
inline unsigned long operator/(const std::string& lhs, const std::string& rhs) {
if (rhs == "") return static_cast<unsigned long>(-1);
unsigned long out = 0;
for (unsigned long s_loc = 0, a; s_loc < lhs.size();) {
if ((a = lhs.find(rhs, s_loc)) != -1) ++out, s_loc = a + 1;
else break;
}
return out;
}
/// Remove all occurances of rhs from lhs.
inline std::string operator%(const std::string& lhs, const std::string& rhs) {
if (rhs == "") return lhs;
auto out = lhs;
std::string::size_type pos;
while ((pos = out.find(rhs)) != std::string::npos) out = out.replace(pos, rhs.size(), "");
return out;
}
inline std::string& operator%=(std::string& lhs, const std::string& rhs) {
return lhs = operator%(lhs, rhs);
}
// Subtract str from STRING_CHARSET (Not done-- will eventually be all ASCII characters).
inline std::string operator~(const std::string& str) {
static const std::string STRING_CHARSET = "";
return operator-(STRING_CHARSET, str);
}
// Find the intersection of characters of lhs and rhs.
inline std::string operator&(const std::string& lhs, const std::string& rhs) {
std::string out;
for (auto c : lhs) if (rhs.find(c) != std::string::npos) out += c;
return out;
}
inline std::string& operator&=(std::string& lhs, const std::string& rhs) {
return lhs = operator&(lhs, rhs);
}
/// Find the union of characters of lhs and rhs.
inline std::string operator|(const std::string& lhs, const std::string& rhs) {
std::string out;
for (auto c : lhs) if (out.find(c) == std::string::npos) out += c;
for (auto r : rhs) if (out.find(r) == std::string::npos) out += r;
return out;
}
inline std::string& operator|=(std::string& lhs, const std::string& rhs) {
return lhs = operator|(lhs, rhs);
}
/// Find the XOR of characters of lhs and rhs.
inline std::string operator^(const std::string& lhs, const std::string& rhs) {
std::string out;
for (auto c : lhs) if (rhs.find(c) == std::string::npos) out += c;
for (auto r : rhs) if (lhs.find(r) == std::string::npos) out += r;
return out;
}
inline std::string& operator^=(std::string& lhs, const std::string& rhs) {
return lhs = operator^(lhs, rhs);
}
/// Add rhs spaces to the front of lhs.
inline std::string operator>>(const std::string& lhs, unsigned long rhs) {
return std::string(rhs, ' ') + lhs;
}
inline std::string& operator>>=(std::string& lhs, unsigned long rhs) {
return lhs = operator>>(lhs, rhs);
}
/// Add rhs spaces to the end of lhs.
inline std::string operator<<(const std::string& lhs, unsigned long rhs) {
return lhs + std::string(rhs, ' ');
}
inline std::string& operator<<=(std::string& lhs, unsigned long rhs) {
return lhs = operator<<(lhs, rhs);
}
#endif //COMPUTERSCIENCE_ENHANCED_STRING_H