From f3025691d12727bbab138c13680cc21a451626b6 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 19 Dec 2021 20:09:38 +0100 Subject: Simplify append_utf in output iterator --- include/unicode.h | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'include') diff --git a/include/unicode.h b/include/unicode.h index 6d8aac5..7965a6e 100644 --- a/include/unicode.h +++ b/include/unicode.h @@ -284,7 +284,8 @@ namespace unicode::detail { return trailing_byte(value); } - void append_utf8(const char32_t& value) + template::type = true> + inline void append_utf(const char32_t& value) { if (value < 0x80) { // 1 byte s.push_back(static_cast(value)); @@ -304,9 +305,10 @@ namespace unicode::detail { throw std::runtime_error("Invalid internal Unicode value: "s + std::to_string(static_cast(value))); } - void append_utf16(const char32_t& value) + template::type = true> + inline void append_utf(const char32_t& value) { - if (value <= 0xFFFF) { // expect value to be already valid Unicode values + if (value <= 0xFFFF) { // expect value to be already valid Unicode values (checked in input iterator) s.push_back(static_cast(value)); } else { char32_t value_reduced{value - 0x10000}; @@ -315,23 +317,16 @@ namespace unicode::detail { } } - void append_utf32(const char32_t& value) + template::type = true> + inline void append_utf(const char32_t& value) { - // expect value to be already valid Unicode values + // expect value to be already valid Unicode values (checked in input iterator) s.push_back(value); } reference operator=(const char32_t& value) { - static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4); - - if constexpr(sizeof(T) == 1) { - append_utf8(value); - } else if constexpr(sizeof(T) == 2) { - append_utf16(value); - } else if constexpr(sizeof(T) == 4) { - append_utf32(value); - } + append_utf(value); return *this; } -- cgit v1.2.3