Debian Codegraph @

source/code/Libs/include/utf8/core.h

raw symbols SCIP
1 // Copyright 2006 Nemanja Trifunovic
2
3 /*
4 Permission is hereby granted, free of charge, to any person or organization
5 obtaining a copy of the software and accompanying documentation covered by
6 this license (the "Software") to use, reproduce, display, distribute,
7 execute, and transmit the Software, and to prepare derivative works of the
8 Software, and to permit third-parties to whom the Software is furnished to
9 do so, all subject to the following:
10
11 The copyright notices in the Software and this entire statement, including
12 the above license grant, this restriction and the following disclaimer,
13 must be included in all copies of the Software, in whole or in part, and
14 all derivative works of the Software, unless such copies or derivative
15 works are solely in the form of machine-executable object code generated by
16 a source language processor.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #ifndef UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
29 #define UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
30
31 #include <iterator>
32 #include <cstring>
33 #include <string>
34
35 // Determine the C++ standard version.
36 // If the user defines UTF_CPP_CPLUSPLUS, use that.
37 // Otherwise, trust the unreliable predefined macro __cplusplus
38
39 #if !defined UTF_CPP_CPLUSPLUS
40 #define UTF_CPP_CPLUSPLUS __cplusplus
41 #endif
42
43 #if UTF_CPP_CPLUSPLUS >= 201103L // C++ 11 or later
44 #define UTF_CPP_OVERRIDE override
45 #define UTF_CPP_NOEXCEPT noexcept
46 #define UTF_CPP_STATIC_ASSERT(condition) static_assert(condition, "UTFCPP static assert");
47 #else // C++ 98/03
48 #define UTF_CPP_OVERRIDE
49 #define UTF_CPP_NOEXCEPT throw()
50 // Simulate static_assert:
51 template <bool Condition> struct StaticAssert {static void utf8_static_assert() {char static_assert_impl[Condition ? 1 : 0]; } };
52 template <> struct StaticAssert<true> {static void utf8_static_assert() {}};
53 #define UTF_CPP_STATIC_ASSERT(condition) StaticAssert<condition>::utf8_static_assert();
54 #endif // C++ 11 or later
55
56
57 namespace utf8
58 {
59 // The typedefs for 8-bit, 16-bit and 32-bit code units
60 #if UTF_CPP_CPLUSPLUS >= 201103L // C++ 11 or later
61 #if UTF_CPP_CPLUSPLUS >= 202002L // C++ 20 or later
62 typedef char8_t utfchar8_t;
63 #else // C++ 11/14/17
64 typedef unsigned char utfchar8_t;
65 #endif
66 typedef char16_t utfchar16_t;
67 typedef char32_t utfchar32_t;
68 #else // C++ 98/03
69 typedef unsigned char utfchar8_t;
70 typedef unsigned short utfchar16_t;
71 typedef unsigned int utfchar32_t;
72 #endif // C++ 11 or later
73
74 // Helper code - not intended to be directly called by the library users. May be changed at any time
75 namespace internal
76 {
77 // Unicode constants
78 // Leading (high) surrogates: 0xd800 - 0xdbff
79 // Trailing (low) surrogates: 0xdc00 - 0xdfff
80 const utfchar16_t LEAD_SURROGATE_MIN = 0xd800u;
81 const utfchar16_t LEAD_SURROGATE_MAX = 0xdbffu;
82 const utfchar16_t TRAIL_SURROGATE_MIN = 0xdc00u;
83 const utfchar16_t TRAIL_SURROGATE_MAX = 0xdfffu;
84 const utfchar16_t LEAD_OFFSET = 0xd7c0u; // LEAD_SURROGATE_MIN - (0x10000 >> 10)
85 const utfchar32_t SURROGATE_OFFSET = 0xfca02400u; // 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN
86
87 // Maximum valid value for a Unicode code point
88 const utfchar32_t CODE_POINT_MAX = 0x0010ffffu;
89
90 template<typename octet_type>
91 inline utfchar8_t mask8(octet_type oc)
92 {
93 return static_cast<utfchar8_t>(0xff & oc);
94 }
95
96 template<typename u16_type>
97 inline utfchar16_t mask16(u16_type oc)
98 {
99 return static_cast<utfchar16_t>(0xffff & oc);
100 }
101
102 template<typename octet_type>
103 inline bool is_trail(octet_type oc)
104 {
105 return ((utf8::internal::mask8(oc) >> 6) == 0x2);
106 }
107
108 inline bool is_lead_surrogate(utfchar32_t cp)
109 {
110 return (cp >= static_cast<utfchar32_t>(LEAD_SURROGATE_MIN) && cp <= static_cast<utfchar32_t>(LEAD_SURROGATE_MAX));
111 }
112
113 inline bool is_trail_surrogate(utfchar32_t cp)
114 {
115 return (cp >= static_cast<utfchar32_t>(TRAIL_SURROGATE_MIN) && cp <= static_cast<utfchar32_t>(TRAIL_SURROGATE_MAX));
116 }
117
118 inline bool is_surrogate(utfchar32_t cp)
119 {
120 return (cp >= static_cast<utfchar32_t>(LEAD_SURROGATE_MIN) && cp <= static_cast<utfchar32_t>(TRAIL_SURROGATE_MAX));
121 }
122
123 inline bool is_code_point_valid(utfchar32_t cp)
124 {
125 return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp));
126 }
127
128 inline bool is_in_bmp(utfchar32_t cp)
129 {
130 return cp < utfchar32_t(0x10000);
131 }
132
133 template <typename octet_iterator>
134 int sequence_length(octet_iterator lead_it)
135 {
136 const utfchar8_t lead = utf8::internal::mask8(*lead_it);
137 if (lead < 0x80)
138 return 1;
139 else if ((lead >> 5) == 0x6)
140 return 2;
141 else if ((lead >> 4) == 0xe)
142 return 3;
143 else if ((lead >> 3) == 0x1e)
144 return 4;
145 else
146 return 0;
147 }
148
149 inline bool is_overlong_sequence(utfchar32_t cp, int length)
150 {
151 if (cp < 0x80) {
152 if (length != 1)
153 return true;
154 }
155 else if (cp < 0x800) {
156 if (length != 2)
157 return true;
158 }
159 else if (cp < 0x10000) {
160 if (length != 3)
161 return true;
162 }
163 return false;
164 }
165
166 enum utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT};
167
168 /// Helper for get_sequence_x
169 template <typename octet_iterator>
170 utf_error increase_safely(octet_iterator& it, const octet_iterator end)
171 {
172 if (++it == end)
173 return NOT_ENOUGH_ROOM;
174
175 if (!utf8::internal::is_trail(*it))
176 return INCOMPLETE_SEQUENCE;
177
178 return UTF8_OK;
179 }
180
181 #define UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(IT, END) {utf_error ret = increase_safely(IT, END); if (ret != UTF8_OK) return ret;}
182
183 /// get_sequence_x functions decode utf-8 sequences of the length x
184 template <typename octet_iterator>
185 utf_error get_sequence_1(octet_iterator& it, octet_iterator end, utfchar32_t& code_point)
186 {
187 if (it == end)
188 return NOT_ENOUGH_ROOM;
189
190 code_point = static_cast<utfchar32_t>(utf8::internal::mask8(*it));
191
192 return UTF8_OK;
193 }
194
195 template <typename octet_iterator>
196 utf_error get_sequence_2(octet_iterator& it, octet_iterator end, utfchar32_t& code_point)
197 {
198 if (it == end)
199 return NOT_ENOUGH_ROOM;
200
201 code_point = static_cast<utfchar32_t>(utf8::internal::mask8(*it));
202
203 UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
204
205 code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f);
206
207 return UTF8_OK;
208 }
209
210 template <typename octet_iterator>
211 utf_error get_sequence_3(octet_iterator& it, octet_iterator end, utfchar32_t& code_point)
212 {
213 if (it == end)
214 return NOT_ENOUGH_ROOM;
215
216 code_point = static_cast<utfchar32_t>(utf8::internal::mask8(*it));
217
218 UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
219
220 code_point = ((code_point << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
221
222 UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
223
224 code_point = static_cast<utfchar32_t>(code_point + ((*it) & 0x3f));
225
226 return UTF8_OK;
227 }
228
229 template <typename octet_iterator>
230 utf_error get_sequence_4(octet_iterator& it, octet_iterator end, utfchar32_t& code_point)
231 {
232 if (it == end)
233 return NOT_ENOUGH_ROOM;
234
235 code_point = static_cast<utfchar32_t>(utf8::internal::mask8(*it));
236
237 UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
238
239 code_point = ((code_point << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
240
241 UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
242
243 code_point = static_cast<utfchar32_t>(code_point + ((utf8::internal::mask8(*it) << 6) & 0xfff));
244
245 UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
246
247 code_point = static_cast<utfchar32_t>(code_point + ((*it) & 0x3f));
248
249 return UTF8_OK;
250 }
251
252 #undef UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR
253
254 template <typename octet_iterator>
255 utf_error validate_next(octet_iterator& it, octet_iterator end, utfchar32_t& code_point)
256 {
257 if (it == end)
258 return NOT_ENOUGH_ROOM;
259
260 // Save the original value of it so we can go back in case of failure
261 // Of course, it does not make much sense with i.e. stream iterators
262 octet_iterator original_it = it;
263
264 utfchar32_t cp = 0;
265 // Determine the sequence length based on the lead octet
266 const int length = utf8::internal::sequence_length(it);
267
268 // Get trail octets and calculate the code point
269 utf_error err = UTF8_OK;
270 switch (length) {
271 case 0:
272 return INVALID_LEAD;
273 case 1:
274 err = utf8::internal::get_sequence_1(it, end, cp);
275 break;
276 case 2:
277 err = utf8::internal::get_sequence_2(it, end, cp);
278 break;
279 case 3:
280 err = utf8::internal::get_sequence_3(it, end, cp);
281 break;
282 case 4:
283 err = utf8::internal::get_sequence_4(it, end, cp);
284 break;
285 }
286
287 if (err == UTF8_OK) {
288 // Decoding succeeded. Now, security checks...
289 if (utf8::internal::is_code_point_valid(cp)) {
290 if (!utf8::internal::is_overlong_sequence(cp, length)){
291 // Passed! Return here.
292 code_point = cp;
293 ++it;
294 return UTF8_OK;
295 }
296 else
297 err = OVERLONG_SEQUENCE;
298 }
299 else
300 err = INVALID_CODE_POINT;
301 }
302
303 // Failure branch - restore the original value of the iterator
304 it = original_it;
305 return err;
306 }
307
308 template <typename octet_iterator>
309 inline utf_error validate_next(octet_iterator& it, octet_iterator end) {
310 utfchar32_t ignored;
311 return utf8::internal::validate_next(it, end, ignored);
312 }
313
314 template <typename word_iterator>
315 utf_error validate_next16(word_iterator& it, word_iterator end, utfchar32_t& code_point)
316 {
317 // Make sure the iterator dereferences a large enough type
318 typedef typename std::iterator_traits<word_iterator>::value_type word_type;
319 UTF_CPP_STATIC_ASSERT(sizeof(word_type) >= sizeof(utfchar16_t));
320 // Check the edge case:
321 if (it == end)
322 return NOT_ENOUGH_ROOM;
323 // Save the original value of it so we can go back in case of failure
324 // Of course, it does not make much sense with i.e. stream iterators
325 word_iterator original_it = it;
326
327 utf_error err = UTF8_OK;
328
329 const utfchar16_t first_word = *it++;
330 if (!is_surrogate(first_word)) {
331 code_point = first_word;
332 return UTF8_OK;
333 }
334 else {
335 if (it == end)
336 err = NOT_ENOUGH_ROOM;
337 else if (is_lead_surrogate(first_word)) {
338 const utfchar16_t second_word = *it++;
339 if (is_trail_surrogate(static_cast<utfchar32_t>(second_word))) {
340 code_point = static_cast<utfchar32_t>(first_word << 10) + static_cast<utfchar32_t>(second_word) + SURROGATE_OFFSET;
341 return UTF8_OK;
342 } else
343 err = INCOMPLETE_SEQUENCE;
344
345 } else {
346 err = INVALID_LEAD;
347 }
348 }
349 // error branch
350 it = original_it;
351 return err;
352 }
353
354 // Internal implementation of both checked and unchecked append() function
355 // This function will be invoked by the overloads below, as they will know
356 // the octet_type.
357 template <typename octet_iterator, typename octet_type>
358 octet_iterator append(utfchar32_t cp, octet_iterator result) {
359 if (cp < 0x80) // one octet
360 *(result++) = static_cast<octet_type>(cp);
361 else if (cp < 0x800) { // two octets
362 *(result++) = static_cast<octet_type>((cp >> 6) | 0xc0);
363 *(result++) = static_cast<octet_type>((cp & 0x3f) | 0x80);
364 }
365 else if (cp < 0x10000) { // three octets
366 *(result++) = static_cast<octet_type>((cp >> 12) | 0xe0);
367 *(result++) = static_cast<octet_type>(((cp >> 6) & 0x3f) | 0x80);
368 *(result++) = static_cast<octet_type>((cp & 0x3f) | 0x80);
369 }
370 else { // four octets
371 *(result++) = static_cast<octet_type>((cp >> 18) | 0xf0);
372 *(result++) = static_cast<octet_type>(((cp >> 12) & 0x3f)| 0x80);
373 *(result++) = static_cast<octet_type>(((cp >> 6) & 0x3f) | 0x80);
374 *(result++) = static_cast<octet_type>((cp & 0x3f) | 0x80);
375 }
376 return result;
377 }
378
379 // One of the following overloads will be invoked from the API calls
380
381 // A simple (but dangerous) case: the caller appends byte(s) to a char array
382 inline char* append(utfchar32_t cp, char* result) {
383 return append<char*, char>(cp, result);
384 }
385
386 // Hopefully, most common case: the caller uses back_inserter
387 // i.e. append(cp, std::back_inserter(str));
388 template<typename container_type>
389 std::back_insert_iterator<container_type> append
390 (utfchar32_t cp, std::back_insert_iterator<container_type> result) {
391 return append<std::back_insert_iterator<container_type>,
392 typename container_type::value_type>(cp, result);
393 }
394
395 // The caller uses some other kind of output operator - not covered above
396 // Note that in this case we are not able to determine octet_type
397 // so we assume it's utfchar8_t; that can cause a conversion warning if we are wrong.
398 template <typename octet_iterator>
399 octet_iterator append(utfchar32_t cp, octet_iterator result) {
400 return append<octet_iterator, utfchar8_t>(cp, result);
401 }
402
403 // Internal implementation of both checked and unchecked append16() function
404 // This function will be invoked by the overloads below, as they will know
405 // the word_type.
406 template <typename word_iterator, typename word_type>
407 word_iterator append16(utfchar32_t cp, word_iterator result) {
408 UTF_CPP_STATIC_ASSERT(sizeof(word_type) >= sizeof(utfchar16_t));
409 if (is_in_bmp(cp))
410 *(result++) = static_cast<word_type>(cp);
411 else {
412 // Code points from the supplementary planes are encoded via surrogate pairs
413 *(result++) = static_cast<word_type>(LEAD_OFFSET + (cp >> 10));
414 *(result++) = static_cast<word_type>(TRAIL_SURROGATE_MIN + (cp & 0x3FF));
415 }
416 return result;
417 }
418
419 // Hopefully, most common case: the caller uses back_inserter
420 // i.e. append16(cp, std::back_inserter(str));
421 template<typename container_type>
422 std::back_insert_iterator<container_type> append16
423 (utfchar32_t cp, std::back_insert_iterator<container_type> result) {
424 return append16<std::back_insert_iterator<container_type>,
425 typename container_type::value_type>(cp, result);
426 }
427
428 // The caller uses some other kind of output operator - not covered above
429 // Note that in this case we are not able to determine word_type
430 // so we assume it's utfchar16_t; that can cause a conversion warning if we are wrong.
431 template <typename word_iterator>
432 word_iterator append16(utfchar32_t cp, word_iterator result) {
433 return append16<word_iterator, utfchar16_t>(cp, result);
434 }
435
436 } // namespace internal
437
438 /// The library API - functions intended to be called by the users
439
440 // Byte order mark
441 const utfchar8_t bom[] = {0xef, 0xbb, 0xbf};
442
443 template <typename octet_iterator>
444 octet_iterator find_invalid(octet_iterator start, octet_iterator end)
445 {
446 octet_iterator result = start;
447 while (result != end) {
448 utf8::internal::utf_error err_code = utf8::internal::validate_next(result, end);
449 if (err_code != internal::UTF8_OK)
450 return result;
451 }
452 return result;
453 }
454
455 inline const char* find_invalid(const char* str)
456 {
457 const char* end = str + std::strlen(str);
458 return find_invalid(str, end);
459 }
460
461 inline std::size_t find_invalid(const std::string& s)
462 {
463 std::string::const_iterator invalid = find_invalid(s.begin(), s.end());
464 return (invalid == s.end()) ? std::string::npos : static_cast<std::size_t>(invalid - s.begin());
465 }
466
467 template <typename octet_iterator>
468 inline bool is_valid(octet_iterator start, octet_iterator end)
469 {
470 return (utf8::find_invalid(start, end) == end);
471 }
472
473 inline bool is_valid(const char* str)
474 {
475 return (*(utf8::find_invalid(str)) == '\0');
476 }
477
478 inline bool is_valid(const std::string& s)
479 {
480 return is_valid(s.begin(), s.end());
481 }
482
483
484
485 template <typename octet_iterator>
486 inline bool starts_with_bom (octet_iterator it, octet_iterator end)
487 {
488 return (
489 ((it != end) && (utf8::internal::mask8(*it++)) == bom[0]) &&
490 ((it != end) && (utf8::internal::mask8(*it++)) == bom[1]) &&
491 ((it != end) && (utf8::internal::mask8(*it)) == bom[2])
492 );
493 }
494
495 inline bool starts_with_bom(const std::string& s)
496 {
497 return starts_with_bom(s.begin(), s.end());
498 }
499 } // namespace utf8
500
501 #endif // header guard
502