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