c - nl_langinfo() - 信息太多

标签 c localization locale glibc

函数 nl_langinfo(INT_CURR_SYMBOL) 返回一个指向格式为字符串常量的指针:

$(THREE-LETTER-PSEUDOACRONYM) $(SIGN)$(SYMBOL)

所以在我的语言环境中 (en_GB.UTF-8),这将是 "GBP -£"。我只想要前三个字母,那么除了将 nul 字符分配给第三个元素或使用 strncpy() 之外,还有其他方法可以做到这一点吗?

strcpy(int_curr_symbol, nl_langinfo(INT_CURR_SYMBOL));  // "GBP -£"
int_curr_symbol[3] = '\0';

// or

strncpy(int_curr_symbol, nl_langinfo(INT_CURR_SYMBOL), 3));  // "GBP"

此外,nl_langinfo(CRNCYSTR),也返回符号和符号,而只需要符号。 “-£” -> “£”

最佳答案

INT_CURR_SYMBOL 似乎是 GNU 扩展。相反,我会建议 localeconv()struct lconvint_curr_symbol 字段。这是来自该字段的 POSIX 规范:

The international currency symbol applicable to the current locale. The first three characters contain the alphabetic international currency symbol in accordance with those specified in the ISO 4217:1995 standard. The fourth character (immediately preceding the null byte) is the character used to separate the international currency symbol from the monetary quantity.

如您所见,它应该包含您得到的内容。

关于您的第二种方法:

Additionally, nl_langinfo(CRNCYSTR), also returns the sign and the symbol, whereas only the symbol in needed. "-£" -> "£"

这是因为符号定义了符号是放在值 (+) 之前、值 (-) 之后,还是应该替换基数字符 (.)。这来自 langinfo.h 的 POSIX 规范:

Local currency symbol, preceded by '-' if the symbol should appear before the value, '+' if the symbol should appear after the value, or '.' if the symbol should replace the radix character. If the local currency symbol is the empty string, implementations may return the empty string ( "" ).

关于获取字符串部分的方法,我建议在调用后立即将返回值中的相关部分复制到您自己的缓冲区中。您必须这样做,因为由任一函数返回的结构和各种指针是由 C 库分配和维护的。

POSIX 为 nl_langinfo() 给出的原因返回值:

The application shall not modify the string returned. The pointer returned by nl_langinfo() might be invalidated or the string content might be overwritten by a subsequent call to nl_langinfo() in any thread or to nl_langinfo_l() in the same thread or the initial thread, by subsequent calls to setlocale() with a category corresponding to the category of item (see ) or the category LC_ALL, or by subsequent calls to uselocale() which change the category corresponding to the category of item. The pointer returned by nl_langinfo_l() might be invalidated or the string content might be overwritten by a subsequent call to nl_langinfo_l() in the same thread or to nl_langinfo() in any thread, or by subsequent calls to freelocale() or newlocale() which free or modify the locale object that was passed to nl_langinfo_l().

这里是 localeconv() 的类似原因:

The localeconv() function shall return a pointer to the filled-in object. The application shall not modify the structure to which the return value points, nor any storage areas pointed to by pointers within the structure. The returned pointer, and pointers within the structure, might be invalidated or the structure or the storage areas might be overwritten by a subsequent call to localeconv(). In addition, the returned pointer, and pointers within the structure, might be invalidated or the structure or the storage areas might be overwritten by subsequent calls to setlocale() with the categories LC_ALL, LC_MONETARY, or LC_NUMERIC, or by calls to uselocale() which change the categories LC_MONETARY or LC_NUMERIC.

关于c - nl_langinfo() - 信息太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19231795/

相关文章:

CUDAatomicAdd() 给出了错误的结果

c - 为什么这个函数要计算整数中设置位的数量

c - 函数指针

cocoa - NSString 支持非标准/非英文字符吗?

java - 如何从 JAR 库中了解 android 语言

iPhone - 更改语言后本地化错误

database - 如何在 Sybase 中查找语言环境?

c# - Windows .NET 框架的所有可用语言列表

windows - 在 Windows 上的 Perl 中获取当前系统本地编码

c - 解析json数组对象