wcsxfrm
来自 cppreference.cn
定义于头文件 <wchar.h> |
||
(直到 C99) (始于 C95) |
||
(始于 C99) | ||
将 src
指向的空终止宽字符串转换为实现定义的格式,使得使用 wcscmp 比较两个转换后的字符串与使用 wcscoll 比较原始字符串的结果相同,在当前的 C 语言区域设置中。
转换后字符串的前 count
个字符被写入到 destination,包括终止空字符,并返回完整转换后字符串的长度,不包括终止空字符。
如果 count
为 0,则允许 dest
为空指针。
目录 |
[编辑] 注解
可以接收完整转换后字符串的缓冲区的正确长度是 1+wcsxfrm(NULL, src, 0)
当使用相同的宽字符串或一组宽字符串进行多次依赖于区域设置的比较时,会使用此函数,因为使用 wcsxfrm
仅转换所有字符串一次,然后使用 wcscmp 比较转换后的宽字符串会更有效率。
[编辑] 参数
dest | - | 指向宽空终止字符串的首元素的指针,用于写入转换后的字符串 |
src | - | 指向要转换的空终止宽字符字符串的指针 |
count | - | 要输出的最大字符数 |
[编辑] 返回值
转换后的宽字符串的长度,不包括终止空字符。
[编辑] 示例
运行此代码
#include <stdio.h> #include <wchar.h> #include <locale.h> int main(void) { setlocale(LC_ALL, "sv_SE.utf8"); const wchar_t *in1 = L"\u00e5r"; wchar_t out1[1+wcsxfrm(NULL, in1, 0)]; wcsxfrm(out1, in1, sizeof out1/sizeof *out1); const wchar_t *in2 = L"\u00e4ngel"; wchar_t out2[1+wcsxfrm(NULL, in2, 0)]; wcsxfrm(out2, in2, sizeof out2/sizeof *out2); printf("In the Swedish locale: "); if(wcscmp(out1, out2) < 0) printf("%ls before %ls\n", in1, in2); else printf("%ls before %ls\n", in2, in1); printf("In lexicographical comparison: "); if(wcscmp(in1, in2) < 0) printf("%ls before %ls\n", in1, in2); else printf("%ls before %ls\n", in2, in1); }
输出
In the Swedish locale: år before ängel In lexicographical comparison: ängel before år