diff --git a/include/support.h b/include/support.h index 19a2e76d..dcb3340a 100644 --- a/include/support.h +++ b/include/support.h @@ -155,9 +155,7 @@ void upcase(std::string &str); void lowcase(std::string &str); void strip_punctuation(std::string &str); -template -bool starts_with(const char (& pfx)[N], const std::string &str) noexcept { - return (strncmp(pfx, str.c_str(), N) == 0); -} +bool starts_with(const std::string &prefix, const std::string &str) noexcept; +bool ends_with(const std::string &suffix, const std::string &str) noexcept; #endif diff --git a/src/misc/support.cpp b/src/misc/support.cpp index 3edb1c27..02860068 100644 --- a/src/misc/support.cpp +++ b/src/misc/support.cpp @@ -64,6 +64,22 @@ void lowcase(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), tf); } +bool starts_with(const std::string &prefix, const std::string &str) noexcept +{ + const size_t n = prefix.length(); + const auto pfx = std::cbegin(prefix); + const auto txt = std::cbegin(str); + return std::equal(pfx, pfx + n, txt, txt + n); +} + +bool ends_with(const std::string &suffix, const std::string &str) noexcept +{ + const size_t n = suffix.length(); + const auto sfx = std::crbegin(suffix); + const auto txt = std::crbegin(str); + return std::equal(sfx, sfx + n, txt, txt + n); +} + void trim(std::string &str) { std::string::size_type loc = str.find_first_not_of(" \r\t\f\n"); if (loc != std::string::npos) str.erase(0,loc);