ICU-8691 fix UnicodeString::startsWith(const UChar *, -1 for NUL-terminated)

X-SVN-Rev: 30292
This commit is contained in:
Markus Scherer 2011-07-07 18:21:28 +00:00
parent 47a0559a84
commit f0f91649ce
3 changed files with 25 additions and 7 deletions

View File

@ -3973,15 +3973,20 @@ UnicodeString::startsWith(const UnicodeString& srcText,
{ return doCompare(0, srcLength, srcText, srcStart, srcLength) == 0; }
inline UBool
UnicodeString::startsWith(const UChar *srcChars,
int32_t srcLength) const
{ return doCompare(0, srcLength, srcChars, 0, srcLength) == 0; }
UnicodeString::startsWith(const UChar *srcChars, int32_t srcLength) const {
if(srcLength < 0) {
srcLength = u_strlen(srcChars);
}
return doCompare(0, srcLength, srcChars, 0, srcLength) == 0;
}
inline UBool
UnicodeString::startsWith(const UChar *srcChars,
int32_t srcStart,
int32_t srcLength) const
{ return doCompare(0, srcLength, srcChars, srcStart, srcLength) == 0;}
UnicodeString::startsWith(const UChar *srcChars, int32_t srcStart, int32_t srcLength) const {
if(srcLength < 0) {
srcLength = u_strlen(srcChars);
}
return doCompare(0, srcLength, srcChars, srcStart, srcLength) == 0;
}
inline UBool
UnicodeString::endsWith(const UnicodeString& text) const

View File

@ -62,6 +62,7 @@ void UnicodeStringTest::runIndexedTest( int32_t index, UBool exec, const char* &
case 20: name = "TestAppendable"; if (exec) TestAppendable(); break;
case 21: name = "TestUnicodeStringImplementsAppendable"; if (exec) TestUnicodeStringImplementsAppendable(); break;
case 22: name = "TestSizeofUnicodeString"; if (exec) TestSizeofUnicodeString(); break;
case 23: name = "TestStartsWithAndEndsWithNulTerminated"; if (exec) TestStartsWithAndEndsWithNulTerminated(); break;
default: name = ""; break; //needed to end loop
}
@ -966,6 +967,17 @@ UnicodeStringTest::TestPrefixAndSuffix()
}
}
void
UnicodeStringTest::TestStartsWithAndEndsWithNulTerminated() {
UnicodeString test("abcde");
const UChar ab[] = { 0x61, 0x62, 0 };
const UChar de[] = { 0x64, 0x65, 0 };
assertTrue("abcde.startsWith(ab, -1)", test.startsWith(ab, -1));
assertTrue("abcde.startsWith(ab, 0, -1)", test.startsWith(ab, 0, -1));
assertTrue("abcde.endsWith(de, -1)", test.endsWith(de, -1));
assertTrue("abcde.endsWith(de, 0, -1)", test.endsWith(de, 0, -1));
}
void
UnicodeStringTest::TestFindAndReplace()
{

View File

@ -54,6 +54,7 @@ public:
* Test methods startsWith and endsWith
**/
void TestPrefixAndSuffix(void);
void TestStartsWithAndEndsWithNulTerminated();
/**
* Test method findAndReplace
**/