使用 UTF8 文字字符的 xcode ENUM

发布于 2025-01-07 07:23:05 字数 499 浏览 0 评论 0原文

我已阅读此相关问题,但它对我没有太大帮助。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不疑不惑不回忆 2025-01-14 07:23:05

使用 C++11 constexpr 和 u8 前缀,a'la http://liveworkspace.org/code/3EtxVE

#include <iostream>
#include <cstdint>

constexpr uint32_t utf8(const char (&c)[2]) {
return uint8_t(c[0]);
}
constexpr uint32_t utf8(const char (&c)[3]) {
return uint8_t(c[1]) | (uint8_t(c[0])<<8);
}
constexpr uint32_t utf8(const char (&c)[4]) {
return uint8_t(c[2]) | (uint8_t(c[1])<<8) | (uint8_t(c[0])<<16);
}
constexpr uint32_t utf8(const char (&c)[5]) {
return uint8_t(c[3]) | (uint8_t(c[2])<<8) | (uint8_t(c[1])<<16) | (uint8_t(c[0])<<24);
}

typedef enum {
TEST_VAL_1BYTE = utf8(u8","),
TEST_VAL_2BYTE = utf8(u8"§"),
TEST_VAL_3BYTE = utf8(u8"✓"),
TEST_VAL_4BYTE = utf8(u8"

Use C++11 constexpr and u8 prefix, a'la http://liveworkspace.org/code/3EtxVE :

#include <iostream>
#include <cstdint>

constexpr uint32_t utf8(const char (&c)[2]) {
   return uint8_t(c[0]);
}
constexpr uint32_t utf8(const char (&c)[3]) {
   return uint8_t(c[1]) | (uint8_t(c[0])<<8);
}
constexpr uint32_t utf8(const char (&c)[4]) {
   return uint8_t(c[2]) | (uint8_t(c[1])<<8) | (uint8_t(c[0])<<16);
}
constexpr uint32_t utf8(const char (&c)[5]) {
   return uint8_t(c[3]) | (uint8_t(c[2])<<8) | (uint8_t(c[1])<<16) | (uint8_t(c[0])<<24);
}

typedef enum {
    TEST_VAL_1BYTE = utf8(u8","),
    TEST_VAL_2BYTE = utf8(u8"§"),
    TEST_VAL_3BYTE = utf8(u8"✓"),
    TEST_VAL_4BYTE = utf8(u8"????"),
} TEST_VALUES_UTF8;

int main() {
   std::cout << std::hex << TEST_VAL_1BYTE << std::endl;
   std::cout << std::hex << TEST_VAL_2BYTE << std::endl;
   std::cout << std::hex << TEST_VAL_3BYTE << std::endl;
   std::cout << std::hex << TEST_VAL_4BYTE << std::endl;
}

which outputs

2c
c2a7
e29c93
f09d8da5

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文