FifeGUI 0.2.0
A C++ GUI library designed for games.
checked.hpp
1// Copyright 2006 Nemanja Trifunovic
2
3/*
4Permission is hereby granted, free of charge, to any person or organization
5obtaining a copy of the software and accompanying documentation covered by
6this license (the "Software") to use, reproduce, display, distribute,
7execute, and transmit the Software, and to prepare derivative works of the
8Software, and to permit third-parties to whom the Software is furnished to
9do so, all subject to the following:
10
11The copyright notices in the Software and this entire statement, including
12the above license grant, this restriction and the following disclaimer,
13must be included in all copies of the Software, in whole or in part, and
14all derivative works of the Software, unless such copies or derivative
15works are solely in the form of machine-executable object code generated by
16a source language processor.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24DEALINGS IN THE SOFTWARE.
25*/
26
27#ifndef UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
28#define UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
29
30#include <stdexcept>
31
32#include "core.hpp"
33
34namespace utf8
35{
36 // Base for the exceptions that may be thrown from the library
37 class exception : public ::std::exception
38 {
39 };
40
41 // Exceptions that may be thrown from the library functions.
42 class invalid_code_point : public exception
43 {
44 uint32_t cp;
45
46 public:
47 explicit invalid_code_point(uint32_t cp) : cp(cp) { }
48 char const * what() const noexcept override
49 {
50 return "Invalid code point";
51 }
52 uint32_t code_point() const
53 {
54 return cp;
55 }
56 };
57
58 class invalid_utf8 : public exception
59 {
60 uint8_t u8;
61
62 public:
63 explicit invalid_utf8(uint8_t u) : u8(u) { }
64 char const * what() const noexcept override
65 {
66 return "Invalid UTF-8";
67 }
68 uint8_t utf8_octet() const
69 {
70 return u8;
71 }
72 };
73
74 class invalid_utf16 : public exception
75 {
76 uint16_t u16;
77
78 public:
79 explicit invalid_utf16(uint16_t u) : u16(u) { }
80 char const * what() const noexcept override
81 {
82 return "Invalid UTF-16";
83 }
84 uint16_t utf16_word() const
85 {
86 return u16;
87 }
88 };
89
91 {
92 public:
93 char const * what() const noexcept override
94 {
95 return "Not enough space";
96 }
97 };
98
100
101 template <typename octet_iterator>
102 octet_iterator append(uint32_t cp, octet_iterator result)
103 {
104 if (!utf8::internal::is_code_point_valid(cp)) {
105 throw invalid_code_point(cp);
106 }
107
108 if (cp < 0x80) { // one octet
109 *(result++) = static_cast<uint8_t>(cp);
110 } else if (cp < 0x800) { // two octets
111 *(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
112 *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
113 } else if (cp < 0x10000) { // three octets
114 *(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
115 *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
116 *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
117 } else { // four octets
118 *(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
119 *(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80);
120 *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
121 *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
122 }
123 return result;
124 }
125
126 template <typename octet_iterator, typename output_iterator>
127 output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
128 {
129 while (start != end) {
130 octet_iterator sequence_start = start;
131 internal::utf_error err_code = utf8::internal::validate_next(start, end);
132 switch (err_code) {
133 case internal::UTF8_OK:
134 for (octet_iterator it = sequence_start; it != start; ++it) {
135 *out++ = *it;
136 }
137 break;
138 case internal::NOT_ENOUGH_ROOM:
139 throw not_enough_room();
140 case internal::INVALID_LEAD:
141 out = utf8::append(replacement, out);
142 ++start;
143 break;
144 case internal::INCOMPLETE_SEQUENCE:
145 case internal::OVERLONG_SEQUENCE:
146 case internal::INVALID_CODE_POINT:
147 out = utf8::append(replacement, out);
148 ++start;
149 // just one replacement mark for the sequence
150 while (start != end && utf8::internal::is_trail(*start)) {
151 ++start;
152 }
153 break;
154 }
155 }
156 return out;
157 }
158
159 template <typename octet_iterator, typename output_iterator>
160 inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out)
161 {
162 static uint32_t const replacement_marker = utf8::internal::mask16(0xfffd);
163 return utf8::replace_invalid(start, end, out, replacement_marker);
164 }
165
166 template <typename octet_iterator>
167 uint32_t next(octet_iterator& it, octet_iterator end)
168 {
169 uint32_t cp = 0;
170 internal::utf_error err_code = utf8::internal::validate_next(it, end, cp);
171 switch (err_code) {
172 case internal::UTF8_OK:
173 break;
174 case internal::NOT_ENOUGH_ROOM:
175 throw not_enough_room();
176 case internal::INVALID_LEAD:
177 case internal::INCOMPLETE_SEQUENCE:
178 case internal::OVERLONG_SEQUENCE:
179 throw invalid_utf8(*it);
180 case internal::INVALID_CODE_POINT:
181 throw invalid_code_point(cp);
182 }
183 return cp;
184 }
185
186 template <typename octet_iterator>
187 uint32_t peek_next(octet_iterator it, octet_iterator end)
188 {
189 return utf8::next(it, end);
190 }
191
192 template <typename octet_iterator>
193 uint32_t prior(octet_iterator& it, octet_iterator start)
194 {
195 // can't do much if it == start
196 if (it == start) {
197 throw not_enough_room();
198 }
199
200 octet_iterator end = it;
201 // Go back until we hit either a lead octet or start
202 while (utf8::internal::is_trail(*(--it))) {
203 if (it == start) {
204 throw invalid_utf8(*it); // error - no lead byte in the sequence
205 }
206 }
207 return utf8::peek_next(it, end);
208 }
209
211 template <typename octet_iterator>
212 uint32_t previous(octet_iterator& it, octet_iterator pass_start)
213 {
214 octet_iterator end = it;
215 while (utf8::internal::is_trail(*(--it))) {
216 if (it == pass_start) {
217 throw invalid_utf8(*it); // error - no lead byte in the sequence
218 }
219 }
220 octet_iterator temp = it;
221 return utf8::next(temp, end);
222 }
223
224 template <typename octet_iterator, typename distance_type>
225 void advance(octet_iterator& it, distance_type n, octet_iterator end)
226 {
227 for (distance_type i = 0; i < n; ++i) {
228 utf8::next(it, end);
229 }
230 }
231
232 template <typename octet_iterator>
233 typename std::iterator_traits<octet_iterator>::difference_type distance(octet_iterator first, octet_iterator last)
234 {
235 typename std::iterator_traits<octet_iterator>::difference_type dist;
236 for (dist = 0; first < last; ++dist) {
237 utf8::next(first, last);
238 }
239 return dist;
240 }
241
242 template <typename u16bit_iterator, typename octet_iterator>
243 octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
244 {
245 while (start != end) {
246 uint32_t cp = utf8::internal::mask16(*start++);
247 // Take care of surrogate pairs first
248 if (utf8::internal::is_lead_surrogate(cp)) {
249 if (start != end) {
250 uint32_t trail_surrogate = utf8::internal::mask16(*start++);
251 if (utf8::internal::is_trail_surrogate(trail_surrogate)) {
252 cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
253 } else {
254 throw invalid_utf16(static_cast<uint16_t>(trail_surrogate));
255 }
256 } else {
257 throw invalid_utf16(static_cast<uint16_t>(cp));
258 }
259
260 }
261 // Lone trail surrogate
262 else if (utf8::internal::is_trail_surrogate(cp)) {
263 throw invalid_utf16(static_cast<uint16_t>(cp));
264 }
265
266 result = utf8::append(cp, result);
267 }
268 return result;
269 }
270
271 template <typename u16bit_iterator, typename octet_iterator>
272 u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
273 {
274 while (start != end) {
275 uint32_t cp = utf8::next(start, end);
276 if (cp > 0xffff) { // make a surrogate pair
277 *result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
278 *result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
279 } else {
280 *result++ = static_cast<uint16_t>(cp);
281 }
282 }
283 return result;
284 }
285
286 template <typename octet_iterator, typename u32bit_iterator>
287 octet_iterator utf32to8(u32bit_iterator start, u32bit_iterator end, octet_iterator result)
288 {
289 while (start != end) {
290 result = utf8::append(*(start++), result);
291 }
292
293 return result;
294 }
295
296 template <typename octet_iterator, typename u32bit_iterator>
297 u32bit_iterator utf8to32(octet_iterator start, octet_iterator end, u32bit_iterator result)
298 {
299 while (start != end) {
300 (*result++) = utf8::next(start, end);
301 }
302
303 return result;
304 }
305
306 template <typename octet_iterator>
307 class iterator
308 {
309 public:
310 using iterator_category = std::bidirectional_iterator_tag;
311 using value_type = uint32_t;
312 using difference_type = std::ptrdiff_t;
313 using pointer = value_type*;
314 using reference = value_type&;
315
316 private:
317 octet_iterator it;
318 octet_iterator range_start;
319 octet_iterator range_end;
320
321 public:
322 iterator() = default;
323 explicit iterator(
324 octet_iterator const & octet_it, octet_iterator const & range_start, octet_iterator const & range_end) :
325 it(octet_it), range_start(range_start), range_end(range_end)
326 {
327 if (it < range_start || it > range_end) {
328 throw std::out_of_range("Invalid utf-8 iterator position");
329 }
330 }
331 // the default "big three" are OK
332 octet_iterator base() const
333 {
334 return it;
335 }
336 uint32_t operator*() const
337 {
338 octet_iterator temp = it;
339 return utf8::next(temp, range_end);
340 }
341 // Remember to implement the rest of your iterator functionality here
342 };
343
344} // namespace utf8
345
346#endif // header guard