diff options
author | Elliott Hughes <enh@google.com> | 2017-12-20 09:41:00 -0800 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2017-12-20 09:42:22 -0800 |
commit | 579e682628805dd9c3f8c96765c0beb3f56f1494 (patch) | |
tree | d9f77eb889f512a8a98ce9fb6826b4810bb2dc11 /base/strings.cpp | |
parent | e1d9e7c343ac4577450b96d4de19da6f099eb154 (diff) | |
download | system_core-579e682628805dd9c3f8c96765c0beb3f56f1494.tar.gz system_core-579e682628805dd9c3f8c96765c0beb3f56f1494.tar.bz2 system_core-579e682628805dd9c3f8c96765c0beb3f56f1494.zip |
Add std::string StartsWith*/EndsWith* overloads.
We should have done this from the beginning. Thanks to Windows, we're not
going to be able to switch libbase over to std::string_view any time soon.
Bug: N/A
Test: ran tests
Change-Id: Iff2f56986e39de53f3ac484415378af17dacf26b
Diffstat (limited to 'base/strings.cpp')
-rw-r--r-- | base/strings.cpp | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/base/strings.cpp b/base/strings.cpp index bfdaf1244..a8bb2a986 100644 --- a/base/strings.cpp +++ b/base/strings.cpp @@ -91,12 +91,20 @@ bool StartsWith(const std::string& s, const char* prefix) { return strncmp(s.c_str(), prefix, strlen(prefix)) == 0; } +bool StartsWith(const std::string& s, const std::string& prefix) { + return strncmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; +} + bool StartsWithIgnoreCase(const std::string& s, const char* prefix) { return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0; } -static bool EndsWith(const std::string& s, const char* suffix, bool case_sensitive) { - size_t suffix_length = strlen(suffix); +bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix) { + return strncasecmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; +} + +static bool EndsWith(const std::string& s, const char* suffix, size_t suffix_length, + bool case_sensitive) { size_t string_length = s.size(); if (suffix_length > string_length) { return false; @@ -106,11 +114,19 @@ static bool EndsWith(const std::string& s, const char* suffix, bool case_sensiti } bool EndsWith(const std::string& s, const char* suffix) { - return EndsWith(s, suffix, true); + return EndsWith(s, suffix, strlen(suffix), true); +} + +bool EndsWith(const std::string& s, const std::string& suffix) { + return EndsWith(s, suffix.c_str(), suffix.size(), true); } bool EndsWithIgnoreCase(const std::string& s, const char* suffix) { - return EndsWith(s, suffix, false); + return EndsWith(s, suffix, strlen(suffix), false); +} + +bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix) { + return EndsWith(s, suffix.c_str(), suffix.size(), false); } bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs) { |