From 6ca288559fef9d54ef7b836861b035938206ab89 Mon Sep 17 00:00:00 2001 From: Fredrik Roubert Date: Wed, 18 Sep 2024 23:57:54 +0200 Subject: [PATCH] ICU-22936 Replace all ICU4C code that uses UBool as an integer. --- icu4c/source/common/unicode/utf8.h | 4 ++-- icu4c/source/common/usprep.cpp | 2 +- icu4c/source/common/utf_impl.cpp | 6 ++---- icu4c/source/i18n/scriptset.cpp | 6 +++--- icu4c/source/i18n/scriptset.h | 2 +- icu4c/source/test/cintltst/cloctst.c | 2 +- icu4c/source/test/cintltst/creststn.c | 3 +-- icu4c/source/test/cintltst/cucdtst.c | 8 ++++---- icu4c/source/test/cintltst/udatatst.c | 13 +------------ icu4c/source/test/intltest/caltest.cpp | 2 +- icu4c/source/test/intltest/collationtest.cpp | 8 ++++---- icu4c/source/test/intltest/uvectest.cpp | 2 +- icu4c/source/tools/makeconv/makeconv.cpp | 4 ++-- icu4c/source/tools/toolutil/ucm.cpp | 8 ++++---- icu4c/source/tools/toolutil/ucm.h | 2 +- 15 files changed, 29 insertions(+), 43 deletions(-) diff --git a/icu4c/source/common/unicode/utf8.h b/icu4c/source/common/unicode/utf8.h index 5a07435fcf60..96ad46161aa1 100644 --- a/icu4c/source/common/unicode/utf8.h +++ b/icu4c/source/common/unicode/utf8.h @@ -124,7 +124,7 @@ * @internal */ U_CAPI UChar32 U_EXPORT2 -utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict); +utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, int8_t strict); /** * Function for handling "append code point" with error-checking. @@ -148,7 +148,7 @@ utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool * @internal */ U_CAPI UChar32 U_EXPORT2 -utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict); +utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, int8_t strict); /** * Function for handling "skip backward one code point" with error-checking. diff --git a/icu4c/source/common/usprep.cpp b/icu4c/source/common/usprep.cpp index 477b8f2309db..048b423645f8 100644 --- a/icu4c/source/common/usprep.cpp +++ b/icu4c/source/common/usprep.cpp @@ -126,7 +126,7 @@ compareEntries(const UHashTok p1, const UHashTok p2) { name2.pointer = b2->name; path1.pointer = b1->path; path2.pointer = b2->path; - return uhash_compareChars(name1, name2) & uhash_compareChars(path1, path2); + return uhash_compareChars(name1, name2) && uhash_compareChars(path1, path2); } static void diff --git a/icu4c/source/common/utf_impl.cpp b/icu4c/source/common/utf_impl.cpp index 827a82daf403..7da10c9b2d36 100644 --- a/icu4c/source/common/utf_impl.cpp +++ b/icu4c/source/common/utf_impl.cpp @@ -124,11 +124,9 @@ errorValue(int32_t count, int8_t strict) { * >0 Obsolete "strict" behavior of UTF8_NEXT_CHAR_SAFE(..., true): * Same as the obsolete "safe" behavior, but non-characters are also treated * like illegal sequences. - * - * Note that a UBool is the same as an int8_t. */ U_CAPI UChar32 U_EXPORT2 -utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict) { +utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, int8_t strict) { // *pi is one after byte c. int32_t i=*pi; // length can be negative for NUL-terminated strings: Read and validate one byte at a time. @@ -233,7 +231,7 @@ utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool } U_CAPI UChar32 U_EXPORT2 -utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict) { +utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, int8_t strict) { // *pi is the index of byte c. int32_t i=*pi; if(U8_IS_TRAIL(c) && i>start) { diff --git a/icu4c/source/i18n/scriptset.cpp b/icu4c/source/i18n/scriptset.cpp index eec1eeb37daf..576917e81c41 100644 --- a/icu4c/source/i18n/scriptset.cpp +++ b/icu4c/source/i18n/scriptset.cpp @@ -285,19 +285,19 @@ uhash_equalsScriptSet(const UElement key1, const UElement key2) { return (*s1 == *s2); } -U_CAPI int8_t U_EXPORT2 +U_CAPI int32_t U_EXPORT2 uhash_compareScriptSet(UElement key0, UElement key1) { icu::ScriptSet *s0 = static_cast(key0.pointer); icu::ScriptSet *s1 = static_cast(key1.pointer); int32_t diff = s0->countMembers() - s1->countMembers(); - if (diff != 0) return static_cast(diff); + if (diff != 0) return diff; int32_t i0 = s0->nextSetBit(0); int32_t i1 = s1->nextSetBit(0); while ((diff = i0-i1) == 0 && i0 > 0) { i0 = s0->nextSetBit(i0+1); i1 = s1->nextSetBit(i1+1); } - return (int8_t)diff; + return diff; } U_CAPI int32_t U_EXPORT2 diff --git a/icu4c/source/i18n/scriptset.h b/icu4c/source/i18n/scriptset.h index df5cfdc74868..d21d0db8a014 100644 --- a/icu4c/source/i18n/scriptset.h +++ b/icu4c/source/i18n/scriptset.h @@ -74,7 +74,7 @@ class U_I18N_API ScriptSet: public UMemory { U_NAMESPACE_END -U_CAPI UBool U_EXPORT2 +U_CAPI int32_t U_EXPORT2 uhash_compareScriptSet(const UElement key1, const UElement key2); U_CAPI int32_t U_EXPORT2 diff --git a/icu4c/source/test/cintltst/cloctst.c b/icu4c/source/test/cintltst/cloctst.c index a032753839f1..1a6c20fdcd2a 100644 --- a/icu4c/source/test/cintltst/cloctst.c +++ b/icu4c/source/test/cintltst/cloctst.c @@ -2879,7 +2879,7 @@ static void TestDisplayNameWarning(void) { * starts with `prefix' plus an additional element, that is, string == * prefix + '_' + x, then return 1. Otherwise return a value < 0. */ -static UBool _loccmp(const char* string, const char* prefix) { +static int32_t _loccmp(const char* string, const char* prefix) { int32_t slen = (int32_t)uprv_strlen(string), plen = (int32_t)uprv_strlen(prefix); int32_t c = uprv_strncmp(string, prefix, plen); diff --git a/icu4c/source/test/cintltst/creststn.c b/icu4c/source/test/cintltst/creststn.c index d5b0bb4febf8..c500b99f5ea5 100644 --- a/icu4c/source/test/cintltst/creststn.c +++ b/icu4c/source/test/cintltst/creststn.c @@ -3035,7 +3035,6 @@ tres_getString(const UResourceBundle *resB, const char *s8; UChar32 c16, c8; int32_t length16, length8, i16, i8; - UBool forceCopy; if(length == NULL) { length = &length16; @@ -3053,7 +3052,7 @@ tres_getString(const UResourceBundle *resB, length16 = *length; /* try the UTF-8 variant of ures_getStringXYZ() */ - for(forceCopy = false; forceCopy <= true; ++forceCopy) { + for (int8_t forceCopy = 0; forceCopy <= 1; ++forceCopy) { p8 = buffer8; length8 = (int32_t)sizeof(buffer8); if(idx >= 0) { diff --git a/icu4c/source/test/cintltst/cucdtst.c b/icu4c/source/test/cintltst/cucdtst.c index 8ea805f93c25..e278622dd295 100644 --- a/icu4c/source/test/cintltst/cucdtst.c +++ b/icu4c/source/test/cintltst/cucdtst.c @@ -431,12 +431,12 @@ compareUSets(const USet *a, const USet *b, const char *a_name, const char *b_name, UBool diffIsError) { /* - * Use an arithmetic & not a logical && so that both branches + * Use temporary variables so that both branches * are always taken and all differences are shown. */ - return - showAMinusB(a, b, a_name, b_name, diffIsError) & - showAMinusB(b, a, b_name, a_name, diffIsError); + UBool ab = showAMinusB(a, b, a_name, b_name, diffIsError); + UBool ba = showAMinusB(b, a, b_name, a_name, diffIsError); + return ab && ba; } /* test isLetter(u_isapha()) and isDigit(u_isdigit()) */ diff --git a/icu4c/source/test/cintltst/udatatst.c b/icu4c/source/test/cintltst/udatatst.c index fc1ab786ad34..003d7d43c31f 100644 --- a/icu4c/source/test/cintltst/udatatst.c +++ b/icu4c/source/test/cintltst/udatatst.c @@ -1139,18 +1139,7 @@ static void TestICUDataName(void) switch(U_CHARSET_FAMILY) { case U_ASCII_FAMILY: - switch((int)U_IS_BIG_ENDIAN) - { - case 1: - typeChar = 'b'; - break; - case 0: - typeChar = 'l'; - break; - default: - log_err("Expected 1 or 0 for U_IS_BIG_ENDIAN, got %d!\n", U_IS_BIG_ENDIAN); - /* return; */ - } + typeChar = U_IS_BIG_ENDIAN ? 'b' : 'l'; break; case U_EBCDIC_FAMILY: typeChar = 'e'; diff --git a/icu4c/source/test/intltest/caltest.cpp b/icu4c/source/test/intltest/caltest.cpp index 6d019086e53e..00485c30de6d 100644 --- a/icu4c/source/test/intltest/caltest.cpp +++ b/icu4c/source/test/intltest/caltest.cpp @@ -3918,7 +3918,7 @@ void CalendarTest::TestClearMonth() { if (failure(status, "Calendar::get(UCAL_MONTH)")) return; cal->clear(UCAL_MONTH); assertEquals("Calendar::isSet(UCAL_MONTH) after clear(UCAL_MONTH)", false, !!cal->isSet(UCAL_MONTH)); - assertEquals("Calendar::get(UCAL_MONTH after clear(UCAL_MONTH))", UCAL_JANUARY, !!cal->get(UCAL_MONTH, status)); + assertEquals("Calendar::get(UCAL_MONTH after clear(UCAL_MONTH))", UCAL_JANUARY, cal->get(UCAL_MONTH, status)); if (failure(status, "Calendar::get(UCAL_MONTH)")) return; cal->set(UCAL_ORDINAL_MONTH, 7); diff --git a/icu4c/source/test/intltest/collationtest.cpp b/icu4c/source/test/intltest/collationtest.cpp index 8308d7ea073d..25da189344ee 100644 --- a/icu4c/source/test/intltest/collationtest.cpp +++ b/icu4c/source/test/intltest/collationtest.cpp @@ -1754,10 +1754,10 @@ UBool CollationTest::checkCompareTwo(const char *norm, const UnicodeString &prev // sortkey(str1 + "\uFFFE" + str2) == mergeSortkeys(sortkey(str1), sortkey(str2)) // only that those two methods yield the same order. // - // Use bit-wise OR so that getMergedCollationKey() is always called for both strings. - if((getMergedCollationKey(prevString.getBuffer(), prevString.length(), prevKey, errorCode) | - getMergedCollationKey(s.getBuffer(), s.length(), key, errorCode)) || - errorCode.isFailure()) { + // Use two variables so that getMergedCollationKey() is always called for both strings. + if (UBool prev = getMergedCollationKey(prevString.getBuffer(), prevString.length(), prevKey, errorCode), + curr = getMergedCollationKey(s.getBuffer(), s.length(), key, errorCode); + prev || curr || errorCode.isFailure()) { order = prevKey.compareTo(key, errorCode); if(order != expectedOrder || errorCode.isFailure()) { infoln(fileTestName); diff --git a/icu4c/source/test/intltest/uvectest.cpp b/icu4c/source/test/intltest/uvectest.cpp index bb089bb3761f..789065eb7bb9 100644 --- a/icu4c/source/test/intltest/uvectest.cpp +++ b/icu4c/source/test/intltest/uvectest.cpp @@ -82,7 +82,7 @@ UVectorTest_compareInt32(UElement key1, UElement key2) { } U_CDECL_BEGIN -static int8_t U_CALLCONV +static UBool U_CALLCONV UVectorTest_compareCstrings(const UElement key1, const UElement key2) { return !strcmp((const char *)key1.pointer, (const char *)key2.pointer); } diff --git a/icu4c/source/tools/makeconv/makeconv.cpp b/icu4c/source/tools/makeconv/makeconv.cpp index 2156248eaf30..0298cf350e3c 100644 --- a/icu4c/source/tools/makeconv/makeconv.cpp +++ b/icu4c/source/tools/makeconv/makeconv.cpp @@ -684,7 +684,7 @@ createConverter(ConvData *data, const char *converterName, UErrorCode *pErrorCod } else if( data->ucm->ext->mappingsLength>0 && - !ucm_checkBaseExt(states, data->ucm->base, data->ucm->ext, data->ucm->ext, false) + !ucm_checkBaseExt(states, data->ucm->base, data->ucm->ext, data->ucm->ext, 0) ) { *pErrorCode=U_INVALID_TABLE_FORMAT; } else if(data->ucm->base->flagsType&UCM_FLAGS_EXPLICIT) { @@ -804,7 +804,7 @@ createConverter(ConvData *data, const char *converterName, UErrorCode *pErrorCod } else if( !ucm_checkValidity(data->ucm->ext, baseStates) || - !ucm_checkBaseExt(baseStates, baseData.ucm->base, data->ucm->ext, data->ucm->ext, false) + !ucm_checkBaseExt(baseStates, baseData.ucm->base, data->ucm->ext, data->ucm->ext, 0) ) { *pErrorCode=U_INVALID_TABLE_FORMAT; } else { diff --git a/icu4c/source/tools/toolutil/ucm.cpp b/icu4c/source/tools/toolutil/ucm.cpp index 923041a53f60..824362a69392 100644 --- a/icu4c/source/tools/toolutil/ucm.cpp +++ b/icu4c/source/tools/toolutil/ucm.cpp @@ -310,7 +310,7 @@ enum { static uint8_t checkBaseExtUnicode(UCMStates *baseStates, UCMTable *base, UCMTable *ext, - UBool moveToExt, UBool intersectBase) { + UBool moveToExt, int8_t intersectBase) { (void)baseStates; UCMapping *mb, *me, *mbLimit, *meLimit; @@ -416,7 +416,7 @@ checkBaseExtUnicode(UCMStates *baseStates, UCMTable *base, UCMTable *ext, static uint8_t checkBaseExtBytes(UCMStates *baseStates, UCMTable *base, UCMTable *ext, - UBool moveToExt, UBool intersectBase) { + UBool moveToExt, int8_t intersectBase) { UCMapping *mb, *me; int32_t *baseMap, *extMap; int32_t b, e, bLimit, eLimit, cmp; @@ -556,7 +556,7 @@ ucm_checkValidity(UCMTable *table, UCMStates *baseStates) { U_CAPI UBool U_EXPORT2 ucm_checkBaseExt(UCMStates *baseStates, UCMTable *base, UCMTable *ext, UCMTable *moveTarget, - UBool intersectBase) { + int8_t intersectBase) { uint8_t result; /* if we have an extension table, we must always use precision flags */ @@ -735,7 +735,7 @@ ucm_separateMappings(UCMFile *ucm, UBool isSISO) { } if(needsMove) { ucm_moveMappings(ucm->base, ucm->ext); - return ucm_checkBaseExt(&ucm->states, ucm->base, ucm->ext, ucm->ext, false); + return ucm_checkBaseExt(&ucm->states, ucm->base, ucm->ext, ucm->ext, 0); } else { ucm_sortTable(ucm->base); return true; diff --git a/icu4c/source/tools/toolutil/ucm.h b/icu4c/source/tools/toolutil/ucm.h index 8ea90604d475..8f78b52e9687 100644 --- a/icu4c/source/tools/toolutil/ucm.h +++ b/icu4c/source/tools/toolutil/ucm.h @@ -227,7 +227,7 @@ ucm_checkValidity(UCMTable *ext, UCMStates *baseStates); */ U_CAPI UBool U_EXPORT2 ucm_checkBaseExt(UCMStates *baseStates, UCMTable *base, UCMTable *ext, - UCMTable *moveTarget, UBool intersectBase); + UCMTable *moveTarget, int8_t intersectBase); U_CAPI void U_EXPORT2 ucm_printTable(UCMTable *table, FILE *f, UBool byUnicode);