From f0f91649cee87d8b3f0ce121d63367d940a08e0b Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Thu, 7 Jul 2011 18:21:28 +0000 Subject: [PATCH] ICU-8691 fix UnicodeString::startsWith(const UChar *, -1 for NUL-terminated) X-SVN-Rev: 30292 --- icu4c/source/common/unicode/unistr.h | 19 ++++++++++++------- icu4c/source/test/intltest/ustrtest.cpp | 12 ++++++++++++ icu4c/source/test/intltest/ustrtest.h | 1 + 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/icu4c/source/common/unicode/unistr.h b/icu4c/source/common/unicode/unistr.h index abbbc6f6b10..ca2e81abc2a 100644 --- a/icu4c/source/common/unicode/unistr.h +++ b/icu4c/source/common/unicode/unistr.h @@ -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 diff --git a/icu4c/source/test/intltest/ustrtest.cpp b/icu4c/source/test/intltest/ustrtest.cpp index 068a026c351..c6c90a371ca 100644 --- a/icu4c/source/test/intltest/ustrtest.cpp +++ b/icu4c/source/test/intltest/ustrtest.cpp @@ -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() { diff --git a/icu4c/source/test/intltest/ustrtest.h b/icu4c/source/test/intltest/ustrtest.h index 141be2a5b93..ddfa609b770 100644 --- a/icu4c/source/test/intltest/ustrtest.h +++ b/icu4c/source/test/intltest/ustrtest.h @@ -54,6 +54,7 @@ public: * Test methods startsWith and endsWith **/ void TestPrefixAndSuffix(void); + void TestStartsWithAndEndsWithNulTerminated(); /** * Test method findAndReplace **/