diff options
-rw-r--r-- | include/core/SkString.h | 14 | ||||
-rw-r--r-- | src/core/SkString.cpp | 6 | ||||
-rw-r--r-- | tests/StringTest.cpp | 13 |
3 files changed, 26 insertions, 7 deletions
diff --git a/include/core/SkString.h b/include/core/SkString.h index 3fa367b859..3dd89a5c9a 100644 --- a/include/core/SkString.h +++ b/include/core/SkString.h @@ -15,9 +15,18 @@ /* Some helper functions for C strings */ -bool SkStrStartsWith(const char string[], const char prefix[]); +static bool SkStrStartsWith(const char string[], const char prefix[]) { + SkASSERT(string); + SkASSERT(prefix); + return !strncmp(string, prefix, strlen(prefix)); +} bool SkStrEndsWith(const char string[], const char suffix[]); int SkStrStartsWithOneOf(const char string[], const char prefixes[]); +static bool SkStrContains(const char string[], const char substring[]) { + SkASSERT(string); + SkASSERT(substring); + return (NULL != strstr(string, substring)); +} #define SkStrAppendS32_MaxSize 11 char* SkStrAppendS32(char buffer[], int32_t); @@ -81,6 +90,9 @@ public: bool endsWith(const char suffix[]) const { return SkStrEndsWith(fRec->data(), suffix); } + bool contains(const char substring[]) const { + return SkStrContains(fRec->data(), substring); + } friend bool operator==(const SkString& a, const SkString& b) { return a.equals(b); diff --git a/src/core/SkString.cpp b/src/core/SkString.cpp index 4658ff8cb1..0e0318b078 100644 --- a/src/core/SkString.cpp +++ b/src/core/SkString.cpp @@ -36,12 +36,6 @@ static const size_t kBufferSize = 256; /////////////////////////////////////////////////////////////////////////////// -bool SkStrStartsWith(const char string[], const char prefix[]) { - SkASSERT(string); - SkASSERT(prefix); - return !strncmp(string, prefix, strlen(prefix)); -} - bool SkStrEndsWith(const char string[], const char suffix[]) { SkASSERT(string); SkASSERT(suffix); diff --git a/tests/StringTest.cpp b/tests/StringTest.cpp index 52a038db44..ebf7006275 100644 --- a/tests/StringTest.cpp +++ b/tests/StringTest.cpp @@ -56,6 +56,19 @@ static void TestString(skiatest::Reporter* reporter) { REPORTER_ASSERT(reporter, a.equals("hello")); REPORTER_ASSERT(reporter, !a.equals("help")); + REPORTER_ASSERT(reporter, a.startsWith("hell")); + REPORTER_ASSERT(reporter, !a.startsWith( "ell")); + REPORTER_ASSERT(reporter, a.startsWith("")); + REPORTER_ASSERT(reporter, a.endsWith("llo")); + REPORTER_ASSERT(reporter, !a.endsWith("ll" )); + REPORTER_ASSERT(reporter, a.endsWith("")); + REPORTER_ASSERT(reporter, a.contains("he")); + REPORTER_ASSERT(reporter, a.contains("ll")); + REPORTER_ASSERT(reporter, a.contains("lo")); + REPORTER_ASSERT(reporter, a.contains("hello")); + REPORTER_ASSERT(reporter, !a.contains("hellohello")); + REPORTER_ASSERT(reporter, a.contains("")); + SkString e(a); SkString f("hello"); SkString g("helloz", 5); |