From 183dadb915e783ae27dadb8d5a5a910c76caafbe Mon Sep 17 00:00:00 2001 From: Andy Heninger Date: Fri, 17 Apr 2009 23:50:13 +0000 Subject: [PATCH] ICU-5572 improved UText of empty string construction X-SVN-Rev: 25824 --- icu4c/source/common/utext.cpp | 12 ++++++++++- icu4c/source/test/cintltst/utexttst.c | 24 ++++++++++++++++++++- icu4c/source/test/intltest/utxttest.cpp | 28 ++++++++++++++++++++++--- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/icu4c/source/common/utext.cpp b/icu4c/source/common/utext.cpp index 40266ffb86..e1ea672bab 100644 --- a/icu4c/source/common/utext.cpp +++ b/icu4c/source/common/utext.cpp @@ -1603,11 +1603,17 @@ static const struct UTextFuncs utf8Funcs = }; +static const char gEmptyString[] = {0}; + U_CAPI UText * U_EXPORT2 utext_openUTF8(UText *ut, const char *s, int64_t length, UErrorCode *status) { if(U_FAILURE(*status)) { return NULL; } + if(s==NULL && length==0) { + s = gEmptyString; + } + if(s==NULL || length<-1 || length>INT32_MAX) { *status=U_ILLEGAL_ARGUMENT_ERROR; return NULL; @@ -2570,13 +2576,17 @@ static const struct UTextFuncs ucstrFuncs = U_CDECL_END +static const UChar gEmptyUString[] = {0}; U_CAPI UText * U_EXPORT2 utext_openUChars(UText *ut, const UChar *s, int64_t length, UErrorCode *status) { if (U_FAILURE(*status)) { return NULL; } - if (length < -1 || length>INT32_MAX) { + if(s==NULL && length==0) { + s = gEmptyUString; + } + if (s==NULL || length < -1 || length>INT32_MAX) { *status = U_ILLEGAL_ARGUMENT_ERROR; return NULL; } diff --git a/icu4c/source/test/cintltst/utexttst.c b/icu4c/source/test/cintltst/utexttst.c index be5941df40..734ee85eff 100644 --- a/icu4c/source/test/cintltst/utexttst.c +++ b/icu4c/source/test/cintltst/utexttst.c @@ -1,6 +1,6 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 2005-2006, International Business Machines Corporation and + * Copyright (c) 2005-2009, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /* @@ -180,6 +180,28 @@ static void TestAPI(void) { utext_close(uta); } + { + /* + * UText opened on a NULL string with zero length + */ + UText *uta; + UChar32 c; + + status = U_ZERO_ERROR; + uta = utext_openUChars(NULL, NULL, 0, &status); + TEST_SUCCESS(status); + c = UTEXT_NEXT32(uta); + TEST_ASSERT(c == U_SENTINEL); + utext_close(uta); + + uta = utext_openUTF8(NULL, NULL, 0, &status); + TEST_SUCCESS(status); + c = UTEXT_NEXT32(uta); + TEST_ASSERT(c == U_SENTINEL); + utext_close(uta); + } + + { /* * extract diff --git a/icu4c/source/test/intltest/utxttest.cpp b/icu4c/source/test/intltest/utxttest.cpp index 6c5c568c3f..c84b3652bf 100644 --- a/icu4c/source/test/intltest/utxttest.cpp +++ b/icu4c/source/test/intltest/utxttest.cpp @@ -278,9 +278,9 @@ void UTextTest::TestString(const UnicodeString &s) { - delete []cpMap; - delete []u8Map; - delete []u8String; + delete []cpMap; + delete []u8Map; + delete []u8String; } // TestCMR test Copy, Move and Replace operations. @@ -904,6 +904,28 @@ void UTextTest::ErrorTest() TEST_ASSERT(utp == &ut); } + // Invalid parameters on open + // + { + UErrorCode status = U_ZERO_ERROR; + UText ut = UTEXT_INITIALIZER; + + utext_openUChars(&ut, NULL, 5, &status); + TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR); + + status = U_ZERO_ERROR; + utext_openUChars(&ut, NULL, -1, &status); + TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR); + + status = U_ZERO_ERROR; + utext_openUTF8(&ut, NULL, 4, &status); + TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR); + + status = U_ZERO_ERROR; + utext_openUTF8(&ut, NULL, -1, &status); + TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR); + } + // // UTF-8 with malformed sequences. // These should come through as the Unicode replacement char, \ufffd