From 4892435224a2bee4ba9c0db3259ed9ad30a5d97b Mon Sep 17 00:00:00 2001 From: Michael Ow Date: Wed, 23 Feb 2011 22:21:58 +0000 Subject: [PATCH] ICU-8006 Add ucnv_isFixedWidth API and test. X-SVN-Rev: 29467 --- icu4c/source/common/ucnv.c | 16 ++++++++- icu4c/source/common/unicode/ucnv.h | 15 +++++++- icu4c/source/test/cintltst/nucnvtst.c | 49 ++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/icu4c/source/common/ucnv.c b/icu4c/source/common/ucnv.c index 5c4eb559d4..cdbf11bf52 100644 --- a/icu4c/source/common/ucnv.c +++ b/icu4c/source/common/ucnv.c @@ -1,7 +1,7 @@ /* ****************************************************************************** * -* Copyright (C) 1998-2010, International Business Machines +* Copyright (C) 1998-2011, International Business Machines * Corporation and others. All Rights Reserved. * ****************************************************************************** @@ -2882,6 +2882,20 @@ ucnv_toUCountPending(const UConverter* cnv, UErrorCode* status){ } return 0; } + +U_DRAFT UBool U_EXPORT2 +ucnv_isFixedWidth(UConverter *cnv, UErrorCode *status){ + if (U_FAILURE(*status)) { + return FALSE; + } + + if (cnv == NULL) { + *status = U_ILLEGAL_ARGUMENT_ERROR; + return FALSE; + } + + return (cnv->sharedData->staticData->minBytesPerChar == cnv->sharedData->staticData->maxBytesPerChar) ? TRUE : FALSE; +} #endif /* diff --git a/icu4c/source/common/unicode/ucnv.h b/icu4c/source/common/unicode/ucnv.h index 261e830f13..26e2e84f27 100644 --- a/icu4c/source/common/unicode/ucnv.h +++ b/icu4c/source/common/unicode/ucnv.h @@ -1,6 +1,6 @@ /* ********************************************************************** -* Copyright (C) 1999-2010, International Business Machines +* Copyright (C) 1999-2011, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * ucnv.h: @@ -1992,6 +1992,19 @@ ucnv_fromUCountPending(const UConverter* cnv, UErrorCode* status); U_STABLE int32_t U_EXPORT2 ucnv_toUCountPending(const UConverter* cnv, UErrorCode* status); + +/** + * Returns whether or not the converter has a fixed ratio of bytes per + * 16-bit char/UChar (e.g. converters that are SBCS or DBCS). + * FALSE is returned with the UErrorCode if error occurs or cnv is NULL. + * @param cnv The converter to be tested + * @param status ICU error code in/out paramter + * @return TRUE if the converter is fixed-width + * @draft ICU 4.8 + */ +U_DRAFT UBool U_EXPORT2 +ucnv_isFixedWidth(UConverter *cnv, UErrorCode *status); + #endif #endif diff --git a/icu4c/source/test/cintltst/nucnvtst.c b/icu4c/source/test/cintltst/nucnvtst.c index e3de661eb9..38ffc41c2d 100644 --- a/icu4c/source/test/cintltst/nucnvtst.c +++ b/icu4c/source/test/cintltst/nucnvtst.c @@ -97,6 +97,8 @@ static void TestJitterbug2411(void); static void TestJB5275(void); static void TestJB5275_1(void); static void TestJitterbug6175(void); + +static void TestIsFixedWidth(void); #endif static void TestInBufSizes(void); @@ -323,8 +325,9 @@ void addTestNewConvert(TestNode** root) addTest(root, &TestJitterbug2346, "tsconv/nucnvtst/TestJitterbug2346"); addTest(root, &TestJitterbug2411, "tsconv/nucnvtst/TestJitterbug2411"); addTest(root, &TestJitterbug6175, "tsconv/nucnvtst/TestJitterbug6175"); -#endif + addTest(root, &TestIsFixedWidth, "tsconv/nucnvtst/TestIsFixedWidth"); +#endif } @@ -5538,3 +5541,47 @@ static void TestJB5275(){ } ucnv_close(conv); } + +static void +TestIsFixedWidth() { + UErrorCode status = U_ZERO_ERROR; + UConverter *cnv = NULL; + int32_t i; + + const char *fixedWidth[] = { + "US-ASCII", + "UTF32", + "ibm-5478_P100-1995", + "UTF16" + }; + int32_t fixedWidthLength = 4; + + const char *notFixedWidth[] = { + "GB18030", + "UTF8", + "windows-949-2000" + }; + int32_t notFixedWidthLength = 3; + + for (i = 0; i < fixedWidthLength; i++) { + cnv = ucnv_open(fixedWidth[i], &status); + if (cnv == NULL || U_FAILURE(status)) { + log_err("Error open converter: %s - %s \n", fixedWidth[i], u_errorName(status)); + } + + if (!ucnv_isFixedWidth(cnv, &status)) { + log_err("%s is a fixedWidth converter but returned FALSE.\n", fixedWidth[i]); + } + } + + for (i = 0; i < notFixedWidthLength; i++) { + cnv = ucnv_open(notFixedWidth[i], &status); + if (cnv == NULL || U_FAILURE(status)) { + log_err("Error open converter: %s - %s \n", fixedWidth[i], u_errorName(status)); + } + + if (ucnv_isFixedWidth(cnv, &status)) { + log_err("%s is NOT a fixedWidth converter but returned TRUE.\n", fixedWidth[i]); + } + } +}