Add String16#contains and strstr16 methods.

These are needed for aapt to find javadoc comments that contain
"@removed" in order to skip them when printing styleable docs.

Bug: 28663748
Change-Id: I8866d2167c41e11d6c2586da369560d5815fd13e
This commit is contained in:
Michael Wright 2016-05-09 14:43:31 +01:00
parent ea41a18c93
commit 5bacef33c9
5 changed files with 71 additions and 5 deletions

View File

@ -90,7 +90,9 @@ public:
bool startsWith(const String16& prefix) const;
bool startsWith(const char16_t* prefix) const;
bool contains(const char16_t* chrs) const;
status_t makeLower();
status_t replaceAll(char16_t replaceThis,

View File

@ -29,6 +29,7 @@ size_t strlen16(const char16_t *);
size_t strnlen16(const char16_t *, size_t);
char16_t *strcpy16(char16_t *, const char16_t *);
char16_t *strncpy16(char16_t *, const char16_t *, size_t);
char16_t *strstr16(const char16_t*, const char16_t*);
// Version of comparison that supports embedded nulls.
// This is different than strncmp() because we don't stop

View File

@ -345,6 +345,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();

View File

@ -222,12 +222,17 @@ int strncmp16(const char16_t *s1, const char16_t *s2, size_t n)
char16_t ch;
int d = 0;
while ( n-- ) {
d = (int)(ch = *s1++) - (int)*s2++;
if ( d || !ch )
break;
if (n == 0) {
return 0;
}
do {
d = (int)(ch = *s1++) - (int)*s2++;
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;

View File

@ -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);
}
}