static jstring getDecimalPatternChars(JNIEnv *env, UResourceBundle *rootElems) { UErrorCode status = U_ZERO_ERROR; int zeroL, digitL, decSepL, groupL, listL, percentL, permillL, expL, currSepL, minusL; int patternLength; jchar *patternChars; const jchar* zero = ures_getStringByIndex(rootElems, 4, &zeroL, &status); const jchar* digit = ures_getStringByIndex(rootElems, 5, &digitL, &status); const jchar* decSep = ures_getStringByIndex(rootElems, 0, &decSepL, &status); const jchar* group = ures_getStringByIndex(rootElems, 1, &groupL, &status); const jchar* list = ures_getStringByIndex(rootElems, 2, &listL, &status); const jchar* percent = ures_getStringByIndex(rootElems, 3, &percentL, &status); const jchar* permill = ures_getStringByIndex(rootElems, 8, &permillL, &status); const jchar* exp = ures_getStringByIndex(rootElems, 7, &expL, &status); const jchar* currSep = ures_getStringByIndex(rootElems, 0, &currSepL, &status); const jchar* minus = ures_getStringByIndex(rootElems, 6, &minusL, &status); if(U_FAILURE(status)) { return NULL; } patternChars = (jchar *) malloc(11 * sizeof(jchar)); patternChars[0] = 0; u_strncat(patternChars, zero, 1); u_strncat(patternChars, digit, 1); u_strncat(patternChars, decSep, 1); u_strncat(patternChars, group, 1); u_strncat(patternChars, list, 1); u_strncat(patternChars, percent, 1); u_strncat(patternChars, permill, 1); u_strncat(patternChars, exp, 1); u_strncat(patternChars, currSep, 1); u_strncat(patternChars, minus, 1); jstring decimalPatternChars = env->NewString(patternChars, 10); free(patternChars); return decimalPatternChars; }
static void demo_C_Unicode_strings() { printf("\n* demo_C_Unicode_strings() --------- ***\n\n"); static const UChar text[]={ 0x41, 0x42, 0x43, 0 }; /* "ABC" */ static const UChar appendText[]={ 0x61, 0x62, 0x63, 0 }; /* "abc" */ static const UChar cmpText[]={ 0x61, 0x53, 0x73, 0x43, 0 }; /* "aSsC" */ UChar buffer[32]; int32_t compare; int32_t length=u_strlen(text); /* length=3 */ /* simple ANSI C-style functions */ buffer[0]=0; /* empty, NUL-terminated string */ u_strncat(buffer, text, 1); /* append just n=1 character ('A') */ u_strcat(buffer, appendText); /* buffer=="Aabc" */ length=u_strlen(buffer); /* length=4 */ printUString("should be \"Aabc\": ", buffer, -1); /* bitwise comparing buffer with text */ compare=u_strcmp(buffer, text); if(compare<=0) { printf("String comparison error, expected \"Aabc\" > \"ABC\"\n"); } /* Build "A<sharp s>C" in the buffer... */ u_strcpy(buffer, text); buffer[1]=0xdf; /* sharp s, case-compares equal to "ss" */ printUString("should be \"A<sharp s>C\": ", buffer, -1); /* Compare two strings case-insensitively using full case folding */ compare=u_strcasecmp(buffer, cmpText, U_FOLD_CASE_DEFAULT); if(compare!=0) { printf("String case insensitive comparison error, expected \"AbC\" to be equal to \"ABC\"\n"); } }
static void TestCompare(){ int32_t i; const char* testName ="uidna_compare"; CompareFunc func = uidna_compare; UChar www[] = {0x0057, 0x0057, 0x0057, 0x002E, 0x0000}; UChar com[] = {0x002E, 0x0043, 0x004F, 0x004D, 0x0000}; UChar buf[MAX_DEST_SIZE]={0x0057, 0x0057, 0x0057, 0x002E, 0x0000}; UChar source[MAX_DEST_SIZE]={0}, uni0[MAX_DEST_SIZE]={0}, uni1[MAX_DEST_SIZE]={0}, ascii0[MAX_DEST_SIZE]={0}, ascii1[MAX_DEST_SIZE]={0}, temp[MAX_DEST_SIZE] ={0}; u_strcat(uni0,unicodeIn[0]); u_strcat(uni0,com); u_strcat(uni1,unicodeIn[1]); u_strcat(uni1,com); u_charsToUChars(asciiIn[0], temp, (int32_t)strlen(asciiIn[0])); u_strcat(ascii0,temp); u_strcat(ascii0,com); memset(temp, 0, U_SIZEOF_UCHAR * MAX_DEST_SIZE); u_charsToUChars(asciiIn[1], temp, (int32_t)strlen(asciiIn[1])); u_strcat(ascii1,temp); u_strcat(ascii1,com); /* prepend www. */ u_strcat(source, www); for(i=0;i< (int32_t)(sizeof(unicodeIn)/sizeof(unicodeIn[0])); i++){ UChar* src; int32_t srcLen; memset(buf+4, 0, (MAX_DEST_SIZE-4) * U_SIZEOF_UCHAR); u_charsToUChars(asciiIn[i],buf+4, (int32_t)strlen(asciiIn[i])); u_strcat(buf,com); /* for every entry in unicodeIn array prepend www. and append .com*/ source[4]=0; u_strncat(source,unicodeIn[i], u_strlen(unicodeIn[i])); u_strcat(source,com); /* a) compare it with itself*/ src = source; srcLen = u_strlen(src); testCompareWithSrc(src,srcLen,src,srcLen,testName, func, TRUE); /* b) compare it with asciiIn equivalent */ testCompareWithSrc(src,srcLen,buf,u_strlen(buf),testName, func,TRUE); /* c) compare it with unicodeIn not equivalent*/ if(i==0){ testCompareWithSrc(src,srcLen,uni1,u_strlen(uni1),testName, func,FALSE); }else{ testCompareWithSrc(src,srcLen,uni0,u_strlen(uni0),testName, func,FALSE); } /* d) compare it with asciiIn not equivalent */ if(i==0){ testCompareWithSrc(src,srcLen,ascii1,u_strlen(ascii1),testName, func,FALSE); }else{ testCompareWithSrc(src,srcLen,ascii0,u_strlen(ascii0),testName, func,FALSE); } } }
U_CAPI int32_t U_EXPORT2 uloc_getDisplayName(const char * locale, const char * displayLocale, UChar * dest, int32_t destCapacity, UErrorCode * pErrorCode) { int32_t length, length2, length3 = 0; UBool hasLanguage, hasScript, hasCountry, hasVariant, hasKeywords; UEnumeration * keywordEnum = NULL; int32_t keywordCount = 0; const char * keyword = NULL; int32_t keywordLen = 0; char keywordValue[256]; int32_t keywordValueLen = 0; int32_t locSepLen = 0; int32_t locPatLen = 0; int32_t p0Len = 0; int32_t defaultPatternLen = 9; const UChar * dispLocSeparator; const UChar * dispLocPattern; static const UChar defaultSeparator[3] = { 0x002c, 0x0020 , 0x0000 }; /* comma + space */ static const UChar defaultPattern[10] = { 0x007b, 0x0030, 0x007d, 0x0020, 0x0028, 0x007b, 0x0031, 0x007d, 0x0029, 0x0000 }; /* {0} ({1}) */ static const UChar pat0[4] = { 0x007b, 0x0030, 0x007d , 0x0000 } ; /* {0} */ static const UChar pat1[4] = { 0x007b, 0x0031, 0x007d , 0x0000 } ; /* {1} */ UResourceBundle * bundle = NULL; UResourceBundle * locdsppat = NULL; UErrorCode status = U_ZERO_ERROR; /* argument checking */ if (pErrorCode == NULL || U_FAILURE(*pErrorCode)) { return 0; } if (destCapacity < 0 || (destCapacity > 0 && dest == NULL)) { *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; return 0; } bundle = ures_open(U_ICUDATA_LANG, displayLocale, &status); locdsppat = ures_getByKeyWithFallback(bundle, _kLocaleDisplayPattern, NULL, &status); dispLocSeparator = ures_getStringByKeyWithFallback(locdsppat, _kSeparator, &locSepLen, &status); dispLocPattern = ures_getStringByKeyWithFallback(locdsppat, _kPattern, &locPatLen, &status); /*close the bundles */ ures_close(locdsppat); ures_close(bundle); /* If we couldn't find any data, then use the defaults */ if (locSepLen == 0) { dispLocSeparator = defaultSeparator; locSepLen = 2; } if (locPatLen == 0) { dispLocPattern = defaultPattern; locPatLen = 9; } /* * if there is a language, then write "language (country, variant)" * otherwise write "country, variant" */ /* write the language */ length = uloc_getDisplayLanguage(locale, displayLocale, dest, destCapacity, pErrorCode); hasLanguage = length > 0; if (hasLanguage) { p0Len = length; /* append " (" */ if (length < destCapacity) { dest[length] = 0x20; } ++length; if (length < destCapacity) { dest[length] = 0x28; } ++length; } if (*pErrorCode == U_BUFFER_OVERFLOW_ERROR) { /* keep preflighting */ *pErrorCode = U_ZERO_ERROR; } /* append the script */ if (length < destCapacity) { length2 = uloc_getDisplayScript(locale, displayLocale, dest + length, destCapacity - length, pErrorCode); } else { length2 = uloc_getDisplayScript(locale, displayLocale, NULL, 0, pErrorCode); } hasScript = length2 > 0; length += length2; if (hasScript) { /* append separator */ if (length + locSepLen <= destCapacity) { u_memcpy(dest + length, dispLocSeparator, locSepLen); } length += locSepLen; } if (*pErrorCode == U_BUFFER_OVERFLOW_ERROR) { /* keep preflighting */ *pErrorCode = U_ZERO_ERROR; } /* append the country */ if (length < destCapacity) { length2 = uloc_getDisplayCountry(locale, displayLocale, dest + length, destCapacity - length, pErrorCode); } else { length2 = uloc_getDisplayCountry(locale, displayLocale, NULL, 0, pErrorCode); } hasCountry = length2 > 0; length += length2; if (hasCountry) { /* append separator */ if (length + locSepLen <= destCapacity) { u_memcpy(dest + length, dispLocSeparator, locSepLen); } length += locSepLen; } if (*pErrorCode == U_BUFFER_OVERFLOW_ERROR) { /* keep preflighting */ *pErrorCode = U_ZERO_ERROR; } /* append the variant */ if (length < destCapacity) { length2 = uloc_getDisplayVariant(locale, displayLocale, dest + length, destCapacity - length, pErrorCode); } else { length2 = uloc_getDisplayVariant(locale, displayLocale, NULL, 0, pErrorCode); } hasVariant = length2 > 0; length += length2; if (hasVariant) { /* append separator */ if (length + locSepLen <= destCapacity) { u_memcpy(dest + length, dispLocSeparator, locSepLen); } length += locSepLen; } keywordEnum = uloc_openKeywords(locale, pErrorCode); for (keywordCount = uenum_count(keywordEnum, pErrorCode); keywordCount > 0 ; keywordCount--) { if (U_FAILURE(*pErrorCode)) { break; } /* the uenum_next returns NUL terminated string */ keyword = uenum_next(keywordEnum, &keywordLen, pErrorCode); if (length + length3 < destCapacity) { length3 += uloc_getDisplayKeyword(keyword, displayLocale, dest + length + length3, destCapacity - length - length3, pErrorCode); } else { length3 += uloc_getDisplayKeyword(keyword, displayLocale, NULL, 0, pErrorCode); } if (*pErrorCode == U_BUFFER_OVERFLOW_ERROR) { /* keep preflighting */ *pErrorCode = U_ZERO_ERROR; } keywordValueLen = uloc_getKeywordValue(locale, keyword, keywordValue, 256, pErrorCode); if (keywordValueLen) { if (length + length3 < destCapacity) { dest[length + length3] = 0x3D; } length3++; if (length + length3 < destCapacity) { length3 += uloc_getDisplayKeywordValue(locale, keyword, displayLocale, dest + length + length3, destCapacity - length - length3, pErrorCode); } else { length3 += uloc_getDisplayKeywordValue(locale, keyword, displayLocale, NULL, 0, pErrorCode); } if (*pErrorCode == U_BUFFER_OVERFLOW_ERROR) { /* keep preflighting */ *pErrorCode = U_ZERO_ERROR; } } if (keywordCount > 1) { if (length + length3 + locSepLen <= destCapacity && keywordCount) { u_memcpy(dest + length + length3, dispLocSeparator, locSepLen); length3 += locSepLen; } } } uenum_close(keywordEnum); hasKeywords = length3 > 0; length += length3; if ((hasScript && !hasCountry) || ((hasScript || hasCountry) && !hasVariant && !hasKeywords) || ((hasScript || hasCountry || hasVariant) && !hasKeywords)) { /* Remove separator */ length -= locSepLen; } else if (hasLanguage && !hasScript && !hasCountry && !hasVariant && !hasKeywords) { /* Remove " (" */ length -= 2; } if (hasLanguage && (hasScript || hasCountry || hasVariant || hasKeywords)) { /* append ")" */ if (length < destCapacity) { dest[length] = 0x29; } ++length; /* If the localized display pattern is something other than the default pattern of "{0} ({1})", then * then we need to do the formatting here. It would be easier to use a messageFormat to do this, but we * can't since we don't have the APIs in the i18n library available to us at this point. */ if (locPatLen != defaultPatternLen || u_strcmp(dispLocPattern, defaultPattern)) /* Something other than the default pattern */ { UChar * p0 = u_strstr(dispLocPattern, pat0); UChar * p1 = u_strstr(dispLocPattern, pat1); u_terminateUChars(dest, destCapacity, length, pErrorCode); if (p0 != NULL && p1 != NULL) /* The pattern is well formed */ { if (dest) { int32_t destLen = 0; UChar * result = (UChar *)uprv_malloc((length + 1) * sizeof(UChar)); UChar * upos = (UChar *)dispLocPattern; u_strcpy(result, dest); dest[0] = 0; while (*upos) { if (upos == p0) /* Handle {0} substitution */ { u_strncat(dest, result, p0Len); destLen += p0Len; dest[destLen] = 0; /* Null terminate */ upos += 3; } else if (upos == p1) /* Handle {1} substitution */ { UChar * p1Start = &result[p0Len + 2]; u_strncat(dest, p1Start, length - p0Len - 3); destLen += (length - p0Len - 3); dest[destLen] = 0; /* Null terminate */ upos += 3; } else /* Something from the pattern not {0} or {1} */ { u_strncat(dest, upos, 1); upos++; destLen++; dest[destLen] = 0; /* Null terminate */ } } length = destLen; uprv_free(result); } } } } if (*pErrorCode == U_BUFFER_OVERFLOW_ERROR) { /* keep preflighting */ *pErrorCode = U_ZERO_ERROR; } return u_terminateUChars(dest, destCapacity, length, pErrorCode); }