]> granicus.if.org Git - icu/commitdiff
ICU-12140 Pass StringPiece by value, return value.
authorFredrik Roubert <roubert@google.com>
Mon, 19 Sep 2016 11:07:55 +0000 (11:07 +0000)
committerFredrik Roubert <roubert@google.com>
Mon, 19 Sep 2016 11:07:55 +0000 (11:07 +0000)
R=markus.icu@gmail.com

Review URL: https://codereview.appspot.com/305010043 .

X-SVN-Rev: 39276

icu4c/source/common/bytestrieiterator.cpp
icu4c/source/common/unicode/bytestrie.h
icu4c/source/test/intltest/bytestrietest.cpp

index a567f1689cb78e2dd38202de6cf00f741a47f7af..4d04247c4933d0a7ef6255761151b923641b4767 100644 (file)
@@ -141,7 +141,6 @@ BytesTrie::Iterator::next(UErrorCode &errorCode) {
             } else {
                 pos_=skipValue(pos, node);
             }
-            sp_.set(str_->data(), str_->length());
             return TRUE;
         }
         if(maxLength_>0 && str_->length()==maxLength_) {
@@ -169,10 +168,14 @@ BytesTrie::Iterator::next(UErrorCode &errorCode) {
     }
 }
 
+StringPiece
+BytesTrie::Iterator::getString() const {
+    return str_ == NULL ? StringPiece() : str_->toStringPiece();
+}
+
 UBool
 BytesTrie::Iterator::truncateAndStop() {
     pos_=NULL;
-    sp_.set(str_->data(), str_->length());
     value_=-1;  // no real value for str
     return TRUE;
 }
@@ -201,7 +204,6 @@ BytesTrie::Iterator::branchNext(const uint8_t *pos, int32_t length, UErrorCode &
     str_->append((char)trieByte, errorCode);
     if(isFinal) {
         pos_=NULL;
-        sp_.set(str_->data(), str_->length());
         value_=value;
         return NULL;
     } else {
index b4a41698ad3454ff93271b4f647b1cd9fd0e0da2..4a30ce1c0428b596ae288bdb480c85cdd1a36ea0 100644 (file)
@@ -308,7 +308,7 @@ public:
          * @return The NUL-terminated byte sequence for the last successful next().
          * @stable ICU 4.8
          */
-        const StringPiece &getString() const { return sp_; }
+        StringPiece getString() const;
         /**
          * @return The value for the last successful next().
          * @stable ICU 4.8
@@ -327,7 +327,6 @@ public:
         int32_t initialRemainingMatchLength_;
 
         CharString *str_;
-        StringPiece sp_;
         int32_t maxLength_;
         int32_t value_;
 
index a9a1b57d5d0666298e5e8a72aac6c1c4bdb26ce5..735f33006364e43945c8b1058ed83e24a27ea5b3 100644 (file)
@@ -55,6 +55,7 @@ public:
     void TestTruncatingIteratorFromLinearMatchShort();
     void TestTruncatingIteratorFromLinearMatchLong();
     void TestIteratorFromBytes();
+    void TestFailedIterator();
 
     void checkData(const StringAndValue data[], int32_t dataLength);
     void checkData(const StringAndValue data[], int32_t dataLength, UStringTrieBuildOption buildOption);
@@ -107,6 +108,7 @@ void BytesTrieTest::runIndexedTest(int32_t index, UBool exec, const char *&name,
     TESTCASE_AUTO(TestTruncatingIteratorFromLinearMatchShort);
     TESTCASE_AUTO(TestTruncatingIteratorFromLinearMatchLong);
     TESTCASE_AUTO(TestIteratorFromBytes);
+    TESTCASE_AUTO(TestFailedIterator);
     TESTCASE_AUTO_END;
 }
 
@@ -587,6 +589,15 @@ void BytesTrieTest::TestIteratorFromBytes() {
     checkIterator(iter, data, UPRV_LENGTHOF(data));
 }
 
+void BytesTrieTest::TestFailedIterator() {
+    UErrorCode failure = U_ILLEGAL_ARGUMENT_ERROR;
+    BytesTrie::Iterator iter(NULL, 0, failure);
+    StringPiece sp = iter.getString();
+    if (!sp.empty()) {
+        errln("failed iterator returned garbage data");
+    }
+}
+
 void BytesTrieTest::checkData(const StringAndValue data[], int32_t dataLength) {
     logln("checkData(dataLength=%d, fast)", (int)dataLength);
     checkData(data, dataLength, USTRINGTRIE_BUILD_FAST);