From 1ef813cb29a9ecb1e7c346fb6cd56e5b35756b15 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Wed, 20 Feb 2002 02:09:42 +0000 Subject: [PATCH] ICU-1036 simplify internal string case mapping functions for more common code - remove growBuffer X-SVN-Rev: 7717 --- icu4c/source/common/unicode/unistr.h | 6 --- icu4c/source/common/unistr.cpp | 60 ++++++++++------------------ icu4c/source/common/ustring.c | 26 ++++++++---- 3 files changed, 41 insertions(+), 51 deletions(-) diff --git a/icu4c/source/common/unicode/unistr.h b/icu4c/source/common/unicode/unistr.h index c5389d68ad..8b0fd6dd7d 100644 --- a/icu4c/source/common/unicode/unistr.h +++ b/icu4c/source/common/unicode/unistr.h @@ -2729,12 +2729,6 @@ private: int32_t **pBufferToDelete = 0, UBool forceClone = FALSE); - // UGrowBuffer function for string case mapping and similar - static UBool U_CALLCONV - growBuffer(void *context, - UChar **buffer, int32_t *pCapacity, int32_t reqCapacity, - int32_t length); - // common function for case mappings UnicodeString & caseMap(const Locale& locale, diff --git a/icu4c/source/common/unistr.cpp b/icu4c/source/common/unistr.cpp index a020c3f9ca..2e19ca7179 100644 --- a/icu4c/source/common/unistr.cpp +++ b/icu4c/source/common/unistr.cpp @@ -1039,23 +1039,6 @@ UnicodeString::foldCase(uint32_t options) { return caseMap(Locale::getDefault(), options, FOLD_CASE); } -// static helper function for string case mapping -// called by u_internalStrToUpper/Lower() -UBool U_CALLCONV -UnicodeString::growBuffer(void *context, - UChar **buffer, int32_t *pCapacity, int32_t reqCapacity, - int32_t length) { - UnicodeString *me = (UnicodeString *)context; - me->fLength = length; - if(me->cloneArrayIfNeeded(reqCapacity)) { - *buffer = me->fArray; - *pCapacity = me->fCapacity; - return TRUE; - } else { - return FALSE; - } -} - UnicodeString & UnicodeString::caseMap(const Locale& locale, uint32_t options, @@ -1082,32 +1065,33 @@ UnicodeString::caseMap(const Locale& locale, capacity = US_STACKBUF_SIZE; } } else { - capacity = fLength + 2; + capacity = fLength + 20; } if(!cloneArrayIfNeeded(capacity, capacity, FALSE, &bufferToDelete, TRUE)) { return *this; } - UErrorCode errorCode = U_ZERO_ERROR; - if(toWhichCase==TO_LOWER) { - fLength = u_internalStrToLower(fArray, fCapacity, - oldArray, oldLength, - locale.getName(), - growBuffer, this, - &errorCode); - } else if(toWhichCase==TO_UPPER) { - fLength = u_internalStrToUpper(fArray, fCapacity, - oldArray, oldLength, - locale.getName(), - growBuffer, this, - &errorCode); - } else { - fLength = u_internalStrFoldCase(fArray, fCapacity, - oldArray, oldLength, - options, - growBuffer, this, - &errorCode); - } + // Case-map, and if the result is too long, then reallocate and repeat. + UErrorCode errorCode; + do { + errorCode = U_ZERO_ERROR; + if(toWhichCase==TO_LOWER) { + fLength = u_internalStrToLower(fArray, fCapacity, + oldArray, oldLength, + locale.getName(), + &errorCode); + } else if(toWhichCase==TO_UPPER) { + fLength = u_internalStrToUpper(fArray, fCapacity, + oldArray, oldLength, + locale.getName(), + &errorCode); + } else { + fLength = u_internalStrFoldCase(fArray, fCapacity, + oldArray, oldLength, + options, + &errorCode); + } + } while(errorCode==U_BUFFER_OVERFLOW_ERROR && cloneArrayIfNeeded(fLength, fLength, FALSE)); delete [] bufferToDelete; if(U_FAILURE(errorCode)) { diff --git a/icu4c/source/common/ustring.c b/icu4c/source/common/ustring.c index aa4328fc51..a3061f224a 100644 --- a/icu4c/source/common/ustring.c +++ b/icu4c/source/common/ustring.c @@ -684,13 +684,13 @@ u_strCaseMap(UChar *dest, int32_t destCapacity, if(toWhichCase==TO_LOWER) { destLength=u_internalStrToLower(temp, destCapacity, src, srcLength, - locale, NULL, NULL, pErrorCode); + locale, pErrorCode); } else if(toWhichCase==TO_UPPER) { destLength=u_internalStrToUpper(temp, destCapacity, src, srcLength, - locale, NULL, NULL, pErrorCode); + locale, pErrorCode); } else { destLength=u_internalStrFoldCase(temp, destCapacity, src, srcLength, - options, NULL, NULL, pErrorCode); + options, pErrorCode); } if(temp!=dest) { /* copy the result string to the destination buffer */ @@ -770,7 +770,10 @@ u_strcasecmp(const UChar *s1, const UChar *s2, uint32_t options) { c=UTF16_GET_PAIR_VALUE(c, uc); ++s1; } - len1=u_internalFoldCase(c, t1, options); + len1=u_internalFoldCase(c, t1, 32, options); + if(len1<0) { + len1=-len1; + } pos1=0; } else if(pos2>=len2 && *s2==0) { return 0; @@ -785,7 +788,10 @@ u_strcasecmp(const UChar *s1, const UChar *s2, uint32_t options) { c=UTF16_GET_PAIR_VALUE(c, uc); ++s2; } - len2=u_internalFoldCase(c, t2, options); + len2=u_internalFoldCase(c, t2, 32, options); + if(len2<0) { + len2=-len2; + } pos2=0; } else { return 1; @@ -855,7 +861,10 @@ u_internalStrcasecmp(const UChar *s1, int32_t length1, } else { --length1; } - len1=u_internalFoldCase(c, t1, options); + len1=u_internalFoldCase(c, t1, 32, options); + if(len1<0) { + len1=-len1; + } pos1=0; } else if(pos2>=len2 && length2<=0) { return 0; @@ -873,7 +882,10 @@ u_internalStrcasecmp(const UChar *s1, int32_t length1, } else { --length2; } - len2=u_internalFoldCase(c, t2, options); + len2=u_internalFoldCase(c, t2, 32, options); + if(len2<0) { + len2=-len2; + } pos2=0; } else { return 1;