start/endsWith

This commit is contained in:
mrbesen 2020-09-29 15:12:46 +02:00
parent 4316d25292
commit 6c927109ec
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
4 changed files with 43 additions and 1 deletions

View File

@ -12,4 +12,8 @@ void toLower(const std::string& in, std::string& out); //copy toLower
void toLower(std::string& in); //inplace toLower
bool endsWith(const std::string& str, const std::string& ending);
bool startsWith(const std::string& str, const std::string& start);
}

View File

@ -54,3 +54,19 @@ void mrbesen::Util::toLower(std::string& in) {
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;
}

View File

@ -9,8 +9,9 @@ int testFiles_extention();
int testUtil_Count();
int testUtil_equalsIgnoreCase();
int testUtil_toLower();
int testUtil_start_endWith();
test_t tests[] = {testFiles_parent, testFiles_file, testFiles_extention, testUtil_Count, testUtil_equalsIgnoreCase, testUtil_toLower, NULL};
test_t tests[] = {testFiles_parent, testFiles_file, testFiles_extention, testUtil_Count, testUtil_equalsIgnoreCase, testUtil_toLower, testUtil_start_endWith, NULL};
int main(int argc, char** argv) {

View File

@ -68,3 +68,24 @@ int testUtil_toLower() {
return TESTGOOD;
}
int testUtil_start_endWith() {
std::string a = "abcdefghijklm";
ASSERT(startsWith(a, ""), "");
ASSERT(startsWith(a, "a"), "");
ASSERT(!startsWith(a, "b"), "");
ASSERT(!startsWith(a, "abcdefghijklmn"), "");
ASSERT(!startsWith(a, "abcdefhhijklm"), "");
ASSERT(startsWith(a, a), "");
ASSERT(endsWith(a, ""), "");
ASSERT(endsWith(a, "m"), "");
ASSERT(!endsWith(a, "n"), "");
ASSERT(!endsWith(a, "abcdefghijklmn"), "");
ASSERT(!endsWith(a, "abcdefhhijklm"), "");
ASSERT(endsWith(a, a), "");
return TESTGOOD;
}