/work/include/simdutf/error.h
Line | Count | Source (jump to first uncovered line) |
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) 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 (=). It is also used |
28 | | // in strict mode when padding is not adequate. |
29 | | BASE64_EXTRA_BITS, // The base64 input terminates with non-zero |
30 | | // padding bits. |
31 | | OUTPUT_BUFFER_TOO_SMALL, // The provided buffer is too small. |
32 | | OTHER // Not related to validation/transcoding. |
33 | | }; |
34 | | #if SIMDUTF_CPLUSPLUS17 |
35 | 0 | inline std::string_view error_to_string(error_code code) noexcept { |
36 | 0 | switch (code) { |
37 | 0 | case SUCCESS: |
38 | 0 | return "SUCCESS"; |
39 | 0 | case HEADER_BITS: |
40 | 0 | return "HEADER_BITS"; |
41 | 0 | case TOO_SHORT: |
42 | 0 | return "TOO_SHORT"; |
43 | 0 | case TOO_LONG: |
44 | 0 | return "TOO_LONG"; |
45 | 0 | case OVERLONG: |
46 | 0 | return "OVERLONG"; |
47 | 0 | case TOO_LARGE: |
48 | 0 | return "TOO_LARGE"; |
49 | 0 | case SURROGATE: |
50 | 0 | return "SURROGATE"; |
51 | 0 | case INVALID_BASE64_CHARACTER: |
52 | 0 | return "INVALID_BASE64_CHARACTER"; |
53 | 0 | case BASE64_INPUT_REMAINDER: |
54 | 0 | return "BASE64_INPUT_REMAINDER"; |
55 | 0 | case BASE64_EXTRA_BITS: |
56 | 0 | return "BASE64_EXTRA_BITS"; |
57 | 0 | case OUTPUT_BUFFER_TOO_SMALL: |
58 | 0 | return "OUTPUT_BUFFER_TOO_SMALL"; |
59 | 0 | default: |
60 | 0 | return "OTHER"; |
61 | 0 | } |
62 | 0 | } |
63 | | #endif |
64 | | |
65 | | struct result { |
66 | | error_code error; |
67 | | size_t count; // In case of error, indicates the position of the error. In |
68 | | // case of success, indicates the number of code units |
69 | | // validated/written. |
70 | | |
71 | | simdutf_really_inline result() noexcept |
72 | 0 | : error{error_code::SUCCESS}, count{0} {} |
73 | | |
74 | | simdutf_really_inline result(error_code err, size_t pos) noexcept |
75 | | : error{err}, count{pos} {} |
76 | | |
77 | 0 | simdutf_really_inline bool is_ok() const noexcept { |
78 | 0 | return error == error_code::SUCCESS; |
79 | 0 | } |
80 | | |
81 | | simdutf_really_inline bool is_err() const noexcept { |
82 | | return error != error_code::SUCCESS; |
83 | | } |
84 | | }; |
85 | | |
86 | | struct full_result { |
87 | | error_code error; |
88 | | size_t input_count; |
89 | | size_t output_count; |
90 | | bool padding_error = false; // true if the error is due to padding, only |
91 | | // meaningful when error is not SUCCESS |
92 | | |
93 | | simdutf_really_inline full_result() noexcept |
94 | 0 | : error{error_code::SUCCESS}, input_count{0}, output_count{0} {} |
95 | | |
96 | | simdutf_really_inline full_result(error_code err, size_t pos_in, |
97 | | size_t pos_out) noexcept |
98 | | : error{err}, input_count{pos_in}, output_count{pos_out} {} |
99 | | simdutf_really_inline full_result(error_code err, size_t pos_in, |
100 | | size_t pos_out, bool padding_err) noexcept |
101 | | : error{err}, input_count{pos_in}, output_count{pos_out}, |
102 | | padding_error{padding_err} {} |
103 | | |
104 | | simdutf_really_inline operator result() const noexcept { |
105 | | if (error == error_code::SUCCESS) { |
106 | | return result{error, output_count}; |
107 | | } else { |
108 | | return result{error, input_count}; |
109 | | } |
110 | | } |
111 | | }; |
112 | | |
113 | | } // namespace simdutf |
114 | | #endif |