simdutf 6.4.0
Unicode at GB/s.
Loading...
Searching...
No Matches
error.h
1#ifndef SIMDUTF_ERROR_H
2#define SIMDUTF_ERROR_H
3namespace simdutf {
4
5enum error_code {
6 SUCCESS = 0,
7 HEADER_BITS, // Any byte must have fewer than 5 header bits.
8 TOO_SHORT, // The leading byte must be followed by N-1 continuation bytes,
9 // where N is the UTF-8 character length This is also the error
10 // when the input is truncated.
11 TOO_LONG, // We either have too many consecutive continuation bytes or the
12 // string starts with a continuation byte.
13 OVERLONG, // The decoded character must be above U+7F for two-byte characters,
14 // U+7FF for three-byte characters, and U+FFFF for four-byte
15 // characters.
16 TOO_LARGE, // The decoded character must be less than or equal to
17 // U+10FFFF,less than or equal than U+7F for ASCII OR less than
18 // equal than U+FF for Latin1
19 SURROGATE, // The decoded character must be not be in U+D800...DFFF (UTF-8 or
20 // UTF-32) OR a high surrogate must be followed by a low surrogate
21 // and a low surrogate must be preceded by a high surrogate
22 // (UTF-16) OR there must be no surrogate at all (Latin1)
23 INVALID_BASE64_CHARACTER, // Found a character that cannot be part of a valid
24 // base64 string. This may include a misplaced
25 // padding character ('=').
26 BASE64_INPUT_REMAINDER, // The base64 input terminates with a single
27 // character, excluding padding (=).
28 BASE64_EXTRA_BITS, // The base64 input terminates with non-zero
29 // padding bits.
30 OUTPUT_BUFFER_TOO_SMALL, // The provided buffer is too small.
31 OTHER // Not related to validation/transcoding.
32};
33
34struct result {
35 error_code error;
36 size_t count; // In case of error, indicates the position of the error. In
37 // case of success, indicates the number of code units
38 // validated/written.
39
40 simdutf_really_inline result() : error{error_code::SUCCESS}, count{0} {}
41
42 simdutf_really_inline result(error_code err, size_t pos)
43 : error{err}, count{pos} {}
44
45 simdutf_really_inline bool is_ok() const {
46 return error == error_code::SUCCESS;
47 }
48
49 simdutf_really_inline bool is_err() const {
50 return error != error_code::SUCCESS;
51 }
52};
53
55 error_code error;
56 size_t input_count;
57 size_t output_count;
58
59 simdutf_really_inline full_result()
60 : error{error_code::SUCCESS}, input_count{0}, output_count{0} {}
61
62 simdutf_really_inline full_result(error_code err, size_t pos_in,
63 size_t pos_out)
64 : error{err}, input_count{pos_in}, output_count{pos_out} {}
65
66 simdutf_really_inline operator result() const noexcept {
67 if (error == error_code::SUCCESS ||
68 error == error_code::BASE64_INPUT_REMAINDER) {
69 return result{error, output_count};
70 } else {
71 return result{error, input_count};
72 }
73 }
74};
75
76} // namespace simdutf
77#endif