Path: blob/master/thirdparty/graphite/src/inc/UtfCodec.h
9906 views
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later1// Copyright 2011, SIL International, All rights reserved.23#pragma once45#include <cstdlib>6#include "inc/Main.h"78namespace graphite2 {910typedef uint32 uchar_t;1112template <int N>13struct _utf_codec14{15typedef uchar_t codeunit_t;1617static void put(codeunit_t * cp, const uchar_t , int8 & len) throw();18static uchar_t get(const codeunit_t * cp, int8 & len) throw();19static bool validate(const codeunit_t * s, const codeunit_t * const e) throw();20};212223template <>24struct _utf_codec<32>25{26private:27static const uchar_t limit = 0x110000;28public:29typedef uint32 codeunit_t;3031inline32static void put(codeunit_t * cp, const uchar_t usv, int8 & l) throw()33{34*cp = usv; l = 1;35}3637inline38static uchar_t get(const codeunit_t * cp, int8 & l) throw()39{40if (cp[0] < limit) { l = 1; return cp[0]; }41else { l = -1; return 0xFFFD; }42}4344inline45static bool validate(const codeunit_t * s, const codeunit_t * const e) throw()46{47return s <= e;48}49};505152template <>53struct _utf_codec<16>54{55private:56static const int32 lead_offset = 0xD800 - (0x10000 >> 10);57static const int32 surrogate_offset = 0x10000 - (0xD800 << 10) - 0xDC00;58public:59typedef uint16 codeunit_t;6061inline62static void put(codeunit_t * cp, const uchar_t usv, int8 & l) throw()63{64if (usv < 0x10000) { l = 1; cp[0] = codeunit_t(usv); }65else66{67cp[0] = codeunit_t(lead_offset + (usv >> 10));68cp[1] = codeunit_t(0xDC00 + (usv & 0x3FF));69l = 2;70}71}7273inline74static uchar_t get(const codeunit_t * cp, int8 & l) throw()75{76const uint32 uh = cp[0];77l = 1;7879if (uh < 0xD800|| uh > 0xDFFF) { return uh; }80if (uh > 0xDBFF) { l = -1; return 0xFFFD; }81const uint32 ul = cp[1];82if (ul < 0xDC00 || ul > 0xDFFF) { l = -1; return 0xFFFD; }83++l;84return (uh<<10) + ul + surrogate_offset;85}8687inline88static bool validate(const codeunit_t * s, const codeunit_t * const e) throw()89{90const ptrdiff_t n = e-s;91if (n <= 0) return n == 0;92const uint32 u = *(e-1); // Get the last codepoint93return (u < 0xD800 || u > 0xDBFF);94}95};969798template <>99struct _utf_codec<8>100{101private:102static const int8 sz_lut[16];103static const byte mask_lut[5];104static const uchar_t limit = 0x110000;105106public:107typedef uint8 codeunit_t;108109inline110static void put(codeunit_t * cp, const uchar_t usv, int8 & l) throw()111{112if (usv < 0x80) {l = 1; cp[0] = usv; return; }113if (usv < 0x0800) {l = 2; cp[0] = 0xC0 + (usv >> 6); cp[1] = 0x80 + (usv & 0x3F); return; }114if (usv < 0x10000) {l = 3; cp[0] = 0xE0 + (usv >> 12); cp[1] = 0x80 + ((usv >> 6) & 0x3F); cp[2] = 0x80 + (usv & 0x3F); return; }115else {l = 4; cp[0] = 0xF0 + (usv >> 18); cp[1] = 0x80 + ((usv >> 12) & 0x3F); cp[2] = 0x80 + ((usv >> 6) & 0x3F); cp[3] = 0x80 + (usv & 0x3F); return; }116}117118inline119static uchar_t get(const codeunit_t * cp, int8 & l) throw()120{121const int8 seq_sz = sz_lut[*cp >> 4];122uchar_t u = *cp & mask_lut[seq_sz];123l = 1;124bool toolong = false;125126switch(seq_sz) {127case 4: u <<= 6; u |= *++cp & 0x3F; if (*cp >> 6 != 2) break; ++l; toolong = (u < 0x10); GR_FALLTHROUGH;128// no break129case 3: u <<= 6; u |= *++cp & 0x3F; if (*cp >> 6 != 2) break; ++l; toolong |= (u < 0x20); GR_FALLTHROUGH;130// no break131case 2: u <<= 6; u |= *++cp & 0x3F; if (*cp >> 6 != 2) break; ++l; toolong |= (u < 0x80); GR_FALLTHROUGH;132// no break133case 1: break;134case 0: l = -1; return 0xFFFD;135}136137if (l != seq_sz || toolong || u >= limit)138{139l = -l;140return 0xFFFD;141}142return u;143}144145inline146static bool validate(const codeunit_t * s, const codeunit_t * const e) throw()147{148const ptrdiff_t n = e-s;149if (n <= 0) return n == 0;150s += (n-1);151if (*s < 0x80) return true;152if (*s >= 0xC0) return false;153if (n == 1) return true;154if (*--s < 0x80) return true;155if (*s >= 0xE0) return false;156if (n == 2 || *s >= 0xC0) return true;157if (*--s < 0x80) return true;158if (*s >= 0xF0) return false;159return true;160}161162};163164165template <typename C>166class _utf_iterator167{168typedef _utf_codec<sizeof(C)*8> codec;169170C * cp;171mutable int8 sl;172173public:174typedef C codeunit_type;175typedef uchar_t value_type;176typedef uchar_t * pointer;177178class reference179{180const _utf_iterator & _i;181182reference(const _utf_iterator & i): _i(i) {}183public:184operator value_type () const throw () { return codec::get(_i.cp, _i.sl); }185reference & operator = (const value_type usv) throw() { codec::put(_i.cp, usv, _i.sl); return *this; }186187friend class _utf_iterator;188};189190191_utf_iterator(const void * us=0) : cp(reinterpret_cast<C *>(const_cast<void *>(us))), sl(1) { }192193_utf_iterator & operator ++ () { cp += abs(sl); return *this; }194_utf_iterator operator ++ (int) { _utf_iterator tmp(*this); operator++(); return tmp; }195196bool operator == (const _utf_iterator & rhs) const throw() { return cp >= rhs.cp; }197bool operator != (const _utf_iterator & rhs) const throw() { return !operator==(rhs); }198199reference operator * () const throw() { return *this; }200pointer operator ->() const throw() { return &operator *(); }201202operator codeunit_type * () const throw() { return cp; }203204bool error() const throw() { return sl < 1; }205bool validate(const _utf_iterator & e) { return codec::validate(cp, e.cp); }206};207208template <typename C>209struct utf210{211typedef typename _utf_codec<sizeof(C)*8>::codeunit_t codeunit_t;212213typedef _utf_iterator<C> iterator;214typedef _utf_iterator<const C> const_iterator;215216inline217static bool validate(codeunit_t * s, codeunit_t * e) throw() {218return _utf_codec<sizeof(C)*8>::validate(s,e);219}220};221222223typedef utf<uint32> utf32;224typedef utf<uint16> utf16;225typedef utf<uint8> utf8;226227} // namespace graphite2228229230