From 1af7465107ae4e90be48d53b60e07e9a62cc0994 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 9 Oct 2012 14:29:37 +0200 Subject: [PATCH] utf8: add dump(OutputIterator& it, Codepoint cp) --- src/utf8.hh | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/utf8.hh b/src/utf8.hh index f9fb87cd..6bc29942 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{}; +} + } }