simdutf 8.0.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)
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
45inline std::string_view error_to_string(error_code code) noexcept {
46 switch (code) {
47 case SUCCESS:
48 return "SUCCESS";
49 case HEADER_BITS:
50 return "HEADER_BITS";
51 case TOO_SHORT:
52 return "TOO_SHORT";
53 case TOO_LONG:
54 return "TOO_LONG";
55 case OVERLONG:
56 return "OVERLONG";
57 case TOO_LARGE:
58 return "TOO_LARGE";
59 case SURROGATE:
60 return "SURROGATE";
61 case INVALID_BASE64_CHARACTER:
62 return "INVALID_BASE64_CHARACTER";
63 case BASE64_INPUT_REMAINDER:
64 return "BASE64_INPUT_REMAINDER";
65 case BASE64_EXTRA_BITS:
66 return "BASE64_EXTRA_BITS";
67 case OUTPUT_BUFFER_TOO_SMALL:
68 return "OUTPUT_BUFFER_TOO_SMALL";
69 default:
70 return "OTHER";
71 }
72}
73#endif
74
75struct 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 simdutf_constexpr23 result() noexcept
82 : error{error_code::SUCCESS}, count{0} {}
83
84 simdutf_really_inline simdutf_constexpr23 result(error_code err,
85 size_t pos) noexcept
86 : error{err}, count{pos} {}
87
88 simdutf_really_inline simdutf_constexpr23 bool is_ok() const noexcept {
89 return error == error_code::SUCCESS;
90 }
91
92 simdutf_really_inline simdutf_constexpr23 bool is_err() const noexcept {
93 return error != error_code::SUCCESS;
94 }
95};
96
98 error_code error;
99 size_t input_count;
100 size_t output_count;
101 bool padding_error = false; // true if the error is due to padding, only
102 // meaningful when error is not SUCCESS
103
104 simdutf_really_inline simdutf_constexpr23 full_result() noexcept
105 : error{error_code::SUCCESS}, input_count{0}, output_count{0} {}
106
107 simdutf_really_inline simdutf_constexpr23 full_result(error_code err,
108 size_t pos_in,
109 size_t pos_out) noexcept
110 : error{err}, input_count{pos_in}, output_count{pos_out} {}
111 simdutf_really_inline simdutf_constexpr23 full_result(
112 error_code err, size_t pos_in, size_t pos_out, bool padding_err) noexcept
113 : error{err}, input_count{pos_in}, output_count{pos_out},
114 padding_error{padding_err} {}
115
116 simdutf_really_inline simdutf_constexpr23 operator result() const noexcept {
117 if (error == error_code::SUCCESS) {
118 return result{error, output_count};
119 } else {
120 return result{error, input_count};
121 }
122 }
123};
124
125} // namespace simdutf
126#endif