From 386fb7a89ffbff9497d8e9ce7d7f44c038c49729 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 8 Mar 2025 11:44:57 +1100 Subject: Define constants for UTF-8 and UTF-16 implementation for clarity. Add tests to check that inverted conversions yield the original value. --- src/UniConversion.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/UniConversion.h') diff --git a/src/UniConversion.h b/src/UniConversion.h index 5990cca8c..ce5de4bc7 100644 --- a/src/UniConversion.h +++ b/src/UniConversion.h @@ -99,8 +99,12 @@ enum { SURROGATE_TRAIL_FIRST = 0xDC00 }; enum { SURROGATE_TRAIL_LAST = 0xDFFF }; enum { SUPPLEMENTAL_PLANE_FIRST = 0x10000 }; +constexpr bool IsSurrogate(wchar_t uch) noexcept { + return (uch >= SURROGATE_LEAD_FIRST) && (uch <= SURROGATE_TRAIL_LAST); +} + constexpr unsigned int UTF16CharLength(wchar_t uch) noexcept { - return ((uch >= SURROGATE_LEAD_FIRST) && (uch <= SURROGATE_LEAD_LAST)) ? 2 : 1; + return IsSurrogate(uch) ? 2 : 1; } constexpr unsigned int UTF16LengthFromUTF8ByteCount(unsigned int byteCount) noexcept { -- cgit v1.2.3