diff options
Diffstat (limited to 'src/libjin/Graphics/Utf8.cpp')
-rw-r--r-- | src/libjin/Graphics/Utf8.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/libjin/Graphics/Utf8.cpp b/src/libjin/Graphics/Utf8.cpp new file mode 100644 index 0000000..1a79d43 --- /dev/null +++ b/src/libjin/Graphics/Utf8.cpp @@ -0,0 +1,68 @@ +#include <stdlib.h> +#include <string.h> +#include "Utf8.h" + +namespace jin +{ +namespace graphics +{ + + /* utf8 byte string to unicode codepoint */ + static const char *utf8toCodepoint(const char *p, unsigned *res) { + unsigned x, mask, shift; + switch (*p & 0xf0) { + case 0xf0: mask = 0x07; shift = 18; break; + case 0xe0: mask = 0x0f; shift = 12; break; + case 0xc0: + case 0xd0: mask = 0x1f; shift = 6; break; + default: + *res = *p; + return p + 1; + } + x = (*p & mask) << shift; + do { + if (*(++p) == '\0') { + *res = x; + return p; + } + shift -= 6; + x |= (*p & 0x3f) << shift; + } while (shift); + *res = x; + return p + 1; + } + + Utf8::Utf8(const char* raw, unsigned int length) + { + _length = length; + _raw = (char*)calloc(1, length); + memcpy(_raw, raw, length); + } + + Utf8::Iterator Utf8::getIterator() + { + return Iterator(*this); + } + + Utf8::~Utf8() + { + free(_raw); + _raw = nullptr; + _length = 0; + } + + Utf8::Iterator::Iterator(const Utf8& utf8) + : _utf8(utf8) + { + _p = utf8._raw; + } + + Codepoint Utf8::Iterator::get() + { + Codepoint c; + _p = utf8toCodepoint(_p, &c); + return c; + } + +} +}
\ No newline at end of file |