diff --git a/src/utf8.hh b/src/utf8.hh index f9fb87cde..6bc299427 100644 --- a/src/utf8.hh +++ b/src/utf8.hh @@ -1,6 +1,9 @@ #ifndef utf8_hh_INCLUDED #define utf8_hh_INCLUDED +#include +#include + namespace Kakoune { @@ -109,6 +112,35 @@ Codepoint codepoint(Iterator it) throw invalid_utf8_sequence{}; } +struct invalid_codepoint{}; + +template +void dump(OutputIterator& it, Codepoint cp) +{ + if (cp <= 0x7F) + *it++ = cp; + else if (cp <= 0x7FF) + { + *it++ = 0xC0 | (cp >> 6); + *it++ = 0x80 | (cp & 0x3F); + } + else if (cp <= 0xFFFF) + { + *it++ = 0xE0 | (cp >> 12); + *it++ = 0x80 | ((cp >> 6) & 0x3F); + *it++ = 0x80 | (cp & 0x3F); + } + else if (cp <= 0x10FFFF) + { + *it++ = 0xF0 | (cp >> 18); + *it++ = 0x80 | ((cp >> 12) & 0x3F); + *it++ = 0x80 | ((cp >> 6) & 0x3F); + *it++ = 0x80 | (cp & 0x3F); + } + else + throw invalid_codepoint{}; +} + } }