From 0b2bbde8109ccf1ce67f5c2e77468846cfb59663 Mon Sep 17 00:00:00 2001 From: Fredrik Roubert <roubert@google.com> Date: Mon, 19 Sep 2016 11:07:55 +0000 Subject: [PATCH] ICU-12140 Pass StringPiece by value, return value. R=markus.icu@gmail.com Review URL: https://codereview.appspot.com/305010043 . X-SVN-Rev: 39276 --- icu4c/source/common/bytestrieiterator.cpp | 8 +++++--- icu4c/source/common/unicode/bytestrie.h | 3 +-- icu4c/source/test/intltest/bytestrietest.cpp | 11 +++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/icu4c/source/common/bytestrieiterator.cpp b/icu4c/source/common/bytestrieiterator.cpp index a567f1689cb..4d04247c493 100644 --- a/icu4c/source/common/bytestrieiterator.cpp +++ b/icu4c/source/common/bytestrieiterator.cpp @@ -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 { diff --git a/icu4c/source/common/unicode/bytestrie.h b/icu4c/source/common/unicode/bytestrie.h index b4a41698ad3..4a30ce1c042 100644 --- a/icu4c/source/common/unicode/bytestrie.h +++ b/icu4c/source/common/unicode/bytestrie.h @@ -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_; diff --git a/icu4c/source/test/intltest/bytestrietest.cpp b/icu4c/source/test/intltest/bytestrietest.cpp index a9a1b57d5d0..735f3300636 100644 --- a/icu4c/source/test/intltest/bytestrietest.cpp +++ b/icu4c/source/test/intltest/bytestrietest.cpp @@ -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); -- 2.40.0