]> granicus.if.org Git - icu/commitdiff
ICU-8691 fix UnicodeString::startsWith(const UChar *, -1 for NUL-terminated)
authorMarkus Scherer <markus.icu@gmail.com>
Thu, 7 Jul 2011 18:21:28 +0000 (18:21 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Thu, 7 Jul 2011 18:21:28 +0000 (18:21 +0000)
X-SVN-Rev: 30292

icu4c/source/common/unicode/unistr.h
icu4c/source/test/intltest/ustrtest.cpp
icu4c/source/test/intltest/ustrtest.h

index abbbc6f6b10ea8f54853fc2297041b19312f532b..ca2e81abc2acb4dd0f688a32856252d725c3b206 100644 (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
index 068a026c3519c4a15b7c7cb55cd6e43ee56696bb..c6c90a371ca4956b811cd5877962cb9794d9e765 100644 (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()
 {
index 141be2a5b939e0a229f113c316d8a5d781959636..ddfa609b770c58ecd8f38b779d0479b564865575 100644 (file)
@@ -54,6 +54,7 @@ public:
      * Test methods startsWith and endsWith
      **/
     void TestPrefixAndSuffix(void);
+    void TestStartsWithAndEndsWithNulTerminated();
     /**
      * Test method findAndReplace
      **/