From 5be423fddd5a700175668ff7f1d896d652bbaadc Mon Sep 17 00:00:00 2001 From: George Rhoten Date: Thu, 31 Aug 2000 23:57:00 +0000 Subject: [PATCH] ICU-463 extract with a NULL destination works properly now. X-SVN-Rev: 2411 --- icu4c/source/common/unistr.cpp | 12 ++++++----- icu4c/source/test/intltest/ustrtest.cpp | 28 +++++++++++++++++++------ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/icu4c/source/common/unistr.cpp b/icu4c/source/common/unistr.cpp index 42437b3729..eec5cf6723 100644 --- a/icu4c/source/common/unistr.cpp +++ b/icu4c/source/common/unistr.cpp @@ -1260,18 +1260,20 @@ UnicodeString::extract(UTextOffset start, &mySource, mySourceLimit, 0, TRUE, &status); } else { /* Find out the size of the target needed for the current codepage */ - char target = 0; + char targetCh = 0; int32_t size = 0; - myTargetLimit = &target + sizeof(char); - while (mySource < mySourceLimit && U_SUCCESS(status)) { - myTarget = ⌖ + myTargetLimit = &targetCh + sizeof(char); + status = U_BUFFER_OVERFLOW_ERROR; + while (mySource < mySourceLimit && status == U_BUFFER_OVERFLOW_ERROR) { + myTarget = &targetCh; + status = U_ZERO_ERROR; ucnv_fromUnicode(converter, &myTarget, myTargetLimit, &mySource, mySourceLimit, 0, TRUE, &status); size += sizeof(char); } /* Use the close at the end of the function */ - myTarget = target + (char *)size; + myTarget = target + size; } // close the converter diff --git a/icu4c/source/test/intltest/ustrtest.cpp b/icu4c/source/test/intltest/ustrtest.cpp index 73861c8bb4..521799186d 100644 --- a/icu4c/source/test/intltest/ustrtest.cpp +++ b/icu4c/source/test/intltest/ustrtest.cpp @@ -185,20 +185,36 @@ UnicodeStringTest::TestCompare() void UnicodeStringTest::TestExtract() { - UnicodeString test1("Now is the time for all good men to come to the aid of their country."); - UnicodeString test2; - UChar test3[13]; - char test4[13]; - UnicodeString test5; + UnicodeString test1("Now is the time for all good men to come to the aid of their country."); + UnicodeString test2; + UChar test3[13] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 10, 11, 12, 13}; + char test4[13] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 10, 11, 12, 13}; + UnicodeString test5; + char test6[13] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 10, 11, 12, 13}; test1.extract(11, 12, test2); test1.extract(11, 12, test3); - test1.extract(11, 12, test4); + if (test1.extract(11, 12, test4) != 12 || test4[12] != 13) { + errln("UnicodeString.extract(char *) failed to return the correct size of destination buffer."); + } test1.extractBetween(11, 23, test5); + if (test1.extract(60, 71, test6) != 9) { + errln("UnicodeString.extract() failed to return the correct size of destination buffer for end of buffer."); + } + if (test1.extract(11, 12, test6) != 12) { + errln("UnicodeString.extract() failed to return the correct size of destination buffer."); + } // convert test4 back to Unicode for comparison UnicodeString test4b(test4, 12); + if (test1.extract(11, 12, (char *)NULL) != 12) { + errln("UnicodeString.extract(NULL) failed to return the correct size of destination buffer."); + } + if (test1.extract(11, -1, test6) != 0) { + errln("UnicodeString.extract(-1) failed to stop reading the string."); + } + for (UTextOffset i = 0; i < 12; i++) { if (test1[(UTextOffset)(11 + i)] != test2[i]) { errln(UnicodeString("extracting into a UnicodeString failed at position ") + i);