From 789895b4b9f99668b8b772f271d07d1ce3115742 Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 5 Oct 2018 15:40:31 +0800 Subject: *update --- src/libjin/Graphics/Utf8.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/libjin/Graphics/Utf8.cpp (limited to 'src/libjin/Graphics/Utf8.cpp') 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 +#include +#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 -- cgit v1.1-26-g67d0