如何测试u32string仅用于字母(使用区域设置)
How to test a u32string for letters only (with locale)
我正在编写一个编译器(为我自己的编程语言),我希望允许用户使用Unicode字母类别中的任何字符来定义标识符(现代语言,如Go已经允许这样的语法)。我在c++ 11中读了很多关于字符编码的信息,根据我发现的所有信息,使用utf32编码是很好的(它在lexer中迭代速度很快,而且它比c++中的utf8有更好的支持)。
c++中有isalpha
函数。我如何测试wchar32_t
,如果它是一个字母(在任何语言中分类为"字母"的Unicode代码点)?
这可能吗?
使用ICU遍历字符串并检查是否满足适当的Unicode属性。下面是一个C语言的例子,检查UTF-8命令行参数是否是一个有效的标识符:
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unicode/uchar.h>
#include <unicode/utf8.h>
int main(int argc, char **argv) {
if (argc != 2) return EXIT_FAILURE;
const char *const str = argv[1];
int32_t off = 0;
// U8_NEXT has a bug causing length < 0 to not work for characters in [U+0080, U+07FF]
const size_t actual_len = strlen(str);
if (actual_len > INT32_MAX) return EXIT_FAILURE;
const int32_t len = actual_len;
if (!len) return EXIT_FAILURE;
UChar32 ch = -1;
U8_NEXT(str, off, len, ch);
if (ch < 0 || !u_isIDStart(ch)) return EXIT_FAILURE;
while (off < len) {
U8_NEXT(str, off, len, ch);
if (ch < 0 || !u_isIDPart(ch)) return EXIT_FAILURE;
}
}
注意,ICU在这里使用Java定义,与uax# 31中的定义略有不同。在实际应用程序中,您可能还希望在
ICU项目中有一个isaplha
。我想你可以用这个
相关文章:
- 在 Linux 中存储区域设置名称的缓冲区大小应该是多少?
- 提升区域设置规范化带状字符,但不规范化重音
- 是否有任何区域设置会影响宽字符编码?
- 提升获取文本的区域设置"Conversion failed"
- 提升 1.41.0 : 提升::区域设置替代方案?
- std::locale 向"en_US.UTF-8"区域设置抛出runtime_error异常
- 默认情况具有哪个区域设置
- 区域设置是否有定义负号的方面?
- QT 将文本翻译成特定的区域设置
- 我可以强制 QFileDialog 使用系统区域设置而不是系统语言吗?
- 如何调用 GetAltMonthNames 来填充外部区域设置月份字符串的安全数组
- 提升区域设置不完整类型boundary_indexing<char32_t>
- 默认区域设置"root"在 ICU 中如何工作?
- std::regex总是可以识别区域设置吗?
- std::string 的运算符<是否应该受到当前区域设置的影响?
- C++为任何区域设置区域设置
- 如何显示与系统不同的区域设置的QDate月
- C++获取用户首选的编码或区域设置编码
- 使用cstring.format()中区域设置的小数分离器
- 当区域设置需要"3,14"时,如何使用scanf解析像"3.14"这样的数字