From 41ffc2ef36f51179f667e75e3ee1b9f58dc0f81a Mon Sep 17 00:00:00 2001 From: George Rhoten Date: Thu, 28 Feb 2002 01:42:40 +0000 Subject: [PATCH] ICU-1733 Integrate some of the easier (and non-redundent) changes from Lotus. X-SVN-Rev: 7802 --- icu4c/source/common/convert.cpp | 6 +- icu4c/source/common/cstring.c | 2 +- icu4c/source/common/locid.cpp | 20 +- icu4c/source/common/ucnv_err.c | 4 +- icu4c/source/common/udatamem.h | 6 +- icu4c/source/common/uhash.c | 2 +- icu4c/source/common/unames.c | 12 +- icu4c/source/common/unicode/unistr.h | 9 +- icu4c/source/common/unistr.cpp | 20 +- icu4c/source/common/uvector.cpp | 8 +- icu4c/source/extra/ustdio/locbund.c | 18 +- icu4c/source/extra/ustdio/sprintf.c | 9 +- icu4c/source/extra/ustdio/sscanf.c | 1554 ++++++++++++------------- icu4c/source/extra/ustdio/ufile.c | 264 ++--- icu4c/source/extra/ustdio/ufmt_cmn.c | 6 +- icu4c/source/extra/ustdio/uprintf.c | 1294 +++++++++++---------- icu4c/source/extra/ustdio/uscanf.c | 1563 +++++++++++++------------- icu4c/source/i18n/choicfmt.cpp | 50 +- icu4c/source/i18n/coleitr.cpp | 2 +- icu4c/source/i18n/fmtable.cpp | 5 - icu4c/source/i18n/msgfmt.cpp | 33 +- icu4c/source/i18n/name2uni.cpp | 4 +- icu4c/source/i18n/nfrs.cpp | 194 ++-- icu4c/source/i18n/nfrule.cpp | 208 ++-- icu4c/source/i18n/nfsubs.cpp | 68 +- icu4c/source/i18n/numfmt.cpp | 120 +- icu4c/source/i18n/rbnf.cpp | 138 +-- icu4c/source/i18n/rbt_set.cpp | 6 +- icu4c/source/i18n/smpdtfmt.cpp | 2 +- icu4c/source/i18n/sortkey.cpp | 154 ++- icu4c/source/i18n/strmatch.cpp | 5 +- icu4c/source/i18n/tblcoll.cpp | 10 +- icu4c/source/i18n/transreg.cpp | 5 +- icu4c/source/i18n/ucol.cpp | 4 +- icu4c/source/i18n/ucol_cnt.cpp | 18 +- icu4c/source/i18n/ucol_cnt.h | 2 +- icu4c/source/i18n/ucol_elm.cpp | 11 +- icu4c/source/i18n/ucol_elm.h | 2 +- icu4c/source/i18n/ucol_tok.cpp | 4 +- icu4c/source/i18n/udat.cpp | 2 +- icu4c/source/i18n/unicode/coll.h | 12 +- icu4c/source/i18n/unicode/rbnf.h | 30 +- icu4c/source/i18n/unicode/sortkey.h | 231 ++-- icu4c/source/i18n/uniset.cpp | 38 +- icu4c/source/i18n/unum.cpp | 4 +- icu4c/source/i18n/utrans.cpp | 6 +- icu4c/source/tools/genrb/ufile.c | 8 - icu4c/source/tools/genrb/ustdio.c | 8 - icu4c/source/tools/gentz/gentz.cpp | 14 +- icu4c/source/tools/genuca/genuca.cpp | 4 +- icu4c/source/tools/toolutil/ucbuf.c | 72 +- 51 files changed, 3131 insertions(+), 3140 deletions(-) delete mode 100644 icu4c/source/tools/genrb/ufile.c delete mode 100644 icu4c/source/tools/genrb/ustdio.c diff --git a/icu4c/source/common/convert.cpp b/icu4c/source/common/convert.cpp index a4c16f5e1f9..d8b0c21a7b5 100644 --- a/icu4c/source/common/convert.cpp +++ b/icu4c/source/common/convert.cpp @@ -35,7 +35,7 @@ UBool UnicodeConverter_cleanup() { if (availableConverterNames) { - delete []availableConverterNames; + uprv_free(availableConverterNames); availableConverterNames = NULL; } availableConverterNamesCount = 0; @@ -429,7 +429,7 @@ UnicodeConverter::getAvailableNames(int32_t& num, UErrorCode& err) if (availableConverterNames==NULL) { int32_t count = ucnv_io_countAvailableConverters(&err); if (count > 0) { - const char **names = new const char *[count]; + const char **names = (const char **) uprv_malloc( sizeof(const char*) * count ); if (names != NULL) { ucnv_io_fillAvailableConverters(names, &err); @@ -444,7 +444,7 @@ UnicodeConverter::getAvailableNames(int32_t& num, UErrorCode& err) /* if a different thread set it first, then delete the extra data */ if (names != 0) { - delete [] names; + uprv_free(names); } } else { num = 0; diff --git a/icu4c/source/common/cstring.c b/icu4c/source/common/cstring.c index 361320593bf..92be9bda2ba 100644 --- a/icu4c/source/common/cstring.c +++ b/icu4c/source/common/cstring.c @@ -178,7 +178,7 @@ T_CString_strnicmp(const char *str1, const char *str2, uint32_t n) { U_CAPI char* U_EXPORT2 uprv_strdup(const char *src) { size_t len = strlen(src) + 1; - char *dup = (char *) malloc(len); + char *dup = (char *) uprv_malloc(len); if (dup) { uprv_memcpy(dup, src, len); diff --git a/icu4c/source/common/locid.cpp b/icu4c/source/common/locid.cpp index 955d7e43f9b..b90b85e19e2 100644 --- a/icu4c/source/common/locid.cpp +++ b/icu4c/source/common/locid.cpp @@ -206,10 +206,10 @@ Locale::LocaleProxy::operator const Locale&(void) const Locale::~Locale() { - /*if fullName is on the heap, we delete it*/ + /*if fullName is on the heap, we free it*/ if (fullName != fullNameBuffer) { - delete []fullName; + uprv_free(fullName); fullName = NULL; } } @@ -294,7 +294,7 @@ Locale::Locale( const char * newLanguage, to go to the heap for temporary buffers*/ if (size > ULOC_FULLNAME_CAPACITY) { - togo_heap = new char[size+1]; + togo_heap = (char *)uprv_malloc(sizeof(char)*(size+1)); togo = togo_heap; } else @@ -336,7 +336,9 @@ Locale::Locale( const char * newLanguage, // string. init(togo); - delete [] togo_heap; /* If it was needed */ + if (togo_heap) { + uprv_free(togo_heap); + } } } @@ -350,13 +352,13 @@ Locale &Locale::operator=(const Locale &other) { /* Free our current storage */ if(fullName != fullNameBuffer) { - delete [] fullName; + uprv_free(fullName); fullName = fullNameBuffer; } /* Allocate the full name if necessary */ if(other.fullName != other.fullNameBuffer) { - fullName = new char[(uprv_strlen(other.fullName)+1)]; + fullName = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(other.fullName)+1)); } /* Copy the full name */ @@ -382,7 +384,7 @@ Locale& Locale::init(const char* localeID) { /* Free our current storage */ if(fullName != fullNameBuffer) { - delete [] fullName; + uprv_free(fullName); fullName = fullNameBuffer; } @@ -404,7 +406,7 @@ Locale& Locale::init(const char* localeID) length = uloc_getName(localeID, fullName, sizeof(fullNameBuffer), &err); if(U_FAILURE(err) || err == U_STRING_NOT_TERMINATED_WARNING) { /*Go to heap for the fullName if necessary*/ - fullName = new char[length + 1]; + fullName = (char *)uprv_malloc(sizeof(char)*(length + 1)); if(fullName == 0) { fullName = fullNameBuffer; break; @@ -974,7 +976,7 @@ Locale::initLocaleCache(void) // // This can be a memory leak for an extra long default locale, // but this code shouldn't normally get executed. - localeCache[idx].fullName = new char[uprv_strlen(localeCache[idx].fullNameBuffer) + 1]; + localeCache[idx].fullName = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(localeCache[idx].fullNameBuffer) + 1)); uprv_strcpy(localeCache[idx].fullName, localeCache[idx].fullNameBuffer); } } diff --git a/icu4c/source/common/ucnv_err.c b/icu4c/source/common/ucnv_err.c index fa2754833c1..e45206e6cb7 100644 --- a/icu4c/source/common/ucnv_err.c +++ b/icu4c/source/common/ucnv_err.c @@ -34,8 +34,8 @@ #define UNICODE_HASH_CODEPOINT 0x0023 #define UNICODE_SEMICOLON_CODEPOINT 0x003B #define UNICODE_PLUS_CODEPOINT 0x002B -#define UNICODE_LEFT_CURLY_CODEPOINT 0x007B -#define UNICODE_RIGHT_CURLY_CODEPOINT 0x007D +#define UNICODE_LEFT_CURLY_CODEPOINT 0x007B +#define UNICODE_RIGHT_CURLY_CODEPOINT 0x007D #define UCNV_PRV_ESCAPE_ICU 0 #define UCNV_PRV_ESCAPE_C 'C' #define UCNV_PRV_ESCAPE_XML_DEC 'D' diff --git a/icu4c/source/common/udatamem.h b/icu4c/source/common/udatamem.h index 1330879c305..840e674e0a4 100644 --- a/icu4c/source/common/udatamem.h +++ b/icu4c/source/common/udatamem.h @@ -26,9 +26,9 @@ struct UDataMemory { const commonDataFuncs *vFuncs; /* Function Pointers for accessing TOC */ const DataHeader *pHeader; /* Header of the memory being described by this */ - /* UDataMemory object. */ + /* UDataMemory object. */ const void *toc; /* For common memory, table of contents for */ - /* the pieces within. */ + /* the pieces within. */ UBool heapAllocated; /* True if this UDataMemory Object is on the */ /* heap and thus needs to be deleted when closed. */ @@ -38,7 +38,7 @@ struct UDataMemory { void *map; /* Handle, or other data, OS dependent. */ /* Only non-null if a close operation should unmap */ /* the associated data, and additional info */ - /* beyond the mapAddr is needed to do that. */ + /* beyond the mapAddr is needed to do that. */ }; UDataMemory *UDataMemory_createNewInstance(UErrorCode *pErr); diff --git a/icu4c/source/common/uhash.c b/icu4c/source/common/uhash.c index 9873b5236a7..2f1580fe229 100644 --- a/icu4c/source/common/uhash.c +++ b/icu4c/source/common/uhash.c @@ -372,7 +372,7 @@ uhash_find(const UHashtable *hash, const void* key) { UHashTok keyholder; const UHashElement *e; keyholder.pointer = (void*) key; - e = _uhash_find(hash, keyholder, hash->keyHasher(keyholder)); + e = _uhash_find(hash, keyholder, hash->keyHasher(keyholder)); return IS_EMPTY_OR_DELETED(e->hashcode) ? NULL : e; } diff --git a/icu4c/source/common/unames.c b/icu4c/source/common/unames.c index 0e28158e7bf..ad7a1a729dd 100644 --- a/icu4c/source/common/unames.c +++ b/icu4c/source/common/unames.c @@ -196,13 +196,13 @@ u_charName(UChar32 code, UCharNameChoice nameChoice, } if(i==0) { - if (nameChoice == U_EXTENDED_CHAR_NAME) { - /* extended character name */ + if (nameChoice == U_EXTENDED_CHAR_NAME) { + /* extended character name */ length = getExtName((uint32_t) code, buffer, (uint16_t) bufferLength); - } else { - /* normal character name */ - length=getName(uCharNames, (uint32_t)code, nameChoice, buffer, (uint16_t)bufferLength); - } + } else { + /* normal character name */ + length=getName(uCharNames, (uint32_t)code, nameChoice, buffer, (uint16_t)bufferLength); + } } return u_terminateChars(buffer, bufferLength, length, pErrorCode); diff --git a/icu4c/source/common/unicode/unistr.h b/icu4c/source/common/unicode/unistr.h index 9cc3859bf8b..a5f58982085 100644 --- a/icu4c/source/common/unicode/unistr.h +++ b/icu4c/source/common/unicode/unistr.h @@ -2825,7 +2825,7 @@ private: UBool allocate(int32_t capacity); // release the array if owned - inline void releaseArray(); + void releaseArray(void); // Pin start and limit to acceptable values. inline void pinIndices(UTextOffset& start, @@ -3813,13 +3813,6 @@ inline int32_t UnicodeString::getCapacity() const { return fCapacity; } -inline void -UnicodeString::releaseArray() { - if((fFlags & kRefCounted) && removeRef() == 0) { - delete [] ((int32_t *)fArray - 1); - } -} - inline int32_t UnicodeString::addRef() { return ++*((int32_t *)fArray - 1); } diff --git a/icu4c/source/common/unistr.cpp b/icu4c/source/common/unistr.cpp index 7c651dc8c33..7a8ce4fd570 100644 --- a/icu4c/source/common/unistr.cpp +++ b/icu4c/source/common/unistr.cpp @@ -304,7 +304,7 @@ UnicodeString::allocate(int32_t capacity) { // round up to a multiple of 16; then divide by 4 and allocate int32_t's // to be safely aligned for the refCount int32_t words = (int32_t)(((sizeof(int32_t) + capacity * U_SIZEOF_UCHAR + 15) & ~15) >> 2); - int32_t *array = new int32_t[words]; + int32_t *array = (int32_t*) uprv_malloc( sizeof(int32_t) * words ); if(array != 0) { // set initial refCount and point behind the refCount *array++ = 1; @@ -331,6 +331,14 @@ UnicodeString::~UnicodeString() releaseArray(); } +void +UnicodeString::releaseArray() { + if((fFlags & kRefCounted) && removeRef() == 0) { + uprv_free((int32_t *)fArray - 1); + } +} + + //======================================== // Assignment //======================================== @@ -1221,7 +1229,7 @@ UnicodeString::caseMap(BreakIterator *titleIter, ubrk_close(cTitleIter); } - delete [] bufferToDelete; + uprv_free(bufferToDelete); if(U_FAILURE(errorCode)) { setToBogus(); } @@ -1312,7 +1320,7 @@ UnicodeString::doReplace(UTextOffset start, // delayed delete in case srcChars == fArray when we started, and // to keep oldArray alive for the above operations - delete [] bufferToDelete; + uprv_free(bufferToDelete); return *this; } @@ -1332,10 +1340,10 @@ UnicodeString::handleReplaceBetween(UTextOffset start, */ void UnicodeString::copy(int32_t start, int32_t limit, int32_t dest) { - UChar* text = new UChar[limit - start]; + UChar* text = (UChar*) uprv_malloc( sizeof(UChar) * (limit - start) ); extractBetween(start, limit, text, 0); insert(dest, text, 0, limit - start); - delete[] text; + uprv_free(text); } UnicodeString& @@ -1847,7 +1855,7 @@ UnicodeString::cloneArrayIfNeeded(int32_t newCapacity, int32_t *pRefCount = ((int32_t *)array - 1); if(--*pRefCount == 0) { if(pBufferToDelete == 0) { - delete [] pRefCount; + uprv_free(pRefCount); } else { // the caller requested to delete it himself *pBufferToDelete = pRefCount; diff --git a/icu4c/source/common/uvector.cpp b/icu4c/source/common/uvector.cpp index eecff54a882..ec7452422c5 100644 --- a/icu4c/source/common/uvector.cpp +++ b/icu4c/source/common/uvector.cpp @@ -56,7 +56,7 @@ UVector::UVector(UObjectDeleter d, UKeyComparator c, int32_t initialCapacity, UE } void UVector::_init(int32_t initialCapacity, UErrorCode &status) { - elements = new UHashTok[initialCapacity]; + elements = (UHashTok *)uprv_malloc(sizeof(UHashTok)*initialCapacity); if (elements == 0) { status = U_MEMORY_ALLOCATION_ERROR; } else { @@ -66,7 +66,7 @@ void UVector::_init(int32_t initialCapacity, UErrorCode &status) { UVector::~UVector() { removeAllElements(); - delete[] elements; + uprv_free(elements); elements = 0; } @@ -109,7 +109,7 @@ void UVector::insertElementAt(void* obj, int32_t index, UErrorCode &status) { elements[i] = elements[i-1]; } elements[index].pointer = obj; - ++count; + ++count; } /* else index out of range */ } @@ -170,7 +170,7 @@ UBool UVector::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) { if (newCap < minimumCapacity) { newCap = minimumCapacity; } - UHashTok* newElems = new UHashTok[newCap]; + UHashTok* newElems = (UHashTok *)uprv_malloc(sizeof(UHashTok)*newCap); if (newElems == 0) { status = U_MEMORY_ALLOCATION_ERROR; return FALSE; diff --git a/icu4c/source/extra/ustdio/locbund.c b/icu4c/source/extra/ustdio/locbund.c index eb6e923cb8d..875668464c6 100644 --- a/icu4c/source/extra/ustdio/locbund.c +++ b/icu4c/source/extra/ustdio/locbund.c @@ -19,23 +19,23 @@ #include #include "locbund.h" -#include "string.h" +#include "cmemory.h" #include "unicode/ustring.h" #include "unicode/uloc.h" ULocaleBundle* u_locbund_new(const char *loc) { - ULocaleBundle *result = (ULocaleBundle*) malloc(sizeof(ULocaleBundle)); + ULocaleBundle *result = (ULocaleBundle*) uprv_malloc(sizeof(ULocaleBundle)); int32_t len; if(result == 0) return 0; len = (loc == 0 ? strlen(uloc_getDefault()) : strlen(loc)); - result->fLocale = (char*) malloc(len + 1); + result->fLocale = (char*) uprv_malloc(len + 1); if(result->fLocale == 0) { - free(result); + uprv_free(result); return 0; } @@ -55,15 +55,15 @@ u_locbund_new(const char *loc) ULocaleBundle* u_locbund_clone(const ULocaleBundle *bundle) { - ULocaleBundle *result = (ULocaleBundle*)malloc(sizeof(ULocaleBundle)); + ULocaleBundle *result = (ULocaleBundle*)uprv_malloc(sizeof(ULocaleBundle)); UErrorCode status = U_ZERO_ERROR; if(result == 0) return 0; - result->fLocale = (char*) malloc(strlen(bundle->fLocale) + 1); + result->fLocale = (char*) uprv_malloc(strlen(bundle->fLocale) + 1); if(result->fLocale == 0) { - free(result); + uprv_free(result); return 0; } @@ -94,7 +94,7 @@ u_locbund_clone(const ULocaleBundle *bundle) void u_locbund_delete(ULocaleBundle *bundle) { - free(bundle->fLocale); + uprv_free(bundle->fLocale); if(bundle->fNumberFormat != 0) unum_close(bundle->fNumberFormat); @@ -111,7 +111,7 @@ u_locbund_delete(ULocaleBundle *bundle) if(bundle->fTimeFormat != 0) udat_close(bundle->fTimeFormat); - free(bundle); + uprv_free(bundle); } UNumberFormat* diff --git a/icu4c/source/extra/ustdio/sprintf.c b/icu4c/source/extra/ustdio/sprintf.c index 41037b45e5d..c22fbf541d4 100644 --- a/icu4c/source/extra/ustdio/sprintf.c +++ b/icu4c/source/extra/ustdio/sprintf.c @@ -26,9 +26,8 @@ #include "unicode/udat.h" #include "unicode/uloc.h" -#include +#include "cmemory.h" #include -#include /* --- Prototypes ---------------------------- */ @@ -330,7 +329,7 @@ u_vsnprintf(UChar *buffer, written = u_vsnprintf_u(buffer, count, locale, pattern, ap); /* clean up */ - free(pattern); + uprv_free(pattern); return written; } @@ -442,7 +441,7 @@ u_sprintf_string_handler(u_localized_string *output, } /* clean up */ - free(s); + uprv_free(s); return written; } @@ -703,7 +702,7 @@ u_sprintf_char_handler(u_localized_string *output, } /* clean up */ - free(s); + uprv_free(s); return written; } diff --git a/icu4c/source/extra/ustdio/sscanf.c b/icu4c/source/extra/ustdio/sscanf.c index c59af27dac1..ee69d74cfa1 100644 --- a/icu4c/source/extra/ustdio/sscanf.c +++ b/icu4c/source/extra/ustdio/sscanf.c @@ -29,8 +29,7 @@ #include "unicode/udat.h" #include "unicode/uloc.h" -#include -#include +#include "cmemory.h" /* --- Prototypes ---------------------------- */ @@ -280,269 +279,271 @@ static const u_sscanf_info g_u_sscanf_infos[108] = { int32_t u_sscanf(UChar *buffer, - const char *locale, - const char *patternSpecification, - ... ) + const char *locale, + const char *patternSpecification, + ... ) { - va_list ap; - int32_t converted; - - va_start(ap, patternSpecification); - converted = u_vsscanf(buffer, locale, patternSpecification, ap); - va_end(ap); - - return converted; + va_list ap; + int32_t converted; + + va_start(ap, patternSpecification); + converted = u_vsscanf(buffer, locale, patternSpecification, ap); + va_end(ap); + + return converted; } int32_t u_sscanf_u(UChar *buffer, - const char *locale, - const UChar *patternSpecification, - ... ) + const char *locale, + const UChar *patternSpecification, + ... ) { - va_list ap; - int32_t converted; - - va_start(ap, patternSpecification); - converted = u_vsscanf_u(buffer, locale, patternSpecification, ap); - va_end(ap); - - return converted; + va_list ap; + int32_t converted; + + va_start(ap, patternSpecification); + converted = u_vsscanf_u(buffer, locale, patternSpecification, ap); + va_end(ap); + + return converted; } U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_vsscanf(UChar *buffer, - const char *locale, - const char *patternSpecification, - va_list ap) + const char *locale, + const char *patternSpecification, + va_list ap) { - int32_t converted; - UChar *pattern; - - /* convert from the default codepage to Unicode */ - pattern = ufmt_defaultCPToUnicode(patternSpecification, - strlen(patternSpecification)); - if(pattern == 0) { - return 0; - } - - /* do the work */ - converted = u_vsscanf_u(buffer, locale, pattern, ap); + int32_t converted; + UChar *pattern; - /* clean up */ - free(pattern); - - return converted; + /* convert from the default codepage to Unicode */ + pattern = ufmt_defaultCPToUnicode(patternSpecification, + strlen(patternSpecification)); + if(pattern == 0) { + return 0; + } + + /* do the work */ + converted = u_vsscanf_u(buffer, locale, pattern, ap); + + /* clean up */ + uprv_free(pattern); + + return converted; } static int32_t u_sscanf_skip_leading_ws(u_localized_string *input, - UChar pad) + UChar pad) { - UChar c; - int32_t count = input->pos; - int32_t skipped; - - /* skip all leading ws in the stream */ - while( ((c = input->str[count]) != 0xFFFF) && (c == pad || ufmt_isws(c)) ) - count++; + UChar c; + int32_t count = input->pos; + int32_t skipped; - if(c == 0xFFFF) - count++; + /* skip all leading ws in the stream */ + while( ((c = input->str[count]) != 0xFFFF) && (c == pad || ufmt_isws(c)) ) + count++; - skipped = count - input->pos; - input->pos = count; - return skipped; + if(c == 0xFFFF) + count++; + + skipped = count - input->pos; + input->pos = count; + return skipped; } int32_t u_sscanf_simple_percent_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - /* make sure the next character in the stream is a percent */ - if(input->str[input->pos++] != 0x0025) { - return -1; - } + /* make sure the next character in the stream is a percent */ + if(input->str[input->pos++] != 0x0025) { + return -1; + } - return 0; + return 0; } int32_t u_sscanf_string_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - UChar c; - int32_t count; - const UChar *source; - UConverter *conv; - UErrorCode status = U_ZERO_ERROR; - char *arg = (char*)(args[0].ptrValue); - char *alias = arg; - char *limit; + UChar c; + int32_t count; + const UChar *source; + UConverter *conv; + UErrorCode status = U_ZERO_ERROR; + char *arg = (char*)(args[0].ptrValue); + char *alias = arg; + char *limit; - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); - - /* get the string one character at a time, truncating to the width */ - count = 0; + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* open the default converter */ - conv = ucnv_open(ucnv_getDefaultName(), &status); + /* get the string one character at a time, truncating to the width */ + count = 0; - if(U_FAILURE(status)) - return -1; + /* open the default converter */ + conv = ucnv_open(ucnv_getDefaultName(), &status); - /* since there is no real limit, just use a reasonable value */ - limit = alias + 2048; /* TODO: Specify a real limit! */ - - while( ((c = input->str[input->pos++]) != 0xFFFF) && - (c != info->fPadChar && ! ufmt_isws(c)) && - (info->fWidth == -1 || count < info->fWidth) ) { - - /* put the character from the stream onto the target */ - source = &c; - - /* convert the character to the default codepage */ - ucnv_fromUnicode(conv, &alias, limit, &source, source + 1, - NULL, TRUE, &status); - if(U_FAILURE(status)) - return -1; - - /* increment the count */ - ++count; - } + return -1; - /* put the final character we read back on the stream */ - if(c != 0xFFFF) - input->pos--; - - /* add the terminator */ - *alias = 0x00; + /* since there is no real limit, just use a reasonable value */ + limit = alias + 2048; /* TODO: Specify a real limit! */ - /* clean up */ - ucnv_close(conv); + while( ((c = input->str[input->pos++]) != 0xFFFF) + && (c != info->fPadChar && ! ufmt_isws(c)) + && (info->fWidth == -1 || count < info->fWidth) ) + { - /* we converted 1 arg */ - return 1; + /* put the character from the stream onto the target */ + source = &c; + + /* convert the character to the default codepage */ + ucnv_fromUnicode(conv, &alias, limit, &source, source + 1, + NULL, TRUE, &status); + + if(U_FAILURE(status)) + return -1; + + /* increment the count */ + ++count; + } + + /* put the final character we read back on the stream */ + if(c != 0xFFFF) + input->pos--; + + /* add the terminator */ + *alias = 0x00; + + /* clean up */ + ucnv_close(conv); + + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_ustring_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - UChar c; - int32_t count; - UChar *arg = (UChar*)(args[0].ptrValue); - UChar *alias = arg; - - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); - - /* get the string one character at a time, truncating to the width */ - count = 0; + UChar c; + int32_t count; + UChar *arg = (UChar*)(args[0].ptrValue); + UChar *alias = arg; - while( ((c = input->str[input->pos++]) != 0xFFFF) && - (c != info->fPadChar && ! ufmt_isws(c)) && - (info->fWidth == -1 || count < info->fWidth) ) { - - /* put the character from the stream onto the target */ - *alias++ = c; - - /* increment the count */ - ++count; - } + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* put the final character we read back on the stream */ - if(c != 0xFFFF) - input->pos--; - - /* add the terminator */ - *alias = 0x0000; + /* get the string one character at a time, truncating to the width */ + count = 0; - /* we converted 1 arg */ - return 1; + while( ((c = input->str[input->pos++]) != 0xFFFF) + && (c != info->fPadChar && ! ufmt_isws(c)) + && (info->fWidth == -1 || count < info->fWidth) ) + { + + /* put the character from the stream onto the target */ + *alias++ = c; + + /* increment the count */ + ++count; + } + + /* put the final character we read back on the stream */ + if(c != 0xFFFF) + input->pos--; + + /* add the terminator */ + *alias = 0x0000; + + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_count_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int *converted = (int*)(args[0].ptrValue); - - /* in the special case of count, the u_sscanf_spec_info's width */ - /* will contain the # of items converted thus far */ - *converted = info->fWidth; + int *converted = (int*)(args[0].ptrValue); - /* we converted 0 args */ - return 0; + /* in the special case of count, the u_sscanf_spec_info's width */ + /* will contain the # of items converted thus far */ + *converted = info->fWidth; + + /* we converted 0 args */ + return 0; } int32_t u_sscanf_integer_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - long *num = (long*) (args[0].ptrValue); - UNumberFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + long *num = (long*) (args[0].ptrValue); + UNumberFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* determine the size of the stream's buffer */ - len = input->len - input->pos; + /* determine the size of the stream's buffer */ + len = input->len - input->pos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getNumberFormat(input->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *num = unum_parse(format, &(input->str[input->pos]), len, &parsePos, &status); + /* get the formatter */ + format = u_locbund_getNumberFormat(input->fBundle); - /* mask off any necessary bits */ - if(info->fIsShort) - *num &= UINT16_MAX; - else if(! info->fIsLong || ! info->fIsLongLong) - *num &= UINT32_MAX; + /* handle error */ + if(format == 0) + return 0; - /* update the stream's position to reflect consumed data */ - input->pos += parsePos; - - /* we converted 1 arg */ - return 1; + /* parse the number */ + *num = unum_parse(format, &(input->str[input->pos]), len, &parsePos, &status); + + /* mask off any necessary bits */ + if(info->fIsShort) + *num &= UINT16_MAX; + else if(! info->fIsLong || ! info->fIsLongLong) + *num &= UINT32_MAX; + + /* update the stream's position to reflect consumed data */ + input->pos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_uinteger_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { ufmt_args uint_args; int32_t converted_args; @@ -560,629 +561,629 @@ u_sscanf_uinteger_handler(u_localized_string *input, int32_t u_sscanf_double_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - double *num = (double*) (args[0].ptrValue); - UNumberFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + double *num = (double*) (args[0].ptrValue); + UNumberFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* determine the size of the stream's buffer */ - len = input->len - input->pos; + /* determine the size of the stream's buffer */ + len = input->len - input->pos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getNumberFormat(input->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *num = unum_parseDouble(format, &(input->str[input->pos]), len, &parsePos, &status); + /* get the formatter */ + format = u_locbund_getNumberFormat(input->fBundle); - /* mask off any necessary bits */ - /* if(! info->fIsLong_double) - num &= DBL_MAX;*/ + /* handle error */ + if(format == 0) + return 0; - /* update the stream's position to reflect consumed data */ - input->pos += parsePos; - - /* we converted 1 arg */ - return 1; + /* parse the number */ + *num = unum_parseDouble(format, &(input->str[input->pos]), len, &parsePos, &status); + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the stream's position to reflect consumed data */ + input->pos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_scientific_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - double *num = (double*) (args[0].ptrValue); - UNumberFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + double *num = (double*) (args[0].ptrValue); + UNumberFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* determine the size of the stream's buffer */ - len = input->len - input->pos; + /* determine the size of the stream's buffer */ + len = input->len - input->pos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getScientificFormat(input->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *num = unum_parseDouble(format, &(input->str[input->pos]), len, &parsePos, &status); + /* get the formatter */ + format = u_locbund_getScientificFormat(input->fBundle); - /* mask off any necessary bits */ - /* if(! info->fIsLong_double) - num &= DBL_MAX;*/ + /* handle error */ + if(format == 0) + return 0; - /* update the stream's position to reflect consumed data */ - input->pos += parsePos; - - /* we converted 1 arg */ - return 1; + /* parse the number */ + *num = unum_parseDouble(format, &(input->str[input->pos]), len, &parsePos, &status); + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the stream's position to reflect consumed data */ + input->pos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_scidbl_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - double *num = (double*) (args[0].ptrValue); - UNumberFormat *scientificFormat, *genericFormat; - /*int32_t scientificResult, genericResult;*/ - double scientificResult, genericResult; - int32_t scientificParsePos = 0, genericParsePos = 0; - UErrorCode scientificStatus = U_ZERO_ERROR; - UErrorCode genericStatus = U_ZERO_ERROR; + int32_t len; + double *num = (double*) (args[0].ptrValue); + UNumberFormat *scientificFormat, *genericFormat; + /*int32_t scientificResult, genericResult;*/ + double scientificResult, genericResult; + int32_t scientificParsePos = 0, genericParsePos = 0; + UErrorCode scientificStatus = U_ZERO_ERROR; + UErrorCode genericStatus = U_ZERO_ERROR; - /* since we can't determine by scanning the characters whether */ - /* a number was formatted in the 'f' or 'g' styles, parse the */ - /* string with both formatters, and assume whichever one */ - /* parsed the most is the correct formatter to use */ + /* since we can't determine by scanning the characters whether */ + /* a number was formatted in the 'f' or 'g' styles, parse the */ + /* string with both formatters, and assume whichever one */ + /* parsed the most is the correct formatter to use */ - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* determine the size of the stream's buffer */ - len = input->len - input->pos; + /* determine the size of the stream's buffer */ + len = input->len - input->pos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatters */ - scientificFormat = u_locbund_getScientificFormat(input->fBundle); - genericFormat = u_locbund_getNumberFormat(input->fBundle); - - /* handle error */ - if(scientificFormat == 0 || genericFormat == 0) - return 0; - - /* parse the number using each format*/ + /* get the formatters */ + scientificFormat = u_locbund_getScientificFormat(input->fBundle); + genericFormat = u_locbund_getNumberFormat(input->fBundle); - scientificResult = unum_parseDouble(scientificFormat, &(input->str[input->pos]), len, - &scientificParsePos, &scientificStatus); + /* handle error */ + if(scientificFormat == 0 || genericFormat == 0) + return 0; - genericResult = unum_parseDouble(genericFormat, &(input->str[input->pos]), len, - &genericParsePos, &genericStatus); + /* parse the number using each format*/ - /* determine which parse made it farther */ - if(scientificParsePos > genericParsePos) { - /* stash the result in num */ - *num = scientificResult; - /* update the stream's position to reflect consumed data */ - input->pos += scientificParsePos; - } - else { - /* stash the result in num */ - *num = genericResult; - /* update the stream's position to reflect consumed data */ - input->pos += genericParsePos; - } - - /* mask off any necessary bits */ - /* if(! info->fIsLong_double) - num &= DBL_MAX;*/ + scientificResult = unum_parseDouble(scientificFormat, &(input->str[input->pos]), len, + &scientificParsePos, &scientificStatus); - /* we converted 1 arg */ - return 1; + genericResult = unum_parseDouble(genericFormat, &(input->str[input->pos]), len, + &genericParsePos, &genericStatus); + + /* determine which parse made it farther */ + if(scientificParsePos > genericParsePos) { + /* stash the result in num */ + *num = scientificResult; + /* update the stream's position to reflect consumed data */ + input->pos += scientificParsePos; + } + else { + /* stash the result in num */ + *num = genericResult; + /* update the stream's position to reflect consumed data */ + input->pos += genericParsePos; + } + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_currency_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - double *num = (double*) (args[0].ptrValue); - UNumberFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + double *num = (double*) (args[0].ptrValue); + UNumberFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* determine the size of the stream's buffer */ - len = input->len - input->pos; + /* determine the size of the stream's buffer */ + len = input->len - input->pos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getCurrencyFormat(input->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *num = unum_parseDouble(format, &(input->str[input->pos]), len, &parsePos, &status); - - /* mask off any necessary bits */ - /* if(! info->fIsLong_double) - num &= DBL_MAX;*/ + /* get the formatter */ + format = u_locbund_getCurrencyFormat(input->fBundle); - /* update the stream's position to reflect consumed data */ - input->pos += parsePos; - - /* we converted 1 arg */ - return 1; + /* handle error */ + if(format == 0) + return 0; + + /* parse the number */ + *num = unum_parseDouble(format, &(input->str[input->pos]), len, &parsePos, &status); + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the stream's position to reflect consumed data */ + input->pos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_percent_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - double *num = (double*) (args[0].ptrValue); - UNumberFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + double *num = (double*) (args[0].ptrValue); + UNumberFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* determine the size of the stream's buffer */ - len = input->len - input->pos; + /* determine the size of the stream's buffer */ + len = input->len - input->pos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getPercentFormat(input->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *num = unum_parseDouble(format, &(input->str[input->pos]), len, &parsePos, &status); + /* get the formatter */ + format = u_locbund_getPercentFormat(input->fBundle); - /* mask off any necessary bits */ - /* if(! info->fIsLong_double) - num &= DBL_MAX;*/ + /* handle error */ + if(format == 0) + return 0; - /* update the stream's position to reflect consumed data */ - input->pos += parsePos; - - /* we converted 1 arg */ - return 1; + /* parse the number */ + *num = unum_parseDouble(format, &(input->str[input->pos]), len, &parsePos, &status); + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the stream's position to reflect consumed data */ + input->pos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_date_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - UDate *date = (UDate*) (args[0].ptrValue); - UDateFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + UDate *date = (UDate*) (args[0].ptrValue); + UDateFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* determine the size of the stream's buffer */ - len = input->len - input->pos; + /* determine the size of the stream's buffer */ + len = input->len - input->pos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getDateFormat(input->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *date = udat_parse(format, &(input->str[input->pos]), len, &parsePos, &status); - - /* update the stream's position to reflect consumed data */ - input->pos += parsePos; - - /* we converted 1 arg */ - return 1; + /* get the formatter */ + format = u_locbund_getDateFormat(input->fBundle); + + /* handle error */ + if(format == 0) + return 0; + + /* parse the number */ + *date = udat_parse(format, &(input->str[input->pos]), len, &parsePos, &status); + + /* update the stream's position to reflect consumed data */ + input->pos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_time_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - UDate *time = (UDate*) (args[0].ptrValue); - UDateFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + UDate *time = (UDate*) (args[0].ptrValue); + UDateFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* determine the size of the stream's buffer */ - len = input->len - input->pos; + /* determine the size of the stream's buffer */ + len = input->len - input->pos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getTimeFormat(input->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *time = udat_parse(format, &(input->str[input->pos]), len, &parsePos, &status); - - /* update the stream's position to reflect consumed data */ - input->pos += parsePos; - - /* we converted 1 arg */ - return 1; + /* get the formatter */ + format = u_locbund_getTimeFormat(input->fBundle); + + /* handle error */ + if(format == 0) + return 0; + + /* parse the number */ + *time = udat_parse(format, &(input->str[input->pos]), len, &parsePos, &status); + + /* update the stream's position to reflect consumed data */ + input->pos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_char_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - UChar uc = 0; - char *result; - char *c = (char*)(args[0].ptrValue); - - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); - - /* get the character from the stream, truncating to the width */ - if(info->fWidth == -1 || info->fWidth > 1) - uc = input->str[input->pos++]; + UChar uc = 0; + char *result; + char *c = (char*)(args[0].ptrValue); - /* handle EOF */ - if(uc == 0xFFFF) - return -1; + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* convert the character to the default codepage */ - result = ufmt_unicodeToDefaultCP(&uc, 1); - *c = result[0]; + /* get the character from the stream, truncating to the width */ + if(info->fWidth == -1 || info->fWidth > 1) + uc = input->str[input->pos++]; - /* clean up */ - free(result); - - /* we converted 1 arg */ - return 1; + /* handle EOF */ + if(uc == 0xFFFF) + return -1; + + /* convert the character to the default codepage */ + result = ufmt_unicodeToDefaultCP(&uc, 1); + *c = result[0]; + + /* clean up */ + uprv_free(result); + + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_uchar_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - UChar *c = (UChar*)(args[0].ptrValue); - - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); - - /* get the character from the stream, truncating to the width */ - if(info->fWidth == -1 || info->fWidth > 1) - *c = input->str[input->pos]; + UChar *c = (UChar*)(args[0].ptrValue); - /* handle EOF */ - if(*c == 0xFFFF) - return -1; + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* we converted 1 arg */ - return 1; + /* get the character from the stream, truncating to the width */ + if(info->fWidth == -1 || info->fWidth > 1) + *c = input->str[input->pos]; + + /* handle EOF */ + if(*c == 0xFFFF) + return -1; + + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_spellout_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - double *num = (double*) (args[0].ptrValue); - UNumberFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + double *num = (double*) (args[0].ptrValue); + UNumberFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* determine the size of the stream's buffer */ - len = input->len - input->pos; + /* determine the size of the stream's buffer */ + len = input->len - input->pos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getSpelloutFormat(input->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *num = unum_parseDouble(format, &(input->str[input->pos]), len, &parsePos, &status); + /* get the formatter */ + format = u_locbund_getSpelloutFormat(input->fBundle); - /* mask off any necessary bits */ - /* if(! info->fIsLong_double) - num &= DBL_MAX;*/ + /* handle error */ + if(format == 0) + return 0; - /* update the stream's position to reflect consumed data */ - input->pos += parsePos; - - /* we converted 1 arg */ - return 1; + /* parse the number */ + *num = unum_parseDouble(format, &(input->str[input->pos]), len, &parsePos, &status); + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the stream's position to reflect consumed data */ + input->pos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_hex_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - long *num = (long*) (args[0].ptrValue); + int32_t len; + long *num = (long*) (args[0].ptrValue); - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* determine the size of the stream's buffer */ - len = input->len - input->pos; + /* determine the size of the stream's buffer */ + len = input->len - input->pos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* check for alternate form */ - if( input->str[input->pos] == 0x0030 && - (input->str[input->pos + 1] == 0x0078 || input->str[input->pos + 1] == 0x0058) ) { + /* check for alternate form */ + if( input->str[input->pos] == 0x0030 && + (input->str[input->pos + 1] == 0x0078 || input->str[input->pos + 1] == 0x0058) ) { - /* skip the '0' and 'x' or 'X' if present */ - input->pos += 2; - len -= 2; - } + /* skip the '0' and 'x' or 'X' if present */ + input->pos += 2; + len -= 2; + } - /* parse the number */ - *num = ufmt_utol(&(input->str[input->pos]), &len, 16); + /* parse the number */ + *num = ufmt_utol(&(input->str[input->pos]), &len, 16); - /* update the stream's position to reflect consumed data */ - input->pos += len; + /* update the stream's position to reflect consumed data */ + input->pos += len; - /* mask off any necessary bits */ - if(info->fIsShort) - *num &= UINT16_MAX; - else if(! info->fIsLong || ! info->fIsLongLong) - *num &= UINT32_MAX; + /* mask off any necessary bits */ + if(info->fIsShort) + *num &= UINT16_MAX; + else if(! info->fIsLong || ! info->fIsLongLong) + *num &= UINT32_MAX; - /* we converted 1 arg */ - return 1; + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_octal_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - long *num = (long*) (args[0].ptrValue); + int32_t len; + long *num = (long*) (args[0].ptrValue); - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* determine the size of the stream's buffer */ - len = input->len - input->pos; + /* determine the size of the stream's buffer */ + len = input->len - input->pos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* parse the number */ - *num = ufmt_utol(&(input->str[input->pos]), &len, 8); + /* parse the number */ + *num = ufmt_utol(&(input->str[input->pos]), &len, 8); - /* update the stream's position to reflect consumed data */ - input->pos += len; + /* update the stream's position to reflect consumed data */ + input->pos += len; - /* mask off any necessary bits */ - if(info->fIsShort) - *num &= UINT16_MAX; - else if(! info->fIsLong || ! info->fIsLongLong) - *num &= UINT32_MAX; + /* mask off any necessary bits */ + if(info->fIsShort) + *num &= UINT16_MAX; + else if(! info->fIsLong || ! info->fIsLongLong) + *num &= UINT32_MAX; - /* we converted 1 arg */ - return 1; + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_pointer_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - void *p = (void*)(args[0].ptrValue); + int32_t len; + void *p = (void*)(args[0].ptrValue); - /* skip all ws in the stream */ - u_sscanf_skip_leading_ws(input, info->fPadChar); + /* skip all ws in the stream */ + u_sscanf_skip_leading_ws(input, info->fPadChar); - /* determine the size of the stream's buffer */ - len = input->len - input->pos; + /* determine the size of the stream's buffer */ + len = input->len - input->pos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* parse the pointer - cast to void** to assign to *p */ - *(void**)p = (void*) ufmt_utol(&(input->str[input->pos]), &len, 16); + /* parse the pointer - cast to void** to assign to *p */ + *(void**)p = (void*) ufmt_utol(&(input->str[input->pos]), &len, 16); - /* update the stream's position to reflect consumed data */ - input->pos += len; + /* update the stream's position to reflect consumed data */ + input->pos += len; - /* we converted 1 arg */ - return 1; + /* we converted 1 arg */ + return 1; } int32_t u_sscanf_scanset_handler(u_localized_string *input, - const u_sscanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_sscanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - u_scanf_scanset scanset; - int32_t len; - UBool success; - UChar c; - const UChar *source; - UConverter *conv; - UErrorCode status = U_ZERO_ERROR; - char *s = (char*) (args[0].ptrValue); - char *alias, *limit; + u_scanf_scanset scanset; + int32_t len; + UBool success; + UChar c; + const UChar *source; + UConverter *conv; + UErrorCode status = U_ZERO_ERROR; + char *s = (char*) (args[0].ptrValue); + char *alias, *limit; - /* determine the size of the stream's buffer */ - len = input->len - input->pos; + /* determine the size of the stream's buffer */ + len = input->len - input->pos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* alias the target */ - alias = s; - limit = alias + len; + /* alias the target */ + alias = s; + limit = alias + len; - /* parse the scanset from the fmt string */ - *consumed = u_strlen(fmt); - success = u_scanf_scanset_init(&scanset, fmt, consumed); + /* parse the scanset from the fmt string */ + *consumed = u_strlen(fmt); + success = u_scanf_scanset_init(&scanset, fmt, consumed); - /* increment consumed by one to eat the final ']' */ - ++(*consumed); + /* increment consumed by one to eat the final ']' */ + ++(*consumed); - /* open the default converter */ - conv = ucnv_open(ucnv_getDefaultName(), &status); + /* open the default converter */ + conv = ucnv_open(ucnv_getDefaultName(), &status); - /* verify that the parse was successful and the converter opened */ - if(! success || U_FAILURE(status)) - return -1; + /* verify that the parse was successful and the converter opened */ + if(! success || U_FAILURE(status)) + return -1; - /* grab characters one at a time and make sure they are in the scanset */ - while( (c = input->str[input->pos++]) != 0xFFFF && alias < limit) { - if(u_scanf_scanset_in(&scanset, c)) { - source = &c; - /* convert the character to the default codepage */ - ucnv_fromUnicode(conv, &alias, limit, &source, source + 1, - NULL, TRUE, &status); - - if(U_FAILURE(status)) - return -1; + /* grab characters one at a time and make sure they are in the scanset */ + while( (c = input->str[input->pos++]) != 0xFFFF && alias < limit) { + if(u_scanf_scanset_in(&scanset, c)) { + source = &c; + /* convert the character to the default codepage */ + ucnv_fromUnicode(conv, &alias, limit, &source, source + 1, + NULL, TRUE, &status); + + if(U_FAILURE(status)) + return -1; + } + /* if the character's not in the scanset, break out */ + else { + break; + } } - /* if the character's not in the scanset, break out */ - else { - break; - } - } - /* put the final character we read back on the stream */ - if(c != 0xFFFF) - input->pos--; + /* put the final character we read back on the stream */ + if(c != 0xFFFF) + input->pos--; - /* if we didn't match at least 1 character, fail */ - if(alias == s) - return -1; - /* otherwise, add the terminator */ - else - *alias = 0x00; + /* if we didn't match at least 1 character, fail */ + if(alias == s) + return -1; + /* otherwise, add the terminator */ + else + *alias = 0x00; - /* clean up */ - ucnv_close(conv); + /* clean up */ + ucnv_close(conv); - /* we converted 1 arg */ - return 1; + /* we converted 1 arg */ + return 1; } @@ -1190,146 +1191,145 @@ u_sscanf_scanset_handler(u_localized_string *input, U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_vsscanf_u(UChar *buffer, - const char *locale, - const UChar *patternSpecification, - va_list ap) + const char *locale, + const UChar *patternSpecification, + va_list ap) { - const UChar *alias; - int32_t count, converted, temp; - uint16_t handlerNum; + const UChar *alias; + int32_t count, converted, temp; + uint16_t handlerNum; - ufmt_args args; - u_localized_string inStr; - u_sscanf_spec spec; - ufmt_type_info info; - u_sscanf_handler handler; + ufmt_args args; + u_localized_string inStr; + u_sscanf_spec spec; + ufmt_type_info info; + u_sscanf_handler handler; - /* alias the pattern */ - alias = patternSpecification; + /* alias the pattern */ + alias = patternSpecification; - inStr.str = buffer; - inStr.len = u_strlen(buffer); - inStr.pos = 0; + inStr.str = buffer; + inStr.len = u_strlen(buffer); + inStr.pos = 0; - /* haven't converted anything yet */ - converted = 0; + /* haven't converted anything yet */ + converted = 0; - /* if locale is 0, use the default */ - if(locale == 0) { - locale = uloc_getDefault(); - } - inStr.fBundle = u_loccache_get(locale); - - if(inStr.fBundle == 0) { - return 0; - } - inStr.fOwnBundle = FALSE; - - /* iterate through the pattern */ - for(;;) { - - /* match any characters up to the next '%' */ - while(*alias != UP_PERCENT && *alias != 0x0000 && inStr.str[inStr.pos++] == *alias) { - alias++; + /* if locale is 0, use the default */ + if(locale == 0) { + locale = uloc_getDefault(); } + inStr.fBundle = u_loccache_get(locale); - /* if we aren't at a '%', or if we're at end of string, break*/ - if(*alias != UP_PERCENT || *alias == 0x0000) - break; + if(inStr.fBundle == 0) { + return 0; + } + inStr.fOwnBundle = FALSE; - /* parse the specifier */ - count = u_sscanf_parse_spec(alias, &spec); + /* iterate through the pattern */ + for(;;) { - /* update the pointer in pattern */ - alias += count; - - /* skip the argument, if necessary */ - if(spec.fSkipArg) - args.ptrValue = va_arg(ap, int*); - - handlerNum = (uint16_t)(spec.fInfo.fSpec - USCANF_BASE_FMT_HANDLERS); - if (handlerNum < USCANF_NUM_FMT_HANDLERS) { - /* query the info function for argument information */ - info = g_u_sscanf_infos[ handlerNum ].info; - if(info > ufmt_simple_percent) { - switch(info) { - - case ufmt_count: - args.intValue = va_arg(ap, int); - /* set the spec's width to the # of items converted */ - spec.fInfo.fWidth = converted; - break; - - case ufmt_char: - case ufmt_uchar: - case ufmt_int: - args.ptrValue = va_arg(ap, int*); - break; - - case ufmt_wchar: - args.ptrValue = va_arg(ap, wchar_t*); - break; - - case ufmt_string: - args.ptrValue = va_arg(ap, char*); - break; - - case ufmt_wstring: - args.ptrValue = va_arg(ap, wchar_t*); - break; - - case ufmt_pointer: - args.ptrValue = va_arg(ap, void*); - break; - - case ufmt_float: - args.ptrValue = va_arg(ap, float*); - break; - - case ufmt_double: - args.ptrValue = va_arg(ap, double*); - break; - - case ufmt_date: - args.ptrValue = va_arg(ap, UDate*); - break; - - case ufmt_ustring: - args.ptrValue = va_arg(ap, UChar*); - break; - - default: - break; /* Should never get here */ - } + /* match any characters up to the next '%' */ + while(*alias != UP_PERCENT && *alias != 0x0000 && inStr.str[inStr.pos++] == *alias) { + alias++; } - /* call the handler function */ - handler = g_u_sscanf_infos[ handlerNum ].handler; - if(handler != 0) { - /* reset count */ - count = 0; - - temp = (*handler)(&inStr, &spec.fInfo, &args, alias, &count); - - /* if the handler encountered an error condition, break */ - if(temp == -1) + /* if we aren't at a '%', or if we're at end of string, break*/ + if(*alias != UP_PERCENT || *alias == 0x0000) break; - /* add to the # of items converted */ - converted += temp; + /* parse the specifier */ + count = u_sscanf_parse_spec(alias, &spec); - /* update the pointer in pattern */ - alias += count; + /* update the pointer in pattern */ + alias += count; + + /* skip the argument, if necessary */ + if(spec.fSkipArg) + args.ptrValue = va_arg(ap, int*); + + handlerNum = (uint16_t)(spec.fInfo.fSpec - USCANF_BASE_FMT_HANDLERS); + if (handlerNum < USCANF_NUM_FMT_HANDLERS) { + /* query the info function for argument information */ + info = g_u_sscanf_infos[ handlerNum ].info; + if(info > ufmt_simple_percent) { + switch(info) { + + case ufmt_count: + args.intValue = va_arg(ap, int); + /* set the spec's width to the # of items converted */ + spec.fInfo.fWidth = converted; + break; + + case ufmt_char: + case ufmt_uchar: + case ufmt_int: + args.ptrValue = va_arg(ap, int*); + break; + + case ufmt_wchar: + args.ptrValue = va_arg(ap, wchar_t*); + break; + + case ufmt_string: + args.ptrValue = va_arg(ap, char*); + break; + + case ufmt_wstring: + args.ptrValue = va_arg(ap, wchar_t*); + break; + + case ufmt_pointer: + args.ptrValue = va_arg(ap, void*); + break; + + case ufmt_float: + args.ptrValue = va_arg(ap, float*); + break; + + case ufmt_double: + args.ptrValue = va_arg(ap, double*); + break; + + case ufmt_date: + args.ptrValue = va_arg(ap, UDate*); + break; + + case ufmt_ustring: + args.ptrValue = va_arg(ap, UChar*); + break; + + default: + break; /* Should never get here */ + } + } + /* call the handler function */ + handler = g_u_sscanf_infos[ handlerNum ].handler; + if(handler != 0) { + + /* reset count */ + count = 0; + + temp = (*handler)(&inStr, &spec.fInfo, &args, alias, &count); + + /* if the handler encountered an error condition, break */ + if(temp == -1) + break; + + /* add to the # of items converted */ + converted += temp; + + /* update the pointer in pattern */ + alias += count; + } + /* else do nothing */ } /* else do nothing */ + + /* just ignore unknown tags */ } - /* else do nothing */ - /* just ignore unknown tags */ - - } - - /* return # of items converted */ - return converted; + /* return # of items converted */ + return converted; } diff --git a/icu4c/source/extra/ustdio/ufile.c b/icu4c/source/extra/ustdio/ufile.c index dd2d3a36997..e3adc986260 100644 --- a/icu4c/source/extra/ustdio/ufile.c +++ b/icu4c/source/extra/ustdio/ufile.c @@ -26,6 +26,7 @@ #include "unicode/ures.h" #include "unicode/ucnv.h" #include "cstring.h" +#include "cmemory.h" static UBool hasICUData(const char *cp) { @@ -33,7 +34,7 @@ static UBool hasICUData(const char *cp) { UConverter *cnv = NULL; #if 0 UResourceBundle *r = NULL; - + r = ures_open(NULL, NULL, &status); if(U_FAILURE(status)) { return FALSE; @@ -55,203 +56,202 @@ static UBool hasICUData(const char *cp) { U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_fopen(const char *filename, - const char *perm, - const char *locale, - const char *codepage) + const char *perm, + const char *locale, + const char *codepage) { - UErrorCode status = U_ZERO_ERROR; - UBool useSysCP = (UBool)(locale == 0 && codepage == 0); - UFILE *result = (UFILE*) malloc(sizeof(UFILE)); - if(result == 0) - return 0; + UErrorCode status = U_ZERO_ERROR; + UBool useSysCP = (UBool)(locale == 0 && codepage == 0); + UFILE *result = (UFILE*) uprv_malloc(sizeof(UFILE)); + if(result == 0) + return 0; - result->fFile = fopen(filename, perm); - if(result->fFile == 0) { - free(result); - return 0; - } + result->fFile = fopen(filename, perm); + if(result->fFile == 0) { + uprv_free(result); + return 0; + } - result->fOwnFile = TRUE; + result->fOwnFile = TRUE; - /* if locale is 0, use the default */ - if(locale == 0) - locale = uloc_getDefault(); + /* if locale is 0, use the default */ + if(locale == 0) + locale = uloc_getDefault(); - result->fBundle = u_loccache_get(locale); - if(result->fBundle == 0) { - fclose(result->fFile); - free(result); - return 0; - } + result->fBundle = u_loccache_get(locale); + if(result->fBundle == 0) { + fclose(result->fFile); + uprv_free(result); + return 0; + } - result->fOwnBundle = FALSE; - result->fUCPos = result->fUCBuffer; - result->fUCLimit = result->fUCBuffer; + result->fOwnBundle = FALSE; + result->fUCPos = result->fUCBuffer; + result->fUCLimit = result->fUCBuffer; - /* if the codepage is 0, use the default for the locale */ - if(codepage == 0) { - codepage = uprv_defaultCodePageForLocale(locale); - - /* if the codepage is still 0, the default codepage will be used */ - } - - /* if both locale and codepage are 0, use the system default codepage */ - else if(useSysCP) - codepage = 0; + /* if the codepage is 0, use the default for the locale */ + if(codepage == 0) { + codepage = uprv_defaultCodePageForLocale(locale); - result->fConverter = ucnv_open(codepage, &status); - if(U_FAILURE(status) || result->fConverter == 0) { - fclose(result->fFile); - free(result); - return 0; - } + /* if the codepage is still 0, the default codepage will be used */ + } + /* if both locale and codepage are 0, use the system default codepage */ + else if(useSysCP) { + codepage = 0; + } - return result; + result->fConverter = ucnv_open(codepage, &status); + if(U_FAILURE(status) || result->fConverter == 0) { + fclose(result->fFile); + uprv_free(result); + return 0; + } + + return result; } U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_finit(FILE *f, - const char *locale, - const char *codepage) + const char *locale, + const char *codepage) { - UErrorCode status = U_ZERO_ERROR; - UBool useSysCP = (UBool)(locale == NULL && codepage == NULL); - UFILE *result = (UFILE*) malloc(sizeof(UFILE)); - if(result == 0) - return 0; + UErrorCode status = U_ZERO_ERROR; + UBool useSysCP = (UBool)(locale == NULL && codepage == NULL); + UFILE *result = (UFILE*) uprv_malloc(sizeof(UFILE)); + if(result == 0) + return 0; #ifdef WIN32 - result->fFile = &_iob[_fileno(f)]; + result->fFile = &_iob[_fileno(f)]; #else - result->fFile = f; + result->fFile = f; #endif - result->fOwnFile = FALSE; - result->fOwnBundle = FALSE; - result->fUCPos = result->fUCBuffer; - result->fUCLimit = result->fUCBuffer; - result->fConverter = NULL; - result->fBundle = NULL; + result->fOwnFile = FALSE; + result->fOwnBundle = FALSE; + result->fUCPos = result->fUCBuffer; + result->fUCLimit = result->fUCBuffer; + result->fConverter = NULL; + result->fBundle = NULL; - if(hasICUData(codepage)) { - /* if locale is 0, use the default */ - if(locale == 0) - locale = uloc_getDefault(); + if(hasICUData(codepage)) { + /* if locale is 0, use the default */ + if(locale == 0) { + locale = uloc_getDefault(); + } - result->fBundle = u_loccache_get(locale); - if(result->fBundle == 0) { - /* DO NOT FCLOSE HERE! */ - free(result); - return 0; - } - } else { - /* bootstrap mode */ - return result; - } - - /* if the codepage is 0, use the default for the locale */ - if(codepage == 0) { - codepage = uprv_defaultCodePageForLocale(locale); - - - - /* if the codepage is still 0, the default codepage will be used */ - if(codepage == 0) { - result->fConverter = ucnv_open(0, &status); - if(U_FAILURE(status) || result->fConverter == 0) { - /* DO NOT fclose here!!!!!! */ - free(result); + result->fBundle = u_loccache_get(locale); + if(result->fBundle == 0) { + /* DO NOT FCLOSE HERE! */ + uprv_free(result); return 0; } + } else { + /* bootstrap mode */ + return result; } - } else if (*codepage != '\0') { - result->fConverter = ucnv_open(codepage, &status); - if(U_FAILURE(status) || result->fConverter == 0) { - /* DO NOT fclose here!!!!!! */ - free(result); - return 0; - } - } else if(useSysCP) { /* if both locale and codepage are 0, use the system default codepage */ - codepage = 0; - } - return result; + + /* if the codepage is 0, use the default for the locale */ + if(codepage == 0) { + codepage = uprv_defaultCodePageForLocale(locale); + + /* if the codepage is still 0, the default codepage will be used */ + if(codepage == 0) { + result->fConverter = ucnv_open(0, &status); + if(U_FAILURE(status) || result->fConverter == 0) { + /* DO NOT fclose here!!!!!! */ + uprv_free(result); + return 0; + } + } + } else if (*codepage != '\0') { + result->fConverter = ucnv_open(codepage, &status); + if(U_FAILURE(status) || result->fConverter == 0) { + /* DO NOT fclose here!!!!!! */ + uprv_free(result); + return 0; + } + } else if(useSysCP) { /* if both locale and codepage are 0, use the system default codepage */ + codepage = 0; + } + return result; } U_CAPI void U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_fclose(UFILE *file) { - fflush(file->fFile); + fflush(file->fFile); - if(file->fOwnFile) - fclose(file->fFile); + if(file->fOwnFile) + fclose(file->fFile); - if(file->fOwnBundle) - u_locbund_delete(file->fBundle); + if(file->fOwnBundle) + u_locbund_delete(file->fBundle); - ucnv_close(file->fConverter); - - free(file); + ucnv_close(file->fConverter); + uprv_free(file); } U_CAPI FILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_fgetfile( UFILE *f) { - return f->fFile; + return f->fFile; } U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_fgetlocale( UFILE *file) { - return file->fBundle->fLocale; + return file->fBundle->fLocale; } U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_fsetlocale(const char *locale, - UFILE *file) + UFILE *file) { - if(file->fOwnBundle) - u_locbund_delete(file->fBundle); + if(file->fOwnBundle) + u_locbund_delete(file->fBundle); - file->fBundle = u_loccache_get(locale); - file->fOwnBundle = FALSE; + file->fBundle = u_loccache_get(locale); + file->fOwnBundle = FALSE; - return file->fBundle == 0 ? -1 : 0; + return file->fBundle == 0 ? -1 : 0; } U_CAPI const char* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_fgetcodepage(UFILE *file) { - UErrorCode status = U_ZERO_ERROR; - const char *codepage; + UErrorCode status = U_ZERO_ERROR; + const char *codepage; - codepage = ucnv_getName(file->fConverter, &status); - if(U_FAILURE(status)) return 0; - return codepage; + codepage = ucnv_getName(file->fConverter, &status); + if(U_FAILURE(status)) + return 0; + return codepage; } U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_fsetcodepage( const char *codepage, - UFILE *file) + UFILE *file) { - UErrorCode status = U_ZERO_ERROR; + UErrorCode status = U_ZERO_ERROR; - /* if the codepage is 0, use the default for the locale */ - if(codepage == 0) { - codepage = uprv_defaultCodePageForLocale(file->fBundle->fLocale); - - /* if the codepage is still 0, fall back on the default codepage */ - } + /* if the codepage is 0, use the default for the locale */ + if(codepage == 0) { + codepage = uprv_defaultCodePageForLocale(file->fBundle->fLocale); - ucnv_close(file->fConverter); - file->fConverter = ucnv_open(codepage, &status); - if(U_FAILURE(status)) - return -1; - return 0; + /* if the codepage is still 0, fall back on the default codepage */ + } + + ucnv_close(file->fConverter); + file->fConverter = ucnv_open(codepage, &status); + if(U_FAILURE(status)) + return -1; + return 0; } U_CAPI UConverter * U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_fgetConverter(UFILE *file) { - return file->fConverter; + return file->fConverter; } diff --git a/icu4c/source/extra/ustdio/ufmt_cmn.c b/icu4c/source/extra/ustdio/ufmt_cmn.c index adf2aa0d488..761a681d39f 100644 --- a/icu4c/source/extra/ustdio/ufmt_cmn.c +++ b/icu4c/source/extra/ustdio/ufmt_cmn.c @@ -18,7 +18,7 @@ ****************************************************************************** */ -#include +#include "cmemory.h" #include "ufmt_cmn.h" #include "unicode/uchar.h" #include "unicode/ucnv.h" @@ -136,7 +136,7 @@ ufmt_defaultCPToUnicode(const char *s, /* perform the conversion in one swoop */ size = (len + 1) / ucnv_getMinCharSize(defConverter); - target = (UChar*) malloc(size * sizeof(UChar)); + target = (UChar*) uprv_malloc(size * sizeof(UChar)); if(target != 0) { alias = target; @@ -167,7 +167,7 @@ ufmt_unicodeToDefaultCP(const UChar *s, /* perform the conversion in one swoop */ target = (char*) - malloc((len + 1) * ucnv_getMaxCharSize(defConverter) * sizeof(char)); + uprv_malloc((len + 1) * ucnv_getMaxCharSize(defConverter) * sizeof(char)); size = (len) * ucnv_getMaxCharSize(defConverter) * sizeof(char); if(target != 0) { diff --git a/icu4c/source/extra/ustdio/uprintf.c b/icu4c/source/extra/ustdio/uprintf.c index 7c7155fc46c..f3ef11c7785 100644 --- a/icu4c/source/extra/ustdio/uprintf.c +++ b/icu4c/source/extra/ustdio/uprintf.c @@ -27,8 +27,7 @@ #include "unicode/unum.h" #include "unicode/udat.h" -#include -#include +#include "cmemory.h" /* --- Prototypes ---------------------------- */ @@ -236,276 +235,275 @@ static const u_printf_info g_u_printf_infos[108] = { int32_t u_fprintf( UFILE *f, - const char *patternSpecification, - ... ) + const char *patternSpecification, + ... ) { - va_list ap; - int32_t count; + va_list ap; + int32_t count; - va_start(ap, patternSpecification); - count = u_vfprintf(f, patternSpecification, ap); - va_end(ap); + va_start(ap, patternSpecification); + count = u_vfprintf(f, patternSpecification, ap); + va_end(ap); - return count; + return count; } int32_t u_fprintf_u( UFILE *f, - const UChar *patternSpecification, - ... ) + const UChar *patternSpecification, + ... ) { - va_list ap; - int32_t count; + va_list ap; + int32_t count; - va_start(ap, patternSpecification); - count = u_vfprintf_u(f, patternSpecification, ap); - va_end(ap); + va_start(ap, patternSpecification); + count = u_vfprintf_u(f, patternSpecification, ap); + va_end(ap); - return count; + return count; } U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_vfprintf( UFILE *f, - const char *patternSpecification, - va_list ap) + const char *patternSpecification, + va_list ap) { - int32_t count; - UChar *pattern; + int32_t count; + UChar *pattern; - /* convert from the default codepage to Unicode */ - pattern = ufmt_defaultCPToUnicode(patternSpecification, - strlen(patternSpecification)); - if(pattern == 0) { - return 0; - } + /* convert from the default codepage to Unicode */ + pattern = ufmt_defaultCPToUnicode(patternSpecification, + strlen(patternSpecification)); + if(pattern == 0) { + return 0; + } - /* do the work */ - count = u_vfprintf_u(f, pattern, ap); + /* do the work */ + count = u_vfprintf_u(f, pattern, ap); - /* clean up */ - free(pattern); + /* clean up */ + uprv_free(pattern); - return count; + return count; } static int32_t u_printf_pad_and_justify(UFILE *stream, - const u_printf_spec_info *info, - const UChar *result, - int32_t resultLen) + const u_printf_spec_info *info, + const UChar *result, + int32_t resultLen) { - int32_t written, i; + int32_t written, i; - /* pad and justify, if needed */ - if(info->fWidth != -1 && resultLen < info->fWidth) { - /* left justify */ - if(info->fLeft) { - written = u_file_write(result, resultLen, stream); - for(i = 0; i < info->fWidth - resultLen; ++i) { - written += u_file_write(&info->fPadChar, 1, stream); - } + /* pad and justify, if needed */ + if(info->fWidth != -1 && resultLen < info->fWidth) { + /* left justify */ + if(info->fLeft) { + written = u_file_write(result, resultLen, stream); + for(i = 0; i < info->fWidth - resultLen; ++i) { + written += u_file_write(&info->fPadChar, 1, stream); + } + } + /* right justify */ + else { + written = 0; + for(i = 0; i < info->fWidth - resultLen; ++i) { + written += u_file_write(&info->fPadChar, 1, stream); + } + written += u_file_write(result, resultLen, stream); + } } - /* right justify */ - else { - written = 0; - for(i = 0; i < info->fWidth - resultLen; ++i) { - written += u_file_write(&info->fPadChar, 1, stream); - } - written += u_file_write(result, resultLen, stream); - } - } - /* just write the formatted output */ - else - written = u_file_write(result, resultLen, stream); + /* just write the formatted output */ + else + written = u_file_write(result, resultLen, stream); - return written; + return written; } /* handle a '%' */ int32_t u_printf_simple_percent_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - /* put a single '%' on the stream */ - u_fputc(0x0025, stream); - /* we wrote one character */ - return 1; + /* put a single '%' on the stream */ + u_fputc(0x0025, stream); + /* we wrote one character */ + return 1; } /* handle 's' */ int32_t u_printf_string_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - UChar *s; - int32_t len, written; - const char *arg = (const char*)(args[0].ptrValue); + UChar *s; + int32_t len, written; + const char *arg = (const char*)(args[0].ptrValue); - /* convert from the default codepage to Unicode */ - s = ufmt_defaultCPToUnicode(arg, strlen(arg)); - if(s == 0) { - return 0; - } - len = u_strlen(s); + /* convert from the default codepage to Unicode */ + s = ufmt_defaultCPToUnicode(arg, strlen(arg)); + if(s == 0) { + return 0; + } + len = u_strlen(s); - /* width = minimum # of characters to write */ - /* precision = maximum # of characters to write */ + /* width = minimum # of characters to write */ + /* precision = maximum # of characters to write */ - /* precision takes precedence over width */ - /* determine if the string should be truncated */ - if(info->fPrecision != -1 && len > info->fPrecision) { - written = u_file_write(s, info->fPrecision, stream); - } - /* determine if the string should be padded */ - else { - written = u_printf_pad_and_justify(stream, info, s, len); - } + /* precision takes precedence over width */ + /* determine if the string should be truncated */ + if(info->fPrecision != -1 && len > info->fPrecision) { + written = u_file_write(s, info->fPrecision, stream); + } + /* determine if the string should be padded */ + else { + written = u_printf_pad_and_justify(stream, info, s, len); + } - /* clean up */ - free(s); + /* clean up */ + uprv_free(s); - return written; + return written; } /* HSYS */ int32_t u_printf_integer_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - long num = (long) (args[0].intValue); - UNumberFormat *format; - UChar result [UFPRINTF_BUFFER_SIZE]; - int32_t minDigits = -1; - UErrorCode status = U_ZERO_ERROR; + long num = (long) (args[0].intValue); + UNumberFormat *format; + UChar result [UFPRINTF_BUFFER_SIZE]; + int32_t minDigits = -1; + UErrorCode status = U_ZERO_ERROR; + /* mask off any necessary bits */ + if(info->fIsShort) + num &= UINT16_MAX; + else if(! info->fIsLong || ! info->fIsLongLong) + num &= UINT32_MAX; - /* mask off any necessary bits */ - if(info->fIsShort) - num &= UINT16_MAX; - else if(! info->fIsLong || ! info->fIsLongLong) - num &= UINT32_MAX; + /* get the formatter */ + format = u_locbund_getNumberFormat(stream->fBundle); - /* get the formatter */ - format = u_locbund_getNumberFormat(stream->fBundle); + /* handle error */ + if(format == 0) + return 0; - /* handle error */ - if(format == 0) - return 0; + /* set the appropriate flags on the formatter */ - /* set the appropriate flags on the formatter */ + /* set the minimum integer digits */ + if(info->fPrecision != -1) { + /* clone the stream's bundle if it isn't owned */ + if(! stream->fOwnBundle) { + stream->fBundle = u_locbund_clone(stream->fBundle); + stream->fOwnBundle = TRUE; + format = u_locbund_getNumberFormat(stream->fBundle); + } - /* set the minimum integer digits */ - if(info->fPrecision != -1) { - /* clone the stream's bundle if it isn't owned */ - if(! stream->fOwnBundle) { - stream->fBundle = u_locbund_clone(stream->fBundle); - stream->fOwnBundle = TRUE; - format = u_locbund_getNumberFormat(stream->fBundle); + /* set the minimum # of digits */ + minDigits = unum_getAttribute(format, UNUM_MIN_INTEGER_DIGITS); + unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, info->fPrecision); } - /* set the minimum # of digits */ - minDigits = unum_getAttribute(format, UNUM_MIN_INTEGER_DIGITS); - unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, info->fPrecision); - } + /* set whether to show the sign */ + if(info->fShowSign) { + /* clone the stream's bundle if it isn't owned */ + if(! stream->fOwnBundle) { + stream->fBundle = u_locbund_clone(stream->fBundle); + stream->fOwnBundle = TRUE; + format = u_locbund_getNumberFormat(stream->fBundle); + } - /* set whether to show the sign */ - if(info->fShowSign) { - /* clone the stream's bundle if it isn't owned */ - if(! stream->fOwnBundle) { - stream->fBundle = u_locbund_clone(stream->fBundle); - stream->fOwnBundle = TRUE; - format = u_locbund_getNumberFormat(stream->fBundle); + /* set whether to show the sign*/ + /* {sfb} TODO */ } - /* set whether to show the sign*/ - /* {sfb} TODO */ - } + /* format the number */ + unum_format(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); - /* format the number */ - unum_format(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); + /* restore the number format */ + if(minDigits != -1) + unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, minDigits); - /* restore the number format */ - if(minDigits != -1) - unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, minDigits); - - return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); + return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); } int32_t u_printf_hex_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - long num = (long) (args[0].intValue); - UChar result [UFPRINTF_BUFFER_SIZE]; - int32_t len = UFPRINTF_BUFFER_SIZE; + long num = (long) (args[0].intValue); + UChar result[UFPRINTF_BUFFER_SIZE]; + int32_t len = UFPRINTF_BUFFER_SIZE; - /* mask off any necessary bits */ - if(info->fIsShort) - num &= UINT16_MAX; - else if(! info->fIsLong || ! info->fIsLongLong) - num &= UINT32_MAX; + /* mask off any necessary bits */ + if(info->fIsShort) + num &= UINT16_MAX; + else if(! info->fIsLong || ! info->fIsLongLong) + num &= UINT32_MAX; - /* format the number, preserving the minimum # of digits */ - ufmt_ltou(result, &len, num, 16, + /* format the number, preserving the minimum # of digits */ + ufmt_ltou(result, &len, num, 16, (UBool)(info->fSpec == 0x0078), (info->fPrecision == -1 && info->fZero) ? info->fWidth : info->fPrecision); - /* convert to alt form, if desired */ - if(num != 0 && info->fAlt && len < UFPRINTF_BUFFER_SIZE - 2) { - /* shift the formatted string right by 2 chars */ - memmove(result + 2, result, len * sizeof(UChar)); - result[0] = 0x0030; - result[1] = info->fSpec; - len += 2; - } + /* convert to alt form, if desired */ + if(num != 0 && info->fAlt && len < UFPRINTF_BUFFER_SIZE - 2) { + /* shift the formatted string right by 2 chars */ + memmove(result + 2, result, len * sizeof(UChar)); + result[0] = 0x0030; + result[1] = info->fSpec; + len += 2; + } - return u_printf_pad_and_justify(stream, info, result, len); + return u_printf_pad_and_justify(stream, info, result, len); } int32_t u_printf_octal_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - long num = (long) (args[0].intValue); - UChar result [UFPRINTF_BUFFER_SIZE]; - int32_t len = UFPRINTF_BUFFER_SIZE; + long num = (long) (args[0].intValue); + UChar result[UFPRINTF_BUFFER_SIZE]; + int32_t len = UFPRINTF_BUFFER_SIZE; - /* mask off any necessary bits */ - if(info->fIsShort) - num &= UINT16_MAX; - else if(! info->fIsLong || ! info->fIsLongLong) - num &= UINT32_MAX; + /* mask off any necessary bits */ + if(info->fIsShort) + num &= UINT16_MAX; + else if(! info->fIsLong || ! info->fIsLongLong) + num &= UINT32_MAX; - /* format the number, preserving the minimum # of digits */ - ufmt_ltou(result, &len, num, 8, + /* format the number, preserving the minimum # of digits */ + ufmt_ltou(result, &len, num, 8, FALSE, /* doesn't matter for octal */ info->fPrecision == -1 && info->fZero ? info->fWidth : info->fPrecision); - /* convert to alt form, if desired */ - if(info->fAlt && result[0] != 0x0030 && len < UFPRINTF_BUFFER_SIZE - 1) { - /* shift the formatted string right by 1 char */ - memmove(result + 1, result, len * sizeof(UChar)); - result[0] = 0x0030; - len += 1; - } + /* convert to alt form, if desired */ + if(info->fAlt && result[0] != 0x0030 && len < UFPRINTF_BUFFER_SIZE - 1) { + /* shift the formatted string right by 1 char */ + memmove(result + 1, result, len * sizeof(UChar)); + result[0] = 0x0030; + len += 1; + } - return u_printf_pad_and_justify(stream, info, result, len); + return u_printf_pad_and_justify(stream, info, result, len); } int32_t u_printf_uinteger_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { u_printf_spec_info uint_info; ufmt_args uint_args; @@ -524,136 +522,136 @@ u_printf_uinteger_handler(UFILE *stream, int32_t u_printf_double_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - double num = (double) (args[0].doubleValue); - UNumberFormat *format; - UChar result [UFPRINTF_BUFFER_SIZE]; - int32_t minDecimalDigits; - int32_t maxDecimalDigits; - UErrorCode status = U_ZERO_ERROR; + double num = (double) (args[0].doubleValue); + UNumberFormat *format; + UChar result [UFPRINTF_BUFFER_SIZE]; + int32_t minDecimalDigits; + int32_t maxDecimalDigits; + UErrorCode status = U_ZERO_ERROR; - /* mask off any necessary bits */ - /* if(! info->fIsLongDouble) - num &= DBL_MAX;*/ + /* mask off any necessary bits */ + /* if(! info->fIsLongDouble) + num &= DBL_MAX;*/ - /* get the formatter */ - format = u_locbund_getNumberFormat(stream->fBundle); + /* get the formatter */ + format = u_locbund_getNumberFormat(stream->fBundle); - /* handle error */ - if(format == 0) - return 0; + /* handle error */ + if(format == 0) + return 0; - /* set the appropriate flags on the formatter */ + /* set the appropriate flags on the formatter */ - /* clone the stream's bundle if it isn't owned */ - if(! stream->fOwnBundle) { - stream->fBundle = u_locbund_clone(stream->fBundle); - stream->fOwnBundle = TRUE; - format = u_locbund_getNumberFormat(stream->fBundle); - } + /* clone the stream's bundle if it isn't owned */ + if(! stream->fOwnBundle) { + stream->fBundle = u_locbund_clone(stream->fBundle); + stream->fOwnBundle = TRUE; + format = u_locbund_getNumberFormat(stream->fBundle); + } - /* set the number of decimal digits */ + /* set the number of decimal digits */ - /* save the formatter's state */ - minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS); - maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS); + /* save the formatter's state */ + minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS); + maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS); - if(info->fPrecision != -1) { - /* set the # of decimal digits */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision); - } - else if(info->fPrecision == 0 && ! info->fAlt) { - /* no decimal point in this case */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, 0); - } - else if(info->fAlt) { - /* '#' means always show decimal point */ - /* copy of printf behavior on Solaris - '#' shows 6 digits */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); - } - else { - /* # of decimal digits is 6 if precision not specified */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); - } + if(info->fPrecision != -1) { + /* set the # of decimal digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision); + } + else if(info->fPrecision == 0 && ! info->fAlt) { + /* no decimal point in this case */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 0); + } + else if(info->fAlt) { + /* '#' means always show decimal point */ + /* copy of printf behavior on Solaris - '#' shows 6 digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } + else { + /* # of decimal digits is 6 if precision not specified */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } - /* set whether to show the sign */ - if(info->fShowSign) { - /* set whether to show the sign*/ - /* {sfb} TODO */ - } + /* set whether to show the sign */ + if(info->fShowSign) { + /* set whether to show the sign*/ + /* {sfb} TODO */ + } - /* format the number */ - unum_formatDouble(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); + /* format the number */ + unum_formatDouble(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); - /* restore the number format */ - unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits); - unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits); + /* restore the number format */ + unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits); + unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits); - return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); + return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); } int32_t u_printf_char_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - UChar *s; - int32_t len = 1, written; - unsigned char arg = (unsigned char)(args[0].intValue); + UChar *s; + int32_t len = 1, written; + unsigned char arg = (unsigned char)(args[0].intValue); - /* convert from default codepage to Unicode */ - s = ufmt_defaultCPToUnicode((const char *)&arg, 1); - if(s == 0) { - return 0; - } - if (arg != 0) { - len = u_strlen(s); - } + /* convert from default codepage to Unicode */ + s = ufmt_defaultCPToUnicode((const char *)&arg, 1); + if(s == 0) { + return 0; + } + if (arg != 0) { + len = u_strlen(s); + } - /* width = minimum # of characters to write */ - /* precision = maximum # of characters to write */ + /* width = minimum # of characters to write */ + /* precision = maximum # of characters to write */ - /* precision takes precedence over width */ - /* determine if the string should be truncated */ - if(info->fPrecision != -1 && len > info->fPrecision) { - written = u_file_write(s, info->fPrecision, stream); - } - else { - /* determine if the string should be padded */ - written = u_printf_pad_and_justify(stream, info, s, len); - } + /* precision takes precedence over width */ + /* determine if the string should be truncated */ + if(info->fPrecision != -1 && len > info->fPrecision) { + written = u_file_write(s, info->fPrecision, stream); + } + else { + /* determine if the string should be padded */ + written = u_printf_pad_and_justify(stream, info, s, len); + } - /* clean up */ - free(s); + /* clean up */ + uprv_free(s); - return written; + return written; } int32_t u_printf_pointer_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - long num = (long) (args[0].intValue); - UChar result [UFPRINTF_BUFFER_SIZE]; - int32_t len = UFPRINTF_BUFFER_SIZE; + long num = (long) (args[0].intValue); + UChar result[UFPRINTF_BUFFER_SIZE]; + int32_t len = UFPRINTF_BUFFER_SIZE; - /* format the pointer in hex */ - ufmt_ltou(result, &len, num, 16, TRUE, info->fPrecision); + /* format the pointer in hex */ + ufmt_ltou(result, &len, num, 16, TRUE, info->fPrecision); - return u_printf_pad_and_justify(stream, info, result, len); + return u_printf_pad_and_justify(stream, info, result, len); } int32_t u_printf_scientific_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { double num = (double) (args[0].doubleValue); UNumberFormat *format; @@ -695,15 +693,15 @@ u_printf_scientific_handler(UFILE *stream, /* Upper/lower case the e */ if (info->fSpec == (UChar)0x65 /* e */) { expLen = u_strToLower(expBuf, (int32_t)sizeof(expBuf), - srcExpBuf, srcLen, - stream->fBundle->fLocale, - &status); + srcExpBuf, srcLen, + stream->fBundle->fLocale, + &status); } else { expLen = u_strToUpper(expBuf, (int32_t)sizeof(expBuf), - srcExpBuf, srcLen, - stream->fBundle->fLocale, - &status); + srcExpBuf, srcLen, + stream->fBundle->fLocale, + &status); } unum_setSymbol(format, @@ -760,259 +758,259 @@ u_printf_scientific_handler(UFILE *stream, int32_t u_printf_date_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - UDate num = (UDate) (args[0].dateValue); - UDateFormat *format; - UChar result [UFPRINTF_BUFFER_SIZE]; - UErrorCode status = U_ZERO_ERROR; + UDate num = (UDate) (args[0].dateValue); + UDateFormat *format; + UChar result [UFPRINTF_BUFFER_SIZE]; + UErrorCode status = U_ZERO_ERROR; - /* get the formatter */ - format = u_locbund_getDateFormat(stream->fBundle); + /* get the formatter */ + format = u_locbund_getDateFormat(stream->fBundle); - /* handle error */ - if(format == 0) - return 0; + /* handle error */ + if(format == 0) + return 0; - /* format the date */ - udat_format(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); + /* format the date */ + udat_format(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); - return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); + return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); } int32_t u_printf_time_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - UDate num = (UDate) (args[0].dateValue); - UDateFormat *format; - UChar result [UFPRINTF_BUFFER_SIZE]; - UErrorCode status = U_ZERO_ERROR; + UDate num = (UDate) (args[0].dateValue); + UDateFormat *format; + UChar result [UFPRINTF_BUFFER_SIZE]; + UErrorCode status = U_ZERO_ERROR; - /* get the formatter */ - format = u_locbund_getTimeFormat(stream->fBundle); + /* get the formatter */ + format = u_locbund_getTimeFormat(stream->fBundle); - /* handle error */ - if(format == 0) - return 0; + /* handle error */ + if(format == 0) + return 0; - /* format the time */ - udat_format(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); + /* format the time */ + udat_format(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); - return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); + return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); } int32_t u_printf_percent_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - double num = (double) (args[0].doubleValue); - UNumberFormat *format; - UChar result [UFPRINTF_BUFFER_SIZE]; - int32_t minDecimalDigits; - int32_t maxDecimalDigits; - UErrorCode status = U_ZERO_ERROR; + double num = (double) (args[0].doubleValue); + UNumberFormat *format; + UChar result [UFPRINTF_BUFFER_SIZE]; + int32_t minDecimalDigits; + int32_t maxDecimalDigits; + UErrorCode status = U_ZERO_ERROR; - /* mask off any necessary bits */ - /* if(! info->fIsLongDouble) - num &= DBL_MAX;*/ + /* mask off any necessary bits */ + /* if(! info->fIsLongDouble) + num &= DBL_MAX;*/ - /* get the formatter */ - format = u_locbund_getPercentFormat(stream->fBundle); + /* get the formatter */ + format = u_locbund_getPercentFormat(stream->fBundle); - /* handle error */ - if(format == 0) - return 0; + /* handle error */ + if(format == 0) + return 0; - /* set the appropriate flags on the formatter */ + /* set the appropriate flags on the formatter */ - /* clone the stream's bundle if it isn't owned */ - if(! stream->fOwnBundle) { - stream->fBundle = u_locbund_clone(stream->fBundle); - stream->fOwnBundle = TRUE; - format = u_locbund_getPercentFormat(stream->fBundle); - } + /* clone the stream's bundle if it isn't owned */ + if(! stream->fOwnBundle) { + stream->fBundle = u_locbund_clone(stream->fBundle); + stream->fOwnBundle = TRUE; + format = u_locbund_getPercentFormat(stream->fBundle); + } - /* set the number of decimal digits */ + /* set the number of decimal digits */ - /* save the formatter's state */ - minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS); - maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS); + /* save the formatter's state */ + minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS); + maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS); - if(info->fPrecision != -1) { - /* set the # of decimal digits */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision); - } - else if(info->fPrecision == 0 && ! info->fAlt) { - /* no decimal point in this case */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, 0); - } - else if(info->fAlt) { - /* '#' means always show decimal point */ - /* copy of printf behavior on Solaris - '#' shows 6 digits */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); - } - else { - /* # of decimal digits is 6 if precision not specified */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); - } + if(info->fPrecision != -1) { + /* set the # of decimal digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision); + } + else if(info->fPrecision == 0 && ! info->fAlt) { + /* no decimal point in this case */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 0); + } + else if(info->fAlt) { + /* '#' means always show decimal point */ + /* copy of printf behavior on Solaris - '#' shows 6 digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } + else { + /* # of decimal digits is 6 if precision not specified */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } - /* set whether to show the sign */ - if(info->fShowSign) { - /* set whether to show the sign*/ - /* {sfb} TODO */ - } + /* set whether to show the sign */ + if(info->fShowSign) { + /* set whether to show the sign*/ + /* {sfb} TODO */ + } - /* format the number */ - unum_formatDouble(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); + /* format the number */ + unum_formatDouble(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); - /* restore the number format */ - unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits); - unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits); + /* restore the number format */ + unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits); + unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits); - return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); + return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); } int32_t u_printf_currency_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - double num = (double) (args[0].doubleValue); - UNumberFormat *format; - UChar result [UFPRINTF_BUFFER_SIZE]; - int32_t minDecimalDigits; - int32_t maxDecimalDigits; - UErrorCode status = U_ZERO_ERROR; + double num = (double) (args[0].doubleValue); + UNumberFormat *format; + UChar result [UFPRINTF_BUFFER_SIZE]; + int32_t minDecimalDigits; + int32_t maxDecimalDigits; + UErrorCode status = U_ZERO_ERROR; - /* mask off any necessary bits */ - /* if(! info->fIsLongDouble) - num &= DBL_MAX;*/ + /* mask off any necessary bits */ + /* if(! info->fIsLongDouble) + num &= DBL_MAX;*/ - /* get the formatter */ - format = u_locbund_getCurrencyFormat(stream->fBundle); + /* get the formatter */ + format = u_locbund_getCurrencyFormat(stream->fBundle); - /* handle error */ - if(format == 0) - return 0; + /* handle error */ + if(format == 0) + return 0; - /* set the appropriate flags on the formatter */ + /* set the appropriate flags on the formatter */ - /* clone the stream's bundle if it isn't owned */ - if(! stream->fOwnBundle) { - stream->fBundle = u_locbund_clone(stream->fBundle); - stream->fOwnBundle = TRUE; - format = u_locbund_getCurrencyFormat(stream->fBundle); - } + /* clone the stream's bundle if it isn't owned */ + if(! stream->fOwnBundle) { + stream->fBundle = u_locbund_clone(stream->fBundle); + stream->fOwnBundle = TRUE; + format = u_locbund_getCurrencyFormat(stream->fBundle); + } - /* set the number of decimal digits */ + /* set the number of decimal digits */ - /* save the formatter's state */ - minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS); - maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS); + /* save the formatter's state */ + minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS); + maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS); - if(info->fPrecision != -1) { - /* set the # of decimal digits */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision); - } - else if(info->fPrecision == 0 && ! info->fAlt) { - /* no decimal point in this case */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, 0); - } - else if(info->fAlt) { - /* '#' means always show decimal point */ - /* copy of printf behavior on Solaris - '#' shows 6 digits */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); - } - else { - /* # of decimal digits is 6 if precision not specified */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); - } + if(info->fPrecision != -1) { + /* set the # of decimal digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision); + } + else if(info->fPrecision == 0 && ! info->fAlt) { + /* no decimal point in this case */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 0); + } + else if(info->fAlt) { + /* '#' means always show decimal point */ + /* copy of printf behavior on Solaris - '#' shows 6 digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } + else { + /* # of decimal digits is 6 if precision not specified */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } - /* set whether to show the sign */ - if(info->fShowSign) { - /* set whether to show the sign*/ - /* {sfb} TODO */ - } + /* set whether to show the sign */ + if(info->fShowSign) { + /* set whether to show the sign*/ + /* {sfb} TODO */ + } - /* format the number */ - unum_formatDouble(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); + /* format the number */ + unum_formatDouble(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); - /* restore the number format */ - unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits); - unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits); + /* restore the number format */ + unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits); + unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits); - return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); + return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); } int32_t u_printf_ustring_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - int32_t len, written; - const UChar *arg = (const UChar*)(args[0].ptrValue); + int32_t len, written; + const UChar *arg = (const UChar*)(args[0].ptrValue); - /* allocate enough space for the buffer */ - len = u_strlen(arg); + /* allocate enough space for the buffer */ + len = u_strlen(arg); - /* width = minimum # of characters to write */ - /* precision = maximum # of characters to write */ + /* width = minimum # of characters to write */ + /* precision = maximum # of characters to write */ - /* precision takes precedence over width */ - /* determine if the string should be truncated */ - if(info->fPrecision != -1 && len > info->fPrecision) { - written = u_file_write(arg, info->fPrecision, stream); - } - else { - /* determine if the string should be padded */ - written = u_printf_pad_and_justify(stream, info, arg, len); - } + /* precision takes precedence over width */ + /* determine if the string should be truncated */ + if(info->fPrecision != -1 && len > info->fPrecision) { + written = u_file_write(arg, info->fPrecision, stream); + } + else { + /* determine if the string should be padded */ + written = u_printf_pad_and_justify(stream, info, arg, len); + } - return written; + return written; } int32_t u_printf_uchar_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - int32_t written = 0; - UChar arg = (UChar)(args[0].intValue); + int32_t written = 0; + UChar arg = (UChar)(args[0].intValue); - /* width = minimum # of characters to write */ - /* precision = maximum # of characters to write */ + /* width = minimum # of characters to write */ + /* precision = maximum # of characters to write */ - /* precision takes precedence over width */ - /* determine if the char should be printed */ - if(info->fPrecision != -1 && info->fPrecision < 1) { - /* write nothing */ - written = 0; - } - else { - /* determine if the string should be padded */ - written = u_printf_pad_and_justify(stream, info, &arg, 1); - } + /* precision takes precedence over width */ + /* determine if the char should be printed */ + if(info->fPrecision != -1 && info->fPrecision < 1) { + /* write nothing */ + written = 0; + } + else { + /* determine if the string should be padded */ + written = u_printf_pad_and_justify(stream, info, &arg, 1); + } - return written; + return written; } int32_t u_printf_scidbl_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { u_printf_spec_info scidbl_info; double num = args[0].doubleValue; @@ -1047,244 +1045,244 @@ u_printf_scidbl_handler(UFILE *stream, int32_t u_printf_count_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - int *count = (int*)(args[0].ptrValue); + int *count = (int*)(args[0].ptrValue); - /* in the special case of count, the u_printf_spec_info's width */ - /* will contain the # of chars written thus far */ - *count = info->fWidth; + /* in the special case of count, the u_printf_spec_info's width */ + /* will contain the # of chars written thus far */ + *count = info->fWidth; - return 0; + return 0; } int32_t u_printf_spellout_handler(UFILE *stream, - const u_printf_spec_info *info, - const ufmt_args *args) + const u_printf_spec_info *info, + const ufmt_args *args) { - double num = (double) (args[0].doubleValue); - UNumberFormat *format; - UChar result [UFPRINTF_BUFFER_SIZE]; - int32_t minDecimalDigits; - int32_t maxDecimalDigits; - UErrorCode status = U_ZERO_ERROR; + double num = (double) (args[0].doubleValue); + UNumberFormat *format; + UChar result [UFPRINTF_BUFFER_SIZE]; + int32_t minDecimalDigits; + int32_t maxDecimalDigits; + UErrorCode status = U_ZERO_ERROR; - /* mask off any necessary bits */ - /* if(! info->fIsLongDouble) - num &= DBL_MAX;*/ + /* mask off any necessary bits */ + /* if(! info->fIsLongDouble) + num &= DBL_MAX;*/ - /* get the formatter */ - format = u_locbund_getSpelloutFormat(stream->fBundle); + /* get the formatter */ + format = u_locbund_getSpelloutFormat(stream->fBundle); - /* handle error */ - if(format == 0) - return 0; + /* handle error */ + if(format == 0) + return 0; - /* set the appropriate flags on the formatter */ + /* set the appropriate flags on the formatter */ - /* clone the stream's bundle if it isn't owned */ - if(! stream->fOwnBundle) { - stream->fBundle = u_locbund_clone(stream->fBundle); - stream->fOwnBundle = TRUE; - format = u_locbund_getSpelloutFormat(stream->fBundle); - } + /* clone the stream's bundle if it isn't owned */ + if(! stream->fOwnBundle) { + stream->fBundle = u_locbund_clone(stream->fBundle); + stream->fOwnBundle = TRUE; + format = u_locbund_getSpelloutFormat(stream->fBundle); + } - /* set the number of decimal digits */ + /* set the number of decimal digits */ - /* save the formatter's state */ - minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS); - maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS); + /* save the formatter's state */ + minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS); + maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS); - if(info->fPrecision != -1) { - /* set the # of decimal digits */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision); - } - else if(info->fPrecision == 0 && ! info->fAlt) { - /* no decimal point in this case */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, 0); - } - else if(info->fAlt) { - /* '#' means always show decimal point */ - /* copy of printf behavior on Solaris - '#' shows 6 digits */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); - } - else { - /* # of decimal digits is 6 if precision not specified */ - unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); - } + if(info->fPrecision != -1) { + /* set the # of decimal digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision); + } + else if(info->fPrecision == 0 && ! info->fAlt) { + /* no decimal point in this case */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 0); + } + else if(info->fAlt) { + /* '#' means always show decimal point */ + /* copy of printf behavior on Solaris - '#' shows 6 digits */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } + else { + /* # of decimal digits is 6 if precision not specified */ + unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6); + } - /* set whether to show the sign */ - if(info->fShowSign) { - /* set whether to show the sign*/ - /* {sfb} TODO */ - } + /* set whether to show the sign */ + if(info->fShowSign) { + /* set whether to show the sign*/ + /* {sfb} TODO */ + } - /* format the number */ - unum_formatDouble(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); + /* format the number */ + unum_formatDouble(format, num, result, UFPRINTF_BUFFER_SIZE, 0, &status); - /* restore the number format */ - unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits); - unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits); + /* restore the number format */ + unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits); + unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits); - return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); + return u_printf_pad_and_justify(stream, info, result, u_strlen(result)); } #define UP_PERCENT 0x0025 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_vfprintf_u( UFILE *f, - const UChar *patternSpecification, - va_list ap) + const UChar *patternSpecification, + va_list ap) { - u_printf_spec spec; - const UChar *alias; - int32_t patCount, written; - uint16_t handlerNum; + u_printf_spec spec; + const UChar *alias; + int32_t patCount, written; + uint16_t handlerNum; - ufmt_args args; + ufmt_args args; - ufmt_type_info info; - u_printf_handler handler; + ufmt_type_info info; + u_printf_handler handler; - /* alias the pattern */ - alias = patternSpecification; + /* alias the pattern */ + alias = patternSpecification; - /* haven't written anything yet */ - written = 0; + /* haven't written anything yet */ + written = 0; - /* iterate through the pattern */ - for(;;) { + /* iterate through the pattern */ + for(;;) { - /* find the next '%' */ - patCount = 0; - while(*alias != UP_PERCENT && *alias != 0x0000) { - alias++; - ++patCount; - } - - /* write any characters before the '%' */ - if(patCount > 0) - written += u_file_write(alias - patCount, patCount, f); - - /* break if at end of string */ - if(*alias == 0x0000) - break; - - /* parse the specifier */ - patCount = u_printf_parse_spec(alias, &spec); - - /* fill in the precision and width, if specified out of line */ - - /* width specified out of line */ - if(spec.fInfo.fWidth == -2) { - if(spec.fWidthPos == -1) { - /* read the width from the argument list */ - spec.fInfo.fWidth = va_arg(ap, int); - } - else { - /* handle positional parameter */ - } - - /* if it's negative, take the absolute value and set left alignment */ - if(spec.fInfo.fWidth < 0) { - spec.fInfo.fWidth *= -1; - spec.fInfo.fLeft = TRUE; - } - } - - /* precision specified out of line */ - if(spec.fInfo.fPrecision == -2) { - if(spec.fPrecisionPos == -1) { - /* read the precision from the argument list */ - spec.fInfo.fPrecision = va_arg(ap, int); - } - else { - /* handle positional parameter */ - } - - /* if it's negative, set it to zero */ - if(spec.fInfo.fPrecision < 0) - spec.fInfo.fPrecision = 0; - } - - handlerNum = (uint16_t)(spec.fInfo.fSpec - UPRINTF_BASE_FMT_HANDLERS); - if (handlerNum < UPRINTF_NUM_FMT_HANDLERS) { - /* query the info function for argument information */ - info = g_u_printf_infos[ handlerNum ].info; - if(info > ufmt_simple_percent) { - switch(info) { - - case ufmt_count: - /* set the spec's width to the # of chars written */ - spec.fInfo.fWidth = written; - - case ufmt_char: - case ufmt_uchar: - case ufmt_int: - args.intValue = va_arg(ap, int); - break; - - case ufmt_wchar: - args.wcharValue = va_arg(ap, wchar_t); - break; - - case ufmt_string: - args.ptrValue = va_arg(ap, char*); - break; - - case ufmt_wstring: - args.ptrValue = va_arg(ap, wchar_t*); - break; - - case ufmt_ustring: - args.ptrValue = va_arg(ap, UChar*); - break; - - case ufmt_pointer: - args.ptrValue = va_arg(ap, void*); - break; - - case ufmt_float: - args.floatValue = (float) va_arg(ap, double); - break; - - case ufmt_double: - args.doubleValue = va_arg(ap, double); - break; - - case ufmt_date: - args.dateValue = va_arg(ap, UDate); - break; - - default: - break; /* Should never get here */ - } + /* find the next '%' */ + patCount = 0; + while(*alias != UP_PERCENT && *alias != 0x0000) { + alias++; + ++patCount; } - /* call the handler function */ - handler = g_u_printf_infos[ handlerNum ].handler; - if(handler != 0) { - written += (*handler)(f, &spec.fInfo, &args); + /* write any characters before the '%' */ + if(patCount > 0) + written += u_file_write(alias - patCount, patCount, f); + + /* break if at end of string */ + if(*alias == 0x0000) + break; + + /* parse the specifier */ + patCount = u_printf_parse_spec(alias, &spec); + + /* fill in the precision and width, if specified out of line */ + + /* width specified out of line */ + if(spec.fInfo.fWidth == -2) { + if(spec.fWidthPos == -1) { + /* read the width from the argument list */ + spec.fInfo.fWidth = va_arg(ap, int); + } + else { + /* handle positional parameter */ + } + + /* if it's negative, take the absolute value and set left alignment */ + if(spec.fInfo.fWidth < 0) { + spec.fInfo.fWidth *= -1; + spec.fInfo.fLeft = TRUE; + } + } + + /* precision specified out of line */ + if(spec.fInfo.fPrecision == -2) { + if(spec.fPrecisionPos == -1) { + /* read the precision from the argument list */ + spec.fInfo.fPrecision = va_arg(ap, int); + } + else { + /* handle positional parameter */ + } + + /* if it's negative, set it to zero */ + if(spec.fInfo.fPrecision < 0) + spec.fInfo.fPrecision = 0; + } + + handlerNum = (uint16_t)(spec.fInfo.fSpec - UPRINTF_BASE_FMT_HANDLERS); + if (handlerNum < UPRINTF_NUM_FMT_HANDLERS) { + /* query the info function for argument information */ + info = g_u_printf_infos[ handlerNum ].info; + if(info > ufmt_simple_percent) { + switch(info) { + + case ufmt_count: + /* set the spec's width to the # of chars written */ + spec.fInfo.fWidth = written; + + case ufmt_char: + case ufmt_uchar: + case ufmt_int: + args.intValue = va_arg(ap, int); + break; + + case ufmt_wchar: + args.wcharValue = va_arg(ap, wchar_t); + break; + + case ufmt_string: + args.ptrValue = va_arg(ap, char*); + break; + + case ufmt_wstring: + args.ptrValue = va_arg(ap, wchar_t*); + break; + + case ufmt_ustring: + args.ptrValue = va_arg(ap, UChar*); + break; + + case ufmt_pointer: + args.ptrValue = va_arg(ap, void*); + break; + + case ufmt_float: + args.floatValue = (float) va_arg(ap, double); + break; + + case ufmt_double: + args.doubleValue = va_arg(ap, double); + break; + + case ufmt_date: + args.dateValue = va_arg(ap, UDate); + break; + + default: + break; /* Should never get here */ + } + } + + /* call the handler function */ + handler = g_u_printf_infos[ handlerNum ].handler; + if(handler != 0) { + written += (*handler)(f, &spec.fInfo, &args); + } + else { + /* just echo unknown tags */ + written += u_file_write(alias, patCount, f); + } } else { - /* just echo unknown tags */ - written += u_file_write(alias, patCount, f); + /* just echo unknown tags */ + written += u_file_write(alias, patCount, f); } - } - else { - /* just echo unknown tags */ - written += u_file_write(alias, patCount, f); + + /* update the pointer in pattern and continue */ + alias += patCount; } - /* update the pointer in pattern and continue */ - alias += patCount; - } - - /* return # of UChars written */ - return written; + /* return # of UChars written */ + return written; } diff --git a/icu4c/source/extra/ustdio/uscanf.c b/icu4c/source/extra/ustdio/uscanf.c index 6865e0dc87c..b43ebc2cf11 100644 --- a/icu4c/source/extra/ustdio/uscanf.c +++ b/icu4c/source/extra/ustdio/uscanf.c @@ -29,8 +29,7 @@ #include "unicode/unum.h" #include "unicode/udat.h" -#include -#include +#include "cmemory.h" /* --- Prototypes ---------------------------- */ @@ -279,267 +278,267 @@ static const u_scanf_info g_u_scanf_infos[108] = { #define USCANF_BASE_FMT_HANDLERS 0x20 int32_t -u_fscanf( UFILE *f, - const char *patternSpecification, - ... ) +u_fscanf(UFILE *f, + const char *patternSpecification, + ... ) { - va_list ap; - int32_t converted; - - va_start(ap, patternSpecification); - converted = u_vfscanf(f, patternSpecification, ap); - va_end(ap); - - return converted; + va_list ap; + int32_t converted; + + va_start(ap, patternSpecification); + converted = u_vfscanf(f, patternSpecification, ap); + va_end(ap); + + return converted; } int32_t -u_fscanf_u( UFILE *f, - const UChar *patternSpecification, - ... ) +u_fscanf_u(UFILE *f, + const UChar *patternSpecification, + ... ) { - va_list ap; - int32_t converted; - - va_start(ap, patternSpecification); - converted = u_vfscanf_u(f, patternSpecification, ap); - va_end(ap); - - return converted; + va_list ap; + int32_t converted; + + va_start(ap, patternSpecification); + converted = u_vfscanf_u(f, patternSpecification, ap); + va_end(ap); + + return converted; } U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ -u_vfscanf( UFILE *f, - const char *patternSpecification, - va_list ap) +u_vfscanf(UFILE *f, + const char *patternSpecification, + va_list ap) { - int32_t converted; - UChar *pattern; - - /* convert from the default codepage to Unicode */ - pattern = ufmt_defaultCPToUnicode(patternSpecification, - strlen(patternSpecification)); - if(pattern == 0) { - return 0; - } - - /* do the work */ - converted = u_vfscanf_u(f, pattern, ap); + int32_t converted; + UChar *pattern; - /* clean up */ - free(pattern); - - return converted; + /* convert from the default codepage to Unicode */ + pattern = ufmt_defaultCPToUnicode(patternSpecification, + strlen(patternSpecification)); + if(pattern == 0) { + return 0; + } + + /* do the work */ + converted = u_vfscanf_u(f, pattern, ap); + + /* clean up */ + uprv_free(pattern); + + return converted; } static int32_t u_scanf_skip_leading_ws(UFILE *stream, - UChar pad) + UChar pad) { - UChar c; - int32_t count = 0; - - /* skip all leading ws in the stream */ - while( ((c = u_fgetc(stream)) != 0xFFFF) && (c == pad || ufmt_isws(c)) ) - ++count; - - /* put the final character back on the stream */ - if(c != 0xFFFF) - u_fungetc(c, stream); + UChar c; + int32_t count = 0; - return count; + /* skip all leading ws in the stream */ + while( ((c = u_fgetc(stream)) != 0xFFFF) && (c == pad || ufmt_isws(c)) ) + ++count; + + /* put the final character back on the stream */ + if(c != 0xFFFF) + u_fungetc(c, stream); + + return count; } int32_t u_scanf_simple_percent_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - /* make sure the next character in the stream is a percent */ - if(u_fgetc(stream) != 0x0025) - return -1; - else - return 0; + /* make sure the next character in the stream is a percent */ + if(u_fgetc(stream) != 0x0025) + return -1; + else + return 0; } int32_t u_scanf_string_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - UChar c; - int32_t count; - const UChar *source; - UConverter *conv; - UErrorCode status = U_ZERO_ERROR; - char *arg = (char*)(args[0].ptrValue); - char *alias = arg; - char *limit; + UChar c; + int32_t count; + const UChar *source; + UConverter *conv; + UErrorCode status = U_ZERO_ERROR; + char *arg = (char*)(args[0].ptrValue); + char *alias = arg; + char *limit; - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); - - /* get the string one character at a time, truncating to the width */ - count = 0; + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* open the default converter */ - conv = ucnv_open(ucnv_getDefaultName(), &status); + /* get the string one character at a time, truncating to the width */ + count = 0; - if(U_FAILURE(status)) - return -1; + /* open the default converter */ + conv = ucnv_open(ucnv_getDefaultName(), &status); - /* since there is no real limit, just use a reasonable value */ - limit = alias + 2048; /* TODO: Specify a real limit! */ - - while( ((c = u_fgetc(stream)) != 0xFFFF) && - (c != info->fPadChar && ! ufmt_isws(c)) && - (info->fWidth == -1 || count < info->fWidth) ) { - - /* put the character from the stream onto the target */ - source = &c; - - /* convert the character to the default codepage */ - ucnv_fromUnicode(conv, &alias, limit, &source, source + 1, - NULL, TRUE, &status); - if(U_FAILURE(status)) - return -1; - - /* increment the count */ - ++count; - } + return -1; - /* put the final character we read back on the stream */ - if(c != 0xFFFF) - u_fungetc(c, stream); - - /* add the terminator */ - *alias = 0x00; + /* since there is no real limit, just use a reasonable value */ + limit = alias + 2048; /* TODO: Specify a real limit! */ - /* clean up */ - ucnv_close(conv); + while( ((c = u_fgetc(stream)) != 0xFFFF) && + (c != info->fPadChar && ! ufmt_isws(c)) && + (info->fWidth == -1 || count < info->fWidth) ) { - /* we converted 1 arg */ - return 1; + /* put the character from the stream onto the target */ + source = &c; + + /* convert the character to the default codepage */ + ucnv_fromUnicode(conv, &alias, limit, &source, source + 1, + NULL, TRUE, &status); + + if(U_FAILURE(status)) + return -1; + + /* increment the count */ + ++count; + } + + /* put the final character we read back on the stream */ + if(c != 0xFFFF) + u_fungetc(c, stream); + + /* add the terminator */ + *alias = 0x00; + + /* clean up */ + ucnv_close(conv); + + /* we converted 1 arg */ + return 1; } int32_t u_scanf_ustring_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - UChar c; - int32_t count; - UChar *arg = (UChar*)(args[0].ptrValue); - UChar *alias = arg; - - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); - - /* get the string one character at a time, truncating to the width */ - count = 0; + UChar c; + int32_t count; + UChar *arg = (UChar*)(args[0].ptrValue); + UChar *alias = arg; - while( ((c = u_fgetc(stream)) != 0xFFFF) && - (c != info->fPadChar && ! ufmt_isws(c)) && - (info->fWidth == -1 || count < info->fWidth) ) { - - /* put the character from the stream onto the target */ - *alias++ = c; - - /* increment the count */ - ++count; - } + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* put the final character we read back on the stream */ - if(c != 0xFFFF) - u_fungetc(c, stream); - - /* add the terminator */ - *alias = 0x0000; + /* get the string one character at a time, truncating to the width */ + count = 0; - /* we converted 1 arg */ - return 1; + while( ((c = u_fgetc(stream)) != 0xFFFF) && + (c != info->fPadChar && ! ufmt_isws(c)) && + (info->fWidth == -1 || count < info->fWidth) ) { + + /* put the character from the stream onto the target */ + *alias++ = c; + + /* increment the count */ + ++count; + } + + /* put the final character we read back on the stream */ + if(c != 0xFFFF) + u_fungetc(c, stream); + + /* add the terminator */ + *alias = 0x0000; + + /* we converted 1 arg */ + return 1; } int32_t u_scanf_count_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int *converted = (int*)(args[0].ptrValue); - - /* in the special case of count, the u_scanf_spec_info's width */ - /* will contain the # of items converted thus far */ - *converted = info->fWidth; + int *converted = (int*)(args[0].ptrValue); - /* we converted 0 args */ - return 0; + /* in the special case of count, the u_scanf_spec_info's width */ + /* will contain the # of items converted thus far */ + *converted = info->fWidth; + + /* we converted 0 args */ + return 0; } int32_t u_scanf_integer_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - long *num = (long*) (args[0].ptrValue); - UNumberFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + long *num = (long*) (args[0].ptrValue); + UNumberFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* fill the stream's internal buffer */ - ufile_fill_uchar_buffer(stream); + /* fill the stream's internal buffer */ + ufile_fill_uchar_buffer(stream); - /* determine the size of the stream's buffer */ - len = stream->fUCLimit - stream->fUCPos; + /* determine the size of the stream's buffer */ + len = stream->fUCLimit - stream->fUCPos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getNumberFormat(stream->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *num = unum_parse(format, stream->fUCPos, len, &parsePos, &status); + /* get the formatter */ + format = u_locbund_getNumberFormat(stream->fBundle); - /* mask off any necessary bits */ - if(info->fIsShort) - *num &= UINT16_MAX; - else if(! info->fIsLong || ! info->fIsLongLong) - *num &= UINT32_MAX; + /* handle error */ + if(format == 0) + return 0; - /* update the stream's position to reflect consumed data */ - stream->fUCPos += parsePos; - - /* we converted 1 arg */ - return 1; + /* parse the number */ + *num = unum_parse(format, stream->fUCPos, len, &parsePos, &status); + + /* mask off any necessary bits */ + if(info->fIsShort) + *num &= UINT16_MAX; + else if(! info->fIsLong || ! info->fIsLongLong) + *num &= UINT32_MAX; + + /* update the stream's position to reflect consumed data */ + stream->fUCPos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_scanf_uinteger_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { ufmt_args uint_args; int32_t converted_args; @@ -557,788 +556,788 @@ u_scanf_uinteger_handler(UFILE *stream, int32_t u_scanf_double_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - double *num = (double*) (args[0].ptrValue); - UNumberFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + double *num = (double*) (args[0].ptrValue); + UNumberFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* fill the stream's internal buffer */ - ufile_fill_uchar_buffer(stream); + /* fill the stream's internal buffer */ + ufile_fill_uchar_buffer(stream); - /* determine the size of the stream's buffer */ - len = stream->fUCLimit - stream->fUCPos; + /* determine the size of the stream's buffer */ + len = stream->fUCLimit - stream->fUCPos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getNumberFormat(stream->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *num = unum_parseDouble(format, stream->fUCPos, len, &parsePos, &status); + /* get the formatter */ + format = u_locbund_getNumberFormat(stream->fBundle); - /* mask off any necessary bits */ - /* if(! info->fIsLong_double) - num &= DBL_MAX;*/ + /* handle error */ + if(format == 0) + return 0; - /* update the stream's position to reflect consumed data */ - stream->fUCPos += parsePos; - - /* we converted 1 arg */ - return 1; + /* parse the number */ + *num = unum_parseDouble(format, stream->fUCPos, len, &parsePos, &status); + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the stream's position to reflect consumed data */ + stream->fUCPos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_scanf_scientific_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - double *num = (double*) (args[0].ptrValue); - UNumberFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + double *num = (double*) (args[0].ptrValue); + UNumberFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* fill the stream's internal buffer */ - ufile_fill_uchar_buffer(stream); + /* fill the stream's internal buffer */ + ufile_fill_uchar_buffer(stream); - /* determine the size of the stream's buffer */ - len = stream->fUCLimit - stream->fUCPos; + /* determine the size of the stream's buffer */ + len = stream->fUCLimit - stream->fUCPos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getScientificFormat(stream->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *num = unum_parseDouble(format, stream->fUCPos, len, &parsePos, &status); + /* get the formatter */ + format = u_locbund_getScientificFormat(stream->fBundle); - /* mask off any necessary bits */ - /* if(! info->fIsLong_double) - num &= DBL_MAX;*/ + /* handle error */ + if(format == 0) + return 0; - /* update the stream's position to reflect consumed data */ - stream->fUCPos += parsePos; - - /* we converted 1 arg */ - return 1; + /* parse the number */ + *num = unum_parseDouble(format, stream->fUCPos, len, &parsePos, &status); + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the stream's position to reflect consumed data */ + stream->fUCPos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_scanf_scidbl_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - double *num = (double*) (args[0].ptrValue); - UNumberFormat *scientificFormat, *genericFormat; - /*int32_t scientificResult, genericResult;*/ - double scientificResult, genericResult; - int32_t scientificParsePos = 0, genericParsePos = 0; - UErrorCode scientificStatus = U_ZERO_ERROR; - UErrorCode genericStatus = U_ZERO_ERROR; - UBool useScientific; + int32_t len; + double *num = (double*) (args[0].ptrValue); + UNumberFormat *scientificFormat, *genericFormat; + /*int32_t scientificResult, genericResult;*/ + double scientificResult, genericResult; + int32_t scientificParsePos = 0, genericParsePos = 0; + UErrorCode scientificStatus = U_ZERO_ERROR; + UErrorCode genericStatus = U_ZERO_ERROR; + UBool useScientific; - /* since we can't determine by scanning the characters whether */ - /* a number was formatted in the 'f' or 'g' styles, parse the */ - /* string with both formatters, and assume whichever one */ - /* parsed the most is the correct formatter to use */ + /* since we can't determine by scanning the characters whether */ + /* a number was formatted in the 'f' or 'g' styles, parse the */ + /* string with both formatters, and assume whichever one */ + /* parsed the most is the correct formatter to use */ - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* fill the stream's internal buffer */ - ufile_fill_uchar_buffer(stream); + /* fill the stream's internal buffer */ + ufile_fill_uchar_buffer(stream); - /* determine the size of the stream's buffer */ - len = stream->fUCLimit - stream->fUCPos; + /* determine the size of the stream's buffer */ + len = stream->fUCLimit - stream->fUCPos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatters */ - scientificFormat = u_locbund_getScientificFormat(stream->fBundle); - genericFormat = u_locbund_getNumberFormat(stream->fBundle); - - /* handle error */ - if(scientificFormat == 0 || genericFormat == 0) - return 0; - - /* parse the number using each format*/ + /* get the formatters */ + scientificFormat = u_locbund_getScientificFormat(stream->fBundle); + genericFormat = u_locbund_getNumberFormat(stream->fBundle); - scientificResult = unum_parseDouble(scientificFormat, stream->fUCPos, len, - &scientificParsePos, &scientificStatus); + /* handle error */ + if(scientificFormat == 0 || genericFormat == 0) + return 0; - genericResult = unum_parseDouble(genericFormat, stream->fUCPos, len, - &genericParsePos, &genericStatus); + /* parse the number using each format*/ - /* determine which parse made it farther */ - useScientific = (UBool)(scientificParsePos > genericParsePos); - - /* stash the result in num */ - *num = useScientific ? scientificResult : genericResult; - - /* mask off any necessary bits */ - /* if(! info->fIsLong_double) - num &= DBL_MAX;*/ + scientificResult = unum_parseDouble(scientificFormat, stream->fUCPos, len, + &scientificParsePos, &scientificStatus); - /* update the stream's position to reflect consumed data */ - stream->fUCPos += useScientific ? scientificParsePos : genericParsePos; - - /* we converted 1 arg */ - return 1; + genericResult = unum_parseDouble(genericFormat, stream->fUCPos, len, + &genericParsePos, &genericStatus); + + /* determine which parse made it farther */ + useScientific = (UBool)(scientificParsePos > genericParsePos); + + /* stash the result in num */ + *num = useScientific ? scientificResult : genericResult; + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the stream's position to reflect consumed data */ + stream->fUCPos += useScientific ? scientificParsePos : genericParsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_scanf_currency_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - double *num = (double*) (args[0].ptrValue); - UNumberFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + double *num = (double*) (args[0].ptrValue); + UNumberFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* fill the stream's internal buffer */ - ufile_fill_uchar_buffer(stream); + /* fill the stream's internal buffer */ + ufile_fill_uchar_buffer(stream); - /* determine the size of the stream's buffer */ - len = stream->fUCLimit - stream->fUCPos; + /* determine the size of the stream's buffer */ + len = stream->fUCLimit - stream->fUCPos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getCurrencyFormat(stream->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *num = unum_parseDouble(format, stream->fUCPos, len, &parsePos, &status); - - /* mask off any necessary bits */ - /* if(! info->fIsLong_double) - num &= DBL_MAX;*/ + /* get the formatter */ + format = u_locbund_getCurrencyFormat(stream->fBundle); - /* update the stream's position to reflect consumed data */ - stream->fUCPos += parsePos; - - /* we converted 1 arg */ - return 1; + /* handle error */ + if(format == 0) + return 0; + + /* parse the number */ + *num = unum_parseDouble(format, stream->fUCPos, len, &parsePos, &status); + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the stream's position to reflect consumed data */ + stream->fUCPos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_scanf_percent_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - double *num = (double*) (args[0].ptrValue); - UNumberFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + double *num = (double*) (args[0].ptrValue); + UNumberFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* fill the stream's internal buffer */ - ufile_fill_uchar_buffer(stream); + /* fill the stream's internal buffer */ + ufile_fill_uchar_buffer(stream); - /* determine the size of the stream's buffer */ - len = stream->fUCLimit - stream->fUCPos; + /* determine the size of the stream's buffer */ + len = stream->fUCLimit - stream->fUCPos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getPercentFormat(stream->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *num = unum_parseDouble(format, stream->fUCPos, len, &parsePos, &status); + /* get the formatter */ + format = u_locbund_getPercentFormat(stream->fBundle); - /* mask off any necessary bits */ - /* if(! info->fIsLong_double) - num &= DBL_MAX;*/ + /* handle error */ + if(format == 0) + return 0; - /* update the stream's position to reflect consumed data */ - stream->fUCPos += parsePos; - - /* we converted 1 arg */ - return 1; + /* parse the number */ + *num = unum_parseDouble(format, stream->fUCPos, len, &parsePos, &status); + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the stream's position to reflect consumed data */ + stream->fUCPos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_scanf_date_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - UDate *date = (UDate*) (args[0].ptrValue); - UDateFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + UDate *date = (UDate*) (args[0].ptrValue); + UDateFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* fill the stream's internal buffer */ - ufile_fill_uchar_buffer(stream); + /* fill the stream's internal buffer */ + ufile_fill_uchar_buffer(stream); - /* determine the size of the stream's buffer */ - len = stream->fUCLimit - stream->fUCPos; + /* determine the size of the stream's buffer */ + len = stream->fUCLimit - stream->fUCPos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getDateFormat(stream->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *date = udat_parse(format, stream->fUCPos, len, &parsePos, &status); - - /* update the stream's position to reflect consumed data */ - stream->fUCPos += parsePos; - - /* we converted 1 arg */ - return 1; + /* get the formatter */ + format = u_locbund_getDateFormat(stream->fBundle); + + /* handle error */ + if(format == 0) + return 0; + + /* parse the number */ + *date = udat_parse(format, stream->fUCPos, len, &parsePos, &status); + + /* update the stream's position to reflect consumed data */ + stream->fUCPos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_scanf_time_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - UDate *time = (UDate*) (args[0].ptrValue); - UDateFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + UDate *time = (UDate*) (args[0].ptrValue); + UDateFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* fill the stream's internal buffer */ - ufile_fill_uchar_buffer(stream); + /* fill the stream's internal buffer */ + ufile_fill_uchar_buffer(stream); - /* determine the size of the stream's buffer */ - len = stream->fUCLimit - stream->fUCPos; + /* determine the size of the stream's buffer */ + len = stream->fUCLimit - stream->fUCPos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getTimeFormat(stream->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *time = udat_parse(format, stream->fUCPos, len, &parsePos, &status); - - /* update the stream's position to reflect consumed data */ - stream->fUCPos += parsePos; - - /* we converted 1 arg */ - return 1; + /* get the formatter */ + format = u_locbund_getTimeFormat(stream->fBundle); + + /* handle error */ + if(format == 0) + return 0; + + /* parse the number */ + *time = udat_parse(format, stream->fUCPos, len, &parsePos, &status); + + /* update the stream's position to reflect consumed data */ + stream->fUCPos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_scanf_char_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - UChar uc = 0; - char *result; - char *c = (char*)(args[0].ptrValue); - - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); - - /* get the character from the stream, truncating to the width */ - if(info->fWidth == -1 || info->fWidth > 1) - uc = u_fgetc(stream); + UChar uc = 0; + char *result; + char *c = (char*)(args[0].ptrValue); - /* handle EOF */ - if(uc == 0xFFFF) - return -1; + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* convert the character to the default codepage */ - result = ufmt_unicodeToDefaultCP(&uc, 1); - *c = result[0]; + /* get the character from the stream, truncating to the width */ + if(info->fWidth == -1 || info->fWidth > 1) + uc = u_fgetc(stream); - /* clean up */ - free(result); - - /* we converted 1 arg */ - return 1; + /* handle EOF */ + if(uc == 0xFFFF) + return -1; + + /* convert the character to the default codepage */ + result = ufmt_unicodeToDefaultCP(&uc, 1); + *c = result[0]; + + /* clean up */ + uprv_free(result); + + /* we converted 1 arg */ + return 1; } int32_t u_scanf_uchar_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - UChar *c = (UChar*)(args[0].ptrValue); - - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); - - /* get the character from the stream, truncating to the width */ - if(info->fWidth == -1 || info->fWidth > 1) - *c = u_fgetc(stream); + UChar *c = (UChar*)(args[0].ptrValue); - /* handle EOF */ - if(*c == 0xFFFF) - return -1; + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* we converted 1 arg */ - return 1; + /* get the character from the stream, truncating to the width */ + if(info->fWidth == -1 || info->fWidth > 1) + *c = u_fgetc(stream); + + /* handle EOF */ + if(*c == 0xFFFF) + return -1; + + /* we converted 1 arg */ + return 1; } int32_t u_scanf_spellout_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - double *num = (double*) (args[0].ptrValue); - UNumberFormat *format; - int32_t parsePos = 0; - UErrorCode status = U_ZERO_ERROR; + int32_t len; + double *num = (double*) (args[0].ptrValue); + UNumberFormat *format; + int32_t parsePos = 0; + UErrorCode status = U_ZERO_ERROR; - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* fill the stream's internal buffer */ - ufile_fill_uchar_buffer(stream); + /* fill the stream's internal buffer */ + ufile_fill_uchar_buffer(stream); - /* determine the size of the stream's buffer */ - len = stream->fUCLimit - stream->fUCPos; + /* determine the size of the stream's buffer */ + len = stream->fUCLimit - stream->fUCPos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* get the formatter */ - format = u_locbund_getSpelloutFormat(stream->fBundle); - - /* handle error */ - if(format == 0) - return 0; - - /* parse the number */ - *num = unum_parseDouble(format, stream->fUCPos, len, &parsePos, &status); + /* get the formatter */ + format = u_locbund_getSpelloutFormat(stream->fBundle); - /* mask off any necessary bits */ - /* if(! info->fIsLong_double) - num &= DBL_MAX;*/ + /* handle error */ + if(format == 0) + return 0; - /* update the stream's position to reflect consumed data */ - stream->fUCPos += parsePos; - - /* we converted 1 arg */ - return 1; + /* parse the number */ + *num = unum_parseDouble(format, stream->fUCPos, len, &parsePos, &status); + + /* mask off any necessary bits */ + /* if(! info->fIsLong_double) + num &= DBL_MAX;*/ + + /* update the stream's position to reflect consumed data */ + stream->fUCPos += parsePos; + + /* we converted 1 arg */ + return 1; } int32_t u_scanf_hex_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - long *num = (long*) (args[0].ptrValue); + int32_t len; + long *num = (long*) (args[0].ptrValue); - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* fill the stream's internal buffer */ - ufile_fill_uchar_buffer(stream); + /* fill the stream's internal buffer */ + ufile_fill_uchar_buffer(stream); - /* determine the size of the stream's buffer */ - len = stream->fUCLimit - stream->fUCPos; + /* determine the size of the stream's buffer */ + len = stream->fUCLimit - stream->fUCPos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* check for alternate form */ - if( *(stream->fUCPos) == 0x0030 && - (*(stream->fUCPos + 1) == 0x0078 || *(stream->fUCPos + 1) == 0x0058) ) { + /* check for alternate form */ + if( *(stream->fUCPos) == 0x0030 && + (*(stream->fUCPos + 1) == 0x0078 || *(stream->fUCPos + 1) == 0x0058) ) { - /* skip the '0' and 'x' or 'X' if present */ - stream->fUCPos += 2; - len -= 2; - } + /* skip the '0' and 'x' or 'X' if present */ + stream->fUCPos += 2; + len -= 2; + } - /* parse the number */ - *num = ufmt_utol(stream->fUCPos, &len, 16); + /* parse the number */ + *num = ufmt_utol(stream->fUCPos, &len, 16); - /* update the stream's position to reflect consumed data */ - stream->fUCPos += len; + /* update the stream's position to reflect consumed data */ + stream->fUCPos += len; - /* mask off any necessary bits */ - if(info->fIsShort) - *num &= UINT16_MAX; - else if(! info->fIsLong || ! info->fIsLongLong) - *num &= UINT32_MAX; + /* mask off any necessary bits */ + if(info->fIsShort) + *num &= UINT16_MAX; + else if(! info->fIsLong || ! info->fIsLongLong) + *num &= UINT32_MAX; - /* we converted 1 arg */ - return 1; + /* we converted 1 arg */ + return 1; } int32_t u_scanf_octal_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - long *num = (long*) (args[0].ptrValue); + int32_t len; + long *num = (long*) (args[0].ptrValue); - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* fill the stream's internal buffer */ - ufile_fill_uchar_buffer(stream); + /* fill the stream's internal buffer */ + ufile_fill_uchar_buffer(stream); - /* determine the size of the stream's buffer */ - len = stream->fUCLimit - stream->fUCPos; + /* determine the size of the stream's buffer */ + len = stream->fUCLimit - stream->fUCPos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* parse the number */ - *num = ufmt_utol(stream->fUCPos, &len, 8); + /* parse the number */ + *num = ufmt_utol(stream->fUCPos, &len, 8); - /* update the stream's position to reflect consumed data */ - stream->fUCPos += len; + /* update the stream's position to reflect consumed data */ + stream->fUCPos += len; - /* mask off any necessary bits */ - if(info->fIsShort) - *num &= UINT16_MAX; - else if(! info->fIsLong || ! info->fIsLongLong) - *num &= UINT32_MAX; + /* mask off any necessary bits */ + if(info->fIsShort) + *num &= UINT16_MAX; + else if(! info->fIsLong || ! info->fIsLongLong) + *num &= UINT32_MAX; - /* we converted 1 arg */ - return 1; + /* we converted 1 arg */ + return 1; } int32_t u_scanf_pointer_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - int32_t len; - void *p = (void*)(args[0].ptrValue); + int32_t len; + void *p = (void*)(args[0].ptrValue); - /* skip all ws in the stream */ - u_scanf_skip_leading_ws(stream, info->fPadChar); + /* skip all ws in the stream */ + u_scanf_skip_leading_ws(stream, info->fPadChar); - /* fill the stream's internal buffer */ - ufile_fill_uchar_buffer(stream); + /* fill the stream's internal buffer */ + ufile_fill_uchar_buffer(stream); - /* determine the size of the stream's buffer */ - len = stream->fUCLimit - stream->fUCPos; + /* determine the size of the stream's buffer */ + len = stream->fUCLimit - stream->fUCPos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* parse the pointer - cast to void** to assign to *p */ - *(void**)p = (void*) ufmt_utol(stream->fUCPos, &len, 16); + /* parse the pointer - cast to void** to assign to *p */ + *(void**)p = (void*) ufmt_utol(stream->fUCPos, &len, 16); - /* update the stream's position to reflect consumed data */ - stream->fUCPos += len; + /* update the stream's position to reflect consumed data */ + stream->fUCPos += len; - /* we converted 1 arg */ - return 1; + /* we converted 1 arg */ + return 1; } int32_t u_scanf_scanset_handler(UFILE *stream, - const u_scanf_spec_info *info, - ufmt_args *args, - const UChar *fmt, - int32_t *consumed) + const u_scanf_spec_info *info, + ufmt_args *args, + const UChar *fmt, + int32_t *consumed) { - u_scanf_scanset scanset; - int32_t len; - UBool success; - UChar c; - const UChar *source; - UConverter *conv; - UErrorCode status = U_ZERO_ERROR; - char *s = (char*) (args[0].ptrValue); - char *alias, *limit; + u_scanf_scanset scanset; + int32_t len; + UBool success; + UChar c; + const UChar *source; + UConverter *conv; + UErrorCode status = U_ZERO_ERROR; + char *s = (char*) (args[0].ptrValue); + char *alias, *limit; - /* fill the stream's internal buffer */ - ufile_fill_uchar_buffer(stream); + /* fill the stream's internal buffer */ + ufile_fill_uchar_buffer(stream); - /* determine the size of the stream's buffer */ - len = stream->fUCLimit - stream->fUCPos; + /* determine the size of the stream's buffer */ + len = stream->fUCLimit - stream->fUCPos; - /* truncate to the width, if specified */ - if(info->fWidth != -1) - len = ufmt_min(len, info->fWidth); + /* truncate to the width, if specified */ + if(info->fWidth != -1) + len = ufmt_min(len, info->fWidth); - /* alias the target */ - alias = s; - limit = alias + len; + /* alias the target */ + alias = s; + limit = alias + len; - /* parse the scanset from the fmt string */ - *consumed = u_strlen(fmt); - success = u_scanf_scanset_init(&scanset, fmt, consumed); + /* parse the scanset from the fmt string */ + *consumed = u_strlen(fmt); + success = u_scanf_scanset_init(&scanset, fmt, consumed); - /* increment consumed by one to eat the final ']' */ - ++(*consumed); + /* increment consumed by one to eat the final ']' */ + ++(*consumed); - /* open the default converter */ - conv = ucnv_open(ucnv_getDefaultName(), &status); + /* open the default converter */ + conv = ucnv_open(ucnv_getDefaultName(), &status); - /* verify that the parse was successful and the converter opened */ - if(! success || U_FAILURE(status)) - return -1; + /* verify that the parse was successful and the converter opened */ + if(! success || U_FAILURE(status)) + return -1; - /* grab characters one at a time and make sure they are in the scanset */ - while( (c = u_fgetc(stream)) != 0xFFFF && alias < limit) { - if(u_scanf_scanset_in(&scanset, c)) { - source = &c; - /* convert the character to the default codepage */ - ucnv_fromUnicode(conv, &alias, limit, &source, source + 1, - NULL, TRUE, &status); - - if(U_FAILURE(status)) - return -1; + /* grab characters one at a time and make sure they are in the scanset */ + while( (c = u_fgetc(stream)) != 0xFFFF && alias < limit) { + if(u_scanf_scanset_in(&scanset, c)) { + source = &c; + /* convert the character to the default codepage */ + ucnv_fromUnicode(conv, &alias, limit, &source, source + 1, + NULL, TRUE, &status); + + if(U_FAILURE(status)) + return -1; + } + /* if the character's not in the scanset, break out */ + else { + break; + } } - /* if the character's not in the scanset, break out */ - else { - break; - } - } - /* put the final character we read back on the stream */ - if(c != 0xFFFF) - u_fungetc(c, stream); + /* put the final character we read back on the stream */ + if(c != 0xFFFF) + u_fungetc(c, stream); - /* if we didn't match at least 1 character, fail */ - if(alias == s) - return -1; - /* otherwise, add the terminator */ - else - *alias = 0x00; + /* if we didn't match at least 1 character, fail */ + if(alias == s) + return -1; + /* otherwise, add the terminator */ + else + *alias = 0x00; - /* clean up */ - ucnv_close(conv); + /* clean up */ + ucnv_close(conv); - /* we converted 1 arg */ - return 1; + /* we converted 1 arg */ + return 1; } #define UP_PERCENT 0x0025 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ -u_vfscanf_u( UFILE *f, - const UChar *patternSpecification, - va_list ap) +u_vfscanf_u(UFILE *f, + const UChar *patternSpecification, + va_list ap) { - u_scanf_spec spec; - const UChar *alias; - int32_t count, converted, temp; - uint16_t handlerNum; + u_scanf_spec spec; + const UChar *alias; + int32_t count, converted, temp; + uint16_t handlerNum; - ufmt_args args; + ufmt_args args; - ufmt_type_info info; - u_scanf_handler handler; + ufmt_type_info info; + u_scanf_handler handler; - /* alias the pattern */ - alias = patternSpecification; + /* alias the pattern */ + alias = patternSpecification; - /* haven't converted anything yet */ - converted = 0; + /* haven't converted anything yet */ + converted = 0; - /* iterate through the pattern */ - for(;;) { - - /* match any characters up to the next '%' */ - while(*alias != UP_PERCENT && *alias != 0x0000 && u_fgetc(f) == *alias) { - alias++; - } + /* iterate through the pattern */ + for(;;) { - /* if we aren't at a '%', or if we're at end of string, break*/ - if(*alias != UP_PERCENT || *alias == 0x0000) - break; - - /* parse the specifier */ - count = u_scanf_parse_spec(alias, &spec); - - /* update the pointer in pattern */ - alias += count; - - /* skip the argument, if necessary */ - if(spec.fSkipArg) - args.ptrValue = va_arg(ap, int*); - - handlerNum = (uint16_t)(spec.fInfo.fSpec - USCANF_BASE_FMT_HANDLERS); - if (handlerNum < USCANF_NUM_FMT_HANDLERS) { - /* query the info function for argument information */ - info = g_u_scanf_infos[ handlerNum ].info; - if(info > ufmt_simple_percent) { - switch(info) { - - case ufmt_count: - args.intValue = va_arg(ap, int); - /* set the spec's width to the # of items converted */ - spec.fInfo.fWidth = converted; - break; - - case ufmt_char: - case ufmt_uchar: - case ufmt_int: - args.ptrValue = va_arg(ap, int*); - break; - - case ufmt_wchar: - args.ptrValue = va_arg(ap, wchar_t*); - break; - - case ufmt_string: - args.ptrValue = va_arg(ap, char*); - break; - - case ufmt_wstring: - args.ptrValue = va_arg(ap, wchar_t*); - break; - - case ufmt_pointer: - args.ptrValue = va_arg(ap, void*); - break; - - case ufmt_float: - args.ptrValue = va_arg(ap, float*); - break; - - case ufmt_double: - args.ptrValue = va_arg(ap, double*); - break; - - case ufmt_date: - args.ptrValue = va_arg(ap, UDate*); - break; - - case ufmt_ustring: - args.ptrValue = va_arg(ap, UChar*); - break; - - default: - break; /* Should never get here */ - } + /* match any characters up to the next '%' */ + while(*alias != UP_PERCENT && *alias != 0x0000 && u_fgetc(f) == *alias) { + alias++; } - /* call the handler function */ - handler = g_u_scanf_infos[ handlerNum ].handler; - if(handler != 0) { - /* reset count */ - count = 0; - - temp = (*handler)(f, &spec.fInfo, &args, alias, &count); - - /* if the handler encountered an error condition, break */ - if(temp == -1) + /* if we aren't at a '%', or if we're at end of string, break*/ + if(*alias != UP_PERCENT || *alias == 0x0000) break; - /* add to the # of items converted */ - converted += temp; + /* parse the specifier */ + count = u_scanf_parse_spec(alias, &spec); - /* update the pointer in pattern */ - alias += count; + /* update the pointer in pattern */ + alias += count; + + /* skip the argument, if necessary */ + if(spec.fSkipArg) + args.ptrValue = va_arg(ap, int*); + + handlerNum = (uint16_t)(spec.fInfo.fSpec - USCANF_BASE_FMT_HANDLERS); + if (handlerNum < USCANF_NUM_FMT_HANDLERS) { + /* query the info function for argument information */ + info = g_u_scanf_infos[ handlerNum ].info; + if(info > ufmt_simple_percent) { + switch(info) { + + case ufmt_count: + args.intValue = va_arg(ap, int); + /* set the spec's width to the # of items converted */ + spec.fInfo.fWidth = converted; + break; + + case ufmt_char: + case ufmt_uchar: + case ufmt_int: + args.ptrValue = va_arg(ap, int*); + break; + + case ufmt_wchar: + args.ptrValue = va_arg(ap, wchar_t*); + break; + + case ufmt_string: + args.ptrValue = va_arg(ap, char*); + break; + + case ufmt_wstring: + args.ptrValue = va_arg(ap, wchar_t*); + break; + + case ufmt_pointer: + args.ptrValue = va_arg(ap, void*); + break; + + case ufmt_float: + args.ptrValue = va_arg(ap, float*); + break; + + case ufmt_double: + args.ptrValue = va_arg(ap, double*); + break; + + case ufmt_date: + args.ptrValue = va_arg(ap, UDate*); + break; + + case ufmt_ustring: + args.ptrValue = va_arg(ap, UChar*); + break; + + default: + break; /* Should never get here */ + } + } + /* call the handler function */ + handler = g_u_scanf_infos[ handlerNum ].handler; + if(handler != 0) { + + /* reset count */ + count = 0; + + temp = (*handler)(f, &spec.fInfo, &args, alias, &count); + + /* if the handler encountered an error condition, break */ + if(temp == -1) + break; + + /* add to the # of items converted */ + converted += temp; + + /* update the pointer in pattern */ + alias += count; + } + /* else do nothing */ } /* else do nothing */ - } - /* else do nothing */ - /* just ignore unknown tags */ + /* just ignore unknown tags */ } diff --git a/icu4c/source/i18n/choicfmt.cpp b/icu4c/source/i18n/choicfmt.cpp index de0e2efc5c2..ffb31d67e84 100644 --- a/icu4c/source/i18n/choicfmt.cpp +++ b/icu4c/source/i18n/choicfmt.cpp @@ -152,11 +152,15 @@ ChoiceFormat::operator=(const ChoiceFormat& that) if (this != &that) { NumberFormat::operator=(that); fCount = that.fCount; - delete [] fChoiceLimits; fChoiceLimits = 0; - delete [] fClosures; fClosures = 0; - delete [] fChoiceFormats; fChoiceFormats = 0; - fChoiceLimits = new double[fCount]; - fClosures = new UBool[fCount]; + uprv_free(fChoiceLimits); + fChoiceLimits = NULL; + uprv_free(fClosures); + fClosures = NULL; + delete [] fChoiceFormats; + fChoiceFormats = NULL; + + fChoiceLimits = (double*) uprv_malloc( sizeof(double) * fCount); + fClosures = (UBool*) uprv_malloc( sizeof(UBool) * fCount); fChoiceFormats = new UnicodeString[fCount]; uprv_arrayCopy(that.fChoiceLimits, fChoiceLimits, fCount); @@ -170,12 +174,12 @@ ChoiceFormat::operator=(const ChoiceFormat& that) ChoiceFormat::~ChoiceFormat() { - delete [] fChoiceLimits; - fChoiceLimits = 0; - delete [] fClosures; - fClosures = 0; + uprv_free(fChoiceLimits); + fChoiceLimits = NULL; + uprv_free(fClosures); + fClosures = NULL; delete [] fChoiceFormats; - fChoiceFormats = 0; + fChoiceFormats = NULL; fCount = 0; } @@ -265,8 +269,8 @@ ChoiceFormat::applyPattern(const UnicodeString& pattern, } // Allocate the required storage. - double *newLimits = new double[count]; - UBool *newClosures = new UBool[count]; + double *newLimits = (double*) uprv_malloc( sizeof(double) * count); + UBool *newClosures = (UBool*) uprv_malloc( sizeof(UBool) * count); UnicodeString *newFormats = new UnicodeString[count]; // Perform the second pass @@ -347,8 +351,8 @@ ChoiceFormat::applyPattern(const UnicodeString& pattern, newFormats[k] = buf; // Don't modify this object until the parse succeeds - delete[] fChoiceLimits; - delete[] fClosures; + uprv_free(fChoiceLimits); + uprv_free(fClosures); delete[] fChoiceFormats; fCount = count; fChoiceLimits = newLimits; @@ -359,8 +363,8 @@ ChoiceFormat::applyPattern(const UnicodeString& pattern, error: status = U_ILLEGAL_ARGUMENT_ERROR; syntaxError(pattern,i,parseError); - delete[] newLimits; - delete[] newClosures; + uprv_free(newLimits); + uprv_free(newClosures); delete[] newFormats; return; @@ -444,8 +448,8 @@ ChoiceFormat::adoptChoices(double *limits, if(limits == 0 || formats == 0) return; - delete [] fChoiceLimits; - delete [] fClosures; + uprv_free(fChoiceLimits); + uprv_free(fClosures); delete [] fChoiceFormats; fChoiceLimits = limits; fClosures = closures; @@ -453,7 +457,7 @@ ChoiceFormat::adoptChoices(double *limits, fCount = cnt; if (fClosures == 0) { - fClosures = new UBool[fCount]; + fClosures = (UBool*) uprv_malloc( sizeof(UBool) * fCount); int32_t i; for (i=0; iisWritable && m_data_->iteratordata_.string != NULL) { diff --git a/icu4c/source/i18n/fmtable.cpp b/icu4c/source/i18n/fmtable.cpp index 278afce7d3e..0d9b7fe188f 100644 --- a/icu4c/source/i18n/fmtable.cpp +++ b/icu4c/source/i18n/fmtable.cpp @@ -12,13 +12,8 @@ * 03/25/97 clhuang Initial Implementation. ******************************************************************************** */ -#ifndef _FMTABLE #include "unicode/fmtable.h" -#endif - -#ifndef _CMEMORY #include "cmemory.h" -#endif // ***************************************************************************** // class Formattable diff --git a/icu4c/source/i18n/msgfmt.cpp b/icu4c/source/i18n/msgfmt.cpp index 0f9dc3c448c..a346aa0cbd3 100644 --- a/icu4c/source/i18n/msgfmt.cpp +++ b/icu4c/source/i18n/msgfmt.cpp @@ -28,6 +28,7 @@ #include "unicode/ucnv_err.h" #include "unicode/uchar.h" #include "ustrfmt.h" +#include "cmemory.h" // ***************************************************************************** // class MessageFormat @@ -116,8 +117,8 @@ MessageFormat::MessageFormat(const UnicodeString& pattern, fCount(kMaxFormat), fArgumentNumbers(NULL) { - fOffsets = new int32_t[fCount]; - fArgumentNumbers = new int32_t[fCount]; + fOffsets = (int32_t*) uprv_malloc( sizeof(int32_t) * fCount ); + fArgumentNumbers = (int32_t*) uprv_malloc( sizeof(int32_t) * fCount ); for (int32_t i = 0; i < fCount; i++) { fFormats[i] = NULL; // Format instances fOffsets[i] = 0; // Starting offset @@ -131,12 +132,11 @@ MessageFormat::MessageFormat(const UnicodeString& pattern, UErrorCode& success) : fLocale(newLocale), // Uses the default locale fOffsets(NULL), - fCount(0), + fCount(kMaxFormat), fArgumentNumbers(NULL) { - fCount = kMaxFormat; - fOffsets = new int32_t[fCount]; - fArgumentNumbers = new int32_t[fCount]; + fOffsets = (int32_t*) uprv_malloc( sizeof(int32_t) * fCount ); + fArgumentNumbers = (int32_t*) uprv_malloc( sizeof(int32_t) * fCount ); for (int32_t i = 0; i < fCount; i++) { fFormats[i] = NULL; // Format instances fOffsets[i] = 0; // Starting offset @@ -151,12 +151,11 @@ MessageFormat::MessageFormat(const UnicodeString& pattern, UErrorCode& success) : fLocale(newLocale), // Uses the default locale fOffsets(NULL), - fCount(0), + fCount(kMaxFormat), fArgumentNumbers(NULL) { - fCount = kMaxFormat; - fOffsets = new int32_t[fCount]; - fArgumentNumbers = new int32_t[fCount]; + fOffsets = (int32_t*) uprv_malloc( sizeof(int32_t) * fCount ); + fArgumentNumbers = (int32_t*) uprv_malloc( sizeof(int32_t) * fCount ); for (int32_t i = 0; i < fCount; i++) { fFormats[i] = NULL; // Format instances fOffsets[i] = 0; // Starting offset @@ -172,8 +171,8 @@ MessageFormat::~MessageFormat() delete fFormats[i]; } } - delete [] fOffsets; - delete [] fArgumentNumbers; + uprv_free(fOffsets); + uprv_free(fArgumentNumbers); fCount = 0; } @@ -214,14 +213,16 @@ MessageFormat::operator=(const MessageFormat& that) delete fFormats[j]; fFormats[j] = NULL; } - delete [] fOffsets; fOffsets = NULL; - delete [] fArgumentNumbers; fArgumentNumbers = NULL; + uprv_free(fOffsets); + fOffsets = NULL; + uprv_free(fArgumentNumbers); + fArgumentNumbers = NULL; fPattern = that.fPattern; fLocale = that.fLocale; fCount = that.fCount; fMaxOffset = that.fMaxOffset; - fOffsets = new int32_t[fCount]; - fArgumentNumbers = new int32_t[fCount]; + fOffsets = (int32_t*) uprv_malloc( sizeof(int32_t) * fCount ); + fArgumentNumbers = (int32_t*) uprv_malloc( sizeof(int32_t) * fCount ); // Sets up the format instance array, offsets and argument numbers. for (int32_t i = 0; i < fCount; i++) { if (that.fFormats[i] == NULL) { diff --git a/icu4c/source/i18n/name2uni.cpp b/icu4c/source/i18n/name2uni.cpp index 3e807468634..c45e51ec252 100644 --- a/icu4c/source/i18n/name2uni.cpp +++ b/icu4c/source/i18n/name2uni.cpp @@ -136,8 +136,8 @@ void NameUnicodeTransliterator::handleTransliterate(Replaceable& text, UTransPos UChar32 ch; - u_UCharsToChars(buf, cbuf, ibuf+1); - ch = u_charFromName(U_EXTENDED_CHAR_NAME, cbuf, &status); + u_UCharsToChars(buf, cbuf, ibuf+1); + ch = u_charFromName(U_EXTENDED_CHAR_NAME, cbuf, &status); if (U_SUCCESS(status)) { // Lookup succeeded str.truncate(0); diff --git a/icu4c/source/i18n/nfrs.cpp b/icu4c/source/i18n/nfrs.cpp index d61e8e2b5b1..69bbe1acab6 100644 --- a/icu4c/source/i18n/nfrs.cpp +++ b/icu4c/source/i18n/nfrs.cpp @@ -35,7 +35,7 @@ util_lcm(llong x, llong y) { x.abs(); y.abs(); - + if (x == 0 || y == 0) { return 0; } else { @@ -45,7 +45,7 @@ util_lcm(llong x, llong y) } x -= y * (x/y); } while (x != 0); - + return y; } } @@ -54,28 +54,28 @@ util_lcm(llong x, llong y) /** * Calculates the least common multiple of x and y. */ -static llong -util_lcm(llong x, llong y) +static llong +util_lcm(llong x, llong y) { // binary gcd algorithm from Knuth, "The Art of Computer Programming," // vol. 2, 1st ed., pp. 298-299 llong x1 = x; llong y1 = y; - + int p2 = 0; while ((x1 & 1) == 0 && (y1 & 1) == 0) { ++p2; x1 >>= 1; y1 >>= 1; } - + llong t; if ((x1 & 1) == 1) { t = -y1; } else { t = x1; } - + while (t != 0) { while ((t & 1) == 0) { t >>= 1; @@ -87,9 +87,9 @@ util_lcm(llong x, llong y) } t = x1 - y1; } - + llong gcd = x1 << p2; - + // x * y == gcd(x, y) * lcm(x, y) return x / gcd * y; } @@ -104,7 +104,7 @@ static const UChar gFourSpaces[] = { 0x20, 0x20, 0x20, 0x20, 0 }; /* " " */ -static const UChar gPercentPercent[] = +static const UChar gPercentPercent[] = { 0x25, 0x25, 0 }; /* "%%" */ @@ -119,13 +119,13 @@ NFRuleSet::NFRuleSet(UnicodeString* descriptions, int32_t index, UErrorCode& sta for (int i = 0; i < 3; ++i) { fractionRules[i] = NULL; } - + if (U_FAILURE(status)) { return; } - + UnicodeString& description = descriptions[index]; // !!! make sure index is valid - + // if the description begins with a rule set name (the rule set // name can be omitted in formatter descriptions that consist // of only one rule set), copy it out into our "name" member @@ -144,33 +144,33 @@ NFRuleSet::NFRuleSet(UnicodeString* descriptions, int32_t index, UErrorCode& sta } else { name.setTo("%default"); } - + if (description.length() == 0) { // throw new IllegalArgumentException("Empty rule set description"); status = U_PARSE_ERROR; } - + fIsPublic = name.indexOf(gPercentPercent) != 0; - + // all of the other members of NFRuleSet are initialized // by parseRules() } -void -NFRuleSet::parseRules(UnicodeString& description, const RuleBasedNumberFormat* owner, UErrorCode& status) +void +NFRuleSet::parseRules(UnicodeString& description, const RuleBasedNumberFormat* owner, UErrorCode& status) { // start by creating a Vector whose elements are Strings containing // the descriptions of the rules (one rule per element). The rules // are separated by semicolons (there's no escape facility: ALL // semicolons are rule delimiters) - + if (U_FAILURE(status)) { return; } - + // dlf - the original code kept a separate description array for no reason, // so I got rid of it. The loop was too complex so I simplified it. - + UnicodeString currentDescription; UTextOffset oldP = 0; while (oldP < description.length()) { @@ -182,20 +182,20 @@ NFRuleSet::parseRules(UnicodeString& description, const RuleBasedNumberFormat* o NFRule::makeRules(currentDescription, this, rules.last(), owner, rules, status); oldP = p + 1; } - + // for rules that didn't specify a base value, their base values // were initialized to 0. Make another pass through the list and // set all those rules' base values. We also remove any special // rules from the list and put them into their own member variables llong defaultBaseValue = (int32_t)0; - + // (this isn't a for loop because we might be deleting items from // the vector-- we want to make sure we only increment i when // we _didn't_ delete aything from the vector) uint32_t i = 0; while (i < rules.size()) { NFRule* rule = rules[i]; - + switch (rule->getType()) { // if the rule's base value is 0, fill in a default // base value (this will be 1 plus the preceding @@ -209,31 +209,31 @@ NFRuleSet::parseRules(UnicodeString& description, const RuleBasedNumberFormat* o } ++i; break; - + // if it's the negative-number rule, copy it into its own // data member and delete it from the list case NFRule::kNegativeNumberRule: negativeNumberRule = rules.remove(i); break; - + // if it's the improper fraction rule, copy it into the // correct element of fractionRules case NFRule::kImproperFractionRule: fractionRules[0] = rules.remove(i); break; - + // if it's the proper fraction rule, copy it into the // correct element of fractionRules case NFRule::kProperFractionRule: fractionRules[1] = rules.remove(i); break; - + // if it's the master rule, copy it into the // correct element of fractionRules case NFRule::kMasterRule: fractionRules[2] = rules.remove(i); break; - + // if it's a regular rule that already knows its base value, // check to make sure the rules are in order, and update // the default base value for the next rule @@ -261,20 +261,20 @@ NFRuleSet::~NFRuleSet() delete fractionRules[2]; } -UBool -util_equalRules(const NFRule* rule1, const NFRule* rule2) +UBool +util_equalRules(const NFRule* rule1, const NFRule* rule2) { if (rule1) { if (rule2) { return *rule1 == *rule2; - } + } } else if (!rule2) { return TRUE; } return FALSE; } -UBool +UBool NFRuleSet::operator==(const NFRuleSet& rhs) const { if (rules.size() == rhs.rules.size() && @@ -284,7 +284,7 @@ NFRuleSet::operator==(const NFRuleSet& rhs) const util_equalRules(fractionRules[0], rhs.fractionRules[0]) && util_equalRules(fractionRules[1], rhs.fractionRules[1]) && util_equalRules(fractionRules[2], rhs.fractionRules[2])) { - + for (uint32_t i = 0; i < rules.size(); ++i) { if (*rules[i] != *rhs.rules[i]) { return FALSE; @@ -295,28 +295,28 @@ NFRuleSet::operator==(const NFRuleSet& rhs) const return FALSE; } -void +void NFRuleSet::format(llong number, UnicodeString& toAppendTo, int32_t pos) const { NFRule *rule = findNormalRule(number); rule->doFormat(number, toAppendTo, pos); } -void +void NFRuleSet::format(double number, UnicodeString& toAppendTo, int32_t pos) const { NFRule *rule = findDoubleRule(number); rule->doFormat(number, toAppendTo, pos); } -NFRule* +NFRule* NFRuleSet::findDoubleRule(double number) const { // if this is a fraction rule set, use findFractionRuleSetRule() if (isFractionRuleSet()) { return findFractionRuleSetRule(number); } - + // if the number is negative, return the negative number rule // (if there isn't a negative-number rule, we pretend it's a // positive number) @@ -327,25 +327,25 @@ NFRuleSet::findDoubleRule(double number) const number = -number; } } - + // if the number isn't an integer, we use one of the fraction rules... if (number != uprv_floor(number)) { // if the number is between 0 and 1, return the proper // fraction rule if (number < 1 && fractionRules[1]) { return fractionRules[1]; - } + } // otherwise, return the improper fraction rule else if (fractionRules[0]) { return fractionRules[0]; } } - + // if there's a master rule, use it to format the number if (fractionRules[2]) { return fractionRules[2]; - } - + } + // and if we haven't yet returned a rule, use findNormalRule() // to find the applicable rule llong r = number + 0.5; @@ -353,7 +353,7 @@ NFRuleSet::findDoubleRule(double number) const } NFRule * -NFRuleSet::findNormalRule(llong number) const +NFRuleSet::findNormalRule(llong number) const { // if this is a fraction rule set, use findFractionRuleSetRule() // to find the rule (we should only go into this clause if the @@ -361,7 +361,7 @@ NFRuleSet::findNormalRule(llong number) const if (fIsFractionRuleSet) { return findFractionRuleSetRule(number.asDouble()); } - + // if the number is negative, return the negative-number rule // (if there isn't one, pretend the number is positive) if (number < 0) { @@ -371,7 +371,7 @@ NFRuleSet::findNormalRule(llong number) const number = -number; } } - + // we have to repeat the preceding two checks, even though we // do them in findRule(), because the version of format() that // takes a long bypasses findRule() and goes straight to this @@ -380,43 +380,43 @@ NFRuleSet::findNormalRule(llong number) const // rule, since it's considered a fraction rule. Skipping the // master rule in this function is also how we avoid infinite // recursion) - - // {dlf} unfortunately this fails if there are no rules except - // special rules. If there are no rules, use the master rule. + + // {dlf} unfortunately this fails if there are no rules except + // special rules. If there are no rules, use the master rule. // binary-search the rule list for the applicable rule // (a rule is used for all values from its base value to // the next rule's base value) int32_t hi = rules.size(); - if (hi > 0) { - int32_t lo = 0; - - while (lo < hi) { - int32_t mid = (lo + hi) / 2; - if (rules[mid]->getBaseValue() == number) { - return rules[mid]; - } - else if (rules[mid]->getBaseValue() > number) { - hi = mid; - } - else { - lo = mid + 1; - } - } - NFRule *result = rules[hi - 1]; - - // use shouldRollBack() to see whether we need to invoke the - // rollback rule (see shouldRollBack()'s documentation for - // an explanation of the rollback rule). If we do, roll back - // one rule and return that one instead of the one we'd normally - // return - if (result->shouldRollBack(number.asDouble())) { - result = rules[hi - 2]; - } - return result; - } - // else use the master rule - return fractionRules[2]; + if (hi > 0) { + int32_t lo = 0; + + while (lo < hi) { + int32_t mid = (lo + hi) / 2; + if (rules[mid]->getBaseValue() == number) { + return rules[mid]; + } + else if (rules[mid]->getBaseValue() > number) { + hi = mid; + } + else { + lo = mid + 1; + } + } + NFRule *result = rules[hi - 1]; + + // use shouldRollBack() to see whether we need to invoke the + // rollback rule (see shouldRollBack()'s documentation for + // an explanation of the rollback rule). If we do, roll back + // one rule and return that one instead of the one we'd normally + // return + if (result->shouldRollBack(number.asDouble())) { + result = rules[hi - 2]; + } + return result; + } + // else use the master rule + return fractionRules[2]; } /** @@ -435,13 +435,13 @@ NFRuleSet::findNormalRule(llong number) const * @return The rule to use to format this number */ NFRule* -NFRuleSet::findFractionRuleSetRule(double number) const +NFRuleSet::findFractionRuleSetRule(double number) const { // the obvious way to do this (multiply the value being formatted // by each rule's base value until you get an integral result) // doesn't work because of rounding error. This method is more // accurate - + // find the least common multiple of the rules' base values // and multiply this by the number being formatted. This is // all the precision we need, and we can do all of the rest @@ -459,22 +459,22 @@ NFRuleSet::findFractionRuleSetRule(double number) const llong difference(uprv_maxMantissa()); int32_t winner = 0; for (uint32_t i = 0; i < rules.size(); ++i) { - // "numerator" is the numerator of the fraction if the + // "numerator" is the numerator of the fraction if the // denominator is the LCD. The numerator if the rule's // base value is the denominator is "numerator" times the // base value divided bythe LCD. Here we check to see if // that's an integer, and if not, how close it is to being // an integer. tempDifference = numerator * rules[i]->getBaseValue() % leastCommonMultiple; - - + + // normalize the result of the above calculation: we want // the numerator's distance from the CLOSEST multiple // of the LCD if (leastCommonMultiple - tempDifference < tempDifference) { tempDifference = leastCommonMultiple - tempDifference; } - + // if this is as close as we've come, keep track of how close // that is, and the line number of the rule that did it. If // we've scored a direct hit, we don't have to look at any more @@ -487,7 +487,7 @@ NFRuleSet::findFractionRuleSetRule(double number) const } } } - + // if we have two successive rules that both have the winning base // value, then the first one (the one we found above) is used if // the numerator of the fraction is 1 and the second one is used if @@ -501,7 +501,7 @@ NFRuleSet::findFractionRuleSetRule(double number) const ++winner; } } - + // finally, return the winning rule return rules[winner]; } @@ -546,17 +546,17 @@ NFRuleSet::parse(const UnicodeString& text, ParsePosition& pos, double upperBoun // try matching each rule in the rule set against the text being // parsed. Whichever one matches the most characters is the one // that determines the value we return. - + result.setLong(0); - + // dump out if there's no text to parse if (text.length() == 0) { return 0; } - + ParsePosition highWaterMark; ParsePosition workingPos = pos; - + #ifdef RBNF_DEBUG fprintf(stderr, " %x '", this); dumpUS(stderr, name); @@ -565,7 +565,7 @@ NFRuleSet::parse(const UnicodeString& text, ParsePosition& pos, double upperBoun fprintf(stderr, "'\n"); fprintf(stderr, " parse negative: %d\n", this, negativeNumberRule != 0); #endif - + // start by trying the negative number rule (if there is one) if (negativeNumberRule) { Formattable tempResult; @@ -606,7 +606,7 @@ NFRuleSet::parse(const UnicodeString& text, ParsePosition& pos, double upperBoun dumpUS(stderr, text); fprintf(stderr, "' hwm: %d\n", highWaterMark.getIndex()); #endif - + // finally, go through the regular rules one at a time. We start // at the end of the list because we want to try matching the most // sigificant rule first (this helps ensure that we parse @@ -645,32 +645,32 @@ NFRuleSet::parse(const UnicodeString& text, ParsePosition& pos, double upperBoun // first character we didn't use, and return the result that // corresponds to that string of characters pos = highWaterMark; - + return 1; } -void +void NFRuleSet::appendRules(UnicodeString& result) const { // the rule set name goes first... result.append(name); result.append(gColon); result.append(gLineFeed); - + // followed by the regular rules... for (uint32_t i = 0; i < rules.size(); i++) { result.append(gFourSpaces); rules[i]->appendRuleText(result); result.append(gLineFeed); } - + // followed by the special rules (if they exist) if (negativeNumberRule) { result.append(gFourSpaces); negativeNumberRule->appendRuleText(result); result.append(gLineFeed); } - + { for (uint32_t i = 0; i < 3; ++i) { if (fractionRules[i]) { diff --git a/icu4c/source/i18n/nfrule.cpp b/icu4c/source/i18n/nfrule.cpp index 96f64838b80..cb59088dab9 100644 --- a/icu4c/source/i18n/nfrule.cpp +++ b/icu4c/source/i18n/nfrule.cpp @@ -84,13 +84,13 @@ static const UChar * const tokenStrings[] = { gEqualPercent, gEqualHash, gEqualZero, NULL }; -void +void NFRule::makeRules(UnicodeString& description, const NFRuleSet *ruleSet, const NFRule *predecessor, const RuleBasedNumberFormat *rbnf, NFRuleList& rules, - UErrorCode& status) + UErrorCode& status) { // we know we're making at least one rule, so go ahead and // new it up and initialize its basevalue and divisor @@ -127,7 +127,7 @@ NFRule::makeRules(UnicodeString& description, && (rule1->baseValue % ((llong)rule1->radix).pow((int32_t)rule1->exponent)) == 0) || rule1->getType() == kImproperFractionRule || rule1->getType() == kMasterRule) { - + // if it passes that test, new up the second rule. If the // rule set both rules will belong to is a fraction rule // set, they both have the same base value; otherwise, @@ -206,7 +206,7 @@ NFRule::makeRules(UnicodeString& description, * "description" with the descriptor and any trailing whitespace * stripped off. Otherwise; it's "descriptor" unchangd. */ -void +void NFRule::parseRuleDescriptor(UnicodeString& description, UErrorCode& status) { // the description consists of a rule descriptor and a rule body, @@ -312,7 +312,7 @@ NFRule::parseRuleDescriptor(UnicodeString& description, UErrorCode& status) // throw new IllegalArgumentException("Rule can't have radix of 0"); status = U_PARSE_ERROR; } - + exponent = expectedExponent(); } @@ -357,7 +357,7 @@ NFRule::parseRuleDescriptor(UnicodeString& description, UErrorCode& status) * @param predecessor The rule preseding this one in "owners" rule list * @param ownersOwner The RuleBasedFormat that owns this rule */ -void +void NFRule::extractSubstitutions(const NFRuleSet* ruleSet, const NFRule* predecessor, const RuleBasedNumberFormat* rbnf, @@ -405,7 +405,7 @@ NFRule::extractSubstitution(const NFRuleSet* ruleSet, // end will actually find the > in the middle if (ruleText.indexOf(gGreaterGreaterGreater) == subStart) { subEnd = subStart + 2; - + // otherwise the substitution token ends with the same character // it began with } else { @@ -441,8 +441,8 @@ NFRule::extractSubstitution(const NFRuleSet* ruleSet, * constructed. It should be used at any other time. * @param The new base value for the rule. */ -void -NFRule::setBaseValue(llong newBaseValue) +void +NFRule::setBaseValue(llong newBaseValue) { // set the base value baseValue = newBaseValue; @@ -480,8 +480,8 @@ NFRule::setBaseValue(llong newBaseValue) * value. This will be the highest power the radix can be raised to * and still produce a result less than or equal to the base value. */ -int16_t -NFRule::expectedExponent() const +int16_t +NFRule::expectedExponent() const { // since the log of 0, or the log base 0 of something, causes an // error, declare the exponent in these cases to be 0 (we also @@ -510,7 +510,7 @@ NFRule::expectedExponent() const * _any_ of the strings in "strings"). If none of the strings in * "strings" is found in the rule's rule text, returns -1. */ -int32_t +int32_t NFRule::indexOfAny(const UChar* const strings[]) const { int result = -1; @@ -545,10 +545,10 @@ NFRule::operator==(const NFRule& rhs) const /* static void -util_append_llong(UnicodeString& result, const llong& value) +util_append_llong(UnicodeString& result, const llong& value) { llong n(value); - + if (n < 0) { result.append(gMinus); n = -n; @@ -647,7 +647,7 @@ NFRule::appendRuleText(UnicodeString& result) const * @param pos The position in toInsertInto where the resultant text * should be inserted */ -void +void NFRule::doFormat(const llong &number, UnicodeString& toInsertInto, int32_t pos) const { // first, insert the rule's rule text into toInsertInto at the @@ -669,7 +669,7 @@ NFRule::doFormat(const llong &number, UnicodeString& toInsertInto, int32_t pos) * @param pos The position in toInsertInto where the resultant text * should be inserted */ -void +void NFRule::doFormat(double number, UnicodeString& toInsertInto, int32_t pos) const { // first, insert the rule's rule text into toInsertInto at the @@ -691,7 +691,7 @@ NFRule::doFormat(double number, UnicodeString& toInsertInto, int32_t pos) const * @return True if the rule set should use the rule that precedes * this one in its list; false if it should use this rule */ -UBool +UBool NFRule::shouldRollBack(double number) const { // we roll back if the rule contains a modulus substitution, @@ -754,7 +754,7 @@ static void dumpUS(FILE* f, const UnicodeString& us) { UBool NFRule::doParse(const UnicodeString& text, - ParsePosition& parsePosition, + ParsePosition& parsePosition, UBool isFractionRule, double upperBound, Formattable& resVal) const @@ -763,14 +763,14 @@ NFRule::doParse(const UnicodeString& text, // (because we're going to change it) and use our own ParsePosition ParsePosition pp; UnicodeString workText(text); - + // check to see whether the text before the first substitution // matches the text at the beginning of the string being // parsed. If it does, strip that off the front of workText; // otherwise, dump out with a mismatch UnicodeString prefix; prefix.setTo(ruleText, 0, sub1->getPos()); - + #ifdef RBNF_DEBUG fprintf(stderr, "doParse %x ", this); { @@ -778,7 +778,7 @@ NFRule::doParse(const UnicodeString& text, appendRuleText(rt); dumpUS(stderr, rt); } - + fprintf(stderr, " text: '", this); dumpUS(stderr, text); fprintf(stderr, "' prefix: '"); @@ -786,11 +786,11 @@ NFRule::doParse(const UnicodeString& text, #endif stripPrefix(workText, prefix, pp); int32_t prefixLength = text.length() - workText.length(); - + #ifdef RBNF_DEBUG fprintf(stderr, "' pl: %d ppi: %d s1p: %d\n", prefixLength, pp.getIndex(), sub1->getPos()); #endif - + if (pp.getIndex() == 0 && sub1->getPos() != 0) { // commented out because ParsePosition doesn't have error index in 1.1.x // restored for ICU4C port @@ -798,7 +798,7 @@ NFRule::doParse(const UnicodeString& text, resVal.setLong(0); return TRUE; } - + // this is the fun part. The basic guts of the rule-matching // logic is matchToDelimiter(), which is called twice. The first // time it searches the input string for the rule text BETWEEN @@ -832,7 +832,7 @@ NFRule::doParse(const UnicodeString& text, double result = 0; int start = 0; double tempBaseValue = (baseValue <= 0) ? 0 : baseValue.asDouble(); - + UnicodeString temp; do { // our partial parse result starts out as this rule's base @@ -840,23 +840,23 @@ NFRule::doParse(const UnicodeString& text, // will compose this in some way with what it gets back from // the substitution, giving us a new partial parse result pp.setIndex(0); - + temp.setTo(ruleText, sub1->getPos(), sub2->getPos() - sub1->getPos()); double partialResult = matchToDelimiter(workText, start, tempBaseValue, temp, pp, sub1, upperBound); - + // if we got a successful match (or were trying to match a // null substitution), pp is now pointing at the first unmatched // character. Take note of that, and try matchToDelimiter() // on the input text again if (pp.getIndex() != 0 || sub1->isNullSubstitution()) { start = pp.getIndex(); - + UnicodeString workText2; workText2.setTo(workText, pp.getIndex(), workText.length() - pp.getIndex()); ParsePosition pp2; - + // the second matchToDelimiter() will compose our previous // partial result with whatever it gets back from its // substitution if there's a successful match, giving us @@ -865,7 +865,7 @@ NFRule::doParse(const UnicodeString& text, partialResult = matchToDelimiter(workText2, 0, partialResult, temp, pp2, sub2, upperBound); - + // if we got a successful match on this second // matchToDelimiter() call, update the high-water mark // and result (if necessary) @@ -895,11 +895,11 @@ NFRule::doParse(const UnicodeString& text, // keep trying to match things until the outer matchToDelimiter() // call fails to make a match (each time, it picks up where it // left off the previous time) - } while (sub1->getPos() != sub2->getPos() - && pp.getIndex() > 0 - && pp.getIndex() < workText.length() + } while (sub1->getPos() != sub2->getPos() + && pp.getIndex() > 0 + && pp.getIndex() < workText.length() && pp.getIndex() != start); - + // update the caller's ParsePosition with our high-water mark // (i.e., it now points at the first character this function // didn't match-- the ParsePosition is therefore unchanged if @@ -910,19 +910,19 @@ NFRule::doParse(const UnicodeString& text, if (highWaterMark > 0) { parsePosition.setErrorIndex(0); } - + // this is a hack for one unusual condition: Normally, whether this // rule belong to a fraction rule set or not is handled by its // substitutions. But if that rule HAS NO substitutions, then // we have to account for it here. By definition, if the matching // rule in a fraction rule set has no substitutions, its numerator // is 1, and so the result is the reciprocal of its base value. - if (isFractionRule && - highWaterMark > 0 && + if (isFractionRule && + highWaterMark > 0 && sub1->isNullSubstitution()) { result = 1 / result; } - + resVal.setDouble(result); return TRUE; // ??? do we need to worry if it is a long or a double? } @@ -943,7 +943,7 @@ NFRule::doParse(const UnicodeString& text, * @return If things match, this is the unparsed part of "text"; * if they didn't match, this is "text". */ -void +void NFRule::stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosition& pp) const { // if the prefix text is empty, dump out without doing anything @@ -992,13 +992,13 @@ NFRule::stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosit * the result to a double, so we might as well return one. */ double -NFRule::matchToDelimiter(const UnicodeString& text, - int32_t startPos, +NFRule::matchToDelimiter(const UnicodeString& text, + int32_t startPos, double _baseValue, - const UnicodeString& delimiter, - ParsePosition& pp, - const NFSubstitution* sub, - double upperBound) const + const UnicodeString& delimiter, + ParsePosition& pp, + const NFSubstitution* sub, + double upperBound) const { // if "delimiter" contains real (i.e., non-ignorable) text, search // it for "delimiter" beginning at "start". If that succeeds, then @@ -1007,14 +1007,14 @@ NFRule::matchToDelimiter(const UnicodeString& text, if (!allIgnorable(delimiter)) { ParsePosition tempPP; Formattable result; - + // use findText() to search for "delimiter". It returns a two- // element array: element 0 is the position of the match, and // element 1 is the number of characters that matched // "delimiter". int32_t dLen; int32_t dPos = findText(text, delimiter, startPos, &dLen); - + // if findText() succeeded, isolate the text preceding the // match, and use "sub" to match that text while (dPos >= 0) { @@ -1023,7 +1023,7 @@ NFRule::matchToDelimiter(const UnicodeString& text, if (subText.length() > 0) { UBool success = sub->doParse(subText, tempPP, _baseValue, upperBound, formatter->isLenient(), result); - + // if the substitution could match all the text up to // where we found "delimiter", then this function has // a successful match. Bump the caller's parse position @@ -1044,7 +1044,7 @@ NFRule::matchToDelimiter(const UnicodeString& text, } } } - + // if we didn't match the substitution, search for another // copy of "delimiter" in "text" and repeat the loop if // we find it @@ -1055,7 +1055,7 @@ NFRule::matchToDelimiter(const UnicodeString& text, // leave pp unchanged and return 0 pp.setIndex(0); return 0; - + // if "delimiter" is empty, or consists only of ignorable characters // (i.e., is semantically empty), thwe we obviously can't search // for "delimiter". Instead, just use "sub" to parse as much of @@ -1063,7 +1063,7 @@ NFRule::matchToDelimiter(const UnicodeString& text, } else { ParsePosition tempPP; Formattable result; - + // try to match the whole string against the substitution UBool success = sub->doParse(text, tempPP, _baseValue, upperBound, formatter->isLenient(), result); @@ -1080,7 +1080,7 @@ NFRule::matchToDelimiter(const UnicodeString& text, else { pp.setErrorIndex(tempPP.getErrorIndex()); } - + // and if we get to here, then nothing matched, so we return // 0 and leave pp alone return 0; @@ -1109,7 +1109,7 @@ NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix) cons if (prefix.length() == 0) { return 0; } - + // go through all this grief if we're in lenient-parse mode if (formatter->isLenient()) { // get the formatter's collator and use it to create two @@ -1122,46 +1122,46 @@ NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix) cons RuleBasedCollator* collator = (RuleBasedCollator*)formatter->getCollator(); CollationElementIterator* strIter = collator->createCollationElementIterator(str); CollationElementIterator* prefixIter = collator->createCollationElementIterator(prefix); - + UErrorCode err = U_ZERO_ERROR; - - // The original code was problematic. Consider this match: - // prefix = "fifty-" - // string = " fifty-7" - // The intent is to match string up to the '7', by matching 'fifty-' at position 1 - // in the string. Unfortunately, we were getting a match, and then computing where - // the match terminated by rematching the string. The rematch code was using as an - // initial guess the substring of string between 0 and prefix.length. Because of - // the leading space and trailing hyphen (both ignorable) this was succeeding, leaving - // the position before the hyphen in the string. Recursing down, we then parsed the - // remaining string '-7' as numeric. The resulting number turned out as 43 (50 - 7). - // This was not pretty, especially since the string "fifty-7" parsed just fine. - // - // We have newer APIs now, so we can use calls on the iterator to determine what we - // matched up to. If we terminate because we hit the last element in the string, - // our match terminates at this length. If we terminate because we hit the last element - // in the target, our match terminates at one before the element iterator position. + + // The original code was problematic. Consider this match: + // prefix = "fifty-" + // string = " fifty-7" + // The intent is to match string up to the '7', by matching 'fifty-' at position 1 + // in the string. Unfortunately, we were getting a match, and then computing where + // the match terminated by rematching the string. The rematch code was using as an + // initial guess the substring of string between 0 and prefix.length. Because of + // the leading space and trailing hyphen (both ignorable) this was succeeding, leaving + // the position before the hyphen in the string. Recursing down, we then parsed the + // remaining string '-7' as numeric. The resulting number turned out as 43 (50 - 7). + // This was not pretty, especially since the string "fifty-7" parsed just fine. + // + // We have newer APIs now, so we can use calls on the iterator to determine what we + // matched up to. If we terminate because we hit the last element in the string, + // our match terminates at this length. If we terminate because we hit the last element + // in the target, our match terminates at one before the element iterator position. // match collation elements between the strings int32_t oStr = strIter->next(err); int32_t oPrefix = prefixIter->next(err); - + while (oPrefix != CollationElementIterator::NULLORDER) { // skip over ignorable characters in the target string - while (CollationElementIterator::primaryOrder(oStr) == 0 + while (CollationElementIterator::primaryOrder(oStr) == 0 && oStr != CollationElementIterator::NULLORDER) { oStr = strIter->next(err); } - + // skip over ignorable characters in the prefix - while (CollationElementIterator::primaryOrder(oPrefix) == 0 + while (CollationElementIterator::primaryOrder(oPrefix) == 0 && oPrefix != CollationElementIterator::NULLORDER) { oPrefix = prefixIter->next(err); } - - // dlf: move this above following test, if we consume the - // entire target, aren't we ok even if the source was also - // entirely consumed? + + // dlf: move this above following test, if we consume the + // entire target, aren't we ok even if the source was also + // entirely consumed? // if skipping over ignorables brought to the end of // the prefix, we DID match: drop out of the loop @@ -1176,16 +1176,16 @@ NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix) cons delete strIter; return 0; } - + // match collation elements from the two strings // (considering only primary differences). If we // get a mismatch, dump out and return 0 - if (CollationElementIterator::primaryOrder(oStr) + if (CollationElementIterator::primaryOrder(oStr) != CollationElementIterator::primaryOrder(oPrefix)) { delete prefixIter; delete strIter; return 0; - + // otherwise, advance to the next character in each string // and loop (we drop out of the loop when we exhaust // collation elements in the prefix) @@ -1194,26 +1194,26 @@ NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix) cons oPrefix = prefixIter->next(err); } } - - int32_t result = strIter->getOffset(); - if (oStr != CollationElementIterator::NULLORDER) { - --result; // back over character that we don't want to consume; - } + + int32_t result = strIter->getOffset(); + if (oStr != CollationElementIterator::NULLORDER) { + --result; // back over character that we don't want to consume; + } #ifdef RBNF_DEBUG - fprintf(stderr, "prefix length: %d\n", result); + fprintf(stderr, "prefix length: %d\n", result); #endif delete prefixIter; delete strIter; - - return result; + + return result; #if 0 //---------------------------------------------------------------- // JDK 1.2-specific API call // return strIter.getOffset(); //---------------------------------------------------------------- // JDK 1.1 HACK (take out for 1.2-specific code) - + // if we make it to here, we have a successful match. Now we // have to find out HOW MANY characters from the target string // matched the prefix (there isn't necessarily a one-to-one @@ -1229,12 +1229,12 @@ NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix) cons temp.setTo(str, 0, prefix.length()); if (collator->equals(temp, prefix)) { #ifdef RBNF_DEBUG - fprintf(stderr, "returning: %d\n", prefix.length()); + fprintf(stderr, "returning: %d\n", prefix.length()); #endif return prefix.length(); } } - + // if they're not equal, then we have to compare successively // larger and larger substrings of the target string until we // get to one that matches the prefix. At that point, we know @@ -1249,12 +1249,12 @@ NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix) cons ++p; } } - + // SHOULD NEVER GET HERE!!! return 0; //---------------------------------------------------------------- #endif - + // If lenient parsing is turned off, forget all that crap above. // Just use String.startsWith() and be done with it. } else { @@ -1282,8 +1282,8 @@ NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix) cons * the same as the length of "key") */ int32_t -NFRule::findText(const UnicodeString& str, - const UnicodeString& key, +NFRule::findText(const UnicodeString& str, + const UnicodeString& key, int32_t startingAt, int32_t* length) const { @@ -1292,13 +1292,13 @@ NFRule::findText(const UnicodeString& str, if (!formatter->isLenient()) { *length = key.length(); return str.indexOf(key, startingAt); - + // but if lenient parsing is turned ON, we've got some work // ahead of us } else { //---------------------------------------------------------------- // JDK 1.1 HACK (take out of 1.2-specific code) - + // in JDK 1.2, CollationElementIterator provides us with an // API to map between character offsets and collation elements // and we can do this by marching through the string comparing @@ -1306,7 +1306,7 @@ NFRule::findText(const UnicodeString& str, // we have to go through this horrible slow mess: int32_t p = startingAt; int32_t keyLen = 0; - + // basically just isolate smaller and smaller substrings of // the target string (each running to the end of the string, // and with the first one running from startingAt to the end) @@ -1329,7 +1329,7 @@ NFRule::findText(const UnicodeString& str, // which should be "safe" *length = 0; return -1; - + //---------------------------------------------------------------- // JDK 1.2 version of this routine //RuleBasedCollator collator = (RuleBasedCollator)formatter.getCollator(); @@ -1398,21 +1398,21 @@ NFRule::allIgnorable(const UnicodeString& str) const if (str.length() == 0) { return TRUE; } - + // if lenient parsing is turned on, walk through the string with // a collation element iterator and make sure each collation // element is 0 (ignorable) at the primary level if (formatter->isLenient()) { RuleBasedCollator* collator = (RuleBasedCollator*)(formatter->getCollator()); CollationElementIterator* iter = collator->createCollationElementIterator(str); - + UErrorCode err = U_ZERO_ERROR; int32_t o = iter->next(err); while (o != CollationElementIterator::NULLORDER && CollationElementIterator::primaryOrder(o) == 0) { o = iter->next(err); } - + delete iter; return o == CollationElementIterator::NULLORDER; } diff --git a/icu4c/source/i18n/nfsubs.cpp b/icu4c/source/i18n/nfsubs.cpp index 94167692a69..82fbb093978 100644 --- a/icu4c/source/i18n/nfsubs.cpp +++ b/icu4c/source/i18n/nfsubs.cpp @@ -23,15 +23,15 @@ static const UChar gPound = 0x0023; static const UChar gZero = 0x0030; static const UChar gSpace = 0x0020; -static const UChar gEqualsEquals[] = +static const UChar gEqualsEquals[] = { 0x3D, 0x3D, 0 }; /* "==" */ -static const UChar gGreaterGreaterGreaterThan[] = +static const UChar gGreaterGreaterGreaterThan[] = { 0x3E, 0x3E, 0x3E, 0 }; /* ">>>" */ -static const UChar gGreaterGreaterThan[] = +static const UChar gGreaterGreaterThan[] = { 0x3E, 0x3E, 0 }; /* ">>" */ @@ -43,7 +43,7 @@ NFSubstitution::makeSubstitution(int32_t pos, const NFRuleSet* ruleSet, const RuleBasedNumberFormat* formatter, const UnicodeString& description, - UErrorCode& status) + UErrorCode& status) { // if the description is empty, return a NullSubstitution if (description.length() == 0) { @@ -89,7 +89,7 @@ NFSubstitution::makeSubstitution(int32_t pos, if (rule->getBaseValue() == NFRule::kNegativeNumberRule) { return new AbsoluteValueSubstitution(pos, ruleSet, formatter, description, status); } - + // if the rule is a fraction rule, return a // FractionalPartSubstitution else if (rule->getBaseValue() == NFRule::kImproperFractionRule @@ -97,7 +97,7 @@ NFSubstitution::makeSubstitution(int32_t pos, || rule->getBaseValue() == NFRule::kMasterRule) { return new FractionalPartSubstitution(pos, ruleSet, formatter, description, status); } - + // if the rule set owning the rule is a fraction rule set, // throw an exception else if (ruleSet->isFractionRuleSet()) { @@ -223,19 +223,19 @@ NFSubstitution::getDynamicClassID() const { * @param The substitution to compare this one to * @return true if the two substitutions are functionally equivalent */ -UBool +UBool NFSubstitution::operator==(const NFSubstitution& rhs) const { // compare class and all of the fields all substitutions have // in common // this should be called by subclasses before their own equality tests - return getDynamicClassID() == rhs.getDynamicClassID() + return getDynamicClassID() == rhs.getDynamicClassID() && pos == rhs.pos - && (ruleSet == NULL) == (rhs.ruleSet == NULL) + && (ruleSet == NULL) == (rhs.ruleSet == NULL) // && ruleSet == rhs.ruleSet causes circularity, other checks to make instead? - && (numberFormat == NULL - ? (rhs.numberFormat == NULL) - : (*numberFormat == *rhs.numberFormat)); + && (numberFormat == NULL + ? (rhs.numberFormat == NULL) + : (*numberFormat == *rhs.numberFormat)); } /** @@ -244,7 +244,7 @@ NFSubstitution::operator==(const NFSubstitution& rhs) const * not be identical to the description it was created from, but * it'll produce the same result. */ -void +void NFSubstitution::toString(UnicodeString& text) const { // use tokenChar() to get the character at the beginning and @@ -312,7 +312,7 @@ NFSubstitution::doSubstitution(const llong &number, UnicodeString& toInsertInto, * rule text begins (this value is added to this substitution's * position to determine exactly where to insert the new text) */ -void +void NFSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos) const { // perform a transformation on the number being formatted that // is dependent on the type of substitution this is @@ -375,11 +375,11 @@ NFSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32 * no match this is new Long(0) (not null), and parsePosition * is left unchanged. */ -UBool -NFSubstitution::doParse(const UnicodeString& text, - ParsePosition& parsePosition, +UBool +NFSubstitution::doParse(const UnicodeString& text, + ParsePosition& parsePosition, double baseValue, - double upperBound, + double upperBound, UBool lenientParse, Formattable& result) const { @@ -421,8 +421,8 @@ NFSubstitution::doParse(const UnicodeString& text, // of its own). Derive a parse result and return it as a Long, // if possible, or a Double if (parsePosition.getIndex() != 0) { - double tempResult = (result.getType() == Formattable::kLong) ? - (double)result.getLong() : + double tempResult = (result.getType() == Formattable::kLong) ? + (double)result.getLong() : result.getDouble(); // composeRuleValue() produces a full parse result from @@ -458,7 +458,7 @@ NFSubstitution::doParse(const UnicodeString& text, } -UBool +UBool NFSubstitution::isNullSubstitution() const { return FALSE; } @@ -469,7 +469,7 @@ NFSubstitution::isNullSubstitution() const { * proliferate and partially because we have to port this to C++.) * @return true if this object is an instance of ModulusSubstitution */ -UBool +UBool NFSubstitution::isModulusSubstitution() const { return FALSE; } @@ -486,7 +486,7 @@ SameValueSubstitution::SameValueSubstitution(int32_t _pos, const NFRuleSet* _ruleSet, const RuleBasedNumberFormat* formatter, const UnicodeString& description, - UErrorCode& status) + UErrorCode& status) : NFSubstitution(_pos, _ruleSet, formatter, description, status) { if (description == gEqualsEquals) { @@ -591,7 +591,7 @@ ModulusSubstitution::doSubstitution(const llong & number, UnicodeString& toIns // to format its substitution value) if (ruleToUse == NULL) { NFSubstitution::doSubstitution(number, toInsertInto, _pos); - + // a >>> substitution goes straight to a particular rule to // format the substitution value } else { @@ -608,7 +608,7 @@ ModulusSubstitution::doSubstitution(const llong & number, UnicodeString& toIns * into * @param pos The position of the rule text in toInsertInto */ -void +void ModulusSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos) const { // if this isn't a >>> substitution, just use the inherited version @@ -616,12 +616,12 @@ ModulusSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, // to format its substitution value) if (ruleToUse == NULL) { NFSubstitution::doSubstitution(number, toInsertInto, _pos); - + // a >>> substitution goes straight to a particular rule to // format the substitution value } else { double numberToFormat = transformNumber(number); - + ruleToUse->doFormat(numberToFormat, toInsertInto, _pos + getPos()); } } @@ -640,8 +640,8 @@ ModulusSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, * @param baseValue The partial parse result prior to calling this * routine. */ -UBool -ModulusSubstitution::doParse(const UnicodeString& text, +UBool +ModulusSubstitution::doParse(const UnicodeString& text, ParsePosition& parsePosition, double baseValue, double upperBound, @@ -730,14 +730,14 @@ FractionalPartSubstitution::FractionalPartSubstitution(int32_t _pos, * @param pos The position of the owning rule's rule text in * toInsertInto */ -void +void FractionalPartSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos) const { // if we're not in "byDigits" mode, just use the inherited // doSubstitution() routine if (!byDigits) { NFSubstitution::doSubstitution(number, toInsertInto, _pos); - + // if we're in "byDigits" mode, transform the value into an integer // by moving the decimal point eight places to the right and // pulling digits off the right one at a time, formatting each digit @@ -816,7 +816,7 @@ FractionalPartSubstitution::doParse(const UnicodeString& text, digit = temp.getType() == Formattable::kLong ? temp.getLong() : (int32_t)temp.getDouble(); - + if (lenientParse && workPos.getIndex() == 0) { if (!fmt) { UErrorCode status = U_ZERO_ERROR; @@ -851,7 +851,7 @@ FractionalPartSubstitution::doParse(const UnicodeString& text, } } -UBool +UBool FractionalPartSubstitution::operator==(const NFSubstitution& rhs) const { return NFSubstitution::operator==(rhs) && @@ -881,7 +881,7 @@ AbsoluteValueSubstitution::getDynamicClassID() const { // NumeratorSubstitution //=================================================================== -UBool +UBool NumeratorSubstitution::operator==(const NFSubstitution& rhs) const { return NFSubstitution::operator==(rhs) && diff --git a/icu4c/source/i18n/numfmt.cpp b/icu4c/source/i18n/numfmt.cpp index 3c74077ac72..b08a46af07b 100644 --- a/icu4c/source/i18n/numfmt.cpp +++ b/icu4c/source/i18n/numfmt.cpp @@ -21,7 +21,7 @@ * Changed setMaxFractionDigits per Java implementation. ******************************************************************************** */ - + #include "unicode/numfmt.h" #include "unicode/locid.h" #include "unicode/resbund.h" @@ -51,7 +51,7 @@ U_NAMESPACE_BEGIN const char NumberFormat::fgClassID = 0; // Value is irrelevant -// If the maximum base 10 exponent were 4, then the largest number would +// If the maximum base 10 exponent were 4, then the largest number would // be 99,999 which has 5 digits. const int32_t NumberFormat::fgMaxIntegerDigits = DBL_MAX_10_EXP + 1; // Should be ~40 ? --srl const int32_t NumberFormat::fgMinIntegerDigits = 127; @@ -63,7 +63,7 @@ const UChar * const NumberFormat::fgLastResortNumberPatterns[] = gLastResortDecimalPat, gLastResortCurrencyPat, gLastResortPercentPat, - gLastResortScientificPat + gLastResortScientificPat }; // ------------------------------------- @@ -113,7 +113,7 @@ NumberFormat::operator=(const NumberFormat& rhs) } // ------------------------------------- - + UBool NumberFormat::operator==(const Format& that) const { @@ -133,7 +133,7 @@ NumberFormat::operator==(const Format& that) const // ------------------------------------- // Formats the number object and save the format // result in the toAppendTo string buffer. - + UnicodeString& NumberFormat::format(const Formattable& obj, UnicodeString& toAppendTo, @@ -151,10 +151,10 @@ NumberFormat::format(const Formattable& obj, // can't try to format a non-numeric object else { status = U_INVALID_FORMAT_ERROR; - return toAppendTo; + return toAppendTo; } } - + // ------------------------------------- // Parses the string and save the result object as well // as the final parsed position. @@ -166,33 +166,33 @@ NumberFormat::parseObject(const UnicodeString& source, { parse(source, result, parse_pos); } - + // ------------------------------------- // Formats a double number and save the result in a string. - + UnicodeString& NumberFormat::format(double number, UnicodeString& toAppendTo) const { FieldPosition pos(0); return format(number, toAppendTo, pos); } - + // ------------------------------------- // Formats a long number and save the result in a string. - + UnicodeString& NumberFormat::format(int32_t number, UnicodeString& toAppendTo) const { FieldPosition pos(0); return format(number, toAppendTo, pos); } - + // ------------------------------------- // Parses the text and save the result object. If the returned // parse position is 0, that means the parsing failed, the status -// code needs to be set to failure. Ignores the returned parse +// code needs to be set to failure. Ignores the returned parse // position, otherwise. - + void NumberFormat::parse(const UnicodeString& text, Formattable& result, @@ -206,98 +206,98 @@ NumberFormat::parse(const UnicodeString& text, status = U_INVALID_FORMAT_ERROR; } } - + // ------------------------------------- // Sets to only parse integers. - + void NumberFormat::setParseIntegerOnly(UBool value) { fParseIntegerOnly = value; } - + // ------------------------------------- // Create a number style NumberFormat instance with the default locale. - + NumberFormat* NumberFormat::createInstance(UErrorCode& status) { return createInstance(Locale::getDefault(), kNumberStyle, status); } - + // ------------------------------------- // Create a number style NumberFormat instance with the inLocale locale. - + NumberFormat* NumberFormat::createInstance(const Locale& inLocale, UErrorCode& status) { return createInstance(inLocale, kNumberStyle, status); } - + // ------------------------------------- // Create a currency style NumberFormat instance with the default locale. - + NumberFormat* NumberFormat::createCurrencyInstance(UErrorCode& status) { return createInstance(Locale::getDefault(), kCurrencyStyle, status); } - + // ------------------------------------- // Create a currency style NumberFormat instance with the inLocale locale. - + NumberFormat* NumberFormat::createCurrencyInstance(const Locale& inLocale, UErrorCode& status) { return createInstance(inLocale, kCurrencyStyle, status); } - + // ------------------------------------- // Create a percent style NumberFormat instance with the default locale. - + NumberFormat* NumberFormat::createPercentInstance(UErrorCode& status) { return createInstance(Locale::getDefault(), kPercentStyle, status); } - + // ------------------------------------- // Create a percent style NumberFormat instance with the inLocale locale. - + NumberFormat* NumberFormat::createPercentInstance(const Locale& inLocale, UErrorCode& status) { return createInstance(inLocale, kPercentStyle, status); } - + // ------------------------------------- // Create a scientific style NumberFormat instance with the default locale. - + NumberFormat* NumberFormat::createScientificInstance(UErrorCode& status) { return createInstance(Locale::getDefault(), kScientificStyle, status); } - + // ------------------------------------- // Create a scientific style NumberFormat instance with the inLocale locale. - + NumberFormat* NumberFormat::createScientificInstance(const Locale& inLocale, UErrorCode& status) { return createInstance(inLocale, kScientificStyle, status); } - + // ------------------------------------- - + const Locale* NumberFormat::getAvailableLocales(int32_t& count) { return Locale::getAvailableLocales(count); } - + // ------------------------------------- -// Checks if the thousand/10 thousand grouping is used in the +// Checks if the thousand/10 thousand grouping is used in the // NumberFormat instance. UBool @@ -305,30 +305,30 @@ NumberFormat::isGroupingUsed() const { return fGroupingUsed; } - + // ------------------------------------- -// Sets to use the thousand/10 thousand grouping in the +// Sets to use the thousand/10 thousand grouping in the // NumberFormat instance. - + void NumberFormat::setGroupingUsed(UBool newValue) { fGroupingUsed = newValue; } - + // ------------------------------------- // Gets the maximum number of digits for the integral part for // this NumberFormat instance. - + int32_t NumberFormat::getMaximumIntegerDigits() const { return fMaxIntegerDigits; } - + // ------------------------------------- // Sets the maximum number of digits for the integral part for // this NumberFormat instance. - + void NumberFormat::setMaximumIntegerDigits(int32_t newValue) { @@ -336,21 +336,21 @@ NumberFormat::setMaximumIntegerDigits(int32_t newValue) if(fMinIntegerDigits > fMaxIntegerDigits) fMinIntegerDigits = fMaxIntegerDigits; } - + // ------------------------------------- // Gets the minimum number of digits for the integral part for // this NumberFormat instance. - + int32_t NumberFormat::getMinimumIntegerDigits() const { return fMinIntegerDigits; } - + // ------------------------------------- // Sets the minimum number of digits for the integral part for // this NumberFormat instance. - + void NumberFormat::setMinimumIntegerDigits(int32_t newValue) { @@ -358,21 +358,21 @@ NumberFormat::setMinimumIntegerDigits(int32_t newValue) if(fMinIntegerDigits > fMaxIntegerDigits) fMaxIntegerDigits = fMinIntegerDigits; } - + // ------------------------------------- // Gets the maximum number of digits for the fractional part for // this NumberFormat instance. - + int32_t NumberFormat::getMaximumFractionDigits() const { return fMaxFractionDigits; } - + // ------------------------------------- // Sets the maximum number of digits for the fractional part for // this NumberFormat instance. - + void NumberFormat::setMaximumFractionDigits(int32_t newValue) { @@ -380,21 +380,21 @@ NumberFormat::setMaximumFractionDigits(int32_t newValue) if(fMaxFractionDigits < fMinFractionDigits) fMinFractionDigits = fMaxFractionDigits; } - + // ------------------------------------- // Gets the minimum number of digits for the fractional part for // this NumberFormat instance. - + int32_t NumberFormat::getMinimumFractionDigits() const { return fMinFractionDigits; } - + // ------------------------------------- // Sets the minimum number of digits for the fractional part for // this NumberFormat instance. - + void NumberFormat::setMinimumFractionDigits(int32_t newValue) { @@ -402,14 +402,14 @@ NumberFormat::setMinimumFractionDigits(int32_t newValue) if (fMaxFractionDigits < fMinFractionDigits) fMaxFractionDigits = fMinFractionDigits; } - + // ------------------------------------- // Creates the NumberFormat instance of the specified style (number, currency, // or percent) for the desired locale. - + NumberFormat* -NumberFormat::createInstance(const Locale& desiredLocale, - EStyles style, +NumberFormat::createInstance(const Locale& desiredLocale, + EStyles style, UErrorCode& status) { if (U_FAILURE(status)) return NULL; @@ -426,7 +426,7 @@ NumberFormat::createInstance(const Locale& desiredLocale, { // We don't appear to have resource data available -- use the last-resort data status = U_USING_FALLBACK_ERROR; - + // Use the DecimalFormatSymbols constructor which uses last-resort data DecimalFormatSymbols* symbolsToAdopt = new DecimalFormatSymbols(status); if (symbolsToAdopt == NULL) { diff --git a/icu4c/source/i18n/rbnf.cpp b/icu4c/source/i18n/rbnf.cpp index b5d35dda748..41ebdbaa8ad 100644 --- a/icu4c/source/i18n/rbnf.cpp +++ b/icu4c/source/i18n/rbnf.cpp @@ -69,7 +69,7 @@ RuleBasedNumberFormat::RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale& if (U_FAILURE(status)) { return; } - + const char* fmt_tag = ""; switch (tag) { case URBNF_SPELLOUT: fmt_tag = "SpelloutRules"; break; @@ -77,7 +77,7 @@ RuleBasedNumberFormat::RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale& case URBNF_DURATION: fmt_tag = "DurationRules"; break; default: status = U_ILLEGAL_ARGUMENT_ERROR; return; } - + UResourceBundle* nfrb = ures_open(NULL, locale.getName(), &status); int32_t len = 0; const UChar* description = ures_getStringByKey(nfrb, fmt_tag, &len, &status); @@ -102,7 +102,7 @@ RuleBasedNumberFormat::RuleBasedNumberFormat(const RuleBasedNumberFormat& rhs) this->operator=(rhs); } -RuleBasedNumberFormat& +RuleBasedNumberFormat& RuleBasedNumberFormat::operator=(const RuleBasedNumberFormat& rhs) { UErrorCode status = U_ZERO_ERROR; @@ -120,30 +120,30 @@ RuleBasedNumberFormat::~RuleBasedNumberFormat() dispose(); } -Format* +Format* RuleBasedNumberFormat::clone(void) const { RuleBasedNumberFormat * result = NULL; UnicodeString rules = getRules(); UErrorCode status = U_ZERO_ERROR; - UParseError perror; - result = new RuleBasedNumberFormat(rules, locale, perror, status); - if (U_FAILURE(status)) { - delete result; - result = NULL; - } else { - result->lenient = lenient; - } + UParseError perror; + result = new RuleBasedNumberFormat(rules, locale, perror, status); + if (U_FAILURE(status)) { + delete result; + result = NULL; + } else { + result->lenient = lenient; + } return result; } -UBool +UBool RuleBasedNumberFormat::operator==(const Format& other) const { if (this == &other) { return TRUE; } - + if (other.getDynamicClassID() == getStaticClassID()) { const RuleBasedNumberFormat& rhs = (const RuleBasedNumberFormat&)other; if (locale == rhs.locale && @@ -157,11 +157,11 @@ RuleBasedNumberFormat::operator==(const Format& other) const return *q == NULL && *p == NULL; } } - + return FALSE; } -UnicodeString +UnicodeString RuleBasedNumberFormat::getRules() const { UnicodeString result; @@ -171,7 +171,7 @@ RuleBasedNumberFormat::getRules() const return result; } -UnicodeString +UnicodeString RuleBasedNumberFormat::getRuleSetName(int32_t index) const { UnicodeString result; @@ -188,7 +188,7 @@ RuleBasedNumberFormat::getRuleSetName(int32_t index) const } int32_t -RuleBasedNumberFormat::getNumberOfRuleSetNames() const +RuleBasedNumberFormat::getNumberOfRuleSetNames() const { int32_t result = 0; for (NFRuleSet** p = ruleSets; *p; ++p) { @@ -214,7 +214,7 @@ RuleBasedNumberFormat::findRuleSet(const UnicodeString& name, UErrorCode& status return NULL; } -UnicodeString& +UnicodeString& RuleBasedNumberFormat::format(int32_t number, UnicodeString& toAppendTo, FieldPosition& pos) const @@ -224,7 +224,7 @@ RuleBasedNumberFormat::format(int32_t number, } #if 0 -UnicodeString& +UnicodeString& RuleBasedNumberFormat::format(llong number, UnicodeString& toAppendTo, FieldPosition& pos) const @@ -234,7 +234,7 @@ RuleBasedNumberFormat::format(llong number, } #endif -UnicodeString& +UnicodeString& RuleBasedNumberFormat::format(double number, UnicodeString& toAppendTo, FieldPosition& pos) const @@ -244,7 +244,7 @@ RuleBasedNumberFormat::format(double number, } -UnicodeString& +UnicodeString& RuleBasedNumberFormat::format(int32_t number, const UnicodeString& ruleSetName, UnicodeString& toAppendTo, @@ -267,7 +267,7 @@ RuleBasedNumberFormat::format(int32_t number, } #if 0 -UnicodeString& +UnicodeString& RuleBasedNumberFormat::format(llong number, const UnicodeString& ruleSetName, UnicodeString& toAppendTo, @@ -290,16 +290,16 @@ RuleBasedNumberFormat::format(llong number, #endif // make linker happy -UnicodeString& +UnicodeString& RuleBasedNumberFormat::format(const Formattable& obj, UnicodeString& toAppendTo, FieldPosition& pos, - UErrorCode& status) const -{ + UErrorCode& status) const +{ return NumberFormat::format(obj, toAppendTo, pos, status); } -UnicodeString& +UnicodeString& RuleBasedNumberFormat::format(double number, const UnicodeString& ruleSetName, UnicodeString& toAppendTo, @@ -320,35 +320,35 @@ RuleBasedNumberFormat::format(double number, return toAppendTo; } -void +void RuleBasedNumberFormat::parse(const UnicodeString& text, Formattable& result, ParsePosition& parsePosition) const { ParsePosition high_pp; Formattable high_result; - + for (NFRuleSet** p = ruleSets; *p; ++p) { NFRuleSet *rp = *p; if (rp->isPublic()) { ParsePosition working_pp = parsePosition; Formattable working_result; - + rp->parse(text, working_pp, kMaxDouble, working_result); if (working_pp.getIndex() > high_pp.getIndex()) { high_pp = working_pp; high_result = working_result; - + if (high_pp.getIndex() == text.length()) { break; } } } } - - if (high_pp.getIndex() > parsePosition.getIndex()) { - high_pp.setErrorIndex(-1); - } + + if (high_pp.getIndex() > parsePosition.getIndex()) { + high_pp.setErrorIndex(-1); + } parsePosition = high_pp; result = high_result; if (result.getType() == Formattable::kDouble) { @@ -369,27 +369,27 @@ RuleBasedNumberFormat::setLenient(UBool enabled) } } -void +void RuleBasedNumberFormat::init(const UnicodeString& rules, UParseError& pErr, UErrorCode& status) { // TODO: implement UParseError if (U_FAILURE(status)) { return; } - + UnicodeString description(rules); if (!description.length()) { status = U_MEMORY_ALLOCATION_ERROR; return; } - + // start by stripping the trailing whitespace from all the rules // (this is all the whitespace follwing each semicolon in the // description). This allows us to look for rule-set boundaries // by searching for ";%" without having to worry about whitespace // between the ; and the % stripWhitespace(description); - + // check to see if there's a set of lenient-parse rules. If there // is, pull them out into our temporary holding place for them, // and delete them from the description before the real desciption- @@ -404,7 +404,7 @@ RuleBasedNumberFormat::init(const UnicodeString& rules, UParseError& pErr, UErro // rules (there may be whitespace between the name and // the first token in the description) int lpEnd = description.indexOf(gSemiPercent, lp); - + if (lpEnd == -1) { lpEnd = description.length() - 1; } @@ -412,16 +412,16 @@ RuleBasedNumberFormat::init(const UnicodeString& rules, UParseError& pErr, UErro while (u_isWhitespace(description.charAt(lpStart))) { ++lpStart; } - + // copy out the lenient-parse rules and delete them // from the description lenientParseRules = new UnicodeString(); lenientParseRules->setTo(description, lpStart, lpEnd - lpStart); - + description.remove(lp, lpEnd + 1 - lp); } } - + // pre-flight parsing the description and count the number of // rule sets (";%" marks the end of one rule set and the beginning // of the next) @@ -431,13 +431,13 @@ RuleBasedNumberFormat::init(const UnicodeString& rules, UParseError& pErr, UErro ++p; } ++numRuleSets; - + // our rule list is an array of the appropriate size ruleSets = new NFRuleSet*[numRuleSets + 1]; for (int i = 0; i <= numRuleSets; ++i) { ruleSets[i] = NULL; } - + // divide up the descriptions into individual rule-set descriptions // and store them in a temporary array. At each step, we also // new up a rule set, but all this does is initialize its name @@ -446,7 +446,7 @@ RuleBasedNumberFormat::init(const UnicodeString& rules, UParseError& pErr, UErro // because we have to know the names and locations of all the rule // sets before we can actually set everything up UnicodeString* ruleSetDescriptions = new UnicodeString[numRuleSets]; - + { int curRuleSet = 0; UTextOffset start = 0; @@ -459,7 +459,7 @@ RuleBasedNumberFormat::init(const UnicodeString& rules, UParseError& pErr, UErro ruleSetDescriptions[curRuleSet].setTo(description, start, description.length() - start); ruleSets[curRuleSet] = new NFRuleSet(ruleSetDescriptions, curRuleSet, status); } - + // now we can take note of the formatter's default rule set, which // is the last public rule set in the description (it's the last // rather than the first so that a user can create a new formatter @@ -477,7 +477,7 @@ RuleBasedNumberFormat::init(const UnicodeString& rules, UParseError& pErr, UErro } } } - + // finally, we can go back through the temporary descriptions // list and finish seting up the substructure (and we throw // away the temporary descriptions as we go) @@ -486,16 +486,16 @@ RuleBasedNumberFormat::init(const UnicodeString& rules, UParseError& pErr, UErro ruleSets[i]->parseRules(ruleSetDescriptions[i], this, status); } } - + delete[] ruleSetDescriptions; } void -RuleBasedNumberFormat::stripWhitespace(UnicodeString& description) +RuleBasedNumberFormat::stripWhitespace(UnicodeString& description) { // iterate through the characters... UnicodeString result; - + int start = 0; while (start != -1 && start < description.length()) { // seek to the first non-whitespace character... @@ -503,7 +503,7 @@ RuleBasedNumberFormat::stripWhitespace(UnicodeString& description) && u_isWhitespace(description.charAt(start))) { ++start; } - + // locate the next semicolon in the text and copy the text from // our current position up to that semicolon into the result UTextOffset p = description.indexOf(gSemiColon, start); @@ -517,7 +517,7 @@ RuleBasedNumberFormat::stripWhitespace(UnicodeString& description) result.append(description, start, p + 1 - start); start = p + 1; } - + // when we get here, we've seeked off the end of the sring, and // we terminate the loop (we continue until *start* is -1 rather // than until *p* is -1, because otherwise we'd miss the last @@ -526,12 +526,12 @@ RuleBasedNumberFormat::stripWhitespace(UnicodeString& description) start = -1; } } - + description.setTo(result); } -void +void RuleBasedNumberFormat::dispose() { if (ruleSets) { @@ -541,15 +541,15 @@ RuleBasedNumberFormat::dispose() delete[] ruleSets; ruleSets = NULL; } - - delete collator; - collator = NULL; - - delete decimalFormatSymbols; - decimalFormatSymbols = NULL; - - delete lenientParseRules; - lenientParseRules = NULL; + + delete collator; + collator = NULL; + + delete decimalFormatSymbols; + decimalFormatSymbols = NULL; + + delete lenientParseRules; + lenientParseRules = NULL; } @@ -572,18 +572,18 @@ RuleBasedNumberFormat::getCollator() const // then pull out that collator's rules, append any additional // rules specified in the description, and create a _new_ // collator based on the combinaiton of those rules - + UErrorCode status = U_ZERO_ERROR; - + Collator* temp = Collator::createInstance(locale, status); if (U_SUCCESS(status) && temp->getDynamicClassID() == RuleBasedCollator::getStaticClassID()) { - + RuleBasedCollator* newCollator = (RuleBasedCollator*)temp; if (lenientParseRules) { UnicodeString rules(newCollator->getRules()); rules.append(*lenientParseRules); - + newCollator = new RuleBasedCollator(rules, status); } else { temp = NULL; @@ -598,7 +598,7 @@ RuleBasedNumberFormat::getCollator() const } delete temp; } - + // if lenient-parse mode is off, this will be null // (see setLenientParseMode()) return collator; diff --git a/icu4c/source/i18n/rbt_set.cpp b/icu4c/source/i18n/rbt_set.cpp index f9a621c7a90..99cfadd5640 100644 --- a/icu4c/source/i18n/rbt_set.cpp +++ b/icu4c/source/i18n/rbt_set.cpp @@ -73,7 +73,7 @@ UnicodeString& _appendHex(uint32_t number, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0 }; while (digits--) { - target += digitString[(number >> (digits*4)) & 0xF]; + target += digitString[(number >> (digits*4)) & 0xF]; } return target; } @@ -277,7 +277,7 @@ void TransliterationRuleSet::freeze(UParseError& parseError,UErrorCode& status) /* Precompute the index values. This saves a LOT of time. */ - int16_t* indexValue = new int16_t[n]; + int16_t* indexValue = (int16_t*) uprv_malloc( sizeof(int16_t) * n ); for (j=0; jelementAt(j); indexValue[j] = r->getIndexValue(); @@ -301,7 +301,7 @@ void TransliterationRuleSet::freeze(UParseError& parseError,UErrorCode& status) } } } - delete[] indexValue; + uprv_free(indexValue); index[256] = v.size(); /* Freeze things into an array. diff --git a/icu4c/source/i18n/smpdtfmt.cpp b/icu4c/source/i18n/smpdtfmt.cpp index 83b616c1047..8960bc8354b 100644 --- a/icu4c/source/i18n/smpdtfmt.cpp +++ b/icu4c/source/i18n/smpdtfmt.cpp @@ -1383,7 +1383,7 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC // case 12: // 'w' - WEEK_OF_YEAR // case 13: // 'W' - WEEK_OF_MONTH // case 16: // 'K' - HOUR: 0-based. eg, 11PM + 1 hour =>> 0 AM - // 'e' - DOW_LOCAL + // 'e' - DOW_LOCAL // WORK AROUND BUG IN NUMBER FORMAT IN 1.2B3 int32_t parseStart = pos.getIndex(); diff --git a/icu4c/source/i18n/sortkey.cpp b/icu4c/source/i18n/sortkey.cpp index 402e4407930..695bc7b7620 100644 --- a/icu4c/source/i18n/sortkey.cpp +++ b/icu4c/source/i18n/sortkey.cpp @@ -8,7 +8,7 @@ // // File sortkey.cpp // -// +// // // Created by: Helena Shih // @@ -22,19 +22,13 @@ // 7/31/98 erm hashCode: minimum inc should be 2 not 1, // Cleaned up operator= // 07/12/99 helena HPUX 11 CC port. -// 03/06/01 synwee Modified compareTo, to handle the result of +// 03/06/01 synwee Modified compareTo, to handle the result of // 2 string similar in contents, but one is longer // than the other //=============================================================================== -#ifndef _SORTKEY #include "unicode/sortkey.h" -#endif - -#ifndef _CMEMORY #include "cmemory.h" -#endif - #include "uhash.h" U_NAMESPACE_BEGIN @@ -42,8 +36,8 @@ U_NAMESPACE_BEGIN // A hash code of kInvalidHashCode indicates that the has code needs // to be computed. A hash code of kEmptyHashCode is used for empty keys // and for any key whose computed hash code is kInvalidHashCode. -const int32_t CollationKey::kInvalidHashCode = 0; -const int32_t CollationKey::kEmptyHashCode = 1; +#define kInvalidHashCode ((int32_t)0) +#define kEmptyHashCode ((int32_t)1) CollationKey::CollationKey() : fBogus(FALSE), fCount(0), fCapacity(0), @@ -110,7 +104,7 @@ void CollationKey::adopt(uint8_t *values, int32_t count) { // set the key to an empty state CollationKey& CollationKey::reset() -{ +{ fCount = 0; fBogus = FALSE; fHashCode = kEmptyHashCode; @@ -180,76 +174,76 @@ CollationKey::operator=(const CollationKey& other) Collator::EComparisonResult CollationKey::compareTo(const CollationKey& target) const { - uint8_t *src = this->fBytes; - uint8_t *tgt = target.fBytes; + uint8_t *src = this->fBytes; + uint8_t *tgt = target.fBytes; - // are we comparing the same string - if (src == tgt) - return Collator::EQUAL; + // are we comparing the same string + if (src == tgt) + return Collator::EQUAL; - /* - int count = (this->fCount < target.fCount) ? this->fCount : target.fCount; - if (count == 0) - { - // If count is 0, at least one of the keys is empty. - // An empty key is always LESS than a non-empty one - // and EQUAL to another empty - if (this->fCount < target.fCount) - { - return Collator::LESS; - } - - if (this->fCount > target.fCount) - { - return Collator::GREATER; - } - return Collator::EQUAL; - } - */ - - int minLength; - Collator::EComparisonResult result; - - // are we comparing different lengths? - if (this->fCount != target.fCount) { - if (this->fCount < target.fCount) { - minLength = this->fCount; - result = Collator::LESS; - } - else { - minLength = target.fCount; - result = Collator::GREATER; - } - } - else { - minLength = target.fCount; - result = Collator::EQUAL; - } - - if (minLength > 0) { - int diff = uprv_memcmp(src, tgt, minLength); - if (diff > 0) { - return Collator::GREATER; - } - else - if (diff < 0) { + /* + int count = (this->fCount < target.fCount) ? this->fCount : target.fCount; + if (count == 0) + { + // If count is 0, at least one of the keys is empty. + // An empty key is always LESS than a non-empty one + // and EQUAL to another empty + if (this->fCount < target.fCount) + { return Collator::LESS; - } - } + } - return result; - /* - if (result < 0) - { + if (this->fCount > target.fCount) + { + return Collator::GREATER; + } + return Collator::EQUAL; + } + */ + + int minLength; + Collator::EComparisonResult result; + + // are we comparing different lengths? + if (this->fCount != target.fCount) { + if (this->fCount < target.fCount) { + minLength = this->fCount; + result = Collator::LESS; + } + else { + minLength = target.fCount; + result = Collator::GREATER; + } + } + else { + minLength = target.fCount; + result = Collator::EQUAL; + } + + if (minLength > 0) { + int diff = uprv_memcmp(src, tgt, minLength); + if (diff > 0) { + return Collator::GREATER; + } + else + if (diff < 0) { + return Collator::LESS; + } + } + + return result; + /* + if (result < 0) + { return Collator::LESS; - } + } - if (result > 0) - { - return Collator::GREATER; - } - return Collator::EQUAL; - */ + if (result > 0) + { + return Collator::GREATER; + } + return Collator::EQUAL; + */ } CollationKey& @@ -276,13 +270,13 @@ CollationKey::ensureCapacity(int32_t newSize) return *this; } - + // Create a copy of the byte array. uint8_t* CollationKey::toByteArray(int32_t& count) const { - uint8_t *result = new uint8_t[fCount]; - + uint8_t *result = (uint8_t*) uprv_malloc( sizeof(uint8_t) * fCount ); + if (result == NULL) { count = 0; @@ -293,7 +287,7 @@ CollationKey::toByteArray(int32_t& count) const uprv_memcpy(result, fBytes, fCount); } - return result; + return result; } int32_t @@ -323,7 +317,7 @@ CollationKey::hashCode() const while (p < limit) { - hash = ( hash * 37 ) + ((p[0] << 8) + p[1]); + hash = ( hash * 37 ) + ((p[0] << 8) + p[1]); p += inc; } diff --git a/icu4c/source/i18n/strmatch.cpp b/icu4c/source/i18n/strmatch.cpp index 146559f4c94..4d21def96a9 100644 --- a/icu4c/source/i18n/strmatch.cpp +++ b/icu4c/source/i18n/strmatch.cpp @@ -142,8 +142,9 @@ UMatchDegree StringMatcher::matches(const Replaceable& text, * Implement UnicodeMatcher */ UnicodeString& StringMatcher::toPattern(UnicodeString& result, - UBool escapeUnprintable) const { - result.truncate(0); + UBool escapeUnprintable) const +{ + result.truncate(0); UnicodeString str, quoteBuf; if (segmentNumber > 0) { result.append((UChar)40); /*(*/ diff --git a/icu4c/source/i18n/tblcoll.cpp b/icu4c/source/i18n/tblcoll.cpp index 49a65847cd8..08f7858cc4b 100644 --- a/icu4c/source/i18n/tblcoll.cpp +++ b/icu4c/source/i18n/tblcoll.cpp @@ -266,17 +266,17 @@ CollationElementIterator* RuleBasedCollator::createCollationElementIterator */ const UnicodeString& RuleBasedCollator::getRules() const { - return (*urulestring); + return (*urulestring); } void RuleBasedCollator::getRules(UColRuleOption delta, UnicodeString &buffer) { - UChar *rules = NULL; - int rulesize = ucol_getRulesEx(ucollator, delta, rules, -1); - rules = new UChar[rulesize]; + int rulesize = ucol_getRulesEx(ucollator, delta, NULL, -1); + UChar *rules = (UChar*) uprv_malloc( sizeof(UChar) * (rulesize) ); + ucol_getRulesEx(ucollator, delta, rules, rulesize); buffer.setTo(rules, rulesize); - delete rules; + uprv_free(rules); } Collator::EComparisonResult RuleBasedCollator::compare( diff --git a/icu4c/source/i18n/transreg.cpp b/icu4c/source/i18n/transreg.cpp index 512d984b743..bcf772fe45e 100644 --- a/icu4c/source/i18n/transreg.cpp +++ b/icu4c/source/i18n/transreg.cpp @@ -713,10 +713,11 @@ static const UChar TRANSLITERATE[] = {84,114,97,110,115,108,105,116,101,114,97,1 Entry* TransliteratorRegistry::findInBundle(const Spec& specToOpen, const Spec& specToFind, const UnicodeString& variant, - UTransDirection direction) { + UTransDirection direction) +{ UnicodeString utag; UnicodeString resStr; - int32_t pass; + int32_t pass; for (pass=0; pass<2; ++pass) { utag.truncate(0); diff --git a/icu4c/source/i18n/ucol.cpp b/icu4c/source/i18n/ucol.cpp index f3f89f68548..b47c41233f3 100644 --- a/icu4c/source/i18n/ucol.cpp +++ b/icu4c/source/i18n/ucol.cpp @@ -2194,7 +2194,7 @@ uint32_t ucol_prv_getSpecialCE(const UCollator *coll, UChar ch, uint32_t CE, col return UTRIE_GET32_FROM_LEAD(UCA->mapping, L); } else { // Jamo is Special - // Since Hanguls pass the FCD check, it is + // Since Hanguls pass the FCD check, it is // guaranteed that we won't be in // the normalization buffer if something like this happens // Move Jamos into normalization buffer @@ -2760,7 +2760,7 @@ uint32_t ucol_prv_getSpecialPrevCE(const UCollator *coll, UChar ch, uint32_t CE, source->toReturn = source->CEpos - 1; return *(source->toReturn); } else { - // Since Hanguls pass the FCD check, it is + // Since Hanguls pass the FCD check, it is // guaranteed that we won't be in // the normalization buffer if something like this happens // Move Jamos into normalization buffer diff --git a/icu4c/source/i18n/ucol_cnt.cpp b/icu4c/source/i18n/ucol_cnt.cpp index a375a83c2d4..eac2069ac85 100644 --- a/icu4c/source/i18n/ucol_cnt.cpp +++ b/icu4c/source/i18n/ucol_cnt.cpp @@ -137,7 +137,7 @@ uprv_cnttab_constructTable(CntTable *table, uint32_t mainOffset, UErrorCode *sta table->position = 0; if(table->offsets != NULL) { - free(table->offsets); + uprv_free(table->offsets); } table->offsets = (int32_t *)uprv_malloc(table->size*sizeof(int32_t)); if(table->offsets == NULL) { @@ -292,15 +292,15 @@ U_CAPI void U_EXPORT2 uprv_cnttab_close(CntTable *table) { int32_t i = 0; for(i = 0; isize; i++) { - free(table->elements[i]->CEs); - free(table->elements[i]->codePoints); - free(table->elements[i]); + uprv_free(table->elements[i]->CEs); + uprv_free(table->elements[i]->codePoints); + uprv_free(table->elements[i]); } - free(table->elements); - free(table->CEs); - free(table->offsets); - free(table->codePoints); - free(table); + uprv_free(table->elements); + uprv_free(table->CEs); + uprv_free(table->offsets); + uprv_free(table->codePoints); + uprv_free(table); } /* this is for adding non contractions */ diff --git a/icu4c/source/i18n/ucol_cnt.h b/icu4c/source/i18n/ucol_cnt.h index 1c72a662cd0..75060f9f678 100644 --- a/icu4c/source/i18n/ucol_cnt.h +++ b/icu4c/source/i18n/ucol_cnt.h @@ -43,7 +43,7 @@ struct ContractionTable { }; struct CntTable { - ContractionTable **elements; + ContractionTable **elements; /*CompactEIntArray *mapping;*/ UNewTrie *mapping; UChar *codePoints; diff --git a/icu4c/source/i18n/ucol_elm.cpp b/icu4c/source/i18n/ucol_elm.cpp index ccdfce321f0..9e2b6d27df2 100644 --- a/icu4c/source/i18n/ucol_elm.cpp +++ b/icu4c/source/i18n/ucol_elm.cpp @@ -26,6 +26,7 @@ #include "ucol_elm.h" #include "unicode/uchar.h" #include "unormimp.h" +#include "cmemory.h" U_NAMESPACE_BEGIN @@ -89,13 +90,13 @@ static int32_t uprv_uca_addExpansion(ExpansionTable *expansions, uint32_t value, return 0; } if(expansions->CEs == NULL) { - expansions->CEs = (uint32_t *)malloc(INIT_EXP_TABLE_SIZE*sizeof(uint32_t)); + expansions->CEs = (uint32_t *)uprv_malloc(INIT_EXP_TABLE_SIZE*sizeof(uint32_t)); expansions->size = INIT_EXP_TABLE_SIZE; expansions->position = 0; } if(expansions->position == expansions->size) { - uint32_t *newData = (uint32_t *)realloc(expansions->CEs, 2*expansions->size*sizeof(uint32_t)); + uint32_t *newData = (uint32_t *)uprv_realloc(expansions->CEs, 2*expansions->size*sizeof(uint32_t)); if(newData == NULL) { #ifdef UCOL_DEBUG fprintf(stderr, "out of memory for expansions\n"); @@ -1177,7 +1178,7 @@ uprv_uca_assembleTable(tempUCATable *t, UErrorCode *status) { paddedsize(UCOL_UNSAFECP_TABLE_SIZE)); /* Contraction Ending chars */ - dataStart = (uint8_t *)malloc(toAllocate); + dataStart = (uint8_t *)uprv_malloc(toAllocate); UCATableHeader *myData = (UCATableHeader *)dataStart; uprv_memcpy(myData, t->image, sizeof(UCATableHeader)); @@ -1200,7 +1201,7 @@ uprv_uca_assembleTable(tempUCATable *t, UErrorCode *status) { tableOffset += (uint32_t)(paddedsize(sizeof(UCATableHeader))); myData->options = tableOffset; - memcpy(dataStart+tableOffset, t->options, sizeof(UColOptionSet)); + uprv_memcpy(dataStart+tableOffset, t->options, sizeof(UColOptionSet)); tableOffset += (uint32_t)(paddedsize(sizeof(UColOptionSet))); /* copy expansions */ @@ -1299,7 +1300,7 @@ uprv_uca_assembleTable(tempUCATable *t, UErrorCode *status) { fprintf(stderr, "calculation screwup!!! Expected to write %i but wrote %i instead!!!\n", toAllocate, tableOffset); #endif *status = U_INTERNAL_PROGRAM_ERROR; - free(dataStart); + uprv_free(dataStart); return 0; } diff --git a/icu4c/source/i18n/ucol_elm.h b/icu4c/source/i18n/ucol_elm.h index 6f23a1d5af8..cb5fda29326 100644 --- a/icu4c/source/i18n/ucol_elm.h +++ b/icu4c/source/i18n/ucol_elm.h @@ -24,9 +24,9 @@ #include "ucol_cnt.h" #include "unicode/utypes.h" #include "ucol_imp.h" -#include "cmemory.h" #ifdef UCOL_DEBUG +#include "cmemory.h" #include #endif diff --git a/icu4c/source/i18n/ucol_tok.cpp b/icu4c/source/i18n/ucol_tok.cpp index 56fc6b0238a..aac81f7df58 100644 --- a/icu4c/source/i18n/ucol_tok.cpp +++ b/icu4c/source/i18n/ucol_tok.cpp @@ -104,7 +104,7 @@ void ucol_tok_initTokenList(UColTokenParser *src, const UChar *rules, const uint nSize = unorm_normalize(rules, rulesLength, UNORM_NFD, 0, src->source, estimatedSize, status); if(nSize > estimatedSize || *status == U_BUFFER_OVERFLOW_ERROR) { *status = U_ZERO_ERROR; - src->source = (UChar *)realloc(src->source, (nSize+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar)); + src->source = (UChar *)uprv_realloc(src->source, (nSize+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar)); nSize = unorm_normalize(rules, rulesLength, UNORM_NFD, 0, src->source, nSize+UCOL_TOK_EXTRA_RULE_SPACE_SIZE, status); } src->current = src->source; @@ -920,7 +920,7 @@ uint32_t ucol_tok_assembleTokenList(UColTokenParser *src, UParseError *parseErro sourceToken->listHeader = lastToken->listHeader; /* - 1. Find the strongest strength in each list, and set strongestP and strongestN + 1. Find the strongest strength in each list, and set strongestP and strongestN accordingly in the headers. */ if(lastStrength == UCOL_TOK_RESET diff --git a/icu4c/source/i18n/udat.cpp b/icu4c/source/i18n/udat.cpp index 5508c2d0244..9e0a58d207c 100644 --- a/icu4c/source/i18n/udat.cpp +++ b/icu4c/source/i18n/udat.cpp @@ -125,7 +125,7 @@ udat_open(UDateFormatStyle timeStyle, TimeZone *zone = 0; int32_t length = (tzIDLength == -1 ? u_strlen(tzID) : tzIDLength); zone = TimeZone::createTimeZone(UnicodeString((UChar*)tzID, - length, length)); + length, length)); if(zone == 0) { *status = U_MEMORY_ALLOCATION_ERROR; delete fmt; diff --git a/icu4c/source/i18n/unicode/coll.h b/icu4c/source/i18n/unicode/coll.h index 2a7759b1ab0..923f4ee1dfb 100644 --- a/icu4c/source/i18n/unicode/coll.h +++ b/icu4c/source/i18n/unicode/coll.h @@ -391,8 +391,8 @@ public: */ virtual CollationKey& getCollationKey(const UChar*source, int32_t sourceLength, - CollationKey& key, - UErrorCode& status) const = 0; + CollationKey& key, + UErrorCode& status) const = 0; /** * Generates the hash code for the collation object * @stable @@ -606,8 +606,9 @@ public: * @return Number of bytes needed for storing the sort key * @draft ICU 1.8 */ - virtual int32_t getSortKey(const UnicodeString& source, uint8_t* result, - int32_t resultLength) const = 0; + virtual int32_t getSortKey(const UnicodeString& source, + uint8_t* result, + int32_t resultLength) const = 0; /** * Get the sort key as an array of bytes from an UChar buffer. @@ -625,7 +626,8 @@ public: * @draft ICU 1.8 */ virtual int32_t getSortKey(const UChar*source, int32_t sourceLength, - uint8_t*result, int32_t resultLength) const = 0; + uint8_t*result, int32_t resultLength) const = 0; + // start deprecated APIs /** * Get the decomposition mode of the Collator object. diff --git a/icu4c/source/i18n/unicode/rbnf.h b/icu4c/source/i18n/unicode/rbnf.h index d53f6b0989b..e3ff0550479 100644 --- a/icu4c/source/i18n/unicode/rbnf.h +++ b/icu4c/source/i18n/unicode/rbnf.h @@ -21,10 +21,10 @@ class NFRuleSet; /** Tags for the predefined rulesets. */ enum URBNFRuleSetTag { - URBNF_SPELLOUT, - URBNF_ORDINAL, - URBNF_DURATION, - URBNF_COUNT + URBNF_SPELLOUT, + URBNF_ORDINAL, + URBNF_DURATION, + URBNF_COUNT }; /** @@ -507,7 +507,7 @@ public: * @draft ICU 2.0 */ RuleBasedNumberFormat(const UnicodeString& rules, const Locale& locale, - UParseError& perror, UErrorCode& status); + UParseError& perror, UErrorCode& status); /** * Creates a RuleBasedNumberFormat from a predefined ruleset. The selector @@ -688,7 +688,7 @@ public: * @draft ICU 2.0 */ virtual void parse(const UnicodeString& text, - Formattable& result, + Formattable& result, ParsePosition& parsePosition) const; @@ -784,12 +784,13 @@ private: inline UnicodeString& RuleBasedNumberFormat::format(const Formattable& obj, UnicodeString& result, - UErrorCode& status) const { + UErrorCode& status) const +{ // Don't use Format:: - use immediate base class only, // in case immediate base modifies behavior later. - // dlf - the above comment is bogus, if there were a reason to modify - // it, it would be virtual, and there's no reason because it is - // a one-line macro in NumberFormat anyway, just like this one. + // dlf - the above comment is bogus, if there were a reason to modify + // it, it would be virtual, and there's no reason because it is + // a one-line macro in NumberFormat anyway, just like this one. return NumberFormat::format(obj, result, status); } @@ -806,18 +807,19 @@ RuleBasedNumberFormat::format(int32_t number, UnicodeString& output) const { } inline void -RuleBasedNumberFormat::parse(const UnicodeString& text, Formattable& result, UErrorCode& status) const { - NumberFormat::parse(text, result, status); +RuleBasedNumberFormat::parse(const UnicodeString& text, Formattable& result, UErrorCode& status) const +{ + NumberFormat::parse(text, result, status); } inline UBool RuleBasedNumberFormat::isLenient(void) const { - return lenient; + return lenient; } inline NFRuleSet* RuleBasedNumberFormat::getDefaultRuleSet() const { - return defaultRuleSet; + return defaultRuleSet; } U_NAMESPACE_END diff --git a/icu4c/source/i18n/unicode/sortkey.h b/icu4c/source/i18n/unicode/sortkey.h index 8da09ac1ecc..4e35a5b40dd 100644 --- a/icu4c/source/i18n/unicode/sortkey.h +++ b/icu4c/source/i18n/unicode/sortkey.h @@ -87,118 +87,120 @@ class RuleBasedCollator; class U_I18N_API CollationKey { public: /** - * This creates an empty collation key based on the null string. An empty - * collation key contains no sorting information. When comparing two empty - * collation keys, the result is Collator::EQUAL. Comparing empty collation key - * with non-empty collation key is always Collator::LESS. - * @stable - */ - CollationKey(); - - - /** - * Creates a collation key based on the collation key values. - * @param values the collation key values - * @param count number of collation key values, including trailing nulls. - * @see #createBits - * @stable - */ - CollationKey(const uint8_t* values, - int32_t count); - /** - * Copy constructor. - * @stable - */ - CollationKey(const CollationKey& other); - /** - * Sort key destructor. - * @stable - */ - ~CollationKey(); - - /** - * Assignment operator - * @stable - */ - const CollationKey& operator=(const CollationKey& other); - - /** - * Compare if two collation keys are the same. - * @param source the collation key to compare to. - * @return Returns true if two collation keys are equal, false otherwise. - * @stable - */ - UBool operator==(const CollationKey& source) const; - - /** - * Compare if two collation keys are not the same. - * @param source the collation key to compare to. - * @return Returns TRUE if two collation keys are different, FALSE otherwise. - * @stable - */ - UBool operator!=(const CollationKey& source) const; + * This creates an empty collation key based on the null string. An empty + * collation key contains no sorting information. When comparing two empty + * collation keys, the result is Collator::EQUAL. Comparing empty collation key + * with non-empty collation key is always Collator::LESS. + * @stable + */ + CollationKey(); /** - * Test to see if the key is in an invalid state. The key will be in an - * invalid state if it couldn't allocate memory for some operation. - * @return Returns TRUE if the key is in an invalid, FALSE otherwise. - * @stable - */ - UBool isBogus(void) const; + * Creates a collation key based on the collation key values. + * @param values the collation key values + * @param count number of collation key values, including trailing nulls. + * @see #createBits + * @stable + */ + CollationKey(const uint8_t* values, + int32_t count); /** - * Returns a pointer to the collation key values. The storage is owned - * by the collation key and the pointer will become invalid if the key - * is deleted. - * @param count the output parameter of number of collation key values, - * including any trailing nulls. - * @stable - */ - const uint8_t* getByteArray(int32_t& count) const; + * Copy constructor. + * @stable + */ + CollationKey(const CollationKey& other); /** - * Extracts the collation key values into a new array. The caller owns - * this storage and should free it. - * @param count the output parameter of number of collation key values, - * including any trailing nulls. - * @stable - */ - uint8_t* toByteArray(int32_t& count) const; + * Sort key destructor. + * @stable + */ + ~CollationKey(); /** - * Convenience method which does a string(bit-wise) comparison of the - * two collation keys. - * @param sourceKey source collation key - * @param targetKey target collation key - * @return Returns Collator::LESS if sourceKey < targetKey, - * Collator::GREATER if sourceKey > targetKey and Collator::EQUAL - * otherwise. - * @stable - */ - Collator::EComparisonResult compareTo(const CollationKey& target) const; + * Assignment operator + * @stable + */ + const CollationKey& operator=(const CollationKey& other); /** - * Creates an integer that is unique to the collation key. NOTE: this - * is not the same as String.hashCode. - *

Example of use: - *

-     * .    UErrorCode status = U_ZERO_ERROR;
-     * .    Collator *myCollation = Collator::createInstance(Locale::US, status);
-     * .    if (U_FAILURE(status)) return;
-     * .    CollationKey key1, key2;
-     * .    UErrorCode status1 = U_ZERO_ERROR, status2 = U_ZERO_ERROR;
-     * .    myCollation->getCollationKey("abc", key1, status1);
-     * .    if (U_FAILURE(status1)) { delete myCollation; return; }
-     * .    myCollation->getCollationKey("ABC", key2, status2);
-     * .    if (U_FAILURE(status2)) { delete myCollation; return; }
-     * .    // key1.hashCode() != key2.hashCode()
-     * 
- * @return the hash value based on the string's collation order. - * @see UnicodeString#hashCode - * @stable - */ - int32_t hashCode(void) const; + * Compare if two collation keys are the same. + * @param source the collation key to compare to. + * @return Returns true if two collation keys are equal, false otherwise. + * @stable + */ + UBool operator==(const CollationKey& source) const; + + /** + * Compare if two collation keys are not the same. + * @param source the collation key to compare to. + * @return Returns TRUE if two collation keys are different, FALSE otherwise. + * @stable + */ + UBool operator!=(const CollationKey& source) const; + + + /** + * Test to see if the key is in an invalid state. The key will be in an + * invalid state if it couldn't allocate memory for some operation. + * @return Returns TRUE if the key is in an invalid, FALSE otherwise. + * @stable + */ + UBool isBogus(void) const; + + /** + * Returns a pointer to the collation key values. The storage is owned + * by the collation key and the pointer will become invalid if the key + * is deleted. + * @param count the output parameter of number of collation key values, + * including any trailing nulls. + * @stable + */ + const uint8_t* getByteArray(int32_t& count) const; + + /** + * Extracts the collation key values into a new array. The caller owns + * this storage and should free it. + * @param count the output parameter of number of collation key values, + * including any trailing nulls. + * @stable + */ + uint8_t* toByteArray(int32_t& count) const; + + /** + * Convenience method which does a string(bit-wise) comparison of the + * two collation keys. + * @param sourceKey source collation key + * @param targetKey target collation key + * @return Returns Collator::LESS if sourceKey < targetKey, + * Collator::GREATER if sourceKey > targetKey and Collator::EQUAL + * otherwise. + * @stable + */ + Collator::EComparisonResult compareTo(const CollationKey& target) const; + + /** + * Creates an integer that is unique to the collation key. NOTE: this + * is not the same as String.hashCode. + *

Example of use: + *

+    * .    UErrorCode status = U_ZERO_ERROR;
+    * .    Collator *myCollation = Collator::createInstance(Locale::US, status);
+    * .    if (U_FAILURE(status)) return;
+    * .    CollationKey key1, key2;
+    * .    UErrorCode status1 = U_ZERO_ERROR, status2 = U_ZERO_ERROR;
+    * .    myCollation->getCollationKey("abc", key1, status1);
+    * .    if (U_FAILURE(status1)) { delete myCollation; return; }
+    * .    myCollation->getCollationKey("ABC", key2, status2);
+    * .    if (U_FAILURE(status2)) { delete myCollation; return; }
+    * .    // key1.hashCode() != key2.hashCode()
+    * 
+ * @return the hash value based on the string's collation order. + * @see UnicodeString#hashCode + * @stable + */ + int32_t hashCode(void) const; private: /** @@ -207,25 +209,22 @@ private: * @param size output parameter of the number of collation key values * @return a pointer to an array of 16-bit collation key values. */ - void adopt(uint8_t *values, int32_t count); + void adopt(uint8_t *values, int32_t count); /* - * Creates a collation key with a string. - */ - CollationKey& ensureCapacity(int32_t newSize); - CollationKey& setToBogus(void); - CollationKey& reset(void); + * Creates a collation key with a string. + */ + CollationKey& ensureCapacity(int32_t newSize); + CollationKey& setToBogus(void); + CollationKey& reset(void); - friend class RuleBasedCollator; + friend class RuleBasedCollator; - static const int32_t kInvalidHashCode; - static const int32_t kEmptyHashCode; - - UBool fBogus; - int32_t fCount; - int32_t fCapacity; - int32_t fHashCode; - uint8_t* fBytes; + UBool fBogus; + int32_t fCount; + int32_t fCapacity; + int32_t fHashCode; + uint8_t* fBytes; }; inline UBool diff --git a/icu4c/source/i18n/uniset.cpp b/icu4c/source/i18n/uniset.cpp index 2c549072b5a..000c32c6932 100644 --- a/icu4c/source/i18n/uniset.cpp +++ b/icu4c/source/i18n/uniset.cpp @@ -150,7 +150,7 @@ UnicodeSet::UnicodeSet() : len(1), capacity(1 + START_EXTRA), bufferCapacity(0), buffer(0) { - list = new UChar32[capacity]; + list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); list[0] = UNICODESET_HIGH; _dbgct(this); } @@ -166,7 +166,7 @@ UnicodeSet::UnicodeSet(UChar32 start, UChar32 end) : len(1), capacity(1 + START_EXTRA), bufferCapacity(0), buffer(0) { - list = new UChar32[capacity]; + list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); list[0] = UNICODESET_HIGH; complement(start, end); _dbgct(this); @@ -183,7 +183,7 @@ UnicodeSet::UnicodeSet(const UnicodeString& pattern, len(0), capacity(START_EXTRA), bufferCapacity(0), buffer(0) { - list = new UChar32[capacity]; + list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); applyPattern(pattern, status); _dbgct(this); } @@ -195,7 +195,7 @@ UnicodeSet::UnicodeSet(const UnicodeString& pattern, ParsePosition& pos, len(0), capacity(START_EXTRA), bufferCapacity(0), buffer(0) { - list = new UChar32[capacity]; + list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); applyPattern(pattern, pos, &symbols, status); _dbgct(this); } @@ -206,7 +206,7 @@ UnicodeSet::UnicodeSet(const UnicodeString& pattern, ParsePosition& pos, len(0), capacity(START_EXTRA), bufferCapacity(0), buffer(0) { - list = new UChar32[capacity]; + list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); applyPattern(pattern, pos, NULL, status); _dbgct(this); } @@ -230,7 +230,7 @@ UnicodeSet::UnicodeSet(int8_t category, UErrorCode& status) : UnicodeString pattern(FALSE, CATEGORY_NAMES + category*2, 2); pattern.insert(0, OPEN); pattern.append(CLOSE); - list = new UChar32[capacity]; + list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); applyPattern(pattern, status); } } @@ -245,7 +245,7 @@ UnicodeSet::UnicodeSet(const UnicodeSet& o) : capacity(o.len + GROW_EXTRA), bufferCapacity(0), buffer(0) { - list = new UChar32[capacity]; + list = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); *this = o; _dbgct(this); } @@ -255,8 +255,8 @@ UnicodeSet::UnicodeSet(const UnicodeSet& o) : */ UnicodeSet::~UnicodeSet() { _dbgdt(this); // first! - delete[] list; - delete[] buffer; + uprv_free(list); + uprv_free(buffer); } /** @@ -925,12 +925,12 @@ UChar32 UnicodeSet::getRangeEnd(int32_t index) const { void UnicodeSet::compact() { if (len != capacity) { capacity = len; - UChar32* temp = new UChar32[capacity]; + UChar32* temp = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); uprv_memcpy(temp, list, len*sizeof(UChar32)); - delete[] list; + uprv_free(list); list = temp; } - delete[] buffer; + uprv_free(buffer); buffer = NULL; } @@ -1402,19 +1402,21 @@ void UnicodeSet::_applyPattern(const UnicodeString& pattern, //---------------------------------------------------------------- void UnicodeSet::ensureCapacity(int32_t newLen) { - if (newLen <= capacity) return; + if (newLen <= capacity) + return; capacity = newLen + GROW_EXTRA; - UChar32* temp = new UChar32[capacity]; + UChar32* temp = (UChar32*) uprv_malloc(sizeof(UChar32) * capacity); uprv_memcpy(temp, list, len*sizeof(UChar32)); - delete[] list; + uprv_free(list); list = temp; } void UnicodeSet::ensureBufferCapacity(int32_t newLen) { - if (buffer != NULL && newLen <= bufferCapacity) return; - delete[] buffer; + if (buffer != NULL && newLen <= bufferCapacity) + return; + uprv_free(buffer); bufferCapacity = newLen + GROW_EXTRA; - buffer = new UChar32[bufferCapacity]; + buffer = (UChar32*) uprv_malloc(sizeof(UChar32) * bufferCapacity); } /** diff --git a/icu4c/source/i18n/unum.cpp b/icu4c/source/i18n/unum.cpp index 393cb353106..6accaab0fda 100644 --- a/icu4c/source/i18n/unum.cpp +++ b/icu4c/source/i18n/unum.cpp @@ -137,7 +137,7 @@ unum_open( UNumberFormatStyle style, break; case UNUM_SPELLOUT: - return (UNumberFormat*)new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale(locale), *status); + return (UNumberFormat*)new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale(locale), *status); default: *status = U_UNSUPPORTED_ERROR; @@ -151,7 +151,7 @@ unum_open( UNumberFormatStyle style, return retVal; }else{ - /* we don't support RBNF patterns yet */ + /* we don't support RBNF patterns yet */ UParseError tErr; int32_t len = (patternLength == -1 ? u_strlen(pattern) : patternLength); const UnicodeString pat((UChar*)pattern, len, len); diff --git a/icu4c/source/i18n/utrans.cpp b/icu4c/source/i18n/utrans.cpp index c7e57bab63d..c3e37b8ad2d 100644 --- a/icu4c/source/i18n/utrans.cpp +++ b/icu4c/source/i18n/utrans.cpp @@ -76,7 +76,7 @@ ReplaceableGlue::ReplaceableGlue(UReplaceable *replaceable, } ReplaceableGlue::~ReplaceableGlue() { - delete[] buf; + uprv_free(buf); } int32_t ReplaceableGlue::getLength() const { @@ -96,9 +96,9 @@ void ReplaceableGlue::handleReplaceBetween(UTextOffset start, const UnicodeString& text) { int32_t len = text.length(); if (buf == 0 || bufLen < len) { - delete[] buf; + uprv_free(buf); bufLen = len + BUF_PAD; - buf = new UChar[bufLen]; + buf = (UChar*) uprv_malloc(sizeof(UChar) * bufLen); } text.extract(0, len, buf); (*func->replace)(rep, start, limit, buf, len); diff --git a/icu4c/source/tools/genrb/ufile.c b/icu4c/source/tools/genrb/ufile.c deleted file mode 100644 index 09e2c8dc0d9..00000000000 --- a/icu4c/source/tools/genrb/ufile.c +++ /dev/null @@ -1,8 +0,0 @@ -/* -******************************************************************************* -* -* Copyright (C) 1998-2000, International Business Machines -* Corporation and others. All Rights Reserved. -* -*******************************************************************************/ -#error This file is obsolete! Use icu\source\extra\ustdio\ufile.c instead! \ No newline at end of file diff --git a/icu4c/source/tools/genrb/ustdio.c b/icu4c/source/tools/genrb/ustdio.c deleted file mode 100644 index db7e213bf59..00000000000 --- a/icu4c/source/tools/genrb/ustdio.c +++ /dev/null @@ -1,8 +0,0 @@ -/* -******************************************************************************* -* -* Copyright (C) 1998-2000, International Business Machines -* Corporation and others. All Rights Reserved. -* -*******************************************************************************/ -#error This file is obsolete! Use icu\source\extra\ustdio\ustdio.c instead! \ No newline at end of file diff --git a/icu4c/source/tools/gentz/gentz.cpp b/icu4c/source/tools/gentz/gentz.cpp index d0274607878..bc98cfba391 100644 --- a/icu4c/source/tools/gentz/gentz.cpp +++ b/icu4c/source/tools/gentz/gentz.cpp @@ -322,7 +322,7 @@ void gentz::fixupNameToEquiv() { uint32_t i; // First make a list that maps indices to offsets - uint32_t *offsets = new uint32_t[equivCount]; + uint32_t *offsets = (uint32_t*) uprv_malloc(sizeof(uint32_t) * equivCount); offsets[0] = header.equivTableDelta; if (offsets[0] % 4 != 0) { die("Header size is not 4-aligned"); @@ -345,7 +345,7 @@ void gentz::fixupNameToEquiv() { nameToEquiv[i] = offsets[x]; } - delete[] offsets; + uprv_free(offsets); } TZEquivalencyGroup* gentz::parseEquivTable(FileStream* in) { @@ -376,7 +376,7 @@ TZEquivalencyGroup* gentz::parseEquivTable(FileStream* in) { } maxPossibleSize *= n; // Get size of entire set of structs. - int8_t *result = new int8_t[maxPossibleSize]; + int8_t *result = (int8_t*) uprv_malloc(sizeof(int8_t) * maxPossibleSize); if (result == 0) { die("Out of memory"); } @@ -476,7 +476,7 @@ OffsetIndex* gentz::parseOffsetIndexTable(FileStream* in) { uint32_t maxPossibleSize = n * (sizeof(OffsetIndex) + (maxPerOffset-1) * sizeof(uint16_t)); - int8_t *result = new int8_t[maxPossibleSize]; + int8_t *result = (int8_t*) uprv_malloc(sizeof(int8_t) * maxPossibleSize); if (result == 0) { die("Out of memory"); } @@ -541,7 +541,7 @@ CountryIndex* gentz::parseCountryIndexTable(FileStream* in) { uint32_t expectedSize = n*(sizeof(CountryIndex)-sizeof(uint16_t)) + header.count * sizeof(uint16_t); uint32_t pad = (4 - (expectedSize % 4)) % 4; // This will be 0 or 2 - int8_t *result = new int8_t[expectedSize + pad]; + int8_t *result = (int8_t*) uprv_malloc(sizeof(int8_t) * (expectedSize + pad)); if (result == 0) { die("Out of memory"); } @@ -655,8 +655,8 @@ char* gentz::parseNameTable(FileStream* in) { if (n != (int32_t)header.count) { die("Zone count doesn't match name table count"); } - char* names = new char[nameTableSize]; - nameToEquiv = new uint32_t[n]; + char* names = (char*) uprv_malloc(sizeof(char) * nameTableSize); + nameToEquiv = (uint32_t*) uprv_malloc(sizeof(uint32_t) * n); if (names == 0 || nameToEquiv == 0) { die("Out of memory"); } diff --git a/icu4c/source/tools/genuca/genuca.cpp b/icu4c/source/tools/genuca/genuca.cpp index 31828f85d5c..2100b4988c9 100644 --- a/icu4c/source/tools/genuca/genuca.cpp +++ b/icu4c/source/tools/genuca/genuca.cpp @@ -28,7 +28,7 @@ #include "toolutil.h" #include "cstring.h" -#include +#include "cmemory.h" #ifdef XP_MAC_CONSOLE #include @@ -257,7 +257,7 @@ static InverseTableHeader *assembleInverseTable(UErrorCode *status) uint32_t contsByteSize = sContPos * sizeof(UChar); uint32_t i = 0; - result = (InverseTableHeader *)malloc(headerByteSize + inverseTableByteSize + contsByteSize); + result = (InverseTableHeader *)uprv_malloc(headerByteSize + inverseTableByteSize + contsByteSize); if(result != NULL) { result->byteSize = headerByteSize + inverseTableByteSize + contsByteSize; diff --git a/icu4c/source/tools/toolutil/ucbuf.c b/icu4c/source/tools/toolutil/ucbuf.c index 9dd32dc6c0f..d3dc5d7564d 100644 --- a/icu4c/source/tools/toolutil/ucbuf.c +++ b/icu4c/source/tools/toolutil/ucbuf.c @@ -71,7 +71,7 @@ static UBool ucbuf_autodetect_nrw(FileStream* in, const char** cp,int* numRead){ }else if(start[0] == '\x0E' && start[1] == '\xFE' && start[2] == '\xFF'){ *cp ="SCSU"; signatureLength=3; - }else if(start[0] == '\x00' && start[1] == '\x00' && + }else if(start[0] == '\x00' && start[1] == '\x00' && start[2] == '\xFE' && start[3]=='\xFF'){ *cp = "UTF-32BE"; signatureLength=4; @@ -79,27 +79,29 @@ static UBool ucbuf_autodetect_nrw(FileStream* in, const char** cp,int* numRead){ signatureLength=0; autodetect=FALSE; } - while(signatureLength<*numRead) { + T_FileStream_rewind(in); + T_FileStream_read(in, start, *numRead); +/* while(signatureLength<*numRead) { T_FileStream_ungetc(start[--*numRead], in); - } + }*/ return autodetect; } /* Autodetects UTF8, UTF-16-BigEndian and UTF-16-LittleEndian BOMs*/ U_CAPI UBool U_EXPORT2 ucbuf_autodetect(FileStream* in,const char** cp){ - UBool autodetect = FALSE; - int numRead =0; - const char* tcp; - autodetect=ucbuf_autodetect_nrw(in,&tcp, &numRead); - *cp =tcp; - /* rewind the file Stream */ - T_FileStream_rewind(in); - return autodetect; + UBool autodetect = FALSE; + int numRead =0; + const char* tcp; + autodetect=ucbuf_autodetect_nrw(in,&tcp, &numRead); + *cp =tcp; + /* rewind the file Stream */ + T_FileStream_rewind(in); + return autodetect; } /* fill the uchar buffer */ -static UCHARBUF* +static UCHARBUF* ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* err){ UChar* pTarget=NULL; UChar* target=NULL; @@ -118,18 +120,22 @@ ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* err){ #if DEBUG memset(pTarget+offset,0xff,sizeof(UChar)*(MAX_IN_BUF-offset)); #endif - + /* read the file */ numRead=T_FileStream_read(buf->in,cbuf,MAX_IN_BUF-offset); buf->remaining-=numRead; + /* just to be sure...*/ + if ( 0 == numRead ) + buf->remaining = 0; + target=pTarget; /* convert the bytes */ if(buf->conv){ /* set the callback to stop */ UConverterToUCallback toUOldAction ; void* toUOldContext; - void* toUNewContext=NULL; + void* toUNewContext=NULL; ucnv_setToUCallBack(buf->conv, UCNV_TO_U_CALLBACK_STOP, toUNewContext, @@ -143,7 +149,7 @@ ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* err){ ucnv_toUnicode(buf->conv,&target,target+(MAX_U_BUF-offset), &source,source+numRead,NULL, (UBool)(buf->remaining==0),err); - + if(U_FAILURE(*err)){ char context[CONTEXT_LEN]; char preContext[CONTEXT_LEN]; @@ -152,9 +158,9 @@ ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* err){ int32_t start=0; int32_t stop =0; int32_t pos =0; - + if( buf->showWarning==TRUE){ - fprintf(stderr,"\n###WARNING: Encountered abnormal bytes while" + fprintf(stderr,"\n###WARNING: Encountered abnormal bytes while" " converting input stream to target encoding: %s\n", u_errorName(*err)); } @@ -164,17 +170,17 @@ ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* err){ /* now get the context chars */ ucnv_getInvalidChars(buf->conv,context,&len,err); context[len]= 0 ; /* null terminate the buffer */ - + pos = source-cbuf-len; /* for pre-context */ start = (pos <=CONTEXT_LEN)? 0 : (pos - (CONTEXT_LEN-1)); stop = pos-len; - + memcpy(preContext,cbuf+start,stop-start); /* null terminate the buffer */ preContext[stop-start] = 0; - + /* for post-context */ start = pos+len; stop = ((pos+CONTEXT_LEN)<= (sourceLimit-cbuf) )? (pos+(CONTEXT_LEN-1)) : (sourceLimit-cbuf); @@ -182,18 +188,18 @@ ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* err){ memcpy(postContext,source,stop-start); /* null terminate the buffer */ postContext[stop-start] = 0; - + if(buf->showWarning ==TRUE){ /* print out the context */ fprintf(stderr,"\tPre-context: %s\n",preContext); fprintf(stderr,"\tContext: %s\n",context); fprintf(stderr,"\tPost-context: %s\n", postContext); } - + /* reset the converter */ ucnv_reset(buf->conv); - /* set the call back to substiture + /* set the call back to substitute * and restart conversion */ ucnv_setToUCallBack(buf->conv, @@ -211,7 +217,7 @@ ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* err){ ucnv_toUnicode(buf->conv,&target,target+(MAX_U_BUF-offset), &source,sourceLimit,NULL, (UBool)(buf->remaining==0),err); - + } numRead= target-pTarget; @@ -253,7 +259,7 @@ ucbuf_getc(UCHARBUF* buf,UErrorCode* err){ /* u_unescapeAt() callback to return a UChar*/ -static UChar +static UChar _charAt(int32_t offset, void *context) { return ((UCHARBUF*) context)->currentPos[offset]; } @@ -276,17 +282,17 @@ ucbuf_getcx(UCHARBUF* buf,UErrorCode* err) { } else { c1 = U_EOF; } - + c2 = *(buf->currentPos); /* If it isn't a backslash, return it */ if (c1 != 0x005C) { return c1; } - + /* Determine the amount of data in the buffer */ length = buf->bufLimit-buf->currentPos; - + /* The longest escape sequence is \Uhhhhhhhh; make sure we have at least that many characters */ if (length < 10) { @@ -295,7 +301,7 @@ ucbuf_getcx(UCHARBUF* buf,UErrorCode* err) { ucbuf_fillucbuf(buf,err); length = buf->bufLimit-buf->buffer; } - + /* Process the escape */ offset = 0; c32 = u_unescapeAt(_charAt, &offset, length, (void*)buf); @@ -307,7 +313,7 @@ ucbuf_getcx(UCHARBUF* buf,UErrorCode* err) { /* Update the current buffer position */ buf->currentPos += offset; }else{ - /* unescaping failed so we just return + /* unescaping failed so we just return * c1 and not consume the buffer * this is useful for rules with escapes * in resouce bundles @@ -364,11 +370,11 @@ ucbuf_open(FileStream* in,const char* cp, UBool showWarning, UErrorCode* err){ } } -/* TODO: this method will fail if at the +/* TODO: this method will fail if at the * begining of buffer and the uchar to unget * is from the previous buffer. Need to implement * system to take care of that situation. - */ + */ U_CAPI void U_EXPORT2 ucbuf_ungetc(UChar32 c,UCHARBUF* buf){ /* decrement currentPos pointer @@ -380,7 +386,7 @@ ucbuf_ungetc(UChar32 c,UCHARBUF* buf){ } /* frees the resources of UChar* buffer */ -static void +static void ucbuf_closebuf(UCHARBUF* buf){ uprv_free(buf->buffer); buf->buffer = NULL;