Debian Codegraph @

gr2fonttest/UtfCodec.h

raw
1 // SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later
2 // Copyright 2010, SIL International, All rights reserved.
3
4 #pragma once
5
6 #include <cstdlib>
7 #include "graphite2/Types.h"
8
9 typedef gr_uint8 uint8;
10 typedef gr_uint8 byte;
11 typedef gr_uint16 uint16;
12 typedef gr_uint32 uint32;
13 typedef gr_int8 int8;
14 typedef gr_int16 int16;
15 typedef gr_int32 int32;
16
17 namespace graphite2 {
18
19 typedef uint32 uchar_t;
20
21 template <int N>
22 struct _utf_codec
23 {
24 typedef uchar_t codeunit_t;
25
26 static void put(codeunit_t * cp, const uchar_t , int8 & len) throw();
27 static uchar_t get(const codeunit_t * cp, int8 & len) throw();
28 };
29
30
31 template <>
32 struct _utf_codec<32>
33 {
34 private:
35 static const uchar_t limit = 0x110000;
36 public:
37 typedef uint32 codeunit_t;
38
39 inline
40 static void put(codeunit_t * cp, const uchar_t usv, int8 & l) throw()
41 {
42 *cp = usv; l = 1;
43 }
44
45 inline
46 static uchar_t get(const codeunit_t * cp, int8 & l) throw()
47 {
48 if (cp[0] < limit) { l = 1; return cp[0]; }
49 else { l = -1; return 0xFFFD; }
50 }
51 };
52
53
54 template <>
55 struct _utf_codec<16>
56 {
57 private:
58 static const int32 lead_offset = 0xD800 - (0x10000 >> 10);
59 static const int32 surrogate_offset = 0x10000 - (0xD800 << 10) - 0xDC00;
60 public:
61 typedef uint16 codeunit_t;
62
63 inline
64 static void put(codeunit_t * cp, const uchar_t usv, int8 & l) throw()
65 {
66 if (usv < 0x10000) { l = 1; cp[0] = codeunit_t(usv); }
67 else
68 {
69 cp[0] = codeunit_t(lead_offset + (usv >> 10));
70 cp[1] = codeunit_t(0xDC00 + (usv & 0x3FF));
71 l = 2;
72 }
73 }
74
75 inline
76 static uchar_t get(const codeunit_t * cp, int8 & l) throw()
77 {
78 const uint32 uh = cp[0];
79 l = 1;
80
81 if (0xD800 > uh || uh > 0xDFFF) { return uh; }
82 const uint32 ul = cp[1];
83 if (uh > 0xDBFF || 0xDC00 > ul || ul > 0xDFFF) { l = -1; return 0xFFFD; }
84 ++l;
85 return (uh<<10) + ul + surrogate_offset;
86 }
87 };
88
89
90 template <>
91 struct _utf_codec<8>
92 {
93 private:
94 static const int8 sz_lut[16];
95 static const byte mask_lut[5];
96
97
98 public:
99 typedef uint8 codeunit_t;
100
101 inline
102 static void put(codeunit_t * cp, const uchar_t usv, int8 & l) throw()
103 {
104 if (usv < 0x80) {l = 1; cp[0] = usv; return; }
105 if (usv < 0x0800) {l = 2; cp[0] = 0xC0 + (usv >> 6); cp[1] = 0x80 + (usv & 0x3F); return; }
106 if (usv < 0x10000) {l = 3; cp[0] = 0xE0 + (usv >> 12); cp[1] = 0x80 + ((usv >> 6) & 0x3F); cp[2] = 0x80 + (usv & 0x3F); return; }
107 else {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; }
108 }
109
110 inline
111 static uchar_t get(const codeunit_t * cp, int8 & l) throw()
112 {
113 const int8 seq_sz = sz_lut[*cp >> 4];
114 uchar_t u = *cp & mask_lut[seq_sz];
115 l = 1;
116 bool toolong = false;
117
118 switch(seq_sz) {
119 case 4: u <<= 6; u |= *++cp & 0x3F; if (*cp >> 6 != 2) break; ++l; toolong = (u < 0x10);
120 // no break
121 case 3: u <<= 6; u |= *++cp & 0x3F; if (*cp >> 6 != 2) break; ++l; toolong |= (u < 0x20);
122 // no break
123 case 2: u <<= 6; u |= *++cp & 0x3F; if (*cp >> 6 != 2) break; ++l; toolong |= (u < 0x80);
124 // no break
125 case 1: break;
126 case 0: l = -1; return 0xFFFD;
127 }
128
129 if (l != seq_sz || toolong)
130 {
131 l = -l;
132 return 0xFFFD;
133 }
134 return u;
135 }
136 };
137
138
139 template <typename C>
140 class _utf_iterator
141 {
142 typedef _utf_codec<sizeof(C)*8> codec;
143
144 C * cp;
145 mutable int8 sl;
146
147 public:
148 typedef C codeunit_type;
149 typedef uchar_t value_type;
150 typedef uchar_t * pointer;
151
152 class reference
153 {
154 const _utf_iterator & _i;
155
156 reference(const _utf_iterator & i): _i(i) {}
157 public:
158 operator value_type () const throw () { return codec::get(_i.cp, _i.sl); }
159 reference & operator = (const value_type usv) throw() { codec::put(_i.cp, usv, _i.sl); return *this; }
160
161 friend class _utf_iterator;
162 };
163
164
165 _utf_iterator(const void * us=0) : cp(reinterpret_cast<C *>(const_cast<void *>(us))), sl(1) { }
166
167 _utf_iterator & operator ++ () { cp += abs(sl); return *this; }
168 _utf_iterator operator ++ (int) { _utf_iterator tmp(*this); operator++(); return tmp; }
169
170 bool operator == (const _utf_iterator & rhs) const throw() { return cp >= rhs.cp; }
171 bool operator != (const _utf_iterator & rhs) const throw() { return !operator==(rhs); }
172
173 reference operator * () const throw() { return *this; }
174 pointer operator ->() const throw() { return &operator *(); }
175
176 operator codeunit_type * () const throw() { return cp; }
177
178 bool error() const throw() { return sl < 1; }
179 };
180
181 template <typename C>
182 struct utf
183 {
184 typedef typename _utf_codec<sizeof(C)*8>::codeunit_t codeunit_t;
185
186 typedef _utf_iterator<C> iterator;
187 typedef _utf_iterator<const C> const_iterator;
188 };
189
190
191 typedef utf<uint32> utf32;
192 typedef utf<uint16> utf16;
193 typedef utf<uint8> utf8;
194
195 } // namespace graphite2