#include "util.h" #include unsigned int mrbesen::Util::count(const std::string& str, char c) { size_t pos = 0; long count = -1; do { pos = str.find(c, pos); ++count; } while((pos++) != std::string::npos); return (unsigned int) count; } bool icompare_pred(unsigned char a, unsigned char b) { return std::tolower(a) == std::tolower(b); } bool mrbesen::Util::equalsIgnoreCase(const std::string& a, const std::string& b, size_t max) { size_t al = a.size(), bl = b.size(); if((al == 0 && bl == 0) || max == 0) return true; if(al != bl && (al < max || bl < max)) { return false; } if(max == std::string::npos) { return std::equal(b.begin(), b.end(), a.begin(), icompare_pred); } else { if(max > al) max = al; if(max > bl) max = bl; return std::equal(b.begin(), b.begin()+max, a.begin(), icompare_pred); } } void mrbesen::Util::toLower(const std::string& in, std::string& out) { if(&in == &out) { //wenn beide strings identisch sind, inplace funktion verwenden; toLower(out); return; } out = ""; out.reserve(in.size()); for(char c : in) { out += std::tolower(c); } } void mrbesen::Util::toLower(std::string& in) { for(size_t p = 0; p < in.size(); p++) { in[p] = std::tolower(in[p]); } } bool mrbesen::Util::endsWith(const std::string& str, const std::string& ending) { size_t es = ending.size(); size_t ss = str.size(); if(es > ss) return false; if(es == 0) return true; return str.rfind(ending) == (ss - es); } bool mrbesen::Util::startsWith(const std::string& str, const std::string& start) { if(start.size() > str.size()) return false; if(start.size() == 0) return true; return str.find(start) == 0; } bool mrbesen::Util::removeEnd(std::string& str, const std::string& ending) { if(ending.empty()) return false; if(endsWith(str, ending)) { str.resize(str.size() - ending.length()); return true; } return false; } bool mrbesen::Util::removeStart(std::string& str, const std::string& start) { if(start.empty()) return false; if(startsWith(str, start)) { str = str.substr(start.length()); return true; } return false; } bool mrbesen::Util::insertEnd(std::string& str, const std::string& ending) { if(ending.empty()) return false; if(endsWith(str, ending)) return false; str.append(ending); return true; } bool mrbesen::Util::insertStart(std::string& str, const std::string& start) { if(start.empty()) return false; if(startsWith(str, start)) return false; str = start + str; return true; }