inplace tolower

This commit is contained in:
mrbesen 2020-09-29 14:12:41 +02:00
parent 7b561f6683
commit 4316d25292
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
3 changed files with 24 additions and 1 deletions

View File

@ -8,6 +8,8 @@ unsigned int count(const std::string& str, char c); //count occurances of c in s
bool equalsIgnoreCase(const std::string& a, const std::string& b, size_t max = std::string::npos);
void toLower(const std::string& in, std::string& out);
void toLower(const std::string& in, std::string& out); //copy toLower
void toLower(std::string& in); //inplace toLower
}

View File

@ -35,9 +35,22 @@ bool mrbesen::Util::equalsIgnoreCase(const std::string& a, const std::string& b,
}
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]);
}
}

View File

@ -58,5 +58,13 @@ int testUtil_toLower() {
toLower("._:DAd-", out);
ASSERT(out == "._:dad-", "");
//inplace testen
toLower(a, a);
ASSERT(a == "abc", "");
a = "ABC";
toLower(a);
ASSERT(a == "abc", "");
return TESTGOOD;
}