inplace tolower
This commit is contained in:
parent
7b561f6683
commit
4316d25292
|
@ -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
|
||||
|
||||
}
|
13
src/Util.cpp
13
src/Util.cpp
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue