Add some more utils funcs

This commit is contained in:
Diavolo
2022-04-10 14:00:55 +02:00
parent 25a84bdb0a
commit 79d4aca33d
5 changed files with 54 additions and 38 deletions

View File

@ -89,12 +89,13 @@ namespace Utils
bool StartsWith(const std::string& haystack, const std::string& needle)
{
return (haystack.size() >= needle.size() && haystack.substr(0, needle.size()) == needle);
return haystack.find(needle) == 0; // If the pos of the first found char is 0, string starts with 'needle'
}
bool EndsWith(const std::string& haystack, const std::string& needle)
{
return (haystack.size() >= needle.size() && haystack.substr(haystack.size() - needle.size()) == needle);
if (needle.size() > haystack.size()) return false;
return std::equal(needle.rbegin(), needle.rend(), haystack.rbegin());
}
int IsSpace(int c)

View File

@ -77,10 +77,10 @@ namespace Utils
int IsSpace(int c);
std::string ToLower(std::string input);
std::string ToUpper(std::string input);
bool EndsWith(const std::string& haystack, const std::string& needle);
std::vector<std::string> Split(const std::string& str, const char delim);
void Replace(std::string& string, const std::string& find, const std::string& replace);
bool EndsWith(const std::string& haystack, const std::string& needle);
bool StartsWith(const std::string& haystack, const std::string& needle);
std::vector<std::string> Split(const std::string& str, const char delim);
std::string& LTrim(std::string& str);
std::string& RTrim(std::string& str);