vswprintf对于Mac OS X下的某些unicode代码点失败 [英] vswprintf fails for certain unicode codepoints under Mac OS X
问题描述
我从 vswprintf
使用GCC和Mac OS X(在Mac OS X 10.6下使用gcc 4.0和4.2.1进行测试)得到了莫名其妙的失败(返回值-1) 10.8。Linux下的GCC受不影响,Visual Studio也受
I am getting inexplicable failures (return value -1) from vswprintf
using GCC and Mac OS X (tested with gcc 4.0 and 4.2.1 under Mac OS X 10.6 and 10.8. GCC under Linux is not affected. Visual Studio is also not affected).
我最低限度修改了这里的示例,以便打印出 vswprintf
的返回值:
To demonstrate the problem I have minimally adapted the example from here so that it prints out vswprintf
's return value:
/* vswprintf example */
#include <stdio.h>
#include <stdarg.h>
#include <wchar.h>
void PrintWide ( const wchar_t * format, ... )
{
wchar_t buffer[256];
va_list args;
va_start ( args, format );
int res = vswprintf ( buffer, 256, format, args );
wprintf ( L"result=%d\n", res );
fputws ( buffer, stdout );
va_end ( args );
}
int main ()
{
wchar_t str[] = L"test string has %d wide characters.\n";
PrintWide ( str, wcslen(str) );
return 0;
}
从我的测试看来,根据 str
, vswprintf
有时会失败。示例:
From my tests it appears that, depending on the value of str
, vswprintf
will sometimes fail. Examples:
wchar_t str[] = L"test string has %d wide characters.\n"; // works
wchar_t str[] = L"ßß® test string has %d wide characters.\n"; // works
wchar_t str[] = L"日本語 test string has %d wide characters.\n"; // FAILS
wchar_t str[] = L"Π test string has %d wide characters.\n"; // FAILS
wchar_t str[] = L"\u03A0 test string has %d wide characters.\n"; // FAILS
看起来任何包含Unicode代码点在 0xff
会触发这个问题。任何人都可以了解为什么会发生这种情况?
It appears that any strings that include characters with Unicode codepoints above 0xff
will trigger this problem. Can anyone shed some light into why this is happening? It seems like too big an issue to not have been noticed before!
推荐答案
如果你设置的语言环境,它应该是罚款。要获取环境变量,您可以这样做:
If you set the locale, it should be fine. To pick up the environment variable you can do this:
setlocale(LC_CTYPE, ""); // include <locale.h>
或明确设置。这是因为所有的输出函数都需要知道使用哪个编码。
or set it explicitly. This is because all of the output functions need to know which encoding to use.
OS X无法执行 vswprintf $ c $
OS X is failing to perform the vswprintf
at all, while Linux runs it (though the characters will be incorrect if printed).
这是glibc文档中的相关章节:
Here's the relevant section from the glibc documentation:
If the format string contains non-ASCII wide characters, the program
will only work correctly if the LC_CTYPE category of the current locale
at run time is the same as the LC_CTYPE category of the current locale
at compile time. This is because the wchar_t representation is plat‐
form- and locale-dependent. (The glibc represents wide characters
using their Unicode (ISO-10646) code point, but other platforms don't
do this.
这篇关于vswprintf对于Mac OS X下的某些unicode代码点失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!