From bdb4c7025a0beecbb84267c843b6d8b4e00241bc Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Wed, 31 Mar 2021 05:36:55 +0000 Subject: [PATCH] ICU-21526 Change return of UElementComparator to int32_t See #1621 --- icu4c/source/common/filteredbrk.cpp | 2 +- icu4c/source/common/locid.cpp | 27 ++++++------------------- icu4c/source/common/uelement.h | 7 +++++-- icu4c/source/common/uniset.cpp | 2 +- icu4c/source/common/uvector.cpp | 2 +- icu4c/source/i18n/uspoof_conf.cpp | 2 +- icu4c/source/test/intltest/uvectest.cpp | 2 +- 7 files changed, 16 insertions(+), 28 deletions(-) diff --git a/icu4c/source/common/filteredbrk.cpp b/icu4c/source/common/filteredbrk.cpp index c07128cbce1..d2a6d983270 100644 --- a/icu4c/source/common/filteredbrk.cpp +++ b/icu4c/source/common/filteredbrk.cpp @@ -48,7 +48,7 @@ static void _fb_trace(const char *m, const UnicodeString *s, UBool b, int32_t d, /** * Used with sortedInsert() */ -static int8_t U_CALLCONV compareUnicodeString(UElement t1, UElement t2) { +static int32_t U_CALLCONV compareUnicodeString(UElement t1, UElement t2) { const UnicodeString &a = *(const UnicodeString*)t1.pointer; const UnicodeString &b = *(const UnicodeString*)t2.pointer; return a.compare(b); diff --git a/icu4c/source/common/locid.cpp b/icu4c/source/common/locid.cpp index 3dcc5bcfa8a..8f1b22c26d1 100644 --- a/icu4c/source/common/locid.cpp +++ b/icu4c/source/common/locid.cpp @@ -1578,13 +1578,8 @@ AliasReplacer::replaceTransformedExtensions( } tkey = nextTKey; } while (tkey != nullptr); - tfields.sort([](UElement e1, UElement e2) -> int8_t { - // uprv_strcmp return int and in some platform, such as arm64-v8a, - // it may return positive values > 127 which cause the casted value - // of int8_t negative. - int res = uprv_strcmp( - (const char*)e1.pointer, (const char*)e2.pointer); - return (res == 0) ? 0 : ((res > 0) ? 1 : -1); + tfields.sort([](UElement e1, UElement e2) -> int32_t { + return uprv_strcmp((const char*)e1.pointer, (const char*)e2.pointer); }, status); for (int32_t i = 0; i < tfields.size(); i++) { if (output.length() > 0) { @@ -1623,13 +1618,8 @@ AliasReplacer::outputToString( if (!notEmpty(script) && !notEmpty(region)) { out.append(SEP_CHAR, status); } - variants.sort([](UElement e1, UElement e2) -> int8_t { - // uprv_strcmp return int and in some platform, such as arm64-v8a, - // it may return positive values > 127 which cause the casted value - // of int8_t negative. - int res = uprv_strcmp( - (const char*)e1.pointer, (const char*)e2.pointer); - return (res == 0) ? 0 : ((res > 0) ? 1 : -1); + variants.sort([](UElement e1, UElement e2) -> int32_t { + return uprv_strcmp((const char*)e1.pointer, (const char*)e2.pointer); }, status); int32_t variantsStart = out.length(); for (int32_t i = 0; i < variants.size(); i++) { @@ -1689,13 +1679,8 @@ AliasReplacer::replace(const Locale& locale, CharString& out, UErrorCode& status if (U_FAILURE(status)) { return false; } // Sort the variants - variants.sort([](UElement e1, UElement e2) -> int8_t { - // uprv_strcmp return int and in some platform, such as arm64-v8a, - // it may return positive values > 127 which cause the casted value - // of int8_t negative. - int res = uprv_strcmp( - (const char*)e1.pointer, (const char*)e2.pointer); - return (res == 0) ? 0 : ((res > 0) ? 1 : -1); + variants.sort([](UElement e1, UElement e2) -> int32_t { + return uprv_strcmp((const char*)e1.pointer, (const char*)e2.pointer); }, status); // A changed count to assert when loop too many times. diff --git a/icu4c/source/common/uelement.h b/icu4c/source/common/uelement.h index 88dd4d66fb9..2c5a2043e17 100644 --- a/icu4c/source/common/uelement.h +++ b/icu4c/source/common/uelement.h @@ -54,9 +54,12 @@ typedef UBool U_CALLCONV UElementsAreEqual(const UElement e1, const UElement e2) * An element sorting (three-way) comparison function. * @param e1 An element (object or integer) * @param e2 An element (object or integer) - * @return 0 if the two elements are equal, -1 if e1 is < e2, or +1 if e1 is > e2. + * @return 32-bit signed integer comparison result: + * ==0 if the two elements are equal, + * <0 if e1 is < e2, or + * >0 if e1 is > e2. */ -typedef int8_t U_CALLCONV UElementComparator(UElement e1, UElement e2); +typedef int32_t U_CALLCONV UElementComparator(UElement e1, UElement e2); /** * An element assignment function. It may copy an integer, copy diff --git a/icu4c/source/common/uniset.cpp b/icu4c/source/common/uniset.cpp index 461e5a7197e..2db45a2b2a8 100644 --- a/icu4c/source/common/uniset.cpp +++ b/icu4c/source/common/uniset.cpp @@ -111,7 +111,7 @@ static void U_CALLCONV cloneUnicodeString(UElement *dst, UElement *src) { dst->pointer = new UnicodeString(*(UnicodeString*)src->pointer); } -static int8_t U_CALLCONV compareUnicodeString(UElement t1, UElement t2) { +static int32_t U_CALLCONV compareUnicodeString(UElement t1, UElement t2) { const UnicodeString &a = *(const UnicodeString*)t1.pointer; const UnicodeString &b = *(const UnicodeString*)t2.pointer; return a.compare(b); diff --git a/icu4c/source/common/uvector.cpp b/icu4c/source/common/uvector.cpp index 9c7e74c6d5a..fd53c603456 100644 --- a/icu4c/source/common/uvector.cpp +++ b/icu4c/source/common/uvector.cpp @@ -466,7 +466,7 @@ void UVector::sortedInsert(UElement e, UElementComparator *compare, UErrorCode& int32_t min = 0, max = count; while (min != max) { int32_t probe = (min + max) / 2; - int8_t c = (*compare)(elements[probe], e); + int32_t c = (*compare)(elements[probe], e); if (c > 0) { max = probe; } else { diff --git a/icu4c/source/i18n/uspoof_conf.cpp b/icu4c/source/i18n/uspoof_conf.cpp index 862f5daef5d..acb7332b10d 100644 --- a/icu4c/source/i18n/uspoof_conf.cpp +++ b/icu4c/source/i18n/uspoof_conf.cpp @@ -113,7 +113,7 @@ SPUString *SPUStringPool::getByIndex(int32_t index) { // by code point order. // Conforms to the type signature for a USortComparator in uvector.h -static int8_t U_CALLCONV SPUStringCompare(UHashTok left, UHashTok right) { +static int32_t U_CALLCONV SPUStringCompare(UHashTok left, UHashTok right) { const SPUString *sL = const_cast( static_cast(left.pointer)); const SPUString *sR = const_cast( diff --git a/icu4c/source/test/intltest/uvectest.cpp b/icu4c/source/test/intltest/uvectest.cpp index 3829ba2668d..0a05ea9f59d 100644 --- a/icu4c/source/test/intltest/uvectest.cpp +++ b/icu4c/source/test/intltest/uvectest.cpp @@ -70,7 +70,7 @@ void UVectorTest::runIndexedTest( int32_t index, UBool exec, const char* &name, }\ } UPRV_BLOCK_MACRO_END -static int8_t U_CALLCONV +static int32_t U_CALLCONV UVectorTest_compareInt32(UElement key1, UElement key2) { if (key1.integer > key2.integer) { return 1; -- 2.40.0