From 8e9c592f1af6d4e428fe65362cce36a39f5844f0 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 31 Mar 2024 20:08:09 +0200 Subject: Prefer constexpr, and rename U8_BYTE_*() --- include/mbstring.h | 20 ++++++++++---------- include/rune.h | 9 +++++---- lib/mbstring/u8prev.c | 15 +++++++-------- lib/mbstring/u8tor.c | 10 +++++----- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/include/mbstring.h b/include/mbstring.h index 2b5ec01..0bf4685 100644 --- a/include/mbstring.h +++ b/include/mbstring.h @@ -13,19 +13,19 @@ #define U8_ARGSP(s) (&(s).p), (&(s).len) /* clang-format off */ -#define U8_BYTE_1(x) (((x) & 0x80) == 0x00) -#define U8_BYTE_2(x) (((x) & 0xE0) == 0xC0) -#define U8_BYTE_3(x) (((x) & 0xF0) == 0xE0) -#define U8_BYTE_4(x) (((x) & 0xF8) == 0xF0) -#define U8_BYTE_C(x) (((x) & 0xC0) == 0x80) +#define u8byte1(x) (((x) & 0x80) == 0x00) +#define u8byte2(x) (((x) & 0xE0) == 0xC0) +#define u8byte3(x) (((x) & 0xF0) == 0xE0) +#define u8byte4(x) (((x) & 0xF8) == 0xF0) +#define u8bytec(x) (((x) & 0xC0) == 0x80) /* clang-format on */ -#define U8_1B_MAX RUNE_C(0x00007FL) -#define U8_2B_MAX RUNE_C(0x0007FFL) -#define U8_3B_MAX RUNE_C(0x00FFFFL) -#define U8_4B_MAX RUNE_C(0x10FFFFL) +constexpr rune U8_1B_MAX = 0x00007F; +constexpr rune U8_2B_MAX = 0x0007FF; +constexpr rune U8_3B_MAX = 0x00FFFF; +constexpr rune U8_4B_MAX = 0x10FFFF; -#define U8_LEN_MAX 4 +constexpr int U8_LEN_MAX = 4; #define PRIsU8 ".*s" #define U8_PRI_ARGS(sv) ((int)(sv).len), ((sv).p) diff --git a/include/rune.h b/include/rune.h index fc9f927..db08de2 100644 --- a/include/rune.h +++ b/include/rune.h @@ -28,9 +28,10 @@ #define RUNE_C(x) UINT32_C(x) -#define ASCII_MAX RUNE_C(0x00007F) -#define LATIN1_MAX RUNE_C(0x0000FF) -#define RUNE_ERROR RUNE_C(0x00FFFD) -#define RUNE_MAX RUNE_C(0x10FFFF) +constexpr rune RUNE_ERROR = 0x00FFFD; + +constexpr rune ASCII_MAX = 0x00007F; +constexpr rune LATIN1_MAX = 0x0000FF; +constexpr rune RUNE_MAX = 0x10FFFF; #endif /* !MLIB_RUNE_H */ diff --git a/lib/mbstring/u8prev.c b/lib/mbstring/u8prev.c index 507f7e1..ff580c2 100644 --- a/lib/mbstring/u8prev.c +++ b/lib/mbstring/u8prev.c @@ -12,23 +12,22 @@ u8prev(rune *ch, const char8_t **p, const char8_t *start) if (d <= 0) { return 0; - } else if (U8_BYTE_1(s[-1])) { + } else if (u8byte1(s[-1])) { if (ch) *ch = s[-1]; off = 1; - } else if (d > 1 && U8_BYTE_C(s[-1]) && U8_BYTE_2(s[-2])) { + } else if (d > 1 && u8bytec(s[-1]) && u8byte2(s[-2])) { if (ch) *ch = ((s[-2] & 0x1F) << 6) | (s[-1] & 0x3F); off = 2; - } else if (d > 2 && U8_BYTE_C(s[-1]) && U8_BYTE_C(s[-2]) - && U8_BYTE_3(s[-3])) { + } else if (d > 2 && u8bytec(s[-1]) && u8bytec(s[-2]) && u8byte3(s[-3])) { if (ch) { - *ch = - ((s[-3] & 0x0F) << 12) | ((s[-2] & 0x3F) << 6) | (s[-1] & 0x3F); + *ch = ((s[-3] & 0x0F) << 12) | ((s[-2] & 0x3F) << 6) + | (s[-1] & 0x3F); } off = 3; - } else if (d > 3 && U8_BYTE_C(s[-1]) && U8_BYTE_C(s[-2]) && U8_BYTE_C(s[-3]) - && U8_BYTE_4(s[-4])) { + } else if (d > 3 && u8bytec(s[-1]) && u8bytec(s[-2]) && u8bytec(s[-3]) + && u8byte4(s[-4])) { if (ch) { *ch = ((s[-4] & 0x07) << 18) | ((s[-3] & 0x3F) << 12) | ((s[-2] & 0x3F) << 6) | (s[-1] & 0x3F); diff --git a/lib/mbstring/u8tor.c b/lib/mbstring/u8tor.c index 85c4c19..eb54689 100644 --- a/lib/mbstring/u8tor.c +++ b/lib/mbstring/u8tor.c @@ -4,17 +4,17 @@ int u8tor(rune *ch, const char8_t *s) { - if (U8_BYTE_1(s[0])) { + if (u8byte1(s[0])) { *ch = s[0]; return 1; - } else if (U8_BYTE_2(s[0]) && U8_BYTE_C(s[1])) { + } else if (u8byte2(s[0]) && u8bytec(s[1])) { *ch = ((s[0] & 0x1F) << 6) | (s[1] & 0x3F); return 2; - } else if (U8_BYTE_3(s[0]) && U8_BYTE_C(s[1]) && U8_BYTE_C(s[2])) { + } else if (u8byte3(s[0]) && u8bytec(s[1]) && u8bytec(s[2])) { *ch = ((s[0] & 0x0F) << 12) | ((s[1] & 0x3F) << 6) | (s[2] & 0x3F); return 3; - } else if (U8_BYTE_4(s[0]) && U8_BYTE_C(s[1]) && U8_BYTE_C(s[2]) - && U8_BYTE_C(s[3])) { + } else if (u8byte4(s[0]) && u8bytec(s[1]) && u8bytec(s[2]) + && u8bytec(s[3])) { *ch = ((s[0] & 0x07) << 18) | ((s[1] & 0x3F) << 12) | ((s[2] & 0x3F) << 6) | (s[3] & 0x3F); return 4; -- cgit v1.2.3