From d22653dfe356e3da1354cdb0f7915e29c4a33e3b Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 31 Mar 2023 10:50:12 -0400 Subject: Factor out NTriples reader --- src/string_utils.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/string_utils.h') diff --git a/src/string_utils.h b/src/string_utils.h index 4102a54c..564c58ad 100644 --- a/src/string_utils.h +++ b/src/string_utils.h @@ -114,20 +114,32 @@ utf8_num_bytes(const uint8_t leading) : 0U; // Invalid } +static inline unsigned +utf8_num_bytes_for_codepoint(const uint32_t code) +{ + return (code < 0x00000080) ? 1U + : (code < 0x00000800) ? 2U + : (code < 0x00010000) ? 3U + : (code < 0x00110000) ? 4U + : 0U; +} + /// Return the code point of a UTF-8 character with known length static inline uint32_t -parse_counted_utf8_char(const uint8_t* utf8, size_t size) +parse_counted_utf8_char(const uint8_t* const utf8, const size_t size) { uint32_t c = utf8[0] & ((1U << (8U - size)) - 1U); + for (size_t i = 1; i < size; ++i) { c = (c << 6) | (utf8[i] & 0x3FU); } + return c; } /// Parse a UTF-8 character, set *size to the length, and return the code point static inline uint32_t -parse_utf8_char(const uint8_t* utf8, size_t* size) +parse_utf8_char(const uint8_t* const utf8, size_t* const size) { switch (*size = utf8_num_bytes(utf8[0])) { case 1: -- cgit v1.2.1