int main(int argc, char ** argv) { const char str1[] = "漢字だす"; const char str2[] = "漢字だす"; locale_t mylocale1 = newlocale(LC_ALL_MASK, "ja_JP.utf8", (locale_t)NULL); locale_t mylocale2 = newlocale(LC_ALL_MASK, "en_GB.utf8", (locale_t)NULL); size_t len1 = strlen(str1); size_t len2 = strlen(str2); size_t ret1, ret2; char *xfrm_str1 = alloca(len1 * 10); char *xfrm_str2 = alloca(len2 * 10); ret1 = strxfrm_l(xfrm_str1, str1, len1 * 10, mylocale1); ret2 = strxfrm_l(xfrm_str2, str2, len2 * 10, mylocale2); printf("len1 = %lu ret1 = %lu\n", (unsigned long)len1, (unsigned long)ret1); printf("len2 = %lu ret2 = %lu\n", (unsigned long)len2, (unsigned long)ret2); int i; printf("Orig: "); for (i=0 ; str1[i] ; i++) printf("%02x ", (unsigned char)str1[i]); printf("\n"); printf("Xfrm1: "); for (i=0 ; xfrm_str1[i] ; i++) printf("%02x ", (unsigned char)xfrm_str1[i]); printf("\n"); printf("Xfrm2: "); for (i=0 ; xfrm_str2[i] ; i++) printf("%02x ", (unsigned char)xfrm_str2[i]); printf("\n"); return 0; }
/** * xsltStrxfrm: * @locale: locale created with xsltNewLocale * @string: UTF-8 string to transform * * Transforms a string according to locale. The transformed string must then be * compared with xsltLocaleStrcmp and freed with xmlFree. * * Returns the transformed string or NULL on error */ xsltLocaleChar * xsltStrxfrm(xsltLocale locale, const xmlChar *string) { #ifdef XSLT_LOCALE_NONE return(NULL); #else size_t xstrlen, r; xsltLocaleChar *xstr; #ifdef XSLT_LOCALE_POSIX xstrlen = strxfrm_l(NULL, (const char *)string, 0, locale) + 1; xstr = (xsltLocaleChar *) xmlMalloc(xstrlen); if (xstr == NULL) { xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : out of memory error\n"); return(NULL); } r = strxfrm_l((char *)xstr, (const char *)string, xstrlen, locale); #endif #ifdef XSLT_LOCALE_WINAPI xstrlen = MultiByteToWideChar(CP_UTF8, 0, (char *) string, -1, NULL, 0); if (xstrlen == 0) { xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : MultiByteToWideChar check failed\n"); return(NULL); } xstr = (xsltLocaleChar*) xmlMalloc(xstrlen * sizeof(xsltLocaleChar)); if (xstr == NULL) { xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : out of memory\n"); return(NULL); } r = MultiByteToWideChar(CP_UTF8, 0, (char *) string, -1, xstr, xstrlen); if (r == 0) { xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : MultiByteToWideChar failed\n"); xmlFree(xstr); return(NULL); } return(xstr); #endif /* XSLT_LOCALE_WINAPI */ if (r >= xstrlen) { xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : strxfrm failed\n"); xmlFree(xstr); return(NULL); } return(xstr); #endif /* XSLT_LOCALE_NONE */ }
int main(int argc, char **argv) { char q[10]; size_t n = strxfrm(q, "abcdef", sizeof(q)); assert(n < sizeof(q)); char q2[10]; locale_t loc = newlocale(LC_ALL_MASK, "", (locale_t)0); n = strxfrm_l(q2, L"qwerty", sizeof(q), loc); assert(n < sizeof(q2)); freelocale(loc); return 0; }
int main(int argc, char ** argv) { const char str1[] = "漢字入力"; const char str2[] = "漢字だす"; locale_t mylocale = newlocale(LC_ALL_MASK, "ja_JP.utf8", (locale_t)NULL); // locale_t mylocale = newlocale(LC_ALL_MASK, "en_GB.utf8", (locale_t)NULL); size_t len1 = strlen(str1); size_t len2 = strlen(str2); size_t ret1, ret2; char *xfrm_str1 = alloca(len1 * 2); char *xfrm_str2 = alloca(len2 * 2); ret1 = strxfrm_l(xfrm_str1, str1, len1 * 2, mylocale); ret2 = strxfrm_l(xfrm_str2, str2, len2 * 2, mylocale); printf("len1 = %lu ret1 = %lu\n", (unsigned long)len1, (unsigned long)ret1); printf("len2 = %lu ret2 = %lu\n", (unsigned long)len2, (unsigned long)ret2); int ret = strcmp(xfrm_str1, xfrm_str2); printf("string compare: %s %s %s\n", str1, ((ret == 0) ? "==" : ((ret > 0) ? ">" : "<")), str2); int i; printf("Orig: "); for (i=0 ; str1[i] ; i++) printf("%02x ", (unsigned char)str1[i]); printf("\n"); printf("Xfrm: "); for (i=0 ; xfrm_str1[i] ; i++) printf("%02x ", (unsigned char)xfrm_str1[i]); printf("\n"); return 0; }
static int test (const char *locale) { size_t bufsize; size_t r; size_t l; char *buf; locale_t loc; int result = 0; if (setlocale (LC_COLLATE, locale) == NULL) { printf ("cannot set locale \"%s\"\n", locale); return 1; } bufsize = strxfrm (NULL, string, 0) + 1; buf = malloc (bufsize); if (buf == NULL) { printf ("cannot allocate %zd bytes\n", bufsize); return 1; } r = strxfrm (buf, string, bufsize); l = strlen (buf); if (r != l) { printf ("locale \"%s\": strxfrm returned %zu, strlen returned %zu\n", locale, r, l); result = 1; } loc = newlocale (1 << LC_ALL, locale, NULL); r = strxfrm_l (buf, string, bufsize, loc); l = strlen (buf); if (r != l) { printf ("locale \"%s\": strxfrm_l returned %zu, strlen returned %zu\n", locale, r, l); result = 1; } freelocale (loc); free (buf); return result; }
static size_t xfrm(char *out,char const *in,size_t n,locale_t l) { return strxfrm_l(out,in,n,l); }