使用 UTF8 文字字符的 xcode ENUM
我已阅读此相关问题,但它对我没有太大帮助。
Enum 的目标是包含 4 字节范围内单个 UTF-8 字符的原始 UTF-8 代码(不是 unicode 代码点)。
以下示例之所以有效,是因为 xcode 源文件采用 UTF-8 格式(这是 xcode 的推荐编码)。它以正确的预期值进行编译和运行。 但我也收到警告“此类型的字符常量太长”。我可以压制它吗?...还是坏主意?
typedef enum {
TEST_VAL_1BYTE = ',', // 0x2C
TEST_VAL_2BYTE = '§', // 0xC2A7 (the warning)
TEST_VAL_3BYTE = '✓', // 0xE29C93 (the warning)
TEST_VAL_4BYTE = '
I have read this related question, but it does not quite help me.
The goal of the Enum is to contain raw UTF-8 code (not the unicode code point) of single UTF-8 characters within the 4 byte range.
The following example works because the xcode source file is in UTF-8 format (which is the recommended encoding for xcode). It compiles and runs with the correct expected values.
But I also get the warning "character constant too long for this type". Might I suppress it?.. or bad idea?
typedef enum {
TEST_VAL_1BYTE = ',', // 0x2C
TEST_VAL_2BYTE = '§', // 0xC2A7 (the warning)
TEST_VAL_3BYTE = '✓', // 0xE29C93 (the warning)
TEST_VAL_4BYTE = '????', // 0xF09D8DA5 (the warning)
} TEST_VALUES_UTF8;
Safest way and without warnings, but it is more tedious to code:
typedef enum {
NUM_VAL_1BYTE = 0x2C, // ,
NUM_VAL_2BYTE = 0xC2A7, // §
NUM_VAL_3BYTE = 0xE29C93, // ✓
NUM_VAL_4BYTE = 0xF09D8DA5, // ????
} TEST_VALUES_UTF8;
Finally please note that enumeration with 1 or 4 ASCII characters is valid and without warnings:
enum {
ENUM_TEST_1 = '1', // 0x31 (no warning)
ENUM_TEST_12 = '12', // 0x3132 (w: multi-character character constant)
ENUM_TEST_123 = '123', // 0x313233 (w: multi-character character constant)
ENUM_TEST_1234 = '1234', // 0x31323334 (no warning)
};
Is there maybe a preprocessor macro that is source encoding generic that can return the UTF-8 code:
enum {
TEST_VAL_2BYTE = AWESOME_UTF8CODE_MACRO('§'), // 0xC2A7
};
Thanks;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 C++11 constexpr 和 u8 前缀,a'la http://liveworkspace.org/code/3EtxVE :
Use C++11 constexpr and u8 prefix, a'la http://liveworkspace.org/code/3EtxVE :
which outputs
If you don't have access to u8 prefix you can simply ensure the source file is encoded in UTF-8, and I guess you can turn the constexpr into macros if needed...but shown is a clean way.