summaryrefslogtreecommitdiffstats
path: root/libutils
diff options
context:
space:
mode:
authorMichael Wright <michaelwr@google.com>2016-05-16 15:06:22 +0100
committerMichael Wright <michaelwr@google.com>2016-05-16 15:06:22 +0100
commit2a9a993af99585af0bce6eca6d92c86370977a27 (patch)
treea91bec16022b9d29174f69418b206fe2a2af3e47 /libutils
parenta0c90085ecd2477e8ccdcc7155e92ab38a8e928c (diff)
parent8b452b876b90dcfff1dab9012b65b5e67c4531a4 (diff)
downloadcore-2a9a993af99585af0bce6eca6d92c86370977a27.tar.gz
core-2a9a993af99585af0bce6eca6d92c86370977a27.tar.bz2
core-2a9a993af99585af0bce6eca6d92c86370977a27.zip
Merge commit '8b452b876b90dcfff1dab9012b65b5e67c4531a4' into manual_merge_8b452b8
Change-Id: Iacdc2d521f669661b4979c03b0476512abdb37c7
Diffstat (limited to 'libutils')
-rw-r--r--libutils/String16.cpp5
-rw-r--r--libutils/Unicode.cpp29
-rw-r--r--libutils/tests/Unicode_test.cpp35
3 files changed, 66 insertions, 3 deletions
diff --git a/libutils/String16.cpp b/libutils/String16.cpp
index 87eda1b35..ac12d8aa7 100644
--- a/libutils/String16.cpp
+++ b/libutils/String16.cpp
@@ -344,6 +344,11 @@ bool String16::startsWith(const char16_t* prefix) const
return strncmp16(mString, prefix, ps) == 0;
}
+bool String16::contains(const char16_t* chrs) const
+{
+ return strstr16(mString, chrs) != nullptr;
+}
+
status_t String16::makeLower()
{
const size_t N = size();
diff --git a/libutils/Unicode.cpp b/libutils/Unicode.cpp
index 6f4b72176..ade896a02 100644
--- a/libutils/Unicode.cpp
+++ b/libutils/Unicode.cpp
@@ -222,11 +222,16 @@ int strncmp16(const char16_t *s1, const char16_t *s2, size_t n)
char16_t ch;
int d = 0;
- while ( n-- ) {
+ if (n == 0) {
+ return 0;
+ }
+
+ do {
d = (int)(ch = *s1++) - (int)*s2++;
- if ( d || !ch )
+ if ( d || !ch ) {
break;
- }
+ }
+ } while (--n);
return d;
}
@@ -284,6 +289,24 @@ size_t strnlen16(const char16_t *s, size_t maxlen)
return ss-s;
}
+char16_t* strstr16(const char16_t* src, const char16_t* target)
+{
+ const char16_t needle = *target++;
+ if (needle != '\0') {
+ do {
+ do {
+ if (*src == '\0') {
+ return nullptr;
+ }
+ } while (*src++ != needle);
+ } while (strcmp16(src, target) != 0);
+ src--;
+ }
+
+ return (char16_t*)src;
+}
+
+
int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2)
{
const char16_t* e1 = s1+n1;
diff --git a/libutils/tests/Unicode_test.cpp b/libutils/tests/Unicode_test.cpp
index 18c130c55..c263f75e2 100644
--- a/libutils/tests/Unicode_test.cpp
+++ b/libutils/tests/Unicode_test.cpp
@@ -29,6 +29,8 @@ protected:
virtual void TearDown() {
}
+
+ char16_t const * const kSearchString = u"I am a leaf on the wind.";
};
TEST_F(UnicodeTest, UTF8toUTF16ZeroLength) {
@@ -112,4 +114,37 @@ TEST_F(UnicodeTest, UTF8toUTF16Normal) {
<< "should be NULL terminated";
}
+TEST_F(UnicodeTest, strstr16EmptyTarget) {
+ EXPECT_EQ(strstr16(kSearchString, u""), kSearchString)
+ << "should return the original pointer";
+}
+
+TEST_F(UnicodeTest, strstr16SameString) {
+ const char16_t* result = strstr16(kSearchString, kSearchString);
+ EXPECT_EQ(kSearchString, result)
+ << "should return the original pointer";
+}
+
+TEST_F(UnicodeTest, strstr16TargetStartOfString) {
+ const char16_t* result = strstr16(kSearchString, u"I am");
+ EXPECT_EQ(kSearchString, result)
+ << "should return the original pointer";
+}
+
+
+TEST_F(UnicodeTest, strstr16TargetEndOfString) {
+ const char16_t* result = strstr16(kSearchString, u"wind.");
+ EXPECT_EQ(kSearchString+19, result);
+}
+
+TEST_F(UnicodeTest, strstr16TargetWithinString) {
+ const char16_t* result = strstr16(kSearchString, u"leaf");
+ EXPECT_EQ(kSearchString+7, result);
+}
+
+TEST_F(UnicodeTest, strstr16TargetNotPresent) {
+ const char16_t* result = strstr16(kSearchString, u"soar");
+ EXPECT_EQ(nullptr, result);
+}
+
}