string: Add a lowercase() function
This function strips out special characters and returns the lowercase version of the string. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
cc6f4c9293
commit
7733e24c07
|
@ -60,3 +60,52 @@ const std::string string :: sec2str_detailed(unsigned int sec)
|
|||
res += _time_detail(sec, 0, "second");
|
||||
return res;
|
||||
}
|
||||
|
||||
static char _to_lower(char c)
|
||||
{
|
||||
if ((c >= 'a') && (c <= 'z'))
|
||||
return c;
|
||||
if ((c >= '0') && (c <= '9'))
|
||||
return c;
|
||||
if ((c >= 'A') && (c <= 'Z'))
|
||||
return c + ('a' - 'A');
|
||||
|
||||
switch (c) {
|
||||
case '\\':
|
||||
case '/':
|
||||
case ',':
|
||||
case ';':
|
||||
case '(':
|
||||
case ')':
|
||||
case '_':
|
||||
case '-':
|
||||
case '~':
|
||||
case '+':
|
||||
case '"':
|
||||
case ' ':
|
||||
case ' ':
|
||||
return ' ';
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
const std::string string :: lowercase(const std::string &str)
|
||||
{
|
||||
std::string ret;
|
||||
char c, last = ' ';
|
||||
|
||||
for (unsigned int i = 0; i < str.size(); i++) {
|
||||
c = _to_lower(str[i]);
|
||||
|
||||
if (c > 0) {
|
||||
if ((c != ' ') || (last != ' '))
|
||||
ret += c;
|
||||
last = c;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret[ret.size() - 1] == ' ')
|
||||
return ret.substr(0, ret.size() - 1);
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -34,6 +34,14 @@ namespace string
|
|||
* months, hours, minutes, and seconds.
|
||||
*/
|
||||
const std::string sec2str_detailed(unsigned int);
|
||||
|
||||
/**
|
||||
* Convert a string to lowercase, stripping out special characters
|
||||
* along the way.
|
||||
* @param str Input string.
|
||||
* @return The same string converted to lowercase.
|
||||
*/
|
||||
const std::string lowercase(const std::string &);
|
||||
}
|
||||
|
||||
#endif /* OCARINA_CORE_STRING_H */
|
||||
|
|
|
@ -52,9 +52,28 @@ void test_sec2str_detailed()
|
|||
test_equal(string :: sec2str_detailed(2), "2 seconds");
|
||||
}
|
||||
|
||||
void test_lowercase()
|
||||
{
|
||||
test_equal(string :: lowercase(" "), "");
|
||||
test_equal(string :: lowercase(" test \
|
||||
text"), "test text");
|
||||
test_equal(string :: lowercase("test/text"), "test text");
|
||||
test_equal(string :: lowercase("Test, Text"), "test text");
|
||||
test_equal(string :: lowercase("Test? Text!"), "test text");
|
||||
test_equal(string :: lowercase("Test?123 Text!456"), "test123 text456");
|
||||
test_equal(string :: lowercase("Test?123 Text!456"), "test123 text456");
|
||||
test_equal(string :: lowercase("Test(text);123-456"), "test text 123 456");
|
||||
test_equal(string :: lowercase("Test((text));;123--456"), "test text 123 456");
|
||||
test_equal(string :: lowercase("! __test(TEXT) + __test(\"TEXT\")"),
|
||||
"test text test text");
|
||||
test_equal(string :: lowercase("Test~tEXt\\123"), "test text 123");
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
test :: run("Unsigned to String Test", test_utos);
|
||||
test :: run("Seconds to String Test", test_sec2str);
|
||||
test :: run("Seconds to String (Detailed) Test", test_sec2str_detailed);
|
||||
test :: run("String Lowercase Test", test_lowercase);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue