simdutf 9.0.0
Unicode at GB/s.
Loading...
Searching...
No Matches
error.h
1#ifndef SIMDUTF_ERROR_H
2#define SIMDUTF_ERROR_H
3#include <string_view>
4
5namespace simdutf {
6
7enum error_code {
8 SUCCESS = 0,
9 HEADER_BITS, // Any byte must have fewer than 5 header bits.
10 TOO_SHORT, // The leading byte must be followed by N-1 continuation bytes,
11 // where N is the UTF-8 character length This is also the error
12 // when the input is truncated.
13 TOO_LONG, // We either have too many consecutive continuation bytes or the
14 // string starts with a continuation byte.
15 OVERLONG, // The decoded character must be above U+7F for two-byte characters,
16 // U+7FF for three-byte characters, and U+FFFF for four-byte
17 // characters.
18 TOO_LARGE, // The decoded character must be less than or equal to
19 // U+10FFFF,less than or equal than U+7F for ASCII OR less than
20 // equal than U+FF for Latin1
21 SURROGATE, // The decoded character must be not be in U+D800...DFFF (UTF-8 or
22 // UTF-32)
23 // OR
24 // a high surrogate must be followed by a low surrogate
25 // and a low surrogate must be preceded by a high surrogate
26 // (UTF-16)
27 // OR
28 // there must be no surrogate at all and one is
29 // found (Latin1 functions)
30 // OR
31 // *specifically* for the function
32 // utf8_length_from_utf16_with_replacement, a surrogate (whether
33 // in error or not) has been found (I.e., whether we are in the
34 // Basic Multilingual Plane or not).
35 INVALID_BASE64_CHARACTER, // Found a character that cannot be part of a valid
36 // base64 string. This may include a misplaced
37 // padding character ('=').
38 BASE64_INPUT_REMAINDER, // The base64 input terminates with a single
39 // character, excluding padding (=). It is also used
40 // in strict mode when padding is not adequate.
41 BASE64_EXTRA_BITS, // The base64 input terminates with non-zero
42 // padding bits.
43 OUTPUT_BUFFER_TOO_SMALL, // The provided buffer is too small.
44 OTHER // Not related to validation/transcoding.
45};
46
47inline std::string_view error_to_string(error_code code) noexcept {
48 switch (code) {
49 case SUCCESS:
50 return "SUCCESS";
51 case HEADER_BITS:
52 return "HEADER_BITS";
53 case TOO_SHORT:
54 return "TOO_SHORT";
55 case TOO_LONG:
56 return "TOO_LONG";
57 case OVERLONG:
58 return "OVERLONG";
59 case TOO_LARGE:
60 return "TOO_LARGE";
61 case SURROGATE:
62 return "SURROGATE";
63 case INVALID_BASE64_CHARACTER:
64 return "INVALID_BASE64_CHARACTER";
65 case BASE64_INPUT_REMAINDER:
66 return "BASE64_INPUT_REMAINDER";
67 case BASE64_EXTRA_BITS:
68 return "BASE64_EXTRA_BITS";
69 case OUTPUT_BUFFER_TOO_SMALL:
70 return "OUTPUT_BUFFER_TOO_SMALL";
71 default:
72 return "OTHER";
73 }
74}
75
76struct result {
77 error_code error;
78 size_t count; // In case of error, indicates the position of the error. In
79 // case of success, indicates the number of code units
80 // validated/written.
81
82 simdutf_really_inline simdutf_constexpr23 result() noexcept
83 : error{error_code::SUCCESS}, count{0} {}
84
85 simdutf_really_inline simdutf_constexpr23 result(error_code err,
86 size_t pos) noexcept
87 : error{err}, count{pos} {}
88
89 simdutf_really_inline simdutf_constexpr23 bool is_ok() const noexcept {
90 return error == error_code::SUCCESS;
91 }
92
93 simdutf_really_inline simdutf_constexpr23 bool is_err() const noexcept {
94 return error != error_code::SUCCESS;
95 }
96};
97
99 error_code error;
100 size_t input_count;
101 size_t output_count;
102 bool padding_error = false; // true if the error is due to padding, only
103 // meaningful when error is not SUCCESS
104
105 simdutf_really_inline simdutf_constexpr23 full_result() noexcept
106 : error{error_code::SUCCESS}, input_count{0}, output_count{0} {}
107
108 simdutf_really_inline simdutf_constexpr23 full_result(error_code err,
109 size_t pos_in,
110 size_t pos_out) noexcept
111 : error{err}, input_count{pos_in}, output_count{pos_out} {}
112 simdutf_really_inline simdutf_constexpr23 full_result(
113 error_code err, size_t pos_in, size_t pos_out, bool padding_err) noexcept
114 : error{err}, input_count{pos_in}, output_count{pos_out},
115 padding_error{padding_err} {}
116
117 simdutf_really_inline simdutf_constexpr23 operator result() const noexcept {
118 if (error == error_code::SUCCESS) {
119 return result{error, output_count};
120 } else {
121 return result{error, input_count};
122 }
123 }
124};
125
126} // namespace simdutf
127#endif