From 1284866114aff59c30ad02e7acb1636b5780ce6d Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Fri, 23 May 2003 21:14:16 +0000 Subject: [PATCH] ICU-2914 fix T_CString_integerToString() length return value X-SVN-Rev: 12091 --- icu4c/source/common/cstring.c | 30 ++++++++++++++++++--------- icu4c/source/test/cintltst/cstrtest.c | 10 ++++----- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/icu4c/source/common/cstring.c b/icu4c/source/common/cstring.c index e52890c9791..f65c9e083cd 100644 --- a/icu4c/source/common/cstring.c +++ b/icu4c/source/common/cstring.c @@ -101,17 +101,26 @@ T_CString_toUpperCase(char* str) return origPtr; } -/*Takes a int32_t and fills in a char* string with that number "radix"-based - Return the length of the string */ - +/* + * Takes a int32_t and fills in a char* string with that number "radix"-based. + * Does not handle negative values (makes an empty string for them). + * Writes at most 11 chars ("2147483647" plus NUL). + * Returns the length of the string. + */ U_CAPI int32_t U_EXPORT2 T_CString_integerToString(char* buffer, int32_t i, int32_t radix) { - int32_t length=0; - int32_t num = 0; + int32_t length; + int32_t num; int8_t digit; char temp; + if(i<0) { + *buffer = 0; + return 0; + } + + length = 0; while (i>=radix) { num = i/radix; @@ -121,13 +130,14 @@ T_CString_integerToString(char* buffer, int32_t i, int32_t radix) } buffer[length] = (char)(T_CString_itosOffset(i)); - buffer[length+1] = '\0'; + buffer[++length] = '\0'; - /*Reverses the string*/ - for (i = 0; i < length; ++i, --length) { - temp = buffer[length]; - buffer[length] = buffer[i]; + /* Reverses the string, swap digits at buffer[0]..buffer[num] */ + num = length - 1; + for (i = 0; i < num; ++i, --num) { + temp = buffer[num]; + buffer[num] = buffer[i]; buffer[i] = temp; } diff --git a/icu4c/source/test/cintltst/cstrtest.c b/icu4c/source/test/cintltst/cstrtest.c index 465ed99df4f..89909443b7a 100644 --- a/icu4c/source/test/cintltst/cstrtest.c +++ b/icu4c/source/test/cintltst/cstrtest.c @@ -82,13 +82,13 @@ static void TestAPI(void) if(intValue != 256){ log_err("FAIL: ****T_CString_stringToInteger(\"100\", 16) failed. Expected: 256, Got: %d\n", intValue); } - T_CString_integerToString(src, 34556, 10); - if(uprv_strcmp(src, "34556") != 0){ + i = T_CString_integerToString(src, 34556, 10); + if(uprv_strcmp(src, "34556") != 0 || i != 5){ log_err("FAIL: ****integerToString(src, 34566, 10); failed. Expected: \"34556\", Got: %s\n", src); } - T_CString_integerToString(src, 256, 16); - if(uprv_strcmp(src, "100") != 0){ - log_err("FAIL: ****integerToString(src, 256, 16); failed. Expected: \"100\", Got: %s\n", src); + i = T_CString_integerToString(src, 431, 16); + if(uprv_stricmp(src, "1AF") != 0 || i != 3){ + log_err("FAIL: ****integerToString(src, 431, 16); failed. Expected: \"1AF\", Got: %s\n", src); } uprv_strcpy(src, "this is lower case");