From 9a3376c37985b31c7bf2214f404d68e253d72837 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Thu, 15 Dec 2011 00:04:40 +0000 Subject: [PATCH] ICU-9000 use our own implementation of uprv_stricmp() and uprv_strnicmp() to make sure we use language-independent case mappings/comparisons X-SVN-Rev: 31118 --- icu4c/source/common/cstring.c | 6 +-- icu4c/source/common/cstring.h | 27 +++++-------- icu4c/source/common/uloc_tag.c | 2 +- icu4c/source/test/cintltst/ccapitst.c | 51 +---------------------- icu4c/source/test/cintltst/cstrtest.c | 58 +++++++++++++-------------- 5 files changed, 45 insertions(+), 99 deletions(-) diff --git a/icu4c/source/common/cstring.c b/icu4c/source/common/cstring.c index e308709a9cd..3af959eb7d0 100644 --- a/icu4c/source/common/cstring.c +++ b/icu4c/source/common/cstring.c @@ -228,9 +228,9 @@ T_CString_stringToInteger(const char *integerString, int32_t radix) return uprv_strtoul(integerString, &end, radix); } - + U_CAPI int U_EXPORT2 -T_CString_stricmp(const char *str1, const char *str2) { +uprv_stricmp(const char *str1, const char *str2) { if(str1==NULL) { if(str2==NULL) { return 0; @@ -269,7 +269,7 @@ T_CString_stricmp(const char *str1, const char *str2) { } U_CAPI int U_EXPORT2 -T_CString_strnicmp(const char *str1, const char *str2, uint32_t n) { +uprv_strnicmp(const char *str1, const char *str2, uint32_t n) { if(str1==NULL) { if(str2==NULL) { return 0; diff --git a/icu4c/source/common/cstring.h b/icu4c/source/common/cstring.h index f67394428a6..e7cea641bbb 100644 --- a/icu4c/source/common/cstring.h +++ b/icu4c/source/common/cstring.h @@ -70,21 +70,6 @@ uprv_ebcdictolower(char c); #define uprv_strtod(source, end) U_STANDARD_CPP_NAMESPACE strtod(source, end) #define uprv_strtoul(str, end, base) U_STANDARD_CPP_NAMESPACE strtoul(str, end, base) #define uprv_strtol(str, end, base) U_STANDARD_CPP_NAMESPACE strtol(str, end, base) -#if U_PLATFORM == U_PF_WINDOWS -# if defined(__BORLANDC__) -# define uprv_stricmp(str1, str2) U_STANDARD_CPP_NAMESPACE stricmp(str1, str2) -# define uprv_strnicmp(str1, str2, n) U_STANDARD_CPP_NAMESPACE strnicmp(str1, str2, n) -# else -# define uprv_stricmp(str1, str2) U_STANDARD_CPP_NAMESPACE _stricmp(str1, str2) -# define uprv_strnicmp(str1, str2, n) U_STANDARD_CPP_NAMESPACE _strnicmp(str1, str2, n) -# endif -#elif defined(POSIX) -# define uprv_stricmp(str1, str2) U_STANDARD_CPP_NAMESPACE strcasecmp(str1, str2) -# define uprv_strnicmp(str1, str2, n) U_STANDARD_CPP_NAMESPACE strncasecmp(str1, str2, n) -#else -# define uprv_stricmp(str1, str2) T_CString_stricmp(str1, str2) -# define uprv_strnicmp(str1, str2, n) T_CString_strnicmp(str1, str2, n) -#endif /* Conversion from a digit to the character with radix base from 2-19 */ /* May need to use U_UPPER_ORDINAL*/ @@ -119,10 +104,18 @@ T_CString_int64ToString(char *buffer, int64_t n, uint32_t radix); U_CAPI int32_t U_EXPORT2 T_CString_stringToInteger(const char *integerString, int32_t radix); +/** + * Case-insensitive, language-independent string comparison + * limited to the ASCII character repertoire. + */ U_CAPI int U_EXPORT2 -T_CString_stricmp(const char *str1, const char *str2); +uprv_stricmp(const char *str1, const char *str2); +/** + * Case-insensitive, language-independent string comparison + * limited to the ASCII character repertoire. + */ U_CAPI int U_EXPORT2 -T_CString_strnicmp(const char *str1, const char *str2, uint32_t n); +uprv_strnicmp(const char *str1, const char *str2, uint32_t n); #endif /* ! CSTRING_H */ diff --git a/icu4c/source/common/uloc_tag.c b/icu4c/source/common/uloc_tag.c index 1f697544621..e477ce39a09 100644 --- a/icu4c/source/common/uloc_tag.c +++ b/icu4c/source/common/uloc_tag.c @@ -2064,7 +2064,7 @@ ultag_parse(const char* tag, int32_t tagLen, int32_t* parsedLen, UErrorCode* sta /* check if the tag is grandfathered */ for (i = 0; GRANDFATHERED[i] != NULL; i += 2) { - if (T_CString_stricmp(GRANDFATHERED[i], tagBuf) == 0) { + if (uprv_stricmp(GRANDFATHERED[i], tagBuf) == 0) { /* a grandfathered tag is always longer than its preferred mapping */ int32_t newTagLength = uprv_strlen(GRANDFATHERED[i+1]); if (tagLen < newTagLength) { diff --git a/icu4c/source/test/cintltst/ccapitst.c b/icu4c/source/test/cintltst/ccapitst.c index 52035fa44f4..ccaafd65308 100644 --- a/icu4c/source/test/cintltst/ccapitst.c +++ b/icu4c/source/test/cintltst/ccapitst.c @@ -26,54 +26,7 @@ #include "cmemory.h" /* for UAlignedMemory */ #include "cintltst.h" #include "ccapitst.h" - -/* for not including "cstring.h" -begin*/ -#if U_PLATFORM_USES_ONLY_WIN32_API -# define ctest_stricmp(str1, str2) U_STANDARD_CPP_NAMESPACE _stricmp(str1, str2) -#elif defined(POSIX) -# define ctest_stricmp(str1, str2) U_STANDARD_CPP_NAMESPACE strcasecmp(str1, str2) -#else -# define ctest_stricmp(str1, str2) T_CString_stricmp(str1, str2) -#endif - -static int U_EXPORT2 -T_CString_stricmp(const char *str1, const char *str2) { - if(str1==NULL) { - if(str2==NULL) { - return 0; - } else { - return -1; - } - } else if(str2==NULL) { - return 1; - } else { - /* compare non-NULL strings lexically with lowercase */ - int rc; - unsigned char c1, c2; - for(;;) { - c1=(unsigned char)*str1; - c2=(unsigned char)*str2; - if(c1==0) { - if(c2==0) { - return 0; - } else { - return -1; - } - } else if(c2==0) { - return 1; - } else { - /* compare non-zero characters with lowercase */ - rc=(int)(unsigned char)tolower(c1)-(int)(unsigned char)tolower(c2); - if(rc!=0) { - return rc; - } - } - ++str1; - ++str2; - } - } -} -/* for not including "cstring.h" -end*/ +#include "cstring.h" #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) @@ -607,7 +560,7 @@ static void TestConvert() { log_verbose("getName o.k. %s\n", ucnv_getName(myConverter, &err)); } - if (ctest_stricmp(ucnv_getName(myConverter, &err), CodePagesToTest[codepage_index])) + if (uprv_stricmp(ucnv_getName(myConverter, &err), CodePagesToTest[codepage_index])) log_err("getName failed\n"); else log_verbose("getName ok\n"); diff --git a/icu4c/source/test/cintltst/cstrtest.c b/icu4c/source/test/cintltst/cstrtest.c index afef9d33ad1..7ab989ab359 100644 --- a/icu4c/source/test/cintltst/cstrtest.c +++ b/icu4c/source/test/cintltst/cstrtest.c @@ -1,6 +1,6 @@ /* ********************************************************************** -* Copyright (C) 1998-2010, International Business Machines Corporation +* Copyright (C) 1998-2011, International Business Machines Corporation * and others. All Rights Reserved. ********************************************************************** * @@ -107,26 +107,26 @@ static void TestAPI(void) } uprv_strcpy(src, "this is lower case"); - if(T_CString_stricmp(src, "THIS is lower CASE") != 0){ - log_err("FAIL: *****T_CString_stricmp() failed."); + if(uprv_stricmp(src, "THIS is lower CASE") != 0){ + log_err("FAIL: *****uprv_stricmp() failed."); } - if((intValue=T_CString_stricmp(NULL, "first string is null") )!= -1){ - log_err("FAIL: T_CString_stricmp() where the first string is null failed. Expected: -1, returned %d\n", intValue); + if((intValue=uprv_stricmp(NULL, "first string is null") )!= -1){ + log_err("FAIL: uprv_stricmp() where the first string is null failed. Expected: -1, returned %d\n", intValue); } - if((intValue=T_CString_stricmp("second string is null", NULL)) != 1){ - log_err("FAIL: T_CString_stricmp() where the second string is null failed. Expected: 1, returned %d\n", intValue); + if((intValue=uprv_stricmp("second string is null", NULL)) != 1){ + log_err("FAIL: uprv_stricmp() where the second string is null failed. Expected: 1, returned %d\n", intValue); } - if((intValue=T_CString_stricmp(NULL, NULL)) != 0){ - log_err("FAIL: T_CString_stricmp(NULL, NULL) failed. Expected: 0, returned %d\n", intValue);; + if((intValue=uprv_stricmp(NULL, NULL)) != 0){ + log_err("FAIL: uprv_stricmp(NULL, NULL) failed. Expected: 0, returned %d\n", intValue);; } - if((intValue=T_CString_stricmp("", "")) != 0){ - log_err("FAIL: T_CString_stricmp(\"\", \"\") failed. Expected: 0, returned %d\n", intValue);; + if((intValue=uprv_stricmp("", "")) != 0){ + log_err("FAIL: uprv_stricmp(\"\", \"\") failed. Expected: 0, returned %d\n", intValue);; } - if((intValue=T_CString_stricmp("", "abc")) != -1){ - log_err("FAIL: T_CString_stricmp(\"\", \"abc\") failed. Expected: -1, returned %d\n", intValue); + if((intValue=uprv_stricmp("", "abc")) != -1){ + log_err("FAIL: uprv_stricmp(\"\", \"abc\") failed. Expected: -1, returned %d\n", intValue); } - if((intValue=T_CString_stricmp("abc", "")) != 1){ - log_err("FAIL: T_CString_stricmp(\"abc\", \"\") failed. Expected: 1, returned %d\n", intValue); + if((intValue=uprv_stricmp("abc", "")) != 1){ + log_err("FAIL: uprv_stricmp(\"abc\", \"\") failed. Expected: 1, returned %d\n", intValue); } temp=uprv_strdup("strdup"); @@ -136,26 +136,26 @@ static void TestAPI(void) uprv_free((char *)temp); uprv_strcpy(src, "this is lower case"); - if(T_CString_strnicmp(src, "THIS", 4 ) != 0){ - log_err("FAIL: *****T_CString_strnicmp() failed."); + if(uprv_strnicmp(src, "THIS", 4 ) != 0){ + log_err("FAIL: *****uprv_strnicmp() failed."); } - if((intValue=T_CString_strnicmp(NULL, "first string is null", 10) )!= -1){ - log_err("FAIL: T_CString_strnicmp() where the first string is null failed. Expected: -1, returned %d\n", intValue); + if((intValue=uprv_strnicmp(NULL, "first string is null", 10) )!= -1){ + log_err("FAIL: uprv_strnicmp() where the first string is null failed. Expected: -1, returned %d\n", intValue); } - if((intValue=T_CString_strnicmp("second string is null", NULL, 10)) != 1){ - log_err("FAIL: T_CString_strnicmp() where the second string is null failed. Expected: 1, returned %d\n", intValue); + if((intValue=uprv_strnicmp("second string is null", NULL, 10)) != 1){ + log_err("FAIL: uprv_strnicmp() where the second string is null failed. Expected: 1, returned %d\n", intValue); } - if((intValue=T_CString_strnicmp(NULL, NULL, 10)) != 0){ - log_err("FAIL: T_CString_strnicmp(NULL, NULL, 10) failed. Expected: 0, returned %d\n", intValue);; + if((intValue=uprv_strnicmp(NULL, NULL, 10)) != 0){ + log_err("FAIL: uprv_strnicmp(NULL, NULL, 10) failed. Expected: 0, returned %d\n", intValue);; } - if((intValue=T_CString_strnicmp("", "", 10)) != 0){ - log_err("FAIL: T_CString_strnicmp(\"\", \"\") failed. Expected: 0, returned %d\n", intValue);; + if((intValue=uprv_strnicmp("", "", 10)) != 0){ + log_err("FAIL: uprv_strnicmp(\"\", \"\") failed. Expected: 0, returned %d\n", intValue);; } - if((intValue=T_CString_strnicmp("", "abc", 10)) != -1){ - log_err("FAIL: T_CString_stricmp(\"\", \"abc\", 10) failed. Expected: -1, returned %d\n", intValue); + if((intValue=uprv_strnicmp("", "abc", 10)) != -1){ + log_err("FAIL: uprv_stricmp(\"\", \"abc\", 10) failed. Expected: -1, returned %d\n", intValue); } - if((intValue=T_CString_strnicmp("abc", "", 10)) != 1){ - log_err("FAIL: T_CString_strnicmp(\"abc\", \"\", 10) failed. Expected: 1, returned %d\n", intValue); + if((intValue=uprv_strnicmp("abc", "", 10)) != 1){ + log_err("FAIL: uprv_strnicmp(\"abc\", \"\", 10) failed. Expected: 1, returned %d\n", intValue); } } -- 2.40.0