/src/jsoncons/include/jsoncons/utility/unicode_traits.hpp
Line | Count | Source |
1 | | // Copyright 2013-2026 Daniel Parker |
2 | | // Distributed under the Boost license, Version 1.0. |
3 | | // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
4 | | |
5 | | // See https://github.com/danielaparker/unicode_traits for latest version |
6 | | |
7 | | /* |
8 | | * Includes code derived from Unicode, Inc decomposition code in ConvertUTF.h and ConvertUTF.c |
9 | | * http://www.unicode.org/ |
10 | | * |
11 | | * "Unicode, Inc. hereby grants the right to freely use the information |
12 | | * supplied in this file in the creation of products supporting the |
13 | | * Unicode Standard." |
14 | | */ |
15 | | |
16 | | #ifndef JSONCONS_UTILITY_UNICODE_TRAITS_HPP |
17 | | #define JSONCONS_UTILITY_UNICODE_TRAITS_HPP |
18 | | |
19 | | #include <cstddef> |
20 | | #include <cstdint> |
21 | | #include <cstring> |
22 | | #include <iterator> |
23 | | #include <string> |
24 | | #include <system_error> |
25 | | #include <type_traits> |
26 | | |
27 | | #include <jsoncons/nonstd/compiler_support.hpp> |
28 | | #include <jsoncons/config/jsoncons_config.hpp> |
29 | | #include <jsoncons/utility/more_type_traits.hpp> |
30 | | |
31 | | namespace jsoncons { |
32 | | namespace unicode_traits { |
33 | | |
34 | | enum class encoding_kind {undetected,utf8,utf16le,utf16be,utf32le,utf32be}; |
35 | | |
36 | | inline |
37 | | std::string to_string(encoding_kind encoding) |
38 | 0 | { |
39 | 0 | switch (encoding) |
40 | 0 | { |
41 | 0 | case encoding_kind::utf8: |
42 | 0 | return "utf8"; |
43 | 0 | case encoding_kind::utf16le: |
44 | 0 | return "utf16le"; |
45 | 0 | case encoding_kind::utf16be: |
46 | 0 | return "utf16be"; |
47 | 0 | case encoding_kind::utf32le: |
48 | 0 | return "utf32le"; |
49 | 0 | case encoding_kind::utf32be: |
50 | 0 | return "utf32be"; |
51 | 0 | default: |
52 | 0 | return "undetected"; |
53 | 0 | } |
54 | 0 | } |
55 | | |
56 | | template <typename Byte> |
57 | | struct detect_encoding_result |
58 | | { |
59 | | const Byte* ptr; |
60 | | encoding_kind encoding; |
61 | | }; |
62 | | |
63 | | template <typename CharT> |
64 | | typename std::enable_if<ext_traits::is_char8<CharT>::value,detect_encoding_result<CharT>>::type |
65 | | detect_encoding_from_bom(const CharT* data, std::size_t length) |
66 | 24.3k | { |
67 | 24.3k | static constexpr uint8_t bom_utf8[] = {0xef,0xbb,0xbf}; |
68 | 24.3k | static constexpr uint8_t bom_utf16le[] = {0xff,0xfe}; |
69 | 24.3k | static constexpr uint8_t bom_utf16be[] = {0xfe,0xff}; |
70 | 24.3k | static constexpr uint8_t bom_utf32le[] = {0xff,0xfe,0x00,0x00}; |
71 | 24.3k | static constexpr uint8_t bom_utf32be[] = {0x00,0x00,0xfe,0xff}; |
72 | | |
73 | 24.3k | if (length >= 4 && !memcmp(data,bom_utf32le,4)) |
74 | 6 | { |
75 | 6 | return detect_encoding_result<CharT>{data+4,encoding_kind::utf32le}; |
76 | 6 | } |
77 | 24.2k | else if (length >= 4 && !memcmp(data,bom_utf32be,4)) |
78 | 6 | { |
79 | 6 | return detect_encoding_result<CharT>{data+4,encoding_kind::utf32be}; |
80 | 6 | } |
81 | 24.2k | else if (length >= 2 && !memcmp(data,bom_utf16le,2)) |
82 | 54 | { |
83 | 54 | return detect_encoding_result<CharT>{data+2,encoding_kind::utf16le}; |
84 | 54 | } |
85 | 24.2k | else if (length >= 2 && !memcmp(data,bom_utf16be,2)) |
86 | 6 | { |
87 | 6 | return detect_encoding_result<CharT>{data+2,encoding_kind::utf16be}; |
88 | 6 | } |
89 | 24.2k | else if (length >= 3 && !memcmp(data,bom_utf8,3)) |
90 | 102 | { |
91 | 102 | return detect_encoding_result<CharT>{data+3,encoding_kind::utf8}; |
92 | 102 | } |
93 | 24.1k | else |
94 | 24.1k | { |
95 | 24.1k | return detect_encoding_result<CharT>{data,encoding_kind::undetected}; |
96 | 24.1k | } |
97 | 24.3k | } |
98 | | |
99 | | template <typename CharT> |
100 | | typename std::enable_if<ext_traits::is_char16<CharT>::value || ext_traits::is_char32<CharT>::value,detect_encoding_result<CharT>>::type |
101 | | detect_encoding_from_bom(const CharT* data, std::size_t) |
102 | 0 | { |
103 | 0 | return detect_encoding_result<CharT>{data,encoding_kind::undetected}; |
104 | 0 | } |
105 | | |
106 | | template <typename CharT> |
107 | | typename std::enable_if<ext_traits::is_char8<CharT>::value,detect_encoding_result<CharT>>::type |
108 | | detect_json_encoding(const CharT* data, std::size_t length) |
109 | 12.1k | { |
110 | 12.1k | detect_encoding_result<CharT> r = detect_encoding_from_bom(data,length); |
111 | 12.1k | if (r.encoding != encoding_kind::undetected) |
112 | 107 | { |
113 | 107 | return r; |
114 | 107 | } |
115 | 12.0k | else if (length < 4) |
116 | 1.36k | { |
117 | 1.36k | return detect_encoding_result<CharT>{data,encoding_kind::utf8}; |
118 | 1.36k | } |
119 | 10.6k | else if (*data == 0 && *(data+1) == 0 && *(data+2) == 0) |
120 | 8 | { |
121 | 8 | return detect_encoding_result<CharT>{data,encoding_kind::utf32be}; |
122 | 8 | } |
123 | 10.6k | else if (*data == 0 && *(data+2) == 0) |
124 | 8 | { |
125 | 8 | return detect_encoding_result<CharT>{data,encoding_kind::utf16be}; |
126 | 8 | } |
127 | 10.6k | else if (*(data+1) == 0 && *(data+2) == 0 && *(data+3) == 0) |
128 | 3 | { |
129 | 3 | return detect_encoding_result<CharT>{data,encoding_kind::utf32le}; |
130 | 3 | } |
131 | 10.6k | else if (*(data+1) == 0 && *(data+3) == 0) |
132 | 7 | { |
133 | 7 | return detect_encoding_result<CharT>{data,encoding_kind::utf16le}; |
134 | 7 | } |
135 | 10.6k | else |
136 | 10.6k | { |
137 | 10.6k | return detect_encoding_result<CharT>{data,encoding_kind::utf8}; |
138 | 10.6k | } |
139 | 12.1k | } |
140 | | |
141 | | template <typename CharT> |
142 | | typename std::enable_if<ext_traits::is_char16<CharT>::value || ext_traits::is_char32<CharT>::value,detect_encoding_result<CharT>>::type |
143 | | detect_json_encoding(const CharT* data, std::size_t) |
144 | | { |
145 | | return detect_encoding_result<CharT>{data,encoding_kind::undetected}; |
146 | | } |
147 | | |
148 | | /* |
149 | | * Magic values subtracted from a buffer value during UTF8 conversion. |
150 | | * This table contains as many values as there might be trailing bytes |
151 | | * in a UTF-8 sequence. Source: ConvertUTF.c |
152 | | * |
153 | | * Seventh element added to satisfy GCC 16; see issue #712 |
154 | | */ |
155 | | JSONCONS_INLINE_CONSTEXPR uint32_t offsets_from_utf8[7] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL, |
156 | | 0x03C82080UL, 0xFA082080UL, 0x82082080UL, 0x00000000UL }; |
157 | | |
158 | | /* |
159 | | * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed |
160 | | * into the first byte, depending on how many bytes follow. There are |
161 | | * as many entries in this table as there are UTF-8 sequence types. |
162 | | * (I.e., one byte sequence, two byte... etc.). Remember that sequencs |
163 | | * for *legal* UTF-8 will be 4 or fewer bytes total. Source: ConvertUTF.c |
164 | | */ |
165 | | JSONCONS_INLINE_CONSTEXPR uint8_t first_byte_mark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; |
166 | | |
167 | | /* |
168 | | * Index into the table below with the first byte of a UTF-8 sequence to |
169 | | * get the number of trailing bytes that are supposed to follow it. |
170 | | * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is |
171 | | * left as-is for anyone who may want to do such conversion, which was |
172 | | * allowed in earlier algorithms. Source: ConvertUTF.c |
173 | | */ |
174 | | JSONCONS_INLINE_CONSTEXPR uint8_t trailing_bytes_for_utf8[256] = { |
175 | | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
176 | | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
177 | | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
178 | | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
179 | | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
180 | | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
181 | | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
182 | | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5 |
183 | | }; |
184 | | |
185 | | // Some fundamental constants. Source: ConvertUTF.h |
186 | | JSONCONS_INLINE_CONSTEXPR uint32_t replacement_char = 0x0000FFFD; |
187 | | JSONCONS_INLINE_CONSTEXPR uint32_t max_bmp = 0x0000FFFF; |
188 | | JSONCONS_INLINE_CONSTEXPR uint32_t max_utf16 = 0x0010FFFF; |
189 | | JSONCONS_INLINE_CONSTEXPR uint32_t max_utf32 = 0x7FFFFFFF; |
190 | | JSONCONS_INLINE_CONSTEXPR uint32_t max_legal_utf32 = 0x0010FFFF; |
191 | | |
192 | | JSONCONS_INLINE_CONSTEXPR int half_shift = 10; // used for shifting by 10 bits |
193 | | JSONCONS_INLINE_CONSTEXPR uint32_t half_base = 0x0010000UL; |
194 | | JSONCONS_INLINE_CONSTEXPR uint32_t half_mask = 0x3FFUL; |
195 | | |
196 | | JSONCONS_INLINE_CONSTEXPR uint16_t sur_high_start = 0xD800; |
197 | | JSONCONS_INLINE_CONSTEXPR uint16_t sur_high_end = 0xDBFF; |
198 | | JSONCONS_INLINE_CONSTEXPR uint16_t sur_low_start = 0xDC00; |
199 | | JSONCONS_INLINE_CONSTEXPR uint16_t sur_low_end = 0xDFFF; |
200 | | |
201 | | inline |
202 | | static bool is_continuation_byte(unsigned char ch) |
203 | 0 | { |
204 | 0 | return (ch & 0xC0) == 0x80; |
205 | 0 | } Unexecuted instantiation: fuzz_parse.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_json_encoder.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_cbor_cursor.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_csv_encoder.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_csv.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_cbor_view.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_cbor_typed_array.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_bson_encoder.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_cbor_noexcept.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_bson_parser_max.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_msgpack_decode_iterator_source.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_msgpack_parser_max.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_cbor_encoder.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_msgpack_decode_bytes_source.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_ubjson_parser_max.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_cbor.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_json_parser_max.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_ubjson.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_json_cursor.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_bson.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_ubjson_encoder.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_cbor_parser_max.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_bigint.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_msgpack.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) Unexecuted instantiation: fuzz_msgpack_encoder.cpp:jsoncons::unicode_traits::is_continuation_byte(unsigned char) |
206 | | |
207 | | inline |
208 | | bool is_high_surrogate(uint32_t ch) noexcept |
209 | 2.01M | { |
210 | 2.01M | return (ch >= sur_high_start && ch <= sur_high_end); |
211 | 2.01M | } |
212 | | |
213 | | inline |
214 | | bool is_low_surrogate(uint32_t ch) noexcept |
215 | 0 | { |
216 | 0 | return (ch >= sur_low_start && ch <= sur_low_end); |
217 | 0 | } |
218 | | |
219 | | inline |
220 | | bool is_surrogate(uint32_t ch) noexcept |
221 | 2.02M | { |
222 | 2.02M | return (ch >= sur_high_start && ch <= sur_low_end); |
223 | 2.02M | } |
224 | | |
225 | | enum class strict_flag |
226 | | { |
227 | | strict = 0, |
228 | | lenient |
229 | | }; |
230 | | |
231 | | // unicode_errc |
232 | | |
233 | | enum class unicode_errc |
234 | | { |
235 | | success = 0, |
236 | | over_long_utf8_sequence = 1, // over long utf8 sequence |
237 | | bad_continuation_byte, // expected continuation byte |
238 | | unpaired_high_surrogate, // unpaired high surrogate UTF-16 |
239 | | illegal_surrogate_value, // UTF-16 surrogate values are illegal in UTF-32 |
240 | | source_exhausted, // partial character in source, but hit end |
241 | | source_illegal // source sequence is illegal/malformed |
242 | | }; |
243 | | |
244 | | class Unicode_traits_error_category_impl_ |
245 | | : public std::error_category |
246 | | { |
247 | | public: |
248 | | virtual const char* name() const noexcept |
249 | 0 | { |
250 | 0 | return "unicode_traits conversion error"; |
251 | 0 | } |
252 | | virtual std::string message(int ev) const |
253 | 0 | { |
254 | 0 | switch (static_cast<unicode_errc>(ev)) |
255 | 0 | { |
256 | 0 | case unicode_errc::over_long_utf8_sequence: |
257 | 0 | return "Over long utf8 sequence"; |
258 | 0 | case unicode_errc::bad_continuation_byte: |
259 | 0 | return "Expected continuation byte"; |
260 | 0 | case unicode_errc::unpaired_high_surrogate: |
261 | 0 | return "Unpaired high surrogate UTF-16"; |
262 | 0 | case unicode_errc::illegal_surrogate_value: |
263 | 0 | return "UTF-16 surrogate values are illegal in UTF-32"; |
264 | 0 | case unicode_errc::source_exhausted: |
265 | 0 | return "Partial character in source, but hit end"; |
266 | 0 | case unicode_errc::source_illegal: |
267 | 0 | return "Source sequence is illegal/malformed"; |
268 | 0 | default: |
269 | 0 | return ""; |
270 | 0 | break; |
271 | 0 | } |
272 | 0 | } |
273 | | }; |
274 | | |
275 | | inline |
276 | | const std::error_category& unicode_traits_error_category() noexcept |
277 | 0 | { |
278 | 0 | static Unicode_traits_error_category_impl_ instance; |
279 | 0 | return instance; |
280 | 0 | } |
281 | | |
282 | | inline |
283 | | std::error_code make_error_code(unicode_errc result) noexcept |
284 | 0 | { |
285 | 0 | return std::error_code(static_cast<int>(result),unicode_traits_error_category()); |
286 | 0 | } |
287 | | |
288 | | } // unicode_traits |
289 | | } // namespace jsoncons |
290 | | |
291 | | namespace std { |
292 | | template<> |
293 | | struct is_error_code_enum<jsoncons::unicode_traits::unicode_errc> : public true_type |
294 | | { |
295 | | }; |
296 | | } // namespace std |
297 | | |
298 | | namespace jsoncons { |
299 | | namespace unicode_traits { |
300 | | |
301 | | // utf8 |
302 | | |
303 | | inline |
304 | | unicode_errc is_legal_utf8(const uint8_t* bytes, std::size_t length) noexcept |
305 | 32.6M | { |
306 | 32.6M | const uint8_t* it = reinterpret_cast<const uint8_t*>(bytes); |
307 | 32.6M | const uint8_t* end = it+length; |
308 | | |
309 | 32.6M | uint8_t byte; |
310 | 32.6M | switch (length) { |
311 | 7.57k | default: |
312 | 7.57k | return unicode_errc::over_long_utf8_sequence; |
313 | 108k | case 4: |
314 | 108k | if (((byte = (*--end))& 0xC0) != 0x80) |
315 | 15.7k | return unicode_errc::bad_continuation_byte; |
316 | 93.0k | JSONCONS_FALLTHROUGH; |
317 | 163k | case 3: |
318 | 163k | if (((byte = (*--end))& 0xC0) != 0x80) |
319 | 2.71k | return unicode_errc::bad_continuation_byte; |
320 | 161k | JSONCONS_FALLTHROUGH; |
321 | 488k | case 2: |
322 | 488k | if (((byte = (*--end))& 0xC0) != 0x80) |
323 | 26.3k | return unicode_errc::bad_continuation_byte; |
324 | | |
325 | 462k | switch (*it) |
326 | 462k | { |
327 | | // no fall-through in this inner switch |
328 | 22.6k | case 0xE0: if (byte < 0xA0) return unicode_errc::source_illegal; break; |
329 | 21.1k | case 0xED: if (byte > 0x9F) return unicode_errc::source_illegal; break; |
330 | 22.4k | case 0xF0: if (byte < 0x90) return unicode_errc::source_illegal; break; |
331 | 34.3k | case 0xF4: if (byte > 0x8F) return unicode_errc::source_illegal; break; |
332 | 375k | default: if (byte < 0x80) return unicode_errc::source_illegal; |
333 | 462k | } |
334 | | |
335 | 448k | JSONCONS_FALLTHROUGH; |
336 | 32.5M | case 1: |
337 | 32.5M | if (*it >= 0x80 && *it < 0xC2) |
338 | 33.6k | return unicode_errc::source_illegal; |
339 | 32.5M | break; |
340 | 32.6M | } |
341 | 32.5M | if (*it > 0xF4) |
342 | 257 | return unicode_errc::source_illegal; |
343 | | |
344 | 32.5M | return unicode_errc(); |
345 | 32.5M | } |
346 | | |
347 | | template <typename...> using void_t = void; |
348 | | |
349 | | template <typename,typename,typename = void> |
350 | | struct is_output_iterator : std::false_type {}; |
351 | | |
352 | | template <typename I,typename E> |
353 | | struct is_output_iterator<I, E, void_t< |
354 | | typename std::iterator_traits<I>::iterator_category, |
355 | | decltype(*std::declval<I>() = std::declval<E>())>> : std::true_type {}; |
356 | | |
357 | | // is_same_size fixes issue with vs2013 |
358 | | |
359 | | // primary template |
360 | | template <typename T1,typename T2,typename Enable = void> |
361 | | struct is_same_size : std::false_type |
362 | | { |
363 | | }; |
364 | | |
365 | | // specialization for non void types |
366 | | template <typename T1,typename T2> |
367 | | struct is_same_size<T1, T2,typename std::enable_if<!std::is_void<T1>::value && !std::is_void<T2>::value>::type> |
368 | | { |
369 | | static constexpr bool value = (sizeof(T1) == sizeof(T2)); |
370 | | }; |
371 | | |
372 | | // convert |
373 | | |
374 | | template <typename CharT> |
375 | | struct unicode_result |
376 | | { |
377 | | const CharT* ptr; |
378 | | unicode_errc ec; |
379 | | }; |
380 | | |
381 | | // to_codepoint |
382 | | |
383 | | template <typename CharT,typename CodepointT> |
384 | | typename std::enable_if<ext_traits::is_char8<CharT>::value && ext_traits::is_char32<CodepointT>::value, |
385 | | unicode_result<CharT>>::type |
386 | | to_codepoint(const CharT* first, const CharT* last, |
387 | | CodepointT& ch, |
388 | | strict_flag flags = strict_flag::strict) noexcept |
389 | 14.6k | { |
390 | 14.6k | const uint8_t* it = reinterpret_cast<const uint8_t*>(first); |
391 | 14.6k | const uint8_t* end = reinterpret_cast<const uint8_t*>(last); |
392 | | |
393 | 14.6k | ch = 0; |
394 | 14.6k | if (it >= end) |
395 | 0 | { |
396 | 0 | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it), unicode_errc::source_exhausted}; |
397 | 0 | } |
398 | 14.6k | unicode_errc result{}; |
399 | | |
400 | 14.6k | uint16_t extra_bytes_to_read = trailing_bytes_for_utf8[*it]; |
401 | 14.6k | if (extra_bytes_to_read >= end - it) |
402 | 0 | { |
403 | 0 | result = unicode_errc::source_exhausted; |
404 | 0 | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it), result}; |
405 | 0 | } |
406 | | // Do this check whether lenient or strict |
407 | 14.6k | if ((result=is_legal_utf8(it, extra_bytes_to_read+1)) != unicode_errc()) |
408 | 0 | { |
409 | 0 | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it), result}; |
410 | 0 | } |
411 | | // The cases all fall through. See "Note A" below. |
412 | 14.6k | switch (extra_bytes_to_read) |
413 | 14.6k | { |
414 | 0 | case 5: |
415 | 0 | ch += *it++; |
416 | 0 | ch <<= 6; |
417 | 0 | JSONCONS_FALLTHROUGH; |
418 | 0 | case 4: |
419 | 0 | ch += *it++; |
420 | 0 | ch <<= 6; |
421 | 0 | JSONCONS_FALLTHROUGH; |
422 | 0 | case 3: |
423 | 0 | ch += *it++; |
424 | 0 | ch <<= 6; |
425 | 0 | JSONCONS_FALLTHROUGH; |
426 | 0 | case 2: |
427 | 0 | ch += *it++; |
428 | 0 | ch <<= 6; |
429 | 0 | JSONCONS_FALLTHROUGH; |
430 | 0 | case 1: |
431 | 0 | ch += *it++; |
432 | 0 | ch <<= 6; |
433 | 0 | JSONCONS_FALLTHROUGH; |
434 | 14.6k | case 0: |
435 | 14.6k | ch += *it++; |
436 | 14.6k | break; |
437 | 14.6k | } |
438 | 14.6k | ch -= offsets_from_utf8[extra_bytes_to_read]; |
439 | | |
440 | 14.6k | if (ch <= max_legal_utf32) { |
441 | | /* |
442 | | * UTF-16 surrogate values are illegal in UTF-32, and anything |
443 | | * over Plane 17 (> 0x10FFFF) is illegal. |
444 | | */ |
445 | 14.6k | if (is_surrogate(ch) ) |
446 | 0 | { |
447 | 0 | if (flags == strict_flag::strict) |
448 | 0 | { |
449 | 0 | it -= (extra_bytes_to_read+1); // return to the illegal value itself |
450 | 0 | result = unicode_errc::source_illegal; |
451 | 0 | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it), result}; |
452 | 0 | } |
453 | 0 | else |
454 | 0 | { |
455 | 0 | ch = replacement_char; |
456 | 0 | } |
457 | 0 | } |
458 | 14.6k | } |
459 | 0 | else // i.e., ch > max_legal_utf32 |
460 | 0 | { |
461 | 0 | result = unicode_errc::source_illegal; |
462 | 0 | ch = replacement_char; |
463 | 0 | } |
464 | | |
465 | 14.6k | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it),result} ; |
466 | 14.6k | } |
467 | | |
468 | | template <typename CharT,typename CodepointT> |
469 | | typename std::enable_if<ext_traits::is_char16<CharT>::value && ext_traits::is_char32<CodepointT>::value, |
470 | | unicode_result<CharT>>::type |
471 | | to_codepoint(const CharT* first, const CharT* last, |
472 | | CodepointT& ch, |
473 | | strict_flag flags = strict_flag::strict) noexcept |
474 | | { |
475 | | ch = 0; |
476 | | if (first >= last) |
477 | | { |
478 | | return unicode_result<CharT>{first, unicode_errc::source_exhausted}; |
479 | | } |
480 | | unicode_errc result{}; |
481 | | |
482 | | ch = *first++; |
483 | | // If we have a surrogate pair, convert to UTF32 first. |
484 | | if (is_high_surrogate(ch)) |
485 | | { |
486 | | // If the 16 bits following the high surrogate are in the first buffer... |
487 | | if (first < last) |
488 | | { |
489 | | uint32_t ch2 = *first; |
490 | | // If ptr's a low surrogate, convert to UTF32. |
491 | | if (ch2 >= sur_low_start && ch2 <= sur_low_end ) |
492 | | { |
493 | | ch = ((ch - sur_high_start) << half_shift) |
494 | | + (ch2 - sur_low_start) + half_base; |
495 | | ++first; |
496 | | } |
497 | | else if (flags == strict_flag::strict) // ptr's an unpaired high surrogate |
498 | | { |
499 | | --first; /* return to the illegal value itself */ |
500 | | result = unicode_errc::source_illegal; |
501 | | return unicode_result<CharT>{first, result}; |
502 | | } |
503 | | } |
504 | | else |
505 | | { /* We don't have the 16 bits following the high surrogate. */ |
506 | | --first; /* return to the high surrogate */ |
507 | | result = unicode_errc::source_exhausted; |
508 | | return unicode_result<CharT>{first, result}; |
509 | | } |
510 | | } else if (flags == strict_flag::strict) { |
511 | | /* UTF-16 surrogate values are illegal in UTF-32 */ |
512 | | if (is_low_surrogate(ch) ) |
513 | | { |
514 | | --first; /* return to the illegal value itself */ |
515 | | result = unicode_errc::source_illegal; |
516 | | return unicode_result<CharT>{first, result}; |
517 | | } |
518 | | } |
519 | | |
520 | | return unicode_result<CharT>{first,result} ; |
521 | | } |
522 | | |
523 | | template <typename CharT,typename CodepointT> |
524 | | typename std::enable_if<ext_traits::is_char32<CharT>::value && ext_traits::is_char32<CodepointT>::value, |
525 | | unicode_result<CharT>>::type |
526 | | to_codepoint(const CharT* first, const CharT* last, |
527 | | CodepointT& ch, |
528 | | strict_flag flags = strict_flag::strict) noexcept |
529 | | { |
530 | | ch = 0; |
531 | | if (first >= last) |
532 | | { |
533 | | return unicode_result<CharT>{first, unicode_errc::source_exhausted}; |
534 | | } |
535 | | unicode_errc result{}; |
536 | | |
537 | | ch = *first++; |
538 | | if (flags == strict_flag::strict ) |
539 | | { |
540 | | /* UTF-16 surrogate values are illegal in UTF-32 */ |
541 | | if (is_surrogate(ch)) |
542 | | { |
543 | | --first; /* return to the illegal value itself */ |
544 | | result = unicode_errc::illegal_surrogate_value; |
545 | | return unicode_result<CharT>{first,result} ; |
546 | | } |
547 | | } |
548 | | if (!(ch <= max_legal_utf32)) |
549 | | { |
550 | | ch = replacement_char; |
551 | | result = unicode_errc::source_illegal; |
552 | | } |
553 | | |
554 | | return unicode_result<CharT>{first,result} ; |
555 | | } |
556 | | |
557 | | // convert |
558 | | |
559 | | template <typename CharT,typename Container> |
560 | | typename std::enable_if<ext_traits::is_char8<CharT>::value |
561 | | && ext_traits::is_back_insertable<Container>::value |
562 | | && ext_traits::is_char8<typename Container::value_type>::value, |
563 | | unicode_result<CharT>>::type |
564 | | convert(const CharT* data, std::size_t length, Container& target, strict_flag flags=strict_flag::strict) |
565 | | { |
566 | | (void)flags; |
567 | | |
568 | | const uint8_t* it = reinterpret_cast<const uint8_t*>(data); |
569 | | const uint8_t* end = it + length; |
570 | | |
571 | | unicode_errc result{}; |
572 | | while (it != end) |
573 | | { |
574 | | std::size_t len = trailing_bytes_for_utf8[*it] + 1; |
575 | | if (len > (std::size_t)(end - it)) |
576 | | { |
577 | | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it), unicode_errc::source_exhausted}; |
578 | | } |
579 | | if ((result=is_legal_utf8(it, len)) != unicode_errc()) |
580 | | { |
581 | | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it),result}; |
582 | | } |
583 | | |
584 | | switch (len) { |
585 | | case 4: target.push_back(*it++); |
586 | | JSONCONS_FALLTHROUGH; |
587 | | case 3: target.push_back(*it++); |
588 | | JSONCONS_FALLTHROUGH; |
589 | | case 2: target.push_back(*it++); |
590 | | JSONCONS_FALLTHROUGH; |
591 | | case 1: target.push_back(*it++); |
592 | | } |
593 | | } |
594 | | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it),result}; |
595 | | } |
596 | | |
597 | | template <typename CharT,typename Container> |
598 | | typename std::enable_if<ext_traits::is_char8<CharT>::value |
599 | | && ext_traits::is_back_insertable<Container>::value |
600 | | && ext_traits::is_char16<typename Container::value_type>::value, |
601 | | unicode_result<CharT>>::type |
602 | | convert(const CharT* data, std::size_t length, |
603 | | Container& target, |
604 | | strict_flag flags = strict_flag::strict) |
605 | | { |
606 | | unicode_errc result{}; |
607 | | |
608 | | const uint8_t* it = reinterpret_cast<const uint8_t*>(data); |
609 | | const uint8_t* end = it + length; |
610 | | |
611 | | while (it != end) |
612 | | { |
613 | | uint16_t extra_bytes_to_read = trailing_bytes_for_utf8[*it]; |
614 | | if (extra_bytes_to_read >= end - it) |
615 | | { |
616 | | result = unicode_errc::source_exhausted; |
617 | | break; |
618 | | } |
619 | | /* Do this check whether lenient or strict */ |
620 | | if ((result=is_legal_utf8(it, extra_bytes_to_read+1)) != unicode_errc()) |
621 | | { |
622 | | break; |
623 | | } |
624 | | /* |
625 | | * The cases all fall through. See "Note A" below. |
626 | | */ |
627 | | uint32_t ch = 0; |
628 | | switch (extra_bytes_to_read) { |
629 | | case 5: ch += *it++; ch <<= 6; /* remember, illegal UTF-8 */ |
630 | | JSONCONS_FALLTHROUGH; |
631 | | case 4: ch += *it++; ch <<= 6; /* remember, illegal UTF-8 */ |
632 | | JSONCONS_FALLTHROUGH; |
633 | | case 3: ch += *it++; ch <<= 6; |
634 | | JSONCONS_FALLTHROUGH; |
635 | | case 2: ch += *it++; ch <<= 6; |
636 | | JSONCONS_FALLTHROUGH; |
637 | | case 1: ch += *it++; ch <<= 6; |
638 | | JSONCONS_FALLTHROUGH; |
639 | | case 0: ch += *it++; |
640 | | break; |
641 | | } |
642 | | ch -= offsets_from_utf8[extra_bytes_to_read]; |
643 | | |
644 | | if (ch <= max_bmp) { /* Target is a character <= 0xFFFF */ |
645 | | /* UTF-16 surrogate values are illegal in UTF-32 */ |
646 | | if (is_surrogate(ch) ) |
647 | | { |
648 | | if (flags == strict_flag::strict) { |
649 | | it -= (extra_bytes_to_read+1); /* return to the illegal value itself */ |
650 | | result = unicode_errc::source_illegal; |
651 | | break; |
652 | | } else { |
653 | | target.push_back(replacement_char); |
654 | | } |
655 | | } else { |
656 | | target.push_back((uint16_t)ch); /* normal case */ |
657 | | } |
658 | | } else if (ch > max_utf16) { |
659 | | if (flags == strict_flag::strict) { |
660 | | result = unicode_errc::source_illegal; |
661 | | it -= (extra_bytes_to_read+1); /* return to the start */ |
662 | | break; /* Bail out; shouldn't continue */ |
663 | | } else { |
664 | | target.push_back(replacement_char); |
665 | | } |
666 | | } else { |
667 | | /* target is a character in range 0xFFFF - 0x10FFFF. */ |
668 | | ch -= half_base; |
669 | | target.push_back((uint16_t)((ch >> half_shift) + sur_high_start)); |
670 | | target.push_back((uint16_t)((ch & half_mask) + sur_low_start)); |
671 | | } |
672 | | } |
673 | | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it),result} ; |
674 | | } |
675 | | |
676 | | template <typename CharT,typename Container> |
677 | | typename std::enable_if<ext_traits::is_char8<CharT>::value |
678 | | && ext_traits::is_back_insertable<Container>::value |
679 | | && ext_traits::is_char32<typename Container::value_type>::value, |
680 | | unicode_result<CharT>>::type |
681 | | convert(const CharT* data, std::size_t length, |
682 | | Container& target, |
683 | | strict_flag flags = strict_flag::strict) |
684 | | { |
685 | | const uint8_t* it = reinterpret_cast<const uint8_t*>(data); |
686 | | const uint8_t* end = it + length; |
687 | | |
688 | | unicode_errc result{}; |
689 | | |
690 | | while (it < end) |
691 | | { |
692 | | uint32_t ch = 0; |
693 | | uint16_t extra_bytes_to_read = trailing_bytes_for_utf8[*it]; |
694 | | if (extra_bytes_to_read >= end - it) |
695 | | { |
696 | | result = unicode_errc::source_exhausted; |
697 | | break; |
698 | | } |
699 | | /* Do this check whether lenient or strict */ |
700 | | if ((result=is_legal_utf8(it, extra_bytes_to_read+1)) != unicode_errc()) |
701 | | { |
702 | | break; |
703 | | } |
704 | | /* |
705 | | * The cases all fall through. See "Note A" below. |
706 | | */ |
707 | | switch (extra_bytes_to_read) |
708 | | { |
709 | | case 5: |
710 | | ch += *it++; |
711 | | ch <<= 6; |
712 | | JSONCONS_FALLTHROUGH; |
713 | | case 4: |
714 | | ch += *it++; |
715 | | ch <<= 6; |
716 | | JSONCONS_FALLTHROUGH; |
717 | | case 3: |
718 | | ch += *it++; |
719 | | ch <<= 6; |
720 | | JSONCONS_FALLTHROUGH; |
721 | | case 2: |
722 | | ch += *it++; |
723 | | ch <<= 6; |
724 | | JSONCONS_FALLTHROUGH; |
725 | | case 1: |
726 | | ch += *it++; |
727 | | ch <<= 6; |
728 | | JSONCONS_FALLTHROUGH; |
729 | | case 0: |
730 | | ch += *it++; |
731 | | break; |
732 | | } |
733 | | ch -= offsets_from_utf8[extra_bytes_to_read]; |
734 | | |
735 | | if (ch <= max_legal_utf32) { |
736 | | /* |
737 | | * UTF-16 surrogate values are illegal in UTF-32, and anything |
738 | | * over Plane 17 (> 0x10FFFF) is illegal. |
739 | | */ |
740 | | if (is_surrogate(ch) ) |
741 | | { |
742 | | if (flags == strict_flag::strict) { |
743 | | it -= (extra_bytes_to_read+1); /* return to the illegal value itself */ |
744 | | result = unicode_errc::source_illegal; |
745 | | break; |
746 | | } else { |
747 | | target.push_back(replacement_char); |
748 | | } |
749 | | } else { |
750 | | target.push_back(ch); |
751 | | } |
752 | | } else { /* i.e., ch > max_legal_utf32 */ |
753 | | result = unicode_errc::source_illegal; |
754 | | target.push_back(replacement_char); |
755 | | } |
756 | | } |
757 | | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it),result} ; |
758 | | } |
759 | | |
760 | | // utf16 |
761 | | |
762 | | template <typename CharT,typename Container> |
763 | | typename std::enable_if<ext_traits::is_char16<CharT>::value |
764 | | && ext_traits::is_back_insertable<Container>::value |
765 | | && ext_traits::is_char8<typename Container::value_type>::value, |
766 | | unicode_result<CharT>>::type |
767 | | convert(const CharT* data, std::size_t length, |
768 | | Container& target, |
769 | | strict_flag flags = strict_flag::strict) { |
770 | | unicode_errc result{}; |
771 | | |
772 | | const CharT* last = data + length; |
773 | | while (data < last) { |
774 | | uint16_t bytes_to_write = 0; |
775 | | static constexpr uint32_t byteMask = 0xBF; |
776 | | static constexpr uint32_t byteMark = 0x80; |
777 | | uint32_t ch = *data++; |
778 | | /* If we have a surrogate pair, convert to uint32_t data. */ |
779 | | if (is_high_surrogate(ch)) |
780 | | { |
781 | | /* If the 16 bits following the high surrogate are in the data buffer... */ |
782 | | if (data < last) { |
783 | | uint32_t ch2 = *data; |
784 | | /* If ptr's a low surrogate, convert to uint32_t. */ |
785 | | if (ch2 >= sur_low_start && ch2 <= sur_low_end) { |
786 | | ch = ((ch - sur_high_start) << half_shift) |
787 | | + (ch2 - sur_low_start) + half_base; |
788 | | ++data; |
789 | | } else if (flags == strict_flag::strict) { /* ptr's an unpaired high surrogate */ |
790 | | --data; /* return to the illegal value itself */ |
791 | | result = unicode_errc::unpaired_high_surrogate; |
792 | | break; |
793 | | } |
794 | | } else { /* We don't have the 16 bits following the high surrogate. */ |
795 | | --data; /* return to the high surrogate */ |
796 | | result = unicode_errc::source_exhausted; |
797 | | break; |
798 | | } |
799 | | } else if (flags == strict_flag::strict) { |
800 | | /* UTF-16 surrogate values are illegal in UTF-32 */ |
801 | | if (is_low_surrogate(ch)) |
802 | | { |
803 | | --data; /* return to the illegal value itself */ |
804 | | result = unicode_errc::source_illegal; |
805 | | break; |
806 | | } |
807 | | } |
808 | | /* Figure out how many bytes the result will require */ |
809 | | if (ch < (uint32_t)0x80) { |
810 | | bytes_to_write = 1; |
811 | | } else if (ch < (uint32_t)0x800) { |
812 | | bytes_to_write = 2; |
813 | | } else if (ch < (uint32_t)0x10000) { |
814 | | bytes_to_write = 3; |
815 | | } else if (ch < (uint32_t)0x110000) { |
816 | | bytes_to_write = 4; |
817 | | } else { |
818 | | bytes_to_write = 3; |
819 | | ch = replacement_char; |
820 | | } |
821 | | |
822 | | uint8_t byte1 = 0; |
823 | | uint8_t byte2 = 0; |
824 | | uint8_t byte3 = 0; |
825 | | uint8_t byte4 = 0; |
826 | | |
827 | | switch (bytes_to_write) { // note: everything falls through |
828 | | case 4: byte4 = (uint8_t)((ch | byteMark) & byteMask); ch >>= 6; |
829 | | JSONCONS_FALLTHROUGH; |
830 | | case 3: byte3 = (uint8_t)((ch | byteMark) & byteMask); ch >>= 6; |
831 | | JSONCONS_FALLTHROUGH; |
832 | | case 2: byte2 = (uint8_t)((ch | byteMark) & byteMask); ch >>= 6; |
833 | | JSONCONS_FALLTHROUGH; |
834 | | case 1: byte1 = (uint8_t)(ch | first_byte_mark[bytes_to_write]); |
835 | | break; |
836 | | } |
837 | | switch (bytes_to_write) |
838 | | { |
839 | | case 4: |
840 | | target.push_back(byte1); |
841 | | target.push_back(byte2); |
842 | | target.push_back(byte3); |
843 | | target.push_back(byte4); |
844 | | break; |
845 | | case 3: |
846 | | target.push_back(byte1); |
847 | | target.push_back(byte2); |
848 | | target.push_back(byte3); |
849 | | break; |
850 | | case 2: |
851 | | target.push_back(byte1); |
852 | | target.push_back(byte2); |
853 | | break; |
854 | | case 1: |
855 | | target.push_back(byte1); |
856 | | break; |
857 | | } |
858 | | } |
859 | | return unicode_result<CharT>{data,result} ; |
860 | | } |
861 | | |
862 | | template <typename CharT,typename Container> |
863 | | typename std::enable_if<ext_traits::is_char16<CharT>::value |
864 | | && ext_traits::is_back_insertable<Container>::value |
865 | | && ext_traits::is_char16<typename Container::value_type>::value, |
866 | | unicode_result<CharT>>::type |
867 | | convert(const CharT* data, std::size_t length, |
868 | | Container& target, |
869 | | strict_flag flags = strict_flag::strict) |
870 | | { |
871 | | unicode_errc result{}; |
872 | | |
873 | | const CharT* last = data + length; |
874 | | while (data != last) |
875 | | { |
876 | | uint32_t ch = *data++; |
877 | | /* If we have a surrogate pair, convert to uint32_t data. */ |
878 | | if (is_high_surrogate(ch)) |
879 | | { |
880 | | /* If the 16 bits following the high surrogate are in the data buffer... */ |
881 | | if (data < last) { |
882 | | uint32_t ch2 = *data; |
883 | | /* If ptr's a low surrogate, */ |
884 | | if (ch2 >= sur_low_start && ch2 <= sur_low_end) { |
885 | | target.push_back((uint16_t)ch); |
886 | | target.push_back((uint16_t)ch2); |
887 | | ++data; |
888 | | } else if (flags == strict_flag::strict) { /* ptr's an unpaired high surrogate */ |
889 | | --data; /* return to the illegal value itself */ |
890 | | result = unicode_errc::unpaired_high_surrogate; |
891 | | break; |
892 | | } |
893 | | } else { /* We don't have the 16 bits following the high surrogate. */ |
894 | | --data; /* return to the high surrogate */ |
895 | | result = unicode_errc::source_exhausted; |
896 | | break; |
897 | | } |
898 | | } else if (is_low_surrogate(ch)) |
899 | | { |
900 | | // illegal leading low surrogate |
901 | | if (flags == strict_flag::strict) { |
902 | | --data; /* return to the illegal value itself */ |
903 | | result = unicode_errc::source_illegal; |
904 | | break; |
905 | | } |
906 | | else |
907 | | { |
908 | | target.push_back((uint16_t)ch); |
909 | | } |
910 | | } |
911 | | else |
912 | | { |
913 | | target.push_back((uint16_t)ch); |
914 | | } |
915 | | } |
916 | | return unicode_result<CharT>{data,result} ; |
917 | | } |
918 | | |
919 | | template <typename CharT,typename Container> |
920 | | typename std::enable_if<ext_traits::is_char16<CharT>::value |
921 | | && ext_traits::is_back_insertable<Container>::value |
922 | | && ext_traits::is_char32<typename Container::value_type>::value, |
923 | | unicode_result<CharT>>::type |
924 | | convert(const CharT* data, std::size_t length, |
925 | | Container& target, |
926 | | strict_flag flags = strict_flag::strict) |
927 | | { |
928 | | unicode_errc result{}; |
929 | | |
930 | | const CharT* last = data + length; |
931 | | while (data != last) |
932 | | { |
933 | | uint32_t ch = *data++; |
934 | | /* If we have a surrogate pair, convert to UTF32 data. */ |
935 | | if (is_high_surrogate(ch)) |
936 | | { |
937 | | /* If the 16 bits following the high surrogate are in the data buffer... */ |
938 | | if (data < last) { |
939 | | uint32_t ch2 = *data; |
940 | | /* If ptr's a low surrogate, convert to UTF32. */ |
941 | | if (ch2 >= sur_low_start && ch2 <= sur_low_end ) |
942 | | { |
943 | | ch = ((ch - sur_high_start) << half_shift) |
944 | | + (ch2 - sur_low_start) + half_base; |
945 | | ++data; |
946 | | } else if (flags == strict_flag::strict) { /* ptr's an unpaired high surrogate */ |
947 | | --data; /* return to the illegal value itself */ |
948 | | result = unicode_errc::source_illegal; |
949 | | break; |
950 | | } |
951 | | } else { /* We don't have the 16 bits following the high surrogate. */ |
952 | | --data; /* return to the high surrogate */ |
953 | | result = unicode_errc::source_exhausted; |
954 | | break; |
955 | | } |
956 | | } else if (flags == strict_flag::strict) { |
957 | | /* UTF-16 surrogate values are illegal in UTF-32 */ |
958 | | if (is_low_surrogate(ch) ) |
959 | | { |
960 | | --data; /* return to the illegal value itself */ |
961 | | result = unicode_errc::source_illegal; |
962 | | break; |
963 | | } |
964 | | } |
965 | | target.push_back(ch); |
966 | | } |
967 | | return unicode_result<CharT>{data,result} ; |
968 | | } |
969 | | |
970 | | // utf32 |
971 | | |
972 | | template <typename CharT,typename Container> |
973 | | typename std::enable_if<ext_traits::is_char32<CharT>::value |
974 | | && ext_traits::is_back_insertable<Container>::value |
975 | | && ext_traits::is_char8<typename Container::value_type>::value, |
976 | | unicode_result<CharT>>::type |
977 | | convert(const CharT* data, std::size_t length, |
978 | | Container& target, |
979 | | strict_flag flags = strict_flag::strict) |
980 | 2.01M | { |
981 | 2.01M | unicode_errc result{}; |
982 | 2.01M | const CharT* last = data + length; |
983 | 3.97M | while (data < last) |
984 | 2.01M | { |
985 | 2.01M | uint16_t bytes_to_write = 0; |
986 | 2.01M | static constexpr uint32_t byteMask = 0xBF; |
987 | 2.01M | static constexpr uint32_t byteMark = 0x80; |
988 | 2.01M | uint32_t ch = *data++; |
989 | 2.01M | if (flags == strict_flag::strict ) |
990 | 2.01M | { |
991 | | /* UTF-16 surrogate values are illegal in UTF-32 */ |
992 | 2.01M | if (is_surrogate(ch)) |
993 | 43.9k | { |
994 | 43.9k | --data; /* return to the illegal value itself */ |
995 | 43.9k | result = unicode_errc::illegal_surrogate_value; |
996 | 43.9k | break; |
997 | 43.9k | } |
998 | 2.01M | } |
999 | | /* |
1000 | | * Figure out how many bytes the result will require. Turn any |
1001 | | * illegally large UTF32 things (> Plane 17) into replacement chars. |
1002 | | */ |
1003 | 1.96M | if (ch < (uint32_t)0x80) { bytes_to_write = 1; |
1004 | 1.95M | } else if (ch < (uint32_t)0x800) { bytes_to_write = 2; |
1005 | 1.94M | } else if (ch < (uint32_t)0x10000) { bytes_to_write = 3; |
1006 | 975k | } else if (ch <= max_legal_utf32) { bytes_to_write = 4; |
1007 | 970k | } else { |
1008 | 0 | bytes_to_write = 3; |
1009 | 0 | ch = replacement_char; |
1010 | 0 | result = unicode_errc::source_illegal; |
1011 | 0 | } |
1012 | | |
1013 | 1.96M | uint8_t byte1 = 0; |
1014 | 1.96M | uint8_t byte2 = 0; |
1015 | 1.96M | uint8_t byte3 = 0; |
1016 | 1.96M | uint8_t byte4 = 0; |
1017 | | |
1018 | 1.96M | switch (bytes_to_write) { |
1019 | 970k | case 4: |
1020 | 970k | byte4 = (uint8_t)((ch | byteMark) & byteMask); ch >>= 6; |
1021 | 970k | JSONCONS_FALLTHROUGH; |
1022 | 1.94M | case 3: |
1023 | 1.94M | byte3 = (uint8_t)((ch | byteMark) & byteMask); ch >>= 6; |
1024 | 1.94M | JSONCONS_FALLTHROUGH; |
1025 | 1.95M | case 2: |
1026 | 1.95M | byte2 = (uint8_t)((ch | byteMark) & byteMask); ch >>= 6; |
1027 | 1.95M | JSONCONS_FALLTHROUGH; |
1028 | 1.96M | case 1: |
1029 | 1.96M | byte1 = (uint8_t) (ch | first_byte_mark[bytes_to_write]); |
1030 | 1.96M | break; |
1031 | 1.96M | } |
1032 | | |
1033 | 1.96M | switch (bytes_to_write) |
1034 | 1.96M | { |
1035 | 970k | case 4: |
1036 | 970k | target.push_back(byte1); |
1037 | 970k | target.push_back(byte2); |
1038 | 970k | target.push_back(byte3); |
1039 | 970k | target.push_back(byte4); |
1040 | 970k | break; |
1041 | 975k | case 3: |
1042 | 975k | target.push_back(byte1); |
1043 | 975k | target.push_back(byte2); |
1044 | 975k | target.push_back(byte3); |
1045 | 975k | break; |
1046 | 6.56k | case 2: |
1047 | 6.56k | target.push_back(byte1); |
1048 | 6.56k | target.push_back(byte2); |
1049 | 6.56k | break; |
1050 | 14.3k | case 1: |
1051 | 14.3k | target.push_back(byte1); |
1052 | 14.3k | break; |
1053 | 1.96M | } |
1054 | 1.96M | } |
1055 | 2.01M | return unicode_result<CharT>{data,result} ; |
1056 | 2.01M | } std::__1::enable_if<(ext_traits::is_char32<unsigned int>::value&&ext_traits::is_back_insertable<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::value)&&ext_traits::is_char8<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::value_type>::value, jsoncons::unicode_traits::unicode_result<unsigned int> >::type jsoncons::unicode_traits::convert<unsigned int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(unsigned int const*, unsigned long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, jsoncons::unicode_traits::strict_flag) Line | Count | Source | 980 | 2.01M | { | 981 | 2.01M | unicode_errc result{}; | 982 | 2.01M | const CharT* last = data + length; | 983 | 3.97M | while (data < last) | 984 | 2.01M | { | 985 | 2.01M | uint16_t bytes_to_write = 0; | 986 | 2.01M | static constexpr uint32_t byteMask = 0xBF; | 987 | 2.01M | static constexpr uint32_t byteMark = 0x80; | 988 | 2.01M | uint32_t ch = *data++; | 989 | 2.01M | if (flags == strict_flag::strict ) | 990 | 2.01M | { | 991 | | /* UTF-16 surrogate values are illegal in UTF-32 */ | 992 | 2.01M | if (is_surrogate(ch)) | 993 | 43.9k | { | 994 | 43.9k | --data; /* return to the illegal value itself */ | 995 | 43.9k | result = unicode_errc::illegal_surrogate_value; | 996 | 43.9k | break; | 997 | 43.9k | } | 998 | 2.01M | } | 999 | | /* | 1000 | | * Figure out how many bytes the result will require. Turn any | 1001 | | * illegally large UTF32 things (> Plane 17) into replacement chars. | 1002 | | */ | 1003 | 1.96M | if (ch < (uint32_t)0x80) { bytes_to_write = 1; | 1004 | 1.95M | } else if (ch < (uint32_t)0x800) { bytes_to_write = 2; | 1005 | 1.94M | } else if (ch < (uint32_t)0x10000) { bytes_to_write = 3; | 1006 | 975k | } else if (ch <= max_legal_utf32) { bytes_to_write = 4; | 1007 | 970k | } else { | 1008 | 0 | bytes_to_write = 3; | 1009 | 0 | ch = replacement_char; | 1010 | 0 | result = unicode_errc::source_illegal; | 1011 | 0 | } | 1012 | | | 1013 | 1.96M | uint8_t byte1 = 0; | 1014 | 1.96M | uint8_t byte2 = 0; | 1015 | 1.96M | uint8_t byte3 = 0; | 1016 | 1.96M | uint8_t byte4 = 0; | 1017 | | | 1018 | 1.96M | switch (bytes_to_write) { | 1019 | 970k | case 4: | 1020 | 970k | byte4 = (uint8_t)((ch | byteMark) & byteMask); ch >>= 6; | 1021 | 970k | JSONCONS_FALLTHROUGH; | 1022 | 1.94M | case 3: | 1023 | 1.94M | byte3 = (uint8_t)((ch | byteMark) & byteMask); ch >>= 6; | 1024 | 1.94M | JSONCONS_FALLTHROUGH; | 1025 | 1.95M | case 2: | 1026 | 1.95M | byte2 = (uint8_t)((ch | byteMark) & byteMask); ch >>= 6; | 1027 | 1.95M | JSONCONS_FALLTHROUGH; | 1028 | 1.96M | case 1: | 1029 | 1.96M | byte1 = (uint8_t) (ch | first_byte_mark[bytes_to_write]); | 1030 | 1.96M | break; | 1031 | 1.96M | } | 1032 | | | 1033 | 1.96M | switch (bytes_to_write) | 1034 | 1.96M | { | 1035 | 970k | case 4: | 1036 | 970k | target.push_back(byte1); | 1037 | 970k | target.push_back(byte2); | 1038 | 970k | target.push_back(byte3); | 1039 | 970k | target.push_back(byte4); | 1040 | 970k | break; | 1041 | 975k | case 3: | 1042 | 975k | target.push_back(byte1); | 1043 | 975k | target.push_back(byte2); | 1044 | 975k | target.push_back(byte3); | 1045 | 975k | break; | 1046 | 6.56k | case 2: | 1047 | 6.56k | target.push_back(byte1); | 1048 | 6.56k | target.push_back(byte2); | 1049 | 6.56k | break; | 1050 | 14.3k | case 1: | 1051 | 14.3k | target.push_back(byte1); | 1052 | 14.3k | break; | 1053 | 1.96M | } | 1054 | 1.96M | } | 1055 | 2.01M | return unicode_result<CharT>{data,result} ; | 1056 | 2.01M | } |
Unexecuted instantiation: std::__1::enable_if<(ext_traits::is_char32<wchar_t>::value&&ext_traits::is_back_insertable<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::value)&&ext_traits::is_char8<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::value_type>::value, jsoncons::unicode_traits::unicode_result<wchar_t> >::type jsoncons::unicode_traits::convert<wchar_t, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(wchar_t const*, unsigned long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, jsoncons::unicode_traits::strict_flag) |
1057 | | |
1058 | | template <typename CharT,typename Container> |
1059 | | typename std::enable_if<ext_traits::is_char32<CharT>::value |
1060 | | && ext_traits::is_back_insertable<Container>::value |
1061 | | && ext_traits::is_char16<typename Container::value_type>::value, |
1062 | | unicode_result<CharT>>::type |
1063 | | convert(const CharT* data, std::size_t length, |
1064 | | Container& target, |
1065 | | strict_flag flags = strict_flag::strict) |
1066 | | { |
1067 | | unicode_errc result{}; |
1068 | | |
1069 | | const CharT* last = data + length; |
1070 | | while (data != last) |
1071 | | { |
1072 | | uint32_t ch = *data++; |
1073 | | if (ch <= max_bmp) { /* Target is a character <= 0xFFFF */ |
1074 | | /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */ |
1075 | | if (is_surrogate(ch) ) |
1076 | | { |
1077 | | if (flags == strict_flag::strict) { |
1078 | | --data; /* return to the illegal value itself */ |
1079 | | result = unicode_errc::source_illegal; |
1080 | | break; |
1081 | | } else { |
1082 | | target.push_back(replacement_char); |
1083 | | } |
1084 | | } else { |
1085 | | target.push_back((uint16_t)ch); /* normal case */ |
1086 | | } |
1087 | | } else if (ch > max_legal_utf32) { |
1088 | | if (flags == strict_flag::strict) { |
1089 | | result = unicode_errc::source_illegal; |
1090 | | } else { |
1091 | | target.push_back(replacement_char); |
1092 | | } |
1093 | | } else { |
1094 | | /* target is a character in range 0xFFFF - 0x10FFFF. */ |
1095 | | ch -= half_base; |
1096 | | target.push_back((uint16_t)((ch >> half_shift) + sur_high_start)); |
1097 | | target.push_back((uint16_t)((ch & half_mask) + sur_low_start)); |
1098 | | } |
1099 | | } |
1100 | | return unicode_result<CharT>{data,result} ; |
1101 | | } |
1102 | | |
1103 | | template <typename CharT,typename Container> |
1104 | | typename std::enable_if<ext_traits::is_char32<CharT>::value |
1105 | | && ext_traits::is_back_insertable<Container>::value |
1106 | | && ext_traits::is_char32<typename Container::value_type>::value, |
1107 | | unicode_result<CharT>>::type |
1108 | | convert(const CharT* data, std::size_t length, |
1109 | | Container& target, |
1110 | | strict_flag flags = strict_flag::strict) |
1111 | 0 | { |
1112 | 0 | unicode_errc result{}; |
1113 | 0 |
|
1114 | 0 | const CharT* last = data + length; |
1115 | 0 | while (data != last) |
1116 | 0 | { |
1117 | 0 | uint32_t ch = *data++; |
1118 | 0 | if (flags == strict_flag::strict ) |
1119 | 0 | { |
1120 | 0 | /* UTF-16 surrogate values are illegal in UTF-32 */ |
1121 | 0 | if (is_surrogate(ch)) |
1122 | 0 | { |
1123 | 0 | --data; /* return to the illegal value itself */ |
1124 | 0 | result = unicode_errc::illegal_surrogate_value; |
1125 | 0 | break; |
1126 | 0 | } |
1127 | 0 | } |
1128 | 0 | if (ch <= max_legal_utf32) |
1129 | 0 | { |
1130 | 0 | target.push_back(ch); |
1131 | 0 | } |
1132 | 0 | else |
1133 | 0 | { |
1134 | 0 | target.push_back(replacement_char); |
1135 | 0 | result = unicode_errc::source_illegal; |
1136 | 0 | } |
1137 | 0 | } |
1138 | 0 | return unicode_result<CharT>{data,result} ; |
1139 | 0 | } |
1140 | | |
1141 | | // validate |
1142 | | |
1143 | | template <typename CharT> |
1144 | | typename std::enable_if<ext_traits::is_char8<CharT>::value, |
1145 | | unicode_result<CharT>>::type |
1146 | | validate(const CharT* data, std::size_t length) noexcept |
1147 | 56.0M | { |
1148 | 56.0M | const uint8_t* it = reinterpret_cast<const uint8_t*>(data); |
1149 | 56.0M | const uint8_t* end = it + length; |
1150 | | |
1151 | 56.0M | unicode_errc result{}; |
1152 | 116M | while (it != end) |
1153 | 60.8M | { |
1154 | 60.8M | if ((end - it) >= 8) |
1155 | 28.7M | { |
1156 | 199M | JSONCONS_REPEAT8({if (JSONCONS_LIKELY((*it & 0x80) == 0)) ++it; else goto non_ascii;}) |
1157 | 28.2M | continue; |
1158 | 199M | } |
1159 | 32.6M | non_ascii: |
1160 | 32.6M | const std::size_t len = static_cast<std::size_t>(trailing_bytes_for_utf8[*it]) + 1; |
1161 | 32.6M | if (len > (std::size_t)(end - it)) |
1162 | 6.27k | { |
1163 | 6.27k | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it), unicode_errc::source_exhausted}; |
1164 | 6.27k | } |
1165 | 32.6M | if ((result=is_legal_utf8(it, len)) != unicode_errc()) |
1166 | 100k | { |
1167 | 100k | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it),result} ; |
1168 | 100k | } |
1169 | 32.5M | it += len; |
1170 | 32.5M | } |
1171 | 55.9M | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it),result} ; |
1172 | 56.0M | } std::__1::enable_if<ext_traits::is_char8<char>::value, jsoncons::unicode_traits::unicode_result<char> >::type jsoncons::unicode_traits::validate<char>(char const*, unsigned long) Line | Count | Source | 1147 | 30.6M | { | 1148 | 30.6M | const uint8_t* it = reinterpret_cast<const uint8_t*>(data); | 1149 | 30.6M | const uint8_t* end = it + length; | 1150 | | | 1151 | 30.6M | unicode_errc result{}; | 1152 | 82.8M | while (it != end) | 1153 | 52.3M | { | 1154 | 52.3M | if ((end - it) >= 8) | 1155 | 25.5M | { | 1156 | 177M | JSONCONS_REPEAT8({if (JSONCONS_LIKELY((*it & 0x80) == 0)) ++it; else goto non_ascii;}) | 1157 | 25.1M | continue; | 1158 | 177M | } | 1159 | 27.2M | non_ascii: | 1160 | 27.2M | const std::size_t len = static_cast<std::size_t>(trailing_bytes_for_utf8[*it]) + 1; | 1161 | 27.2M | if (len > (std::size_t)(end - it)) | 1162 | 6.06k | { | 1163 | 6.06k | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it), unicode_errc::source_exhausted}; | 1164 | 6.06k | } | 1165 | 27.2M | if ((result=is_legal_utf8(it, len)) != unicode_errc()) | 1166 | 99.2k | { | 1167 | 99.2k | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it),result} ; | 1168 | 99.2k | } | 1169 | 27.1M | it += len; | 1170 | 27.1M | } | 1171 | 30.5M | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it),result} ; | 1172 | 30.6M | } |
std::__1::enable_if<ext_traits::is_char8<unsigned char>::value, jsoncons::unicode_traits::unicode_result<unsigned char> >::type jsoncons::unicode_traits::validate<unsigned char>(unsigned char const*, unsigned long) Line | Count | Source | 1147 | 25.3M | { | 1148 | 25.3M | const uint8_t* it = reinterpret_cast<const uint8_t*>(data); | 1149 | 25.3M | const uint8_t* end = it + length; | 1150 | | | 1151 | 25.3M | unicode_errc result{}; | 1152 | 33.8M | while (it != end) | 1153 | 8.50M | { | 1154 | 8.50M | if ((end - it) >= 8) | 1155 | 3.14M | { | 1156 | 21.8M | JSONCONS_REPEAT8({if (JSONCONS_LIKELY((*it & 0x80) == 0)) ++it; else goto non_ascii;}) | 1157 | 3.09M | continue; | 1158 | 21.8M | } | 1159 | 5.40M | non_ascii: | 1160 | 5.40M | const std::size_t len = static_cast<std::size_t>(trailing_bytes_for_utf8[*it]) + 1; | 1161 | 5.40M | if (len > (std::size_t)(end - it)) | 1162 | 210 | { | 1163 | 210 | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it), unicode_errc::source_exhausted}; | 1164 | 210 | } | 1165 | 5.40M | if ((result=is_legal_utf8(it, len)) != unicode_errc()) | 1166 | 1.06k | { | 1167 | 1.06k | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it),result} ; | 1168 | 1.06k | } | 1169 | 5.40M | it += len; | 1170 | 5.40M | } | 1171 | 25.3M | return unicode_result<CharT>{reinterpret_cast<const CharT*>(it),result} ; | 1172 | 25.3M | } |
Unexecuted instantiation: std::__1::enable_if<ext_traits::is_char8<char>::value, jsoncons::unicode_traits::unicode_result<char> >::type jsoncons::unicode_traits::validate<char>(char const*, unsigned long) |
1173 | | |
1174 | | // utf16 |
1175 | | |
1176 | | template <typename CharT> |
1177 | | typename std::enable_if<ext_traits::is_char16<CharT>::value, |
1178 | | unicode_result<CharT>>::type |
1179 | | validate(const CharT* data, std::size_t length) noexcept |
1180 | | { |
1181 | | unicode_errc result{}; |
1182 | | |
1183 | | const CharT* last = data + length; |
1184 | | while (data != last) |
1185 | | { |
1186 | | uint32_t ch = *data++; |
1187 | | /* If we have a surrogate pair, validate to uint32_t data. */ |
1188 | | if (is_high_surrogate(ch)) |
1189 | | { |
1190 | | /* If the 16 bits following the high surrogate are in the data buffer... */ |
1191 | | if (data < last) { |
1192 | | uint32_t ch2 = *data; |
1193 | | /* If ptr's a low surrogate, */ |
1194 | | if (ch2 >= sur_low_start && ch2 <= sur_low_end) { |
1195 | | ++data; |
1196 | | } else { |
1197 | | --data; /* return to the illegal value itself */ |
1198 | | result = unicode_errc::unpaired_high_surrogate; |
1199 | | break; |
1200 | | } |
1201 | | } |
1202 | | else // We don't have the 16 bits following the high surrogate. |
1203 | | { |
1204 | | --data; /* return to the high surrogate */ |
1205 | | result = unicode_errc::source_exhausted; |
1206 | | break; |
1207 | | } |
1208 | | } |
1209 | | else if (is_low_surrogate(ch)) |
1210 | | { |
1211 | | /* UTF-16 surrogate values are illegal in UTF-32 */ |
1212 | | --data; /* return to the illegal value itself */ |
1213 | | result = unicode_errc::source_illegal; |
1214 | | break; |
1215 | | } |
1216 | | } |
1217 | | return unicode_result<CharT>{data,result} ; |
1218 | | } |
1219 | | |
1220 | | // utf32 |
1221 | | |
1222 | | template <typename CharT> |
1223 | | typename std::enable_if<ext_traits::is_char32<CharT>::value, |
1224 | | unicode_result<CharT>>::type |
1225 | | validate(const CharT* data, std::size_t length) noexcept |
1226 | 0 | { |
1227 | 0 | unicode_errc result{}; |
1228 | 0 |
|
1229 | 0 | const CharT* last = data + length; |
1230 | 0 | while (data != last) |
1231 | 0 | { |
1232 | 0 | uint32_t ch = *data++; |
1233 | 0 | /* UTF-16 surrogate values are illegal in UTF-32 */ |
1234 | 0 | if (is_surrogate(ch)) |
1235 | 0 | { |
1236 | 0 | --data; /* return to the illegal value itself */ |
1237 | 0 | result = unicode_errc::illegal_surrogate_value; |
1238 | 0 | break; |
1239 | 0 | } |
1240 | 0 | if (!(ch <= max_legal_utf32)) |
1241 | 0 | { |
1242 | 0 | result = unicode_errc::source_illegal; |
1243 | 0 | } |
1244 | 0 | } |
1245 | 0 | return unicode_result<CharT>{data, result} ; |
1246 | 0 | } |
1247 | | |
1248 | | enum class encoding {u8,u16le,u16be,u32le,u32be,undetected}; |
1249 | | |
1250 | | template <typename Iterator> |
1251 | | struct determine_encoding_result |
1252 | | { |
1253 | | Iterator it; |
1254 | | encoding ec; |
1255 | | }; |
1256 | | |
1257 | | template <typename Iterator> |
1258 | | typename std::enable_if<std::is_integral<typename std::iterator_traits<Iterator>::value_type>::value && sizeof(typename std::iterator_traits<Iterator>::value_type) == sizeof(uint8_t), |
1259 | | determine_encoding_result<Iterator>>::type |
1260 | | detect_encoding(Iterator first, Iterator last) noexcept |
1261 | | { |
1262 | | Iterator it1 = first; |
1263 | | if (std::distance(first,last) < 4) |
1264 | | { |
1265 | | if (std::distance(first,last) == 3) |
1266 | | { |
1267 | | Iterator it2 = ++first; |
1268 | | Iterator it3 = ++first; |
1269 | | if (static_cast<uint8_t>(*it1) == 0xEF && static_cast<uint8_t>(*it2) == 0xBB && static_cast<uint8_t>(*it3) == 0xBF) |
1270 | | { |
1271 | | return determine_encoding_result<Iterator>{last,encoding::u8}; |
1272 | | } |
1273 | | } |
1274 | | return determine_encoding_result<Iterator>{it1,encoding::undetected}; |
1275 | | } |
1276 | | else |
1277 | | { |
1278 | | Iterator it2 = ++first; |
1279 | | Iterator it3 = ++first; |
1280 | | Iterator it4 = ++first; |
1281 | | |
1282 | | uint32_t bom = static_cast<uint8_t>(*it1) | (static_cast<uint8_t>(*it2) << 8) | (static_cast<uint8_t>(*it3) << 16) | (static_cast<uint8_t>(*it4) << 24); |
1283 | | if (bom == 0xFFFE0000) |
1284 | | { |
1285 | | return determine_encoding_result<Iterator>{it4++,encoding::u32be}; |
1286 | | } |
1287 | | else if (bom == 0x0000FEFF) |
1288 | | { |
1289 | | return determine_encoding_result<Iterator>{first,encoding::u32le}; |
1290 | | } |
1291 | | else if ((bom & 0xFFFF) == 0xFFFE) |
1292 | | { |
1293 | | return determine_encoding_result<Iterator>{it3,encoding::u16be}; |
1294 | | } |
1295 | | else if ((bom & 0xFFFF) == 0xFEFF) |
1296 | | { |
1297 | | return determine_encoding_result<Iterator>{it3,encoding::u16le}; |
1298 | | } |
1299 | | else if ((bom & 0xFFFFFF) == 0xBFBBEF) |
1300 | | { |
1301 | | return determine_encoding_result<Iterator>{it4,encoding::u8}; |
1302 | | } |
1303 | | else |
1304 | | { |
1305 | | uint32_t pattern = (static_cast<uint8_t>(*it1) ? 1 : 0) | (static_cast<uint8_t>(*it2) ? 2 : 0) | (static_cast<uint8_t>(*it3) ? 4 : 0) | (static_cast<uint8_t>(*it4) ? 8 : 0); |
1306 | | switch (pattern) { |
1307 | | case 0x08: |
1308 | | return determine_encoding_result<Iterator>{it1,encoding::u32be}; |
1309 | | case 0x0A: |
1310 | | return determine_encoding_result<Iterator>{it1,encoding::u16be}; |
1311 | | case 0x01: |
1312 | | return determine_encoding_result<Iterator>{it1,encoding::u32le}; |
1313 | | case 0x05: |
1314 | | return determine_encoding_result<Iterator>{it1,encoding::u16le}; |
1315 | | case 0x0F: |
1316 | | return determine_encoding_result<Iterator>{it1,encoding::u8}; |
1317 | | default: |
1318 | | return determine_encoding_result<Iterator>{it1,encoding::undetected}; |
1319 | | } |
1320 | | } |
1321 | | } |
1322 | | } |
1323 | | |
1324 | | // count_codepoints |
1325 | | |
1326 | | template <typename CharT> |
1327 | | typename std::enable_if<ext_traits::is_char8<CharT>::value || ext_traits::is_char16<CharT>::value || ext_traits::is_char32<CharT>::value, std::size_t>::type |
1328 | | count_codepoints(const CharT* data, std::size_t length, |
1329 | | strict_flag flags = strict_flag::strict) noexcept |
1330 | | { |
1331 | | unicode_errc ec = unicode_errc(); |
1332 | | |
1333 | | std::size_t count = 0; |
1334 | | const CharT* it = data; |
1335 | | const CharT* end = data + length; |
1336 | | |
1337 | | for (; it < end; ++count) |
1338 | | { |
1339 | | uint32_t cp = 0; |
1340 | | auto r = to_codepoint(it, end, cp, flags); |
1341 | | if (r.ec != unicode_errc()) |
1342 | | { |
1343 | | ec = r.ec; |
1344 | | break; |
1345 | | } |
1346 | | it = r.ptr; |
1347 | | } |
1348 | | return ec == unicode_errc() && it == end ? count : 0; |
1349 | | } |
1350 | | |
1351 | | } // unicode_traits |
1352 | | } // namespace jsoncons |
1353 | | |
1354 | | #endif //JSONCONS_UTILITY_UNICODE_TRAITS_HPP |
1355 | | |