我问了一个问题以了解 C 中“strxfrm”的用法。
我知道这个功能是根据当前的语言环境配置转换一个字符串。
但我不知道什么是“变换”,以及这个函数是如何变换的。
例如,我在 macOS 中尝试了如下代码:
#include <stdio.h>
#include <string.h>
#include <locale.h>
int main(int argc, char * argv[])
{
char str1[512] = { 0x68, 0x6c, 0x61, 0x76, 0x61, 0x00 }; //"hlava";
char str2[512] = { 0xc4, 0x8d, 0xc3, 0xad, 0xc5, 0xa1, 0x6e, 0xc3, 0xad, 0x6b, 0x00 }; //"číšník";
char xfm1[512] = { '\0', };
char xfm2[512] = { '\0', };
char * result = NULL;
size_t lxfm1 = 0;
size_t lxfm2 = 0;
result = setlocale(LC_ALL, "en_US.UTF-8");
lxfm1 = strxfrm(xfm1, str1, sizeof xfm1);
lxfm2 = strxfrm(xfm2, str2, sizeof xfm2);
printf("<en-US>\n");
printf("setlocale = \"%s\"\n", (result == NULL) ? "NULL" : result);
printf("str1: \"%s\" --> \"%s\"\n", str1, xfm1);
printf("str2: \"%s\" --> \"%s\"\n", str2, xfm2);
printf("strcmp(str1, str2) = %d\n", strcmp(str1, str2));
printf("strcmp(xfm1, xfm2) = %d\n", strcmp(xfm1, xfm2));
printf("strcoll(xfm1, xfm2) = %d\n", strcoll(str1, str2));
printf("returns of strxfrm: %zu / %zu\n", lxfm1, lxfm2);
result = setlocale(LC_ALL, "cs_CZ.UTF-8");
lxfm1 = strxfrm(xfm1, str1, sizeof xfm1);
lxfm2 = strxfrm(xfm2, str2, sizeof xfm2);
printf("<cs-CZ>\n");
printf("setlocale = \"%s\"\n", result);
printf("str1: \"%s\" --> \"%s\"\n", str1, xfm1);
printf("str2: \"%s\" --> \"%s\"\n", str2, xfm2);
printf("strcmp(str1, str2) = %d\n", strcmp(str1, str2));
printf("strcmp(xfm1, xfm2) = %d\n", strcmp(xfm1, xfm2));
printf("strcoll(xfm1, xfm2) = %d\n", strcoll(str1, str2));
printf("returns of strxfrm: %zu / %zu\n", lxfm1, lxfm2);
return 0;
}
我预计“strcmp(xfm1,xfm2)”的结果将是正整数,因为在捷克语中字符“č”在“h”之前。
然而,结果是……
<en-US>
setlocale = "en_US.UTF-8"
str1: "hlava" --> "001Z001^001S001h001S0000001Z001^001S001h001S"
str2: "číšník" --> "0042003_0042001`003_001]0000008?003_009S001`003_001]"
strcmp(str1, str2) = -92
strcmp(xfm1, xfm2) = -3
strcoll(xfm1, xfm2) = -152
returns of strxfrm: 44 / 52
<cs-CZ>
setlocale = "cs_CZ.UTF-8"
str1: "hlava" --> "001Z001^001S001h001S0000001Z001^001S001h001S"
str2: "číšník" --> "0042003_0042001`003_001]0000008?003_009S001`003_001]"
strcmp(str1, str2) = -92
strcmp(xfm1, xfm2) = -3
strcoll(xfm1, xfm2) = -152
returns of strxfrm: 44 / 52
我对这个函数'strxfrm'有误解吗?其实我现在也不太清楚“变身”是什么意思。
请让我知道该功能的正确用法和目的。