summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorElliott Hughes2017-12-20 11:41:00 -0600
committerElliott Hughes2017-12-20 11:42:22 -0600
commit579e682628805dd9c3f8c96765c0beb3f56f1494 (patch)
treed9f77eb889f512a8a98ce9fb6826b4810bb2dc11 /base
parente1d9e7c343ac4577450b96d4de19da6f099eb154 (diff)
downloadplatform-system-core-579e682628805dd9c3f8c96765c0beb3f56f1494.tar.gz
platform-system-core-579e682628805dd9c3f8c96765c0beb3f56f1494.tar.xz
platform-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')
-rw-r--r--base/include/android-base/strings.h6
-rw-r--r--base/strings.cpp24
-rw-r--r--base/strings_test.cpp20
3 files changed, 46 insertions, 4 deletions
diff --git a/base/include/android-base/strings.h b/base/include/android-base/strings.h
index f5f5c1139..c11acb10d 100644
--- a/base/include/android-base/strings.h
+++ b/base/include/android-base/strings.h
@@ -57,12 +57,18 @@ extern template std::string Join(const std::vector<std::string>&, const std::str
57extern template std::string Join(const std::vector<const char*>&, const std::string&); 57extern template std::string Join(const std::vector<const char*>&, const std::string&);
58 58
59// Tests whether 's' starts with 'prefix'. 59// Tests whether 's' starts with 'prefix'.
60// TODO: string_view
60bool StartsWith(const std::string& s, const char* prefix); 61bool StartsWith(const std::string& s, const char* prefix);
61bool StartsWithIgnoreCase(const std::string& s, const char* prefix); 62bool StartsWithIgnoreCase(const std::string& s, const char* prefix);
63bool StartsWith(const std::string& s, const std::string& prefix);
64bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix);
62 65
63// Tests whether 's' ends with 'suffix'. 66// Tests whether 's' ends with 'suffix'.
67// TODO: string_view
64bool EndsWith(const std::string& s, const char* suffix); 68bool EndsWith(const std::string& s, const char* suffix);
65bool EndsWithIgnoreCase(const std::string& s, const char* suffix); 69bool EndsWithIgnoreCase(const std::string& s, const char* suffix);
70bool EndsWith(const std::string& s, const std::string& prefix);
71bool EndsWithIgnoreCase(const std::string& s, const std::string& prefix);
66 72
67// Tests whether 'lhs' equals 'rhs', ignoring case. 73// Tests whether 'lhs' equals 'rhs', ignoring case.
68bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs); 74bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs);
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) {
91 return strncmp(s.c_str(), prefix, strlen(prefix)) == 0; 91 return strncmp(s.c_str(), prefix, strlen(prefix)) == 0;
92} 92}
93 93
94bool StartsWith(const std::string& s, const std::string& prefix) {
95 return strncmp(s.c_str(), prefix.c_str(), prefix.size()) == 0;
96}
97
94bool StartsWithIgnoreCase(const std::string& s, const char* prefix) { 98bool StartsWithIgnoreCase(const std::string& s, const char* prefix) {
95 return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0; 99 return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0;
96} 100}
97 101
98static bool EndsWith(const std::string& s, const char* suffix, bool case_sensitive) { 102bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix) {
99 size_t suffix_length = strlen(suffix); 103 return strncasecmp(s.c_str(), prefix.c_str(), prefix.size()) == 0;
104}
105
106static bool EndsWith(const std::string& s, const char* suffix, size_t suffix_length,
107 bool case_sensitive) {
100 size_t string_length = s.size(); 108 size_t string_length = s.size();
101 if (suffix_length > string_length) { 109 if (suffix_length > string_length) {
102 return false; 110 return false;
@@ -106,11 +114,19 @@ static bool EndsWith(const std::string& s, const char* suffix, bool case_sensiti
106} 114}
107 115
108bool EndsWith(const std::string& s, const char* suffix) { 116bool EndsWith(const std::string& s, const char* suffix) {
109 return EndsWith(s, suffix, true); 117 return EndsWith(s, suffix, strlen(suffix), true);
118}
119
120bool EndsWith(const std::string& s, const std::string& suffix) {
121 return EndsWith(s, suffix.c_str(), suffix.size(), true);
110} 122}
111 123
112bool EndsWithIgnoreCase(const std::string& s, const char* suffix) { 124bool EndsWithIgnoreCase(const std::string& s, const char* suffix) {
113 return EndsWith(s, suffix, false); 125 return EndsWith(s, suffix, strlen(suffix), false);
126}
127
128bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix) {
129 return EndsWith(s, suffix.c_str(), suffix.size(), false);
114} 130}
115 131
116bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs) { 132bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs) {
diff --git a/base/strings_test.cpp b/base/strings_test.cpp
index 121197cab..b8639ead4 100644
--- a/base/strings_test.cpp
+++ b/base/strings_test.cpp
@@ -253,6 +253,26 @@ TEST(strings, EndsWithIgnoreCase_contains_prefix) {
253 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO")); 253 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO"));
254} 254}
255 255
256TEST(strings, StartsWith_std_string) {
257 ASSERT_TRUE(android::base::StartsWith("hello", std::string{"hell"}));
258 ASSERT_FALSE(android::base::StartsWith("goodbye", std::string{"hell"}));
259}
260
261TEST(strings, StartsWithIgnoreCase_std_string) {
262 ASSERT_TRUE(android::base::StartsWithIgnoreCase("HeLlO", std::string{"hell"}));
263 ASSERT_FALSE(android::base::StartsWithIgnoreCase("GoOdByE", std::string{"hell"}));
264}
265
266TEST(strings, EndsWith_std_string) {
267 ASSERT_TRUE(android::base::EndsWith("hello", std::string{"lo"}));
268 ASSERT_FALSE(android::base::EndsWith("goodbye", std::string{"lo"}));
269}
270
271TEST(strings, EndsWithIgnoreCase_std_string) {
272 ASSERT_TRUE(android::base::EndsWithIgnoreCase("HeLlO", std::string{"lo"}));
273 ASSERT_FALSE(android::base::EndsWithIgnoreCase("GoOdByE", std::string{"lo"}));
274}
275
256TEST(strings, EqualsIgnoreCase) { 276TEST(strings, EqualsIgnoreCase) {
257 ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO")); 277 ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO"));
258 ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo")); 278 ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo"));