ICU-5572 improved UText of empty string construction

X-SVN-Rev: 25824
This commit is contained in:
Andy Heninger 2009-04-17 23:50:13 +00:00
parent dab5f92559
commit 183dadb915
3 changed files with 59 additions and 5 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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