diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2012-10-09 14:29:37 +0200 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2012-10-09 14:29:37 +0200 |
| commit | 1af7465107ae4e90be48d53b60e07e9a62cc0994 (patch) | |
| tree | e83da8377152f90f6ff49f44b2e89421cbcf5325 /src/utf8.hh | |
| parent | c7272e427dab6dde9fb87e7e4890eac9447f2736 (diff) | |
utf8: add dump(OutputIterator& it, Codepoint cp)
Diffstat (limited to 'src/utf8.hh')
| -rw-r--r-- | src/utf8.hh | 32 |
1 files changed, 32 insertions, 0 deletions
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 <cstdint> +#include <cstddef> + namespace Kakoune { @@ -109,6 +112,35 @@ Codepoint codepoint(Iterator it) throw invalid_utf8_sequence{}; } +struct invalid_codepoint{}; + +template<typename OutputIterator> +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{}; +} + } } |
