Browse Source

Use more efficient algorithm for `string::startsWith()` and `endsWith()`.

tags/v2.0.0
Andrew Belt 5 years ago
parent
commit
822af9468f
1 changed files with 7 additions and 3 deletions
  1. +7
    -3
      src/string.cpp

+ 7
- 3
src/string.cpp View File

@@ -1,5 +1,5 @@
#include <cctype> // for tolower and toupper #include <cctype> // for tolower and toupper
#include <algorithm> // for transform
#include <algorithm> // for transform and equal
#include <libgen.h> // for dirname and basename #include <libgen.h> // for dirname and basename


#if defined ARCH_WIN #if defined ARCH_WIN
@@ -78,12 +78,16 @@ std::string ellipsizePrefix(const std::string& s, size_t len) {




bool startsWith(const std::string& str, const std::string& prefix) { bool startsWith(const std::string& str, const std::string& prefix) {
return str.substr(0, prefix.size()) == prefix;
if (str.size() < prefix.size())
return false;
return std::equal(prefix.begin(), prefix.end(), str.begin());
} }




bool endsWith(const std::string& str, const std::string& suffix) { bool endsWith(const std::string& str, const std::string& suffix) {
return str.substr(str.size() - suffix.size(), suffix.size()) == suffix;
if (str.size() < suffix.size())
return false;
return std::equal(suffix.begin(), suffix.end(), str.end() - suffix.size());
} }






Loading…
Cancel
Save