/work/include/simdutf/error.h
Line | Count | Source |
1 | | #ifndef SIMDUTF_ERROR_H |
2 | | #define SIMDUTF_ERROR_H |
3 | | namespace simdutf { |
4 | | |
5 | | enum 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) |
21 | | // OR |
22 | | // a high surrogate must be followed by a low surrogate |
23 | | // and a low surrogate must be preceded by a high surrogate |
24 | | // (UTF-16) |
25 | | // OR |
26 | | // there must be no surrogate at all and one is |
27 | | // found (Latin1 functions) |
28 | | // OR |
29 | | // *specifically* for the function |
30 | | // utf8_length_from_utf16_with_replacement, a surrogate (whether |
31 | | // in error or not) has been found (I.e., whether we are in the |
32 | | // Basic Multilingual Plane or not). |
33 | | INVALID_BASE64_CHARACTER, // Found a character that cannot be part of a valid |
34 | | // base64 string. This may include a misplaced |
35 | | // padding character ('='). |
36 | | BASE64_INPUT_REMAINDER, // The base64 input terminates with a single |
37 | | // character, excluding padding (=). It is also used |
38 | | // in strict mode when padding is not adequate. |
39 | | BASE64_EXTRA_BITS, // The base64 input terminates with non-zero |
40 | | // padding bits. |
41 | | OUTPUT_BUFFER_TOO_SMALL, // The provided buffer is too small. |
42 | | OTHER // Not related to validation/transcoding. |
43 | | }; |
44 | | #if SIMDUTF_CPLUSPLUS17 |
45 | 0 | inline std::string_view error_to_string(error_code code) noexcept { |
46 | 0 | switch (code) { |
47 | 0 | case SUCCESS: |
48 | 0 | return "SUCCESS"; |
49 | 0 | case HEADER_BITS: |
50 | 0 | return "HEADER_BITS"; |
51 | 0 | case TOO_SHORT: |
52 | 0 | return "TOO_SHORT"; |
53 | 0 | case TOO_LONG: |
54 | 0 | return "TOO_LONG"; |
55 | 0 | case OVERLONG: |
56 | 0 | return "OVERLONG"; |
57 | 0 | case TOO_LARGE: |
58 | 0 | return "TOO_LARGE"; |
59 | 0 | case SURROGATE: |
60 | 0 | return "SURROGATE"; |
61 | 0 | case INVALID_BASE64_CHARACTER: |
62 | 0 | return "INVALID_BASE64_CHARACTER"; |
63 | 0 | case BASE64_INPUT_REMAINDER: |
64 | 0 | return "BASE64_INPUT_REMAINDER"; |
65 | 0 | case BASE64_EXTRA_BITS: |
66 | 0 | return "BASE64_EXTRA_BITS"; |
67 | 0 | case OUTPUT_BUFFER_TOO_SMALL: |
68 | 0 | return "OUTPUT_BUFFER_TOO_SMALL"; |
69 | 0 | default: |
70 | 0 | return "OTHER"; |
71 | 0 | } |
72 | 0 | } |
73 | | #endif |
74 | | |
75 | | struct result { |
76 | | error_code error; |
77 | | size_t count; // In case of error, indicates the position of the error. In |
78 | | // case of success, indicates the number of code units |
79 | | // validated/written. |
80 | | |
81 | | simdutf_really_inline result() noexcept |
82 | | : error{error_code::SUCCESS}, count{0} {} |
83 | | |
84 | | simdutf_really_inline result(error_code err, size_t pos) noexcept |
85 | | : error{err}, count{pos} {} |
86 | | |
87 | 0 | simdutf_really_inline bool is_ok() const noexcept { |
88 | 0 | return error == error_code::SUCCESS; |
89 | 0 | } |
90 | | |
91 | | simdutf_really_inline bool is_err() const noexcept { |
92 | | return error != error_code::SUCCESS; |
93 | | } |
94 | | }; |
95 | | |
96 | | struct full_result { |
97 | | error_code error; |
98 | | size_t input_count; |
99 | | size_t output_count; |
100 | | bool padding_error = false; // true if the error is due to padding, only |
101 | | // meaningful when error is not SUCCESS |
102 | | |
103 | | simdutf_really_inline full_result() noexcept |
104 | 0 | : error{error_code::SUCCESS}, input_count{0}, output_count{0} {} |
105 | | |
106 | | simdutf_really_inline full_result(error_code err, size_t pos_in, |
107 | | size_t pos_out) noexcept |
108 | | : error{err}, input_count{pos_in}, output_count{pos_out} {} |
109 | | simdutf_really_inline full_result(error_code err, size_t pos_in, |
110 | | size_t pos_out, bool padding_err) noexcept |
111 | | : error{err}, input_count{pos_in}, output_count{pos_out}, |
112 | | padding_error{padding_err} {} |
113 | | |
114 | | simdutf_really_inline operator result() const noexcept { |
115 | | if (error == error_code::SUCCESS) { |
116 | | return result{error, output_count}; |
117 | | } else { |
118 | | return result{error, input_count}; |
119 | | } |
120 | | } |
121 | | }; |
122 | | |
123 | | } // namespace simdutf |
124 | | #endif |