summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2021-12-19 20:09:38 +0100
committerRoland Reichwein <mail@reichwein.it>2021-12-19 20:09:38 +0100
commitf3025691d12727bbab138c13680cc21a451626b6 (patch)
tree6d64e3c8e813858d9dae03a8838f1b64304d089a /include
parent9dc97269201603dd479e15a736a64479a5095556 (diff)
Simplify append_utf in output iterator
Diffstat (limited to 'include')
-rw-r--r--include/unicode.h23
1 files changed, 9 insertions, 14 deletions
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<m - n - 1>(value);
}
- void append_utf8(const char32_t& value)
+ template<class X = T, typename std::enable_if<(sizeof(X) == 1), bool>::type = true>
+ inline void append_utf(const char32_t& value)
{
if (value < 0x80) { // 1 byte
s.push_back(static_cast<value_type>(value));
@@ -304,9 +305,10 @@ namespace unicode::detail {
throw std::runtime_error("Invalid internal Unicode value: "s + std::to_string(static_cast<uint32_t>(value)));
}
- void append_utf16(const char32_t& value)
+ template<class X = T, typename std::enable_if<(sizeof(X) == 2), bool>::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_type>(value));
} else {
char32_t value_reduced{value - 0x10000};
@@ -315,23 +317,16 @@ namespace unicode::detail {
}
}
- void append_utf32(const char32_t& value)
+ template<class X = T, typename std::enable_if<(sizeof(X) == 4), bool>::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;
}