be/src/util/string_parser.hpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // This file is copied from |
18 | | // https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/string-parser.hpp |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <fast_float/fast_float.h> |
24 | | #include <fast_float/parse_number.h> |
25 | | #include <glog/logging.h> |
26 | | #include <sys/types.h> |
27 | | |
28 | | #include <algorithm> |
29 | | #include <cstdlib> |
30 | | // IWYU pragma: no_include <bits/std_abs.h> |
31 | | #include <cmath> // IWYU pragma: keep |
32 | | #include <cstdint> |
33 | | #include <limits> |
34 | | #include <map> |
35 | | #include <string> |
36 | | #include <type_traits> |
37 | | #include <utility> |
38 | | |
39 | | #include "common/status.h" |
40 | | #include "core/data_type/number_traits.h" |
41 | | #include "core/data_type/primitive_type.h" |
42 | | #include "core/extended_types.h" |
43 | | #include "core/value/large_int_value.h" |
44 | | #include "exec/common/int_exp.h" |
45 | | #include "exec/common/string_utils/string_utils.h" |
46 | | |
47 | | namespace doris { |
48 | | #include "common/compile_check_avoid_begin.h" |
49 | | template <DecimalNativeTypeConcept T> |
50 | | struct Decimal; |
51 | | |
52 | | // they rely on the template parameter `IS_STRICT`. in strict mode, it will set error code and otherwise it will not. |
53 | | #ifndef SET_PARAMS_RET_FALSE_IFN |
54 | | #define SET_PARAMS_RET_FALSE_IFN(stmt, ...) \ |
55 | 4.01M | do { \ |
56 | 4.01M | if (!(stmt)) [[unlikely]] { \ |
57 | 136k | if constexpr (IsStrict) { \ |
58 | 300 | params.status = Status::InvalidArgument(__VA_ARGS__); \ |
59 | 300 | } \ |
60 | 136k | return false; \ |
61 | 136k | } \ |
62 | 4.01M | } while (false) |
63 | | #endif |
64 | | |
65 | | #ifndef SET_PARAMS_RET_FALSE_FROM_EXCEPTION |
66 | | #define SET_PARAMS_RET_FALSE_FROM_EXCEPTION(stmt) \ |
67 | 688 | do { \ |
68 | 688 | try { \ |
69 | 688 | { stmt; } \ |
70 | 688 | } catch (const doris::Exception& e) { \ |
71 | 69 | if constexpr (IsStrict) { \ |
72 | 2 | params.status = e.to_status(); \ |
73 | 2 | } \ |
74 | 69 | return false; \ |
75 | 69 | } \ |
76 | 688 | } while (false) |
77 | | #endif |
78 | | |
79 | | // skip leading and trailing ascii whitespaces, |
80 | | // return the pointer to the first non-whitespace char, |
81 | | // and update the len to the new length, which does not include |
82 | | // leading and trailing whitespaces |
83 | | template <typename T> |
84 | 20.8M | inline const char* skip_ascii_whitespaces(const char* s, T& len) { |
85 | 21.3M | while (len > 0 && is_whitespace_ascii(*s)) { |
86 | 471k | ++s; |
87 | 471k | --len; |
88 | 471k | } |
89 | | |
90 | 21.3M | while (len > 0 && is_whitespace_ascii(s[len - 1])) { |
91 | 467k | --len; |
92 | 467k | } |
93 | | |
94 | 20.8M | return s; |
95 | 20.8M | } _ZN5doris22skip_ascii_whitespacesImEEPKcS2_RT_ Line | Count | Source | 84 | 20.8M | inline const char* skip_ascii_whitespaces(const char* s, T& len) { | 85 | 21.2M | while (len > 0 && is_whitespace_ascii(*s)) { | 86 | 393k | ++s; | 87 | 393k | --len; | 88 | 393k | } | 89 | | | 90 | 21.2M | while (len > 0 && is_whitespace_ascii(s[len - 1])) { | 91 | 390k | --len; | 92 | 390k | } | 93 | | | 94 | 20.8M | return s; | 95 | 20.8M | } |
_ZN5doris22skip_ascii_whitespacesIlEEPKcS2_RT_ Line | Count | Source | 84 | 31.4k | inline const char* skip_ascii_whitespaces(const char* s, T& len) { | 85 | 105k | while (len > 0 && is_whitespace_ascii(*s)) { | 86 | 74.3k | ++s; | 87 | 74.3k | --len; | 88 | 74.3k | } | 89 | | | 90 | 105k | while (len > 0 && is_whitespace_ascii(s[len - 1])) { | 91 | 74.0k | --len; | 92 | 74.0k | } | 93 | | | 94 | 31.4k | return s; | 95 | 31.4k | } |
_ZN5doris22skip_ascii_whitespacesIiEEPKcS2_RT_ Line | Count | Source | 84 | 4.84k | inline const char* skip_ascii_whitespaces(const char* s, T& len) { | 85 | 8.37k | while (len > 0 && is_whitespace_ascii(*s)) { | 86 | 3.52k | ++s; | 87 | 3.52k | --len; | 88 | 3.52k | } | 89 | | | 90 | 8.37k | while (len > 0 && is_whitespace_ascii(s[len - 1])) { | 91 | 3.52k | --len; | 92 | 3.52k | } | 93 | | | 94 | 4.84k | return s; | 95 | 4.84k | } |
|
96 | | |
97 | | template <typename T> |
98 | 118k | inline const char* skip_leading_whitespace(const char* __restrict s, T& len) { |
99 | 236k | while (len > 0 && is_whitespace_ascii(*s)) { |
100 | 117k | ++s; |
101 | 117k | --len; |
102 | 117k | } |
103 | | |
104 | 118k | return s; |
105 | 118k | } |
106 | | |
107 | | // skip trailing ascii whitespaces, |
108 | | // return the pointer to the first char, |
109 | | // and update the len to the new length, which does not include |
110 | | // trailing whitespaces |
111 | | template <typename T> |
112 | 102k | inline const char* skip_trailing_whitespaces(const char* s, T& len) { |
113 | 226k | while (len > 0 && is_whitespace_ascii(s[len - 1])) { |
114 | 123k | --len; |
115 | 123k | } |
116 | | |
117 | 102k | return s; |
118 | 102k | } |
119 | | |
120 | | template <bool (*Pred)(char)> |
121 | 93.5k | bool range_suite(const char* s, const char* end) { |
122 | 93.5k | return std::ranges::all_of(s, end, Pred); |
123 | 93.5k | } _ZN5doris11range_suiteIXadL_Z16is_numeric_asciicEEEEbPKcS2_ Line | Count | Source | 121 | 91.0k | bool range_suite(const char* s, const char* end) { | 122 | 91.0k | return std::ranges::all_of(s, end, Pred); | 123 | 91.0k | } |
_ZN5doris11range_suiteIXadL_Z19is_whitespace_asciicEEEEbPKcS2_ Line | Count | Source | 121 | 2.52k | bool range_suite(const char* s, const char* end) { | 122 | 2.52k | return std::ranges::all_of(s, end, Pred); | 123 | 2.52k | } |
|
124 | | |
125 | | inline auto is_digit_range = range_suite<is_numeric_ascii>; |
126 | | inline auto is_space_range = range_suite<is_whitespace_ascii>; |
127 | | |
128 | | // combine in_bound and range_suite is ok. won't lead to duplicated calculation. |
129 | 160k | inline bool in_bound(const char* s, const char* end, size_t offset) { |
130 | 160k | if (s + offset >= end) [[unlikely]] { |
131 | 73.4k | return false; |
132 | 73.4k | } |
133 | 86.7k | return true; |
134 | 160k | } |
135 | | |
136 | | // LEN = 0 means any length(include zero). LEN = 1 means only one character. so on. LEN = -x means x or more. |
137 | | // if need result, use StringRef{origin_s, s} outside |
138 | | template <int LEN, bool (*Pred)(char)> |
139 | 1.25M | bool skip_qualified_char(const char*& s, const char* end) { |
140 | 1.25M | if constexpr (LEN == 0) { |
141 | | // Consume any length of characters that match the predicate. |
142 | 2.31M | while (s != end && Pred(*s)) { |
143 | 1.23M | ++s; |
144 | 1.23M | } |
145 | 1.08M | } else if constexpr (LEN > 0) { |
146 | | // Consume exactly LEN characters that match the predicate. |
147 | 293k | for (int i = 0; i < LEN; ++i, ++s) { |
148 | 151k | if (s == end || !Pred(*s)) [[unlikely]] { |
149 | 10.9k | return false; |
150 | 10.9k | } |
151 | 151k | } |
152 | 151k | } else { // LEN < 0 |
153 | | // Consume at least -LEN characters that match the predicate. |
154 | 16.2k | int count = 0; |
155 | 33.5k | while (s != end && Pred(*s)) { |
156 | 17.2k | ++s; |
157 | 17.2k | ++count; |
158 | 17.2k | } |
159 | 16.2k | if (count < -LEN) [[unlikely]] { |
160 | 0 | return false; |
161 | 0 | } |
162 | 16.2k | } |
163 | 157k | return true; |
164 | 1.25M | } _ZN5doris19skip_qualified_charILi0EXadL_Z19is_whitespace_asciicEEEEbRPKcS2_ Line | Count | Source | 139 | 449k | bool skip_qualified_char(const char*& s, const char* end) { | 140 | 449k | if constexpr (LEN == 0) { | 141 | | // Consume any length of characters that match the predicate. | 142 | 453k | while (s != end && Pred(*s)) { | 143 | 4.20k | ++s; | 144 | 4.20k | } | 145 | | } else if constexpr (LEN > 0) { | 146 | | // Consume exactly LEN characters that match the predicate. | 147 | | for (int i = 0; i < LEN; ++i, ++s) { | 148 | | if (s == end || !Pred(*s)) [[unlikely]] { | 149 | | return false; | 150 | | } | 151 | | } | 152 | | } else { // LEN < 0 | 153 | | // Consume at least -LEN characters that match the predicate. | 154 | | int count = 0; | 155 | | while (s != end && Pred(*s)) { | 156 | | ++s; | 157 | | ++count; | 158 | | } | 159 | | if (count < -LEN) [[unlikely]] { | 160 | | return false; | 161 | | } | 162 | | } | 163 | 449k | return true; | 164 | 449k | } |
_ZN5doris19skip_qualified_charILi0EXadL_Z16is_numeric_asciicEEEEbRPKcS2_ Line | Count | Source | 139 | 638k | bool skip_qualified_char(const char*& s, const char* end) { | 140 | 638k | if constexpr (LEN == 0) { | 141 | | // Consume any length of characters that match the predicate. | 142 | 1.86M | while (s != end && Pred(*s)) { | 143 | 1.22M | ++s; | 144 | 1.22M | } | 145 | | } else if constexpr (LEN > 0) { | 146 | | // Consume exactly LEN characters that match the predicate. | 147 | | for (int i = 0; i < LEN; ++i, ++s) { | 148 | | if (s == end || !Pred(*s)) [[unlikely]] { | 149 | | return false; | 150 | | } | 151 | | } | 152 | | } else { // LEN < 0 | 153 | | // Consume at least -LEN characters that match the predicate. | 154 | | int count = 0; | 155 | | while (s != end && Pred(*s)) { | 156 | | ++s; | 157 | | ++count; | 158 | | } | 159 | | if (count < -LEN) [[unlikely]] { | 160 | | return false; | 161 | | } | 162 | | } | 163 | 638k | return true; | 164 | 638k | } |
_ZN5doris19skip_qualified_charILin1EXadL_Z23is_not_whitespace_asciicEEEEbRPKcS2_ Line | Count | Source | 139 | 16.2k | bool skip_qualified_char(const char*& s, const char* end) { | 140 | | if constexpr (LEN == 0) { | 141 | | // Consume any length of characters that match the predicate. | 142 | | while (s != end && Pred(*s)) { | 143 | | ++s; | 144 | | } | 145 | | } else if constexpr (LEN > 0) { | 146 | | // Consume exactly LEN characters that match the predicate. | 147 | | for (int i = 0; i < LEN; ++i, ++s) { | 148 | | if (s == end || !Pred(*s)) [[unlikely]] { | 149 | | return false; | 150 | | } | 151 | | } | 152 | 16.2k | } else { // LEN < 0 | 153 | | // Consume at least -LEN characters that match the predicate. | 154 | 16.2k | int count = 0; | 155 | 33.5k | while (s != end && Pred(*s)) { | 156 | 17.2k | ++s; | 157 | 17.2k | ++count; | 158 | 17.2k | } | 159 | 16.2k | if (count < -LEN) [[unlikely]] { | 160 | 0 | return false; | 161 | 0 | } | 162 | 16.2k | } | 163 | 16.2k | return true; | 164 | 16.2k | } |
Unexecuted instantiation: _ZN5doris19skip_qualified_charILi1EXadL_Z14is_slash_asciicEEEEbRPKcS2_ _ZN5doris19skip_qualified_charILi1EXadL_Z12is_non_alnumcEEEEbRPKcS2_ Line | Count | Source | 139 | 36.2k | bool skip_qualified_char(const char*& s, const char* end) { | 140 | | if constexpr (LEN == 0) { | 141 | | // Consume any length of characters that match the predicate. | 142 | | while (s != end && Pred(*s)) { | 143 | | ++s; | 144 | | } | 145 | 36.2k | } else if constexpr (LEN > 0) { | 146 | | // Consume exactly LEN characters that match the predicate. | 147 | 61.7k | for (int i = 0; i < LEN; ++i, ++s) { | 148 | 36.2k | if (s == end || !Pred(*s)) [[unlikely]] { | 149 | 10.7k | return false; | 150 | 10.7k | } | 151 | 36.2k | } | 152 | | } else { // LEN < 0 | 153 | | // Consume at least -LEN characters that match the predicate. | 154 | | int count = 0; | 155 | | while (s != end && Pred(*s)) { | 156 | | ++s; | 157 | | ++count; | 158 | | } | 159 | | if (count < -LEN) [[unlikely]] { | 160 | | return false; | 161 | | } | 162 | | } | 163 | 25.5k | return true; | 164 | 36.2k | } |
_ZN5doris19skip_qualified_charILi1EXadL_ZNS_12is_delimiterEcEEEEbRPKcS2_ Line | Count | Source | 139 | 18.5k | bool skip_qualified_char(const char*& s, const char* end) { | 140 | | if constexpr (LEN == 0) { | 141 | | // Consume any length of characters that match the predicate. | 142 | | while (s != end && Pred(*s)) { | 143 | | ++s; | 144 | | } | 145 | 18.5k | } else if constexpr (LEN > 0) { | 146 | | // Consume exactly LEN characters that match the predicate. | 147 | 36.9k | for (int i = 0; i < LEN; ++i, ++s) { | 148 | 18.5k | if (s == end || !Pred(*s)) [[unlikely]] { | 149 | 71 | return false; | 150 | 71 | } | 151 | 18.5k | } | 152 | | } else { // LEN < 0 | 153 | | // Consume at least -LEN characters that match the predicate. | 154 | | int count = 0; | 155 | | while (s != end && Pred(*s)) { | 156 | | ++s; | 157 | | ++count; | 158 | | } | 159 | | if (count < -LEN) [[unlikely]] { | 160 | | return false; | 161 | | } | 162 | | } | 163 | 18.4k | return true; | 164 | 18.5k | } |
_ZN5doris19skip_qualified_charILi1EXadL_ZNS_11is_date_sepEcEEEEbRPKcS2_ Line | Count | Source | 139 | 65.3k | bool skip_qualified_char(const char*& s, const char* end) { | 140 | | if constexpr (LEN == 0) { | 141 | | // Consume any length of characters that match the predicate. | 142 | | while (s != end && Pred(*s)) { | 143 | | ++s; | 144 | | } | 145 | 65.3k | } else if constexpr (LEN > 0) { | 146 | | // Consume exactly LEN characters that match the predicate. | 147 | 130k | for (int i = 0; i < LEN; ++i, ++s) { | 148 | 65.3k | if (s == end || !Pred(*s)) [[unlikely]] { | 149 | 55 | return false; | 150 | 55 | } | 151 | 65.3k | } | 152 | | } else { // LEN < 0 | 153 | | // Consume at least -LEN characters that match the predicate. | 154 | | int count = 0; | 155 | | while (s != end && Pred(*s)) { | 156 | | ++s; | 157 | | ++count; | 158 | | } | 159 | | if (count < -LEN) [[unlikely]] { | 160 | | return false; | 161 | | } | 162 | | } | 163 | 65.3k | return true; | 164 | 65.3k | } |
_ZN5doris19skip_qualified_charILi1EXadL_ZNS_8is_colonEcEEEEbRPKcS2_ Line | Count | Source | 139 | 31.8k | bool skip_qualified_char(const char*& s, const char* end) { | 140 | | if constexpr (LEN == 0) { | 141 | | // Consume any length of characters that match the predicate. | 142 | | while (s != end && Pred(*s)) { | 143 | | ++s; | 144 | | } | 145 | 31.8k | } else if constexpr (LEN > 0) { | 146 | | // Consume exactly LEN characters that match the predicate. | 147 | 63.5k | for (int i = 0; i < LEN; ++i, ++s) { | 148 | 31.8k | if (s == end || !Pred(*s)) [[unlikely]] { | 149 | 60 | return false; | 150 | 60 | } | 151 | 31.8k | } | 152 | | } else { // LEN < 0 | 153 | | // Consume at least -LEN characters that match the predicate. | 154 | | int count = 0; | 155 | | while (s != end && Pred(*s)) { | 156 | | ++s; | 157 | | ++count; | 158 | | } | 159 | | if (count < -LEN) [[unlikely]] { | 160 | | return false; | 161 | | } | 162 | | } | 163 | 31.7k | return true; | 164 | 31.8k | } |
|
165 | | |
166 | | inline auto skip_any_whitespace = skip_qualified_char<0, is_whitespace_ascii>; |
167 | | inline auto skip_any_digit = skip_qualified_char<0, is_numeric_ascii>; |
168 | | inline auto skip_tz_name_part = skip_qualified_char<-1, is_not_whitespace_ascii>; |
169 | | inline auto skip_one_slash = skip_qualified_char<1, is_slash_ascii>; |
170 | | inline auto skip_one_non_alnum = skip_qualified_char<1, is_non_alnum>; |
171 | | |
172 | 18.5k | inline bool is_delimiter(char c) { |
173 | 18.5k | return c == ' ' || c == 'T' || c == ':'; |
174 | 18.5k | } |
175 | | inline auto consume_one_delimiter = skip_qualified_char<1, is_delimiter>; |
176 | | |
177 | 103k | inline bool is_date_sep(char c) { |
178 | 103k | return c == '-' || c == '/'; |
179 | 103k | } |
180 | | inline auto consume_one_date_sep = skip_qualified_char<1, is_date_sep>; |
181 | | |
182 | 31.7k | inline bool is_colon(char c) { |
183 | 31.7k | return c == ':'; |
184 | 31.7k | } |
185 | | inline auto consume_one_colon = skip_qualified_char<1, is_colon>; |
186 | | |
187 | | // only consume a string of digit, not include sign. |
188 | | // when has MAX_LEN > 0, do greedy match but at most MAX_LEN. |
189 | | // LEN = 0 means any length, otherwise(must > 0) it means exactly LEN digits. |
190 | | template <typename T, int LEN = 0, int MAX_LEN = -1> |
191 | 33 | bool consume_digit(const char*& s, const char* end, T& out) { |
192 | 33 | static_assert(LEN >= 0); |
193 | | if constexpr (MAX_LEN > 0) { |
194 | | out = 0; |
195 | | for (int i = 0; i < MAX_LEN; ++i, ++s) { |
196 | | if (s == end || !is_numeric_ascii(*s)) { |
197 | | if (i < LEN) [[unlikely]] { |
198 | | return false; |
199 | | } |
200 | | break; // stop consuming if we have consumed enough digits. |
201 | | } |
202 | | out = out * 10 + (*s - '0'); |
203 | | } |
204 | | } else if constexpr (LEN == 0) { |
205 | | // Consume any length of digits. |
206 | | out = 0; |
207 | | while (s != end && is_numeric_ascii(*s)) { |
208 | | out = out * 10 + (*s - '0'); |
209 | | ++s; |
210 | | } |
211 | 33 | } else if constexpr (LEN > 0) { |
212 | | // Consume exactly LEN digits. |
213 | 33 | out = 0; |
214 | 126 | for (int i = 0; i < LEN; ++i, ++s) { |
215 | 93 | if (s == end || !is_numeric_ascii(*s)) [[unlikely]] { |
216 | 0 | return false; |
217 | 0 | } |
218 | 93 | out = out * 10 + (*s - '0'); |
219 | 93 | } |
220 | 33 | } |
221 | 33 | return true; |
222 | 33 | } _ZN5doris13consume_digitIjLi4ELin1EEEbRPKcS2_RT_ Line | Count | Source | 191 | 20 | bool consume_digit(const char*& s, const char* end, T& out) { | 192 | 20 | static_assert(LEN >= 0); | 193 | | if constexpr (MAX_LEN > 0) { | 194 | | out = 0; | 195 | | for (int i = 0; i < MAX_LEN; ++i, ++s) { | 196 | | if (s == end || !is_numeric_ascii(*s)) { | 197 | | if (i < LEN) [[unlikely]] { | 198 | | return false; | 199 | | } | 200 | | break; // stop consuming if we have consumed enough digits. | 201 | | } | 202 | | out = out * 10 + (*s - '0'); | 203 | | } | 204 | | } else if constexpr (LEN == 0) { | 205 | | // Consume any length of digits. | 206 | | out = 0; | 207 | | while (s != end && is_numeric_ascii(*s)) { | 208 | | out = out * 10 + (*s - '0'); | 209 | | ++s; | 210 | | } | 211 | 20 | } else if constexpr (LEN > 0) { | 212 | | // Consume exactly LEN digits. | 213 | 20 | out = 0; | 214 | 100 | for (int i = 0; i < LEN; ++i, ++s) { | 215 | 80 | if (s == end || !is_numeric_ascii(*s)) [[unlikely]] { | 216 | 0 | return false; | 217 | 0 | } | 218 | 80 | out = out * 10 + (*s - '0'); | 219 | 80 | } | 220 | 20 | } | 221 | 20 | return true; | 222 | 20 | } |
_ZN5doris13consume_digitIjLi1ELin1EEEbRPKcS2_RT_ Line | Count | Source | 191 | 13 | bool consume_digit(const char*& s, const char* end, T& out) { | 192 | 13 | static_assert(LEN >= 0); | 193 | | if constexpr (MAX_LEN > 0) { | 194 | | out = 0; | 195 | | for (int i = 0; i < MAX_LEN; ++i, ++s) { | 196 | | if (s == end || !is_numeric_ascii(*s)) { | 197 | | if (i < LEN) [[unlikely]] { | 198 | | return false; | 199 | | } | 200 | | break; // stop consuming if we have consumed enough digits. | 201 | | } | 202 | | out = out * 10 + (*s - '0'); | 203 | | } | 204 | | } else if constexpr (LEN == 0) { | 205 | | // Consume any length of digits. | 206 | | out = 0; | 207 | | while (s != end && is_numeric_ascii(*s)) { | 208 | | out = out * 10 + (*s - '0'); | 209 | | ++s; | 210 | | } | 211 | 13 | } else if constexpr (LEN > 0) { | 212 | | // Consume exactly LEN digits. | 213 | 13 | out = 0; | 214 | 26 | for (int i = 0; i < LEN; ++i, ++s) { | 215 | 13 | if (s == end || !is_numeric_ascii(*s)) [[unlikely]] { | 216 | 0 | return false; | 217 | 0 | } | 218 | 13 | out = out * 10 + (*s - '0'); | 219 | 13 | } | 220 | 13 | } | 221 | 13 | return true; | 222 | 13 | } |
|
223 | | |
224 | | // specialized version for 2 digits, which is used very often in date/time parsing. |
225 | | template <> |
226 | 192k | inline bool consume_digit<uint32_t, 2, -1>(const char*& s, const char* end, uint32_t& out) { |
227 | 192k | out = 0; |
228 | 192k | if (s == end || s + 1 == end || !is_numeric_ascii(*s) || !is_numeric_ascii(*(s + 1))) |
229 | 59.8k | [[unlikely]] { |
230 | 59.8k | return false; |
231 | 59.8k | } |
232 | 132k | out = (s[0] - '0') * 10 + (s[1] - '0'); |
233 | 132k | s += 2; // consume 2 digits |
234 | 132k | return true; |
235 | 192k | } |
236 | | |
237 | | // specialized version for 1 or 2 digits, which is used very often in date/time parsing. |
238 | | template <> |
239 | 142k | inline bool consume_digit<uint32_t, 1, 2>(const char*& s, const char* end, uint32_t& out) { |
240 | 142k | out = 0; |
241 | 142k | if (s == end || !is_numeric_ascii(*s)) [[unlikely]] { |
242 | 487 | return false; |
243 | 141k | } else if (s + 1 != end && is_numeric_ascii(*(s + 1))) { |
244 | | // consume 2 digits |
245 | 93.9k | out = (*s - '0') * 10 + (*(s + 1) - '0'); |
246 | 93.9k | s += 2; |
247 | 93.9k | } else { |
248 | | // consume 1 digit |
249 | 47.5k | out = *s - '0'; |
250 | 47.5k | ++s; |
251 | 47.5k | } |
252 | 141k | return true; |
253 | 142k | } |
254 | | |
255 | | template <bool (*Pred)(char)> |
256 | 2.56k | uint32_t count_valid_length(const char* s, const char* end) { |
257 | 2.56k | DCHECK(s <= end) << "s: " << s << ", end: " << end; |
258 | 2.56k | uint32_t count = 0; |
259 | 7.71k | while (s != end && Pred(*s)) { |
260 | 5.14k | ++count; |
261 | 5.14k | ++s; |
262 | 5.14k | } |
263 | 2.56k | return count; |
264 | 2.56k | } |
265 | | |
266 | | inline auto count_digits = count_valid_length<is_numeric_ascii>; |
267 | | |
268 | 22 | inline std::string combine_tz_offset(char sign, uint32_t hour_offset, uint32_t minute_offset) { |
269 | 22 | std::string result(6, '0'); |
270 | 22 | result[0] = sign; |
271 | 22 | result[1] = '0' + (hour_offset / 10); |
272 | 22 | result[2] = '0' + (hour_offset % 10); |
273 | 22 | result[3] = ':'; |
274 | 22 | result[4] = '0' + (minute_offset / 10); |
275 | 22 | result[5] = '0' + (minute_offset % 10); |
276 | 22 | DCHECK_EQ(result.size(), 6); |
277 | 22 | return result; |
278 | 22 | } |
279 | | |
280 | | // Utility functions for doing atoi/atof on non-null terminated strings. On micro benchmarks, |
281 | | // this is significantly faster than libc (atoi/strtol and atof/strtod). |
282 | | // |
283 | | // Strings with leading and trailing whitespaces are accepted. |
284 | | // Branching is heavily optimized for the non-whitespace successful case. |
285 | | // All the StringTo* functions first parse the input string assuming it has no leading whitespace. |
286 | | // If that first attempt was unsuccessful, these functions retry the parsing after removing |
287 | | // whitespace. Therefore, strings with whitespace take a perf hit on branch mis-prediction. |
288 | | // |
289 | | // For overflows, we are following the mysql behavior, to cap values at the max/min value for that |
290 | | // data type. This is different from hive, which returns NULL for overflow slots for int types |
291 | | // and inf/-inf for float types. |
292 | | // |
293 | | // Things we tried that did not work: |
294 | | // - lookup table for converting character to digit |
295 | | // Improvements (TODO): |
296 | | // - Validate input using _simd_compare_ranges |
297 | | // - Since we know the length, we can parallelize this: i.e. result = 100*s[0] + 10*s[1] + s[2] |
298 | | class StringParser { |
299 | | public: |
300 | | enum ParseResult { PARSE_SUCCESS = 0, PARSE_FAILURE, PARSE_OVERFLOW, PARSE_UNDERFLOW }; |
301 | | |
302 | | template <typename T> |
303 | 128M | static T numeric_limits(bool negative) { |
304 | 128M | if constexpr (std::is_same_v<T, __int128>) { |
305 | 816k | return negative ? MIN_INT128 : MAX_INT128; |
306 | 127M | } else { |
307 | 127M | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); |
308 | 127M | } |
309 | 128M | } _ZN5doris12StringParser14numeric_limitsInEET_b Line | Count | Source | 303 | 816k | static T numeric_limits(bool negative) { | 304 | 816k | if constexpr (std::is_same_v<T, __int128>) { | 305 | 816k | return negative ? MIN_INT128 : MAX_INT128; | 306 | | } else { | 307 | | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 308 | | } | 309 | 816k | } |
_ZN5doris12StringParser14numeric_limitsIjEET_b Line | Count | Source | 303 | 2.38k | static T numeric_limits(bool negative) { | 304 | | if constexpr (std::is_same_v<T, __int128>) { | 305 | | return negative ? MIN_INT128 : MAX_INT128; | 306 | 2.38k | } else { | 307 | 2.38k | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 308 | 2.38k | } | 309 | 2.38k | } |
_ZN5doris12StringParser14numeric_limitsIaEET_b Line | Count | Source | 303 | 4.68M | static T numeric_limits(bool negative) { | 304 | | if constexpr (std::is_same_v<T, __int128>) { | 305 | | return negative ? MIN_INT128 : MAX_INT128; | 306 | 4.68M | } else { | 307 | 4.68M | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 308 | 4.68M | } | 309 | 4.68M | } |
_ZN5doris12StringParser14numeric_limitsIsEET_b Line | Count | Source | 303 | 854k | static T numeric_limits(bool negative) { | 304 | | if constexpr (std::is_same_v<T, __int128>) { | 305 | | return negative ? MIN_INT128 : MAX_INT128; | 306 | 854k | } else { | 307 | 854k | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 308 | 854k | } | 309 | 854k | } |
_ZN5doris12StringParser14numeric_limitsIiEET_b Line | Count | Source | 303 | 49.8M | static T numeric_limits(bool negative) { | 304 | | if constexpr (std::is_same_v<T, __int128>) { | 305 | | return negative ? MIN_INT128 : MAX_INT128; | 306 | 49.8M | } else { | 307 | 49.8M | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 308 | 49.8M | } | 309 | 49.8M | } |
_ZN5doris12StringParser14numeric_limitsIlEET_b Line | Count | Source | 303 | 71.9M | static T numeric_limits(bool negative) { | 304 | | if constexpr (std::is_same_v<T, __int128>) { | 305 | | return negative ? MIN_INT128 : MAX_INT128; | 306 | 71.9M | } else { | 307 | 71.9M | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 308 | 71.9M | } | 309 | 71.9M | } |
_ZN5doris12StringParser14numeric_limitsImEET_b Line | Count | Source | 303 | 6.18k | static T numeric_limits(bool negative) { | 304 | | if constexpr (std::is_same_v<T, __int128>) { | 305 | | return negative ? MIN_INT128 : MAX_INT128; | 306 | 6.18k | } else { | 307 | 6.18k | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 308 | 6.18k | } | 309 | 6.18k | } |
_ZN5doris12StringParser14numeric_limitsIN4wide7integerILm256EiEEEET_b Line | Count | Source | 303 | 4 | static T numeric_limits(bool negative) { | 304 | | if constexpr (std::is_same_v<T, __int128>) { | 305 | | return negative ? MIN_INT128 : MAX_INT128; | 306 | 4 | } else { | 307 | 4 | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 308 | 4 | } | 309 | 4 | } |
_ZN5doris12StringParser14numeric_limitsIoEET_b Line | Count | Source | 303 | 1.69k | static T numeric_limits(bool negative) { | 304 | | if constexpr (std::is_same_v<T, __int128>) { | 305 | | return negative ? MIN_INT128 : MAX_INT128; | 306 | 1.69k | } else { | 307 | 1.69k | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 308 | 1.69k | } | 309 | 1.69k | } |
|
310 | | |
311 | | template <typename T> |
312 | 37.4M | static T get_scale_multiplier(int scale) { |
313 | 37.4M | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || |
314 | 37.4M | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, |
315 | 37.4M | "You can only instantiate as int32_t, int64_t, __int128."); |
316 | 37.4M | if constexpr (std::is_same_v<T, int32_t>) { |
317 | 594k | return common::exp10_i32(scale); |
318 | 33.6M | } else if constexpr (std::is_same_v<T, int64_t>) { |
319 | 33.6M | return common::exp10_i64(scale); |
320 | 33.6M | } else if constexpr (std::is_same_v<T, __int128>) { |
321 | 2.80M | return common::exp10_i128(scale); |
322 | 2.80M | } else if constexpr (std::is_same_v<T, wide::Int256>) { |
323 | 393k | return common::exp10_i256(scale); |
324 | 393k | } |
325 | 37.4M | } _ZN5doris12StringParser20get_scale_multiplierIiEET_i Line | Count | Source | 312 | 594k | static T get_scale_multiplier(int scale) { | 313 | 594k | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || | 314 | 594k | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, | 315 | 594k | "You can only instantiate as int32_t, int64_t, __int128."); | 316 | 594k | if constexpr (std::is_same_v<T, int32_t>) { | 317 | 594k | return common::exp10_i32(scale); | 318 | | } else if constexpr (std::is_same_v<T, int64_t>) { | 319 | | return common::exp10_i64(scale); | 320 | | } else if constexpr (std::is_same_v<T, __int128>) { | 321 | | return common::exp10_i128(scale); | 322 | | } else if constexpr (std::is_same_v<T, wide::Int256>) { | 323 | | return common::exp10_i256(scale); | 324 | | } | 325 | 594k | } |
_ZN5doris12StringParser20get_scale_multiplierIlEET_i Line | Count | Source | 312 | 33.6M | static T get_scale_multiplier(int scale) { | 313 | 33.6M | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || | 314 | 33.6M | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, | 315 | 33.6M | "You can only instantiate as int32_t, int64_t, __int128."); | 316 | | if constexpr (std::is_same_v<T, int32_t>) { | 317 | | return common::exp10_i32(scale); | 318 | 33.6M | } else if constexpr (std::is_same_v<T, int64_t>) { | 319 | 33.6M | return common::exp10_i64(scale); | 320 | | } else if constexpr (std::is_same_v<T, __int128>) { | 321 | | return common::exp10_i128(scale); | 322 | | } else if constexpr (std::is_same_v<T, wide::Int256>) { | 323 | | return common::exp10_i256(scale); | 324 | | } | 325 | 33.6M | } |
_ZN5doris12StringParser20get_scale_multiplierInEET_i Line | Count | Source | 312 | 2.80M | static T get_scale_multiplier(int scale) { | 313 | 2.80M | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || | 314 | 2.80M | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, | 315 | 2.80M | "You can only instantiate as int32_t, int64_t, __int128."); | 316 | | if constexpr (std::is_same_v<T, int32_t>) { | 317 | | return common::exp10_i32(scale); | 318 | | } else if constexpr (std::is_same_v<T, int64_t>) { | 319 | | return common::exp10_i64(scale); | 320 | 2.80M | } else if constexpr (std::is_same_v<T, __int128>) { | 321 | 2.80M | return common::exp10_i128(scale); | 322 | | } else if constexpr (std::is_same_v<T, wide::Int256>) { | 323 | | return common::exp10_i256(scale); | 324 | | } | 325 | 2.80M | } |
_ZN5doris12StringParser20get_scale_multiplierIN4wide7integerILm256EiEEEET_i Line | Count | Source | 312 | 393k | static T get_scale_multiplier(int scale) { | 313 | 393k | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || | 314 | 393k | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, | 315 | 393k | "You can only instantiate as int32_t, int64_t, __int128."); | 316 | | if constexpr (std::is_same_v<T, int32_t>) { | 317 | | return common::exp10_i32(scale); | 318 | | } else if constexpr (std::is_same_v<T, int64_t>) { | 319 | | return common::exp10_i64(scale); | 320 | | } else if constexpr (std::is_same_v<T, __int128>) { | 321 | | return common::exp10_i128(scale); | 322 | 393k | } else if constexpr (std::is_same_v<T, wide::Int256>) { | 323 | 393k | return common::exp10_i256(scale); | 324 | 393k | } | 325 | 393k | } |
|
326 | | |
327 | | // This is considerably faster than glibc's implementation (25x). |
328 | | // Assumes s represents a decimal number. |
329 | | template <typename T, bool enable_strict_mode = false> |
330 | 128M | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { |
331 | 128M | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); |
332 | 128M | if (LIKELY(*result == PARSE_SUCCESS)) { |
333 | 127M | return ans; |
334 | 127M | } |
335 | 123k | s = skip_leading_whitespace(s, len); |
336 | 123k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); |
337 | 128M | } _ZN5doris12StringParser13string_to_intInLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 812k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 812k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 812k | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 800k | return ans; | 334 | 800k | } | 335 | 11.9k | s = skip_leading_whitespace(s, len); | 336 | 11.9k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 812k | } |
_ZN5doris12StringParser13string_to_intIaLb1EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 1.65k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 1.65k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 1.65k | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 612 | return ans; | 334 | 612 | } | 335 | 1.04k | s = skip_leading_whitespace(s, len); | 336 | 1.04k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 1.65k | } |
_ZN5doris12StringParser13string_to_intIsLb1EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 1.48k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 1.48k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 1.48k | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 588 | return ans; | 334 | 588 | } | 335 | 896 | s = skip_leading_whitespace(s, len); | 336 | 896 | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 1.48k | } |
_ZN5doris12StringParser13string_to_intIiLb1EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 1.72k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 1.72k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 1.72k | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 829 | return ans; | 334 | 829 | } | 335 | 891 | s = skip_leading_whitespace(s, len); | 336 | 891 | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 1.72k | } |
_ZN5doris12StringParser13string_to_intIlLb1EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 1.62k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 1.62k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 1.62k | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 623 | return ans; | 334 | 623 | } | 335 | 1.00k | s = skip_leading_whitespace(s, len); | 336 | 1.00k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 1.62k | } |
_ZN5doris12StringParser13string_to_intInLb1EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 1.48k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 1.48k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 1.48k | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 595 | return ans; | 334 | 595 | } | 335 | 890 | s = skip_leading_whitespace(s, len); | 336 | 890 | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 1.48k | } |
_ZN5doris12StringParser13string_to_intIaLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 4.63M | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 4.63M | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 4.63M | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 4.60M | return ans; | 334 | 4.60M | } | 335 | 28.9k | s = skip_leading_whitespace(s, len); | 336 | 28.9k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 4.63M | } |
_ZN5doris12StringParser13string_to_intIsLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 849k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 849k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 849k | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 833k | return ans; | 334 | 833k | } | 335 | 15.9k | s = skip_leading_whitespace(s, len); | 336 | 15.9k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 849k | } |
_ZN5doris12StringParser13string_to_intIiLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 49.8M | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 49.8M | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 49.8M | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 49.7M | return ans; | 334 | 49.7M | } | 335 | 18.6k | s = skip_leading_whitespace(s, len); | 336 | 18.6k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 49.8M | } |
_ZN5doris12StringParser13string_to_intIlLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 71.9M | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 71.9M | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 71.9M | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 71.9M | return ans; | 334 | 71.9M | } | 335 | 42.2k | s = skip_leading_whitespace(s, len); | 336 | 42.2k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 71.9M | } |
_ZN5doris12StringParser13string_to_intIjLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 442 | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 442 | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 442 | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 43 | return ans; | 334 | 43 | } | 335 | 399 | s = skip_leading_whitespace(s, len); | 336 | 399 | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 442 | } |
_ZN5doris12StringParser13string_to_intImLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 1.44k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 1.44k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 1.44k | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 1.04k | return ans; | 334 | 1.04k | } | 335 | 399 | s = skip_leading_whitespace(s, len); | 336 | 399 | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 1.44k | } |
_ZN5doris12StringParser13string_to_intIN4wide7integerILm256EiEELb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 4 | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 4 | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 4 | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 4 | return ans; | 334 | 4 | } | 335 | 0 | s = skip_leading_whitespace(s, len); | 336 | 0 | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 4 | } |
_ZN5doris12StringParser13string_to_intIoLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 330 | 445 | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 331 | 445 | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 332 | 445 | if (LIKELY(*result == PARSE_SUCCESS)) { | 333 | 46 | return ans; | 334 | 46 | } | 335 | 399 | s = skip_leading_whitespace(s, len); | 336 | 399 | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 337 | 445 | } |
|
338 | | |
339 | | // This is considerably faster than glibc's implementation. |
340 | | // In the case of overflow, the max/min value for the data type will be returned. |
341 | | // Assumes s represents a decimal number. |
342 | | template <typename T> |
343 | 3.81k | static inline T string_to_unsigned_int(const char* __restrict s, int len, ParseResult* result) { |
344 | 3.81k | s = skip_ascii_whitespaces(s, len); |
345 | 3.81k | return string_to_unsigned_int_internal<T>(s, len, result); |
346 | 3.81k | } |
347 | | |
348 | | // Convert a string s representing a number in given base into a decimal number. |
349 | | template <typename T> |
350 | | static inline T string_to_int(const char* __restrict s, int64_t len, int base, |
351 | 3.49k | ParseResult* result) { |
352 | 3.49k | s = skip_ascii_whitespaces(s, len); |
353 | 3.49k | return string_to_int_internal<T>(s, len, base, result); |
354 | 3.49k | } |
355 | | |
356 | | template <typename T> |
357 | 3.46M | static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) { |
358 | 3.46M | s = skip_ascii_whitespaces(s, len); |
359 | 3.46M | return string_to_float_internal<T>(s, len, result); |
360 | 3.46M | } _ZN5doris12StringParser15string_to_floatIdEET_PKcmPNS0_11ParseResultE Line | Count | Source | 357 | 2.68M | static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) { | 358 | 2.68M | s = skip_ascii_whitespaces(s, len); | 359 | 2.68M | return string_to_float_internal<T>(s, len, result); | 360 | 2.68M | } |
_ZN5doris12StringParser15string_to_floatIfEET_PKcmPNS0_11ParseResultE Line | Count | Source | 357 | 786k | static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) { | 358 | 786k | s = skip_ascii_whitespaces(s, len); | 359 | 786k | return string_to_float_internal<T>(s, len, result); | 360 | 786k | } |
|
361 | | |
362 | | // Parses a string for 'true' or 'false', case insensitive. |
363 | 596k | static inline bool string_to_bool(const char* __restrict s, size_t len, ParseResult* result) { |
364 | 596k | s = skip_ascii_whitespaces(s, len); |
365 | 596k | return string_to_bool_internal(s, len, result); |
366 | 596k | } |
367 | | |
368 | | template <PrimitiveType P> |
369 | | static typename PrimitiveTypeTraits<P>::CppType::NativeType string_to_decimal( |
370 | | const char* __restrict s, size_t len, int type_precision, int type_scale, |
371 | | ParseResult* result); |
372 | | |
373 | | template <typename T> |
374 | | static Status split_string_to_map(const std::string& base, const T element_separator, |
375 | | const T key_value_separator, |
376 | | std::map<std::string, std::string>* result) { |
377 | | int key_pos = 0; |
378 | | int key_end; |
379 | | int val_pos; |
380 | | int val_end; |
381 | | |
382 | | while ((key_end = base.find(key_value_separator, key_pos)) != std::string::npos) { |
383 | | if ((val_pos = base.find_first_not_of(key_value_separator, key_end)) == |
384 | | std::string::npos) { |
385 | | break; |
386 | | } |
387 | | if ((val_end = base.find(element_separator, val_pos)) == std::string::npos) { |
388 | | val_end = base.size(); |
389 | | } |
390 | | result->insert(std::make_pair(base.substr(key_pos, key_end - key_pos), |
391 | | base.substr(val_pos, val_end - val_pos))); |
392 | | key_pos = val_end; |
393 | | if (key_pos != std::string::npos) { |
394 | | ++key_pos; |
395 | | } |
396 | | } |
397 | | |
398 | | return Status::OK(); |
399 | | } |
400 | | |
401 | | // This is considerably faster than glibc's implementation. |
402 | | // In the case of overflow, the max/min value for the data type will be returned. |
403 | | // Assumes s represents a decimal number. |
404 | | // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed. |
405 | | template <typename T, bool enable_strict_mode = false> |
406 | | static inline T string_to_int_internal(const char* __restrict s, int len, ParseResult* result); |
407 | | |
408 | | // This is considerably faster than glibc's implementation. |
409 | | // In the case of overflow, the max/min value for the data type will be returned. |
410 | | // Assumes s represents a decimal number. |
411 | | // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed. |
412 | | template <typename T> |
413 | | static inline T string_to_unsigned_int_internal(const char* __restrict s, int len, |
414 | | ParseResult* result); |
415 | | |
416 | | // Convert a string s representing a number in given base into a decimal number. |
417 | | // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed. |
418 | | template <typename T> |
419 | | static inline T string_to_int_internal(const char* __restrict s, int64_t len, int base, |
420 | | ParseResult* result); |
421 | | |
422 | | // Converts an ascii string to an integer of type T assuming it cannot overflow |
423 | | // and the number is positive. |
424 | | // Leading whitespace is not allowed. Trailing whitespace will be skipped. |
425 | | template <typename T, bool enable_strict_mode = false> |
426 | | static inline T string_to_int_no_overflow(const char* __restrict s, int len, |
427 | | ParseResult* result); |
428 | | |
429 | | // zero length, or at least one legal digit. at most consume MAX_LEN digits and stop. or stop when next |
430 | | // char is not a digit. |
431 | | template <typename T> |
432 | | static inline T string_to_uint_greedy_no_overflow(const char* __restrict s, int max_len, |
433 | | ParseResult* result); |
434 | | |
435 | | // This is considerably faster than glibc's implementation (>100x why???) |
436 | | // No special case handling needs to be done for overflows, the floating point spec |
437 | | // already does it and will cap the values to -inf/inf |
438 | | // To avoid inaccurate conversions this function falls back to strtod for |
439 | | // scientific notation. |
440 | | // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed. |
441 | | // TODO: Investigate using intrinsics to speed up the slow strtod path. |
442 | | template <typename T> |
443 | | static inline T string_to_float_internal(const char* __restrict s, int len, |
444 | | ParseResult* result); |
445 | | |
446 | | // parses a string for 'true' or 'false', case insensitive |
447 | | // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed. |
448 | | static inline bool string_to_bool_internal(const char* __restrict s, int len, |
449 | | ParseResult* result); |
450 | | |
451 | | // Returns true if s only contains whitespace. |
452 | 5.08k | static inline bool is_all_whitespace(const char* __restrict s, int len) { |
453 | 11.7k | for (int i = 0; i < len; ++i) { |
454 | 10.0k | if (!LIKELY(is_whitespace_ascii(s[i]))) { |
455 | 3.38k | return false; |
456 | 3.38k | } |
457 | 10.0k | } |
458 | 1.70k | return true; |
459 | 5.08k | } |
460 | | |
461 | | // For strings like "3.0", "3.123", and "3.", can parse them as 3. |
462 | 57.2k | static inline bool is_float_suffix(const char* __restrict s, int len) { |
463 | 57.2k | return (s[0] == '.' && is_all_digit(s + 1, len - 1)); |
464 | 57.2k | } |
465 | | |
466 | 45.8k | static inline bool is_all_digit(const char* __restrict s, int len) { |
467 | 91.9k | for (int i = 0; i < len; ++i) { |
468 | 46.2k | if (!LIKELY(s[i] >= '0' && s[i] <= '9')) { |
469 | 155 | return false; |
470 | 155 | } |
471 | 46.2k | } |
472 | 45.6k | return true; |
473 | 45.8k | } |
474 | | }; // end of class StringParser |
475 | | |
476 | | template <typename T, bool enable_strict_mode> |
477 | 128M | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { |
478 | 128M | if (UNLIKELY(len <= 0)) { |
479 | 101k | *result = PARSE_FAILURE; |
480 | 101k | return 0; |
481 | 101k | } |
482 | | |
483 | 128M | using UnsignedT = MakeUnsignedT<T>; |
484 | 128M | UnsignedT val = 0; |
485 | 128M | UnsignedT max_val = StringParser::numeric_limits<T>(false); |
486 | 128M | bool negative = false; |
487 | 128M | int i = 0; |
488 | 128M | switch (*s) { |
489 | 2.61M | case '-': |
490 | 2.61M | negative = true; |
491 | | // Only signed types have an extra value in their negative range. |
492 | 2.61M | max_val += std::numeric_limits<T>::is_signed; |
493 | 2.61M | [[fallthrough]]; |
494 | 2.61M | case '+': |
495 | 2.61M | ++i; |
496 | | // only one '+'/'-' char, so could return failure directly |
497 | 2.61M | if (UNLIKELY(len == 1)) { |
498 | 6 | *result = PARSE_FAILURE; |
499 | 6 | return 0; |
500 | 6 | } |
501 | 128M | } |
502 | | |
503 | | // This is the fast path where the string cannot overflow. |
504 | 128M | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { |
505 | 127M | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); |
506 | 127M | return static_cast<T>(negative ? -val : val); |
507 | 127M | } |
508 | | |
509 | 1.02M | const T max_div_10 = max_val / 10; |
510 | 1.02M | const T max_mod_10 = max_val % 10; |
511 | | |
512 | 1.02M | int first = i; |
513 | 9.04M | for (; i < len; ++i) { |
514 | 8.12M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { |
515 | 8.07M | T digit = s[i] - '0'; |
516 | | // This is a tricky check to see if adding this digit will cause an overflow. |
517 | 8.07M | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { |
518 | 49.3k | *result = PARSE_OVERFLOW; |
519 | 49.3k | return negative ? -max_val : max_val; |
520 | 49.3k | } |
521 | 8.02M | val = val * 10 + digit; |
522 | 8.02M | } else { |
523 | 49.7k | if constexpr (enable_strict_mode) { |
524 | 5.00k | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { |
525 | | // Reject the string because the remaining chars are not all whitespace |
526 | 4.36k | *result = PARSE_FAILURE; |
527 | 4.36k | return 0; |
528 | 4.36k | } |
529 | 44.7k | } else { |
530 | | // Save original position where non-digit was found |
531 | 44.7k | int remaining_len = len - i; |
532 | 44.7k | const char* remaining_s = s + i; |
533 | | // Skip trailing whitespaces from the remaining portion |
534 | 44.7k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); |
535 | 44.7k | if ((UNLIKELY(i == first || (remaining_len != 0 && |
536 | 44.7k | !is_float_suffix(remaining_s, remaining_len))))) { |
537 | | // Reject the string because either the first char was not a digit, |
538 | | // or the remaining chars are not all whitespace |
539 | 31.3k | *result = PARSE_FAILURE; |
540 | 31.3k | return 0; |
541 | 31.3k | } |
542 | 44.7k | } |
543 | | // Returning here is slightly faster than breaking the loop. |
544 | 14.0k | *result = PARSE_SUCCESS; |
545 | 49.7k | return static_cast<T>(negative ? -val : val); |
546 | 49.7k | } |
547 | 8.12M | } |
548 | 922k | *result = PARSE_SUCCESS; |
549 | 922k | return static_cast<T>(negative ? -val : val); |
550 | 1.02M | } _ZN5doris12StringParser22string_to_int_internalInLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 824k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 824k | if (UNLIKELY(len <= 0)) { | 479 | 10.5k | *result = PARSE_FAILURE; | 480 | 10.5k | return 0; | 481 | 10.5k | } | 482 | | | 483 | 814k | using UnsignedT = MakeUnsignedT<T>; | 484 | 814k | UnsignedT val = 0; | 485 | 814k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 814k | bool negative = false; | 487 | 814k | int i = 0; | 488 | 814k | switch (*s) { | 489 | 288k | case '-': | 490 | 288k | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 288k | max_val += std::numeric_limits<T>::is_signed; | 493 | 288k | [[fallthrough]]; | 494 | 289k | case '+': | 495 | 289k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 289k | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 814k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 814k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 809k | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 809k | return static_cast<T>(negative ? -val : val); | 507 | 809k | } | 508 | | | 509 | 4.92k | const T max_div_10 = max_val / 10; | 510 | 4.92k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 4.92k | int first = i; | 513 | 183k | for (; i < len; ++i) { | 514 | 179k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 179k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 179k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 518 | *result = PARSE_OVERFLOW; | 519 | 518 | return negative ? -max_val : max_val; | 520 | 518 | } | 521 | 178k | val = val * 10 + digit; | 522 | 178k | } else { | 523 | | if constexpr (enable_strict_mode) { | 524 | | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | | *result = PARSE_FAILURE; | 527 | | return 0; | 528 | | } | 529 | 533 | } else { | 530 | | // Save original position where non-digit was found | 531 | 533 | int remaining_len = len - i; | 532 | 533 | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | 533 | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | 533 | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | 533 | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | 376 | *result = PARSE_FAILURE; | 540 | 376 | return 0; | 541 | 376 | } | 542 | 533 | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 157 | *result = PARSE_SUCCESS; | 545 | 533 | return static_cast<T>(negative ? -val : val); | 546 | 533 | } | 547 | 179k | } | 548 | 3.87k | *result = PARSE_SUCCESS; | 549 | 3.87k | return static_cast<T>(negative ? -val : val); | 550 | 4.92k | } |
_ZN5doris12StringParser22string_to_int_internalIjLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 1.54k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 1.54k | if (UNLIKELY(len <= 0)) { | 479 | 7 | *result = PARSE_FAILURE; | 480 | 7 | return 0; | 481 | 7 | } | 482 | | | 483 | 1.53k | using UnsignedT = MakeUnsignedT<T>; | 484 | 1.53k | UnsignedT val = 0; | 485 | 1.53k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 1.53k | bool negative = false; | 487 | 1.53k | int i = 0; | 488 | 1.53k | switch (*s) { | 489 | 364 | case '-': | 490 | 364 | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 364 | max_val += std::numeric_limits<T>::is_signed; | 493 | 364 | [[fallthrough]]; | 494 | 364 | case '+': | 495 | 364 | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 364 | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 1.53k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 1.53k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 936 | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 936 | return static_cast<T>(negative ? -val : val); | 507 | 936 | } | 508 | | | 509 | 603 | const T max_div_10 = max_val / 10; | 510 | 603 | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 603 | int first = i; | 513 | 4.07k | for (; i < len; ++i) { | 514 | 4.05k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 3.64k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 3.64k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 168 | *result = PARSE_OVERFLOW; | 519 | 168 | return negative ? -max_val : max_val; | 520 | 168 | } | 521 | 3.47k | val = val * 10 + digit; | 522 | 3.47k | } else { | 523 | 414 | if constexpr (enable_strict_mode) { | 524 | 414 | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | 288 | *result = PARSE_FAILURE; | 527 | 288 | return 0; | 528 | 288 | } | 529 | | } else { | 530 | | // Save original position where non-digit was found | 531 | | int remaining_len = len - i; | 532 | | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | | *result = PARSE_FAILURE; | 540 | | return 0; | 541 | | } | 542 | | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 126 | *result = PARSE_SUCCESS; | 545 | 414 | return static_cast<T>(negative ? -val : val); | 546 | 414 | } | 547 | 4.05k | } | 548 | 21 | *result = PARSE_SUCCESS; | 549 | 21 | return static_cast<T>(negative ? -val : val); | 550 | 603 | } |
_ZN5doris12StringParser22string_to_int_internalIaLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 2.70k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 2.70k | if (UNLIKELY(len <= 0)) { | 479 | 8 | *result = PARSE_FAILURE; | 480 | 8 | return 0; | 481 | 8 | } | 482 | | | 483 | 2.69k | using UnsignedT = MakeUnsignedT<T>; | 484 | 2.69k | UnsignedT val = 0; | 485 | 2.69k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 2.69k | bool negative = false; | 487 | 2.69k | int i = 0; | 488 | 2.69k | switch (*s) { | 489 | 1.02k | case '-': | 490 | 1.02k | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 1.02k | max_val += std::numeric_limits<T>::is_signed; | 493 | 1.02k | [[fallthrough]]; | 494 | 1.38k | case '+': | 495 | 1.38k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 1.38k | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 2.69k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 2.69k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 478 | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 478 | return static_cast<T>(negative ? -val : val); | 507 | 478 | } | 508 | | | 509 | 2.21k | const T max_div_10 = max_val / 10; | 510 | 2.21k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 2.21k | int first = i; | 513 | 7.59k | for (; i < len; ++i) { | 514 | 7.43k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 6.03k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 6.03k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 656 | *result = PARSE_OVERFLOW; | 519 | 656 | return negative ? -max_val : max_val; | 520 | 656 | } | 521 | 5.37k | val = val * 10 + digit; | 522 | 5.37k | } else { | 523 | 1.40k | if constexpr (enable_strict_mode) { | 524 | 1.40k | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | 1.22k | *result = PARSE_FAILURE; | 527 | 1.22k | return 0; | 528 | 1.22k | } | 529 | | } else { | 530 | | // Save original position where non-digit was found | 531 | | int remaining_len = len - i; | 532 | | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | | *result = PARSE_FAILURE; | 540 | | return 0; | 541 | | } | 542 | | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 172 | *result = PARSE_SUCCESS; | 545 | 1.40k | return static_cast<T>(negative ? -val : val); | 546 | 1.40k | } | 547 | 7.43k | } | 548 | 162 | *result = PARSE_SUCCESS; | 549 | 162 | return static_cast<T>(negative ? -val : val); | 550 | 2.21k | } |
_ZN5doris12StringParser22string_to_int_internalIsLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 2.38k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 2.38k | if (UNLIKELY(len <= 0)) { | 479 | 8 | *result = PARSE_FAILURE; | 480 | 8 | return 0; | 481 | 8 | } | 482 | | | 483 | 2.37k | using UnsignedT = MakeUnsignedT<T>; | 484 | 2.37k | UnsignedT val = 0; | 485 | 2.37k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 2.37k | bool negative = false; | 487 | 2.37k | int i = 0; | 488 | 2.37k | switch (*s) { | 489 | 630 | case '-': | 490 | 630 | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 630 | max_val += std::numeric_limits<T>::is_signed; | 493 | 630 | [[fallthrough]]; | 494 | 980 | case '+': | 495 | 980 | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 980 | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 2.37k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 2.37k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 648 | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 648 | return static_cast<T>(negative ? -val : val); | 507 | 648 | } | 508 | | | 509 | 1.72k | const T max_div_10 = max_val / 10; | 510 | 1.72k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 1.72k | int first = i; | 513 | 7.99k | for (; i < len; ++i) { | 514 | 7.93k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 6.84k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 6.84k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 576 | *result = PARSE_OVERFLOW; | 519 | 576 | return negative ? -max_val : max_val; | 520 | 576 | } | 521 | 6.27k | val = val * 10 + digit; | 522 | 6.27k | } else { | 523 | 1.08k | if constexpr (enable_strict_mode) { | 524 | 1.08k | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | 1.00k | *result = PARSE_FAILURE; | 527 | 1.00k | return 0; | 528 | 1.00k | } | 529 | | } else { | 530 | | // Save original position where non-digit was found | 531 | | int remaining_len = len - i; | 532 | | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | | *result = PARSE_FAILURE; | 540 | | return 0; | 541 | | } | 542 | | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 88 | *result = PARSE_SUCCESS; | 545 | 1.08k | return static_cast<T>(negative ? -val : val); | 546 | 1.08k | } | 547 | 7.93k | } | 548 | 60 | *result = PARSE_SUCCESS; | 549 | 60 | return static_cast<T>(negative ? -val : val); | 550 | 1.72k | } |
_ZN5doris12StringParser22string_to_int_internalIiLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 2.61k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 2.61k | if (UNLIKELY(len <= 0)) { | 479 | 8 | *result = PARSE_FAILURE; | 480 | 8 | return 0; | 481 | 8 | } | 482 | | | 483 | 2.60k | using UnsignedT = MakeUnsignedT<T>; | 484 | 2.60k | UnsignedT val = 0; | 485 | 2.60k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 2.60k | bool negative = false; | 487 | 2.60k | int i = 0; | 488 | 2.60k | switch (*s) { | 489 | 622 | case '-': | 490 | 622 | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 622 | max_val += std::numeric_limits<T>::is_signed; | 493 | 622 | [[fallthrough]]; | 494 | 966 | case '+': | 495 | 966 | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 966 | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 2.60k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 2.60k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 1.19k | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 1.19k | return static_cast<T>(negative ? -val : val); | 507 | 1.19k | } | 508 | | | 509 | 1.40k | const T max_div_10 = max_val / 10; | 510 | 1.40k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 1.40k | int first = i; | 513 | 11.0k | for (; i < len; ++i) { | 514 | 10.9k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 10.1k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 10.1k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 552 | *result = PARSE_OVERFLOW; | 519 | 552 | return negative ? -max_val : max_val; | 520 | 552 | } | 521 | 9.60k | val = val * 10 + digit; | 522 | 9.60k | } else { | 523 | 795 | if constexpr (enable_strict_mode) { | 524 | 795 | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | 735 | *result = PARSE_FAILURE; | 527 | 735 | return 0; | 528 | 735 | } | 529 | | } else { | 530 | | // Save original position where non-digit was found | 531 | | int remaining_len = len - i; | 532 | | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | | *result = PARSE_FAILURE; | 540 | | return 0; | 541 | | } | 542 | | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 60 | *result = PARSE_SUCCESS; | 545 | 795 | return static_cast<T>(negative ? -val : val); | 546 | 795 | } | 547 | 10.9k | } | 548 | 58 | *result = PARSE_SUCCESS; | 549 | 58 | return static_cast<T>(negative ? -val : val); | 550 | 1.40k | } |
_ZN5doris12StringParser22string_to_int_internalIlLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 2.62k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 2.62k | if (UNLIKELY(len <= 0)) { | 479 | 10 | *result = PARSE_FAILURE; | 480 | 10 | return 0; | 481 | 10 | } | 482 | | | 483 | 2.61k | using UnsignedT = MakeUnsignedT<T>; | 484 | 2.61k | UnsignedT val = 0; | 485 | 2.61k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 2.61k | bool negative = false; | 487 | 2.61k | int i = 0; | 488 | 2.61k | switch (*s) { | 489 | 715 | case '-': | 490 | 715 | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 715 | max_val += std::numeric_limits<T>::is_signed; | 493 | 715 | [[fallthrough]]; | 494 | 1.05k | case '+': | 495 | 1.05k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 1.05k | if (UNLIKELY(len == 1)) { | 498 | 2 | *result = PARSE_FAILURE; | 499 | 2 | return 0; | 500 | 2 | } | 501 | 2.61k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 2.61k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 1.22k | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 1.22k | return static_cast<T>(negative ? -val : val); | 507 | 1.22k | } | 508 | | | 509 | 1.38k | const T max_div_10 = max_val / 10; | 510 | 1.38k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 1.38k | int first = i; | 513 | 20.5k | for (; i < len; ++i) { | 514 | 20.5k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 19.7k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 19.7k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 586 | *result = PARSE_OVERFLOW; | 519 | 586 | return negative ? -max_val : max_val; | 520 | 586 | } | 521 | 19.1k | val = val * 10 + digit; | 522 | 19.1k | } else { | 523 | 733 | if constexpr (enable_strict_mode) { | 524 | 733 | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | 617 | *result = PARSE_FAILURE; | 527 | 617 | return 0; | 528 | 617 | } | 529 | | } else { | 530 | | // Save original position where non-digit was found | 531 | | int remaining_len = len - i; | 532 | | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | | *result = PARSE_FAILURE; | 540 | | return 0; | 541 | | } | 542 | | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 116 | *result = PARSE_SUCCESS; | 545 | 733 | return static_cast<T>(negative ? -val : val); | 546 | 733 | } | 547 | 20.5k | } | 548 | 70 | *result = PARSE_SUCCESS; | 549 | 70 | return static_cast<T>(negative ? -val : val); | 550 | 1.38k | } |
_ZN5doris12StringParser22string_to_int_internalInLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 2.37k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 2.37k | if (UNLIKELY(len <= 0)) { | 479 | 8 | *result = PARSE_FAILURE; | 480 | 8 | return 0; | 481 | 8 | } | 482 | | | 483 | 2.36k | using UnsignedT = MakeUnsignedT<T>; | 484 | 2.36k | UnsignedT val = 0; | 485 | 2.36k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 2.36k | bool negative = false; | 487 | 2.36k | int i = 0; | 488 | 2.36k | switch (*s) { | 489 | 643 | case '-': | 490 | 643 | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 643 | max_val += std::numeric_limits<T>::is_signed; | 493 | 643 | [[fallthrough]]; | 494 | 975 | case '+': | 495 | 975 | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 975 | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 2.36k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 2.36k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 1.23k | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 1.23k | return static_cast<T>(negative ? -val : val); | 507 | 1.23k | } | 508 | | | 509 | 1.13k | const T max_div_10 = max_val / 10; | 510 | 1.13k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 1.13k | int first = i; | 513 | 34.1k | for (; i < len; ++i) { | 514 | 34.0k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 33.4k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 33.4k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 504 | *result = PARSE_OVERFLOW; | 519 | 504 | return negative ? -max_val : max_val; | 520 | 504 | } | 521 | 32.9k | val = val * 10 + digit; | 522 | 32.9k | } else { | 523 | 572 | if constexpr (enable_strict_mode) { | 524 | 572 | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | 498 | *result = PARSE_FAILURE; | 527 | 498 | return 0; | 528 | 498 | } | 529 | | } else { | 530 | | // Save original position where non-digit was found | 531 | | int remaining_len = len - i; | 532 | | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | | *result = PARSE_FAILURE; | 540 | | return 0; | 541 | | } | 542 | | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 74 | *result = PARSE_SUCCESS; | 545 | 572 | return static_cast<T>(negative ? -val : val); | 546 | 572 | } | 547 | 34.0k | } | 548 | 59 | *result = PARSE_SUCCESS; | 549 | 59 | return static_cast<T>(negative ? -val : val); | 550 | 1.13k | } |
_ZN5doris12StringParser22string_to_int_internalIaLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 4.68M | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 4.68M | if (UNLIKELY(len <= 0)) { | 479 | 27.9k | *result = PARSE_FAILURE; | 480 | 27.9k | return 0; | 481 | 27.9k | } | 482 | | | 483 | 4.65M | using UnsignedT = MakeUnsignedT<T>; | 484 | 4.65M | UnsignedT val = 0; | 485 | 4.65M | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 4.65M | bool negative = false; | 487 | 4.65M | int i = 0; | 488 | 4.65M | switch (*s) { | 489 | 321k | case '-': | 490 | 321k | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 321k | max_val += std::numeric_limits<T>::is_signed; | 493 | 321k | [[fallthrough]]; | 494 | 321k | case '+': | 495 | 321k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 321k | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 4.65M | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 4.65M | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 4.44M | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 4.44M | return static_cast<T>(negative ? -val : val); | 507 | 4.44M | } | 508 | | | 509 | 205k | const T max_div_10 = max_val / 10; | 510 | 205k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 205k | int first = i; | 513 | 699k | for (; i < len; ++i) { | 514 | 551k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 514k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 514k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 21.2k | *result = PARSE_OVERFLOW; | 519 | 21.2k | return negative ? -max_val : max_val; | 520 | 21.2k | } | 521 | 493k | val = val * 10 + digit; | 522 | 493k | } else { | 523 | | if constexpr (enable_strict_mode) { | 524 | | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | | *result = PARSE_FAILURE; | 527 | | return 0; | 528 | | } | 529 | 36.9k | } else { | 530 | | // Save original position where non-digit was found | 531 | 36.9k | int remaining_len = len - i; | 532 | 36.9k | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | 36.9k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | 36.9k | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | 36.9k | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | 25.4k | *result = PARSE_FAILURE; | 540 | 25.4k | return 0; | 541 | 25.4k | } | 542 | 36.9k | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 11.4k | *result = PARSE_SUCCESS; | 545 | 36.9k | return static_cast<T>(negative ? -val : val); | 546 | 36.9k | } | 547 | 551k | } | 548 | 147k | *result = PARSE_SUCCESS; | 549 | 147k | return static_cast<T>(negative ? -val : val); | 550 | 205k | } |
_ZN5doris12StringParser22string_to_int_internalIsLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 865k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 865k | if (UNLIKELY(len <= 0)) { | 479 | 14.5k | *result = PARSE_FAILURE; | 480 | 14.5k | return 0; | 481 | 14.5k | } | 482 | | | 483 | 851k | using UnsignedT = MakeUnsignedT<T>; | 484 | 851k | UnsignedT val = 0; | 485 | 851k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 851k | bool negative = false; | 487 | 851k | int i = 0; | 488 | 851k | switch (*s) { | 489 | 310k | case '-': | 490 | 310k | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 310k | max_val += std::numeric_limits<T>::is_signed; | 493 | 310k | [[fallthrough]]; | 494 | 311k | case '+': | 495 | 311k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 311k | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 851k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 851k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 411k | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 411k | return static_cast<T>(negative ? -val : val); | 507 | 411k | } | 508 | | | 509 | 439k | const T max_div_10 = max_val / 10; | 510 | 439k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 439k | int first = i; | 513 | 2.61M | for (; i < len; ++i) { | 514 | 2.19M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 2.19M | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 2.19M | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 12.9k | *result = PARSE_OVERFLOW; | 519 | 12.9k | return negative ? -max_val : max_val; | 520 | 12.9k | } | 521 | 2.17M | val = val * 10 + digit; | 522 | 2.17M | } else { | 523 | | if constexpr (enable_strict_mode) { | 524 | | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | | *result = PARSE_FAILURE; | 527 | | return 0; | 528 | | } | 529 | 2.39k | } else { | 530 | | // Save original position where non-digit was found | 531 | 2.39k | int remaining_len = len - i; | 532 | 2.39k | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | 2.39k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | 2.39k | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | 2.39k | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | 1.79k | *result = PARSE_FAILURE; | 540 | 1.79k | return 0; | 541 | 1.79k | } | 542 | 2.39k | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 603 | *result = PARSE_SUCCESS; | 545 | 2.39k | return static_cast<T>(negative ? -val : val); | 546 | 2.39k | } | 547 | 2.19M | } | 548 | 424k | *result = PARSE_SUCCESS; | 549 | 424k | return static_cast<T>(negative ? -val : val); | 550 | 439k | } |
_ZN5doris12StringParser22string_to_int_internalIiLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 49.8M | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 49.8M | if (UNLIKELY(len <= 0)) { | 479 | 22.1k | *result = PARSE_FAILURE; | 480 | 22.1k | return 0; | 481 | 22.1k | } | 482 | | | 483 | 49.8M | using UnsignedT = MakeUnsignedT<T>; | 484 | 49.8M | UnsignedT val = 0; | 485 | 49.8M | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 49.8M | bool negative = false; | 487 | 49.8M | int i = 0; | 488 | 49.8M | switch (*s) { | 489 | 686k | case '-': | 490 | 686k | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 686k | max_val += std::numeric_limits<T>::is_signed; | 493 | 686k | [[fallthrough]]; | 494 | 687k | case '+': | 495 | 687k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 687k | if (UNLIKELY(len == 1)) { | 498 | 4 | *result = PARSE_FAILURE; | 499 | 4 | return 0; | 500 | 4 | } | 501 | 49.8M | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 49.8M | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 49.7M | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 49.7M | return static_cast<T>(negative ? -val : val); | 507 | 49.7M | } | 508 | | | 509 | 83.7k | const T max_div_10 = max_val / 10; | 510 | 83.7k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 83.7k | int first = i; | 513 | 730k | for (; i < len; ++i) { | 514 | 654k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 652k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 652k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 5.80k | *result = PARSE_OVERFLOW; | 519 | 5.80k | return negative ? -max_val : max_val; | 520 | 5.80k | } | 521 | 646k | val = val * 10 + digit; | 522 | 646k | } else { | 523 | | if constexpr (enable_strict_mode) { | 524 | | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | | *result = PARSE_FAILURE; | 527 | | return 0; | 528 | | } | 529 | 1.93k | } else { | 530 | | // Save original position where non-digit was found | 531 | 1.93k | int remaining_len = len - i; | 532 | 1.93k | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | 1.93k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | 1.93k | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | 1.93k | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | 1.55k | *result = PARSE_FAILURE; | 540 | 1.55k | return 0; | 541 | 1.55k | } | 542 | 1.93k | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 381 | *result = PARSE_SUCCESS; | 545 | 1.93k | return static_cast<T>(negative ? -val : val); | 546 | 1.93k | } | 547 | 654k | } | 548 | 76.0k | *result = PARSE_SUCCESS; | 549 | 76.0k | return static_cast<T>(negative ? -val : val); | 550 | 83.7k | } |
_ZN5doris12StringParser22string_to_int_internalIlLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 71.9M | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 71.9M | if (UNLIKELY(len <= 0)) { | 479 | 26.2k | *result = PARSE_FAILURE; | 480 | 26.2k | return 0; | 481 | 26.2k | } | 482 | | | 483 | 71.9M | using UnsignedT = MakeUnsignedT<T>; | 484 | 71.9M | UnsignedT val = 0; | 485 | 71.9M | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 71.9M | bool negative = false; | 487 | 71.9M | int i = 0; | 488 | 71.9M | switch (*s) { | 489 | 1.00M | case '-': | 490 | 1.00M | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 1.00M | max_val += std::numeric_limits<T>::is_signed; | 493 | 1.00M | [[fallthrough]]; | 494 | 1.00M | case '+': | 495 | 1.00M | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 1.00M | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 71.9M | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 71.9M | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 71.6M | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 71.6M | return static_cast<T>(negative ? -val : val); | 507 | 71.6M | } | 508 | | | 509 | 276k | const T max_div_10 = max_val / 10; | 510 | 276k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 276k | int first = i; | 513 | 4.70M | for (; i < len; ++i) { | 514 | 4.43M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 4.43M | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 4.43M | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 5.23k | *result = PARSE_OVERFLOW; | 519 | 5.23k | return negative ? -max_val : max_val; | 520 | 5.23k | } | 521 | 4.43M | val = val * 10 + digit; | 522 | 4.43M | } else { | 523 | | if constexpr (enable_strict_mode) { | 524 | | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | | *result = PARSE_FAILURE; | 527 | | return 0; | 528 | | } | 529 | 1.52k | } else { | 530 | | // Save original position where non-digit was found | 531 | 1.52k | int remaining_len = len - i; | 532 | 1.52k | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | 1.52k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | 1.52k | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | 1.52k | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | 1.24k | *result = PARSE_FAILURE; | 540 | 1.24k | return 0; | 541 | 1.24k | } | 542 | 1.52k | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 280 | *result = PARSE_SUCCESS; | 545 | 1.52k | return static_cast<T>(negative ? -val : val); | 546 | 1.52k | } | 547 | 4.43M | } | 548 | 269k | *result = PARSE_SUCCESS; | 549 | 269k | return static_cast<T>(negative ? -val : val); | 550 | 276k | } |
_ZN5doris12StringParser22string_to_int_internalIjLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 841 | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 841 | if (UNLIKELY(len <= 0)) { | 479 | 0 | *result = PARSE_FAILURE; | 480 | 0 | return 0; | 481 | 0 | } | 482 | | | 483 | 841 | using UnsignedT = MakeUnsignedT<T>; | 484 | 841 | UnsignedT val = 0; | 485 | 841 | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 841 | bool negative = false; | 487 | 841 | int i = 0; | 488 | 841 | switch (*s) { | 489 | 357 | case '-': | 490 | 357 | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 357 | max_val += std::numeric_limits<T>::is_signed; | 493 | 357 | [[fallthrough]]; | 494 | 357 | case '+': | 495 | 357 | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 357 | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 841 | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 841 | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 238 | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 238 | return static_cast<T>(negative ? -val : val); | 507 | 238 | } | 508 | | | 509 | 603 | const T max_div_10 = max_val / 10; | 510 | 603 | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 603 | int first = i; | 513 | 4.07k | for (; i < len; ++i) { | 514 | 4.05k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 3.64k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 3.64k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 168 | *result = PARSE_OVERFLOW; | 519 | 168 | return negative ? -max_val : max_val; | 520 | 168 | } | 521 | 3.47k | val = val * 10 + digit; | 522 | 3.47k | } else { | 523 | | if constexpr (enable_strict_mode) { | 524 | | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | | *result = PARSE_FAILURE; | 527 | | return 0; | 528 | | } | 529 | 414 | } else { | 530 | | // Save original position where non-digit was found | 531 | 414 | int remaining_len = len - i; | 532 | 414 | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | 414 | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | 414 | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | 414 | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | 288 | *result = PARSE_FAILURE; | 540 | 288 | return 0; | 541 | 288 | } | 542 | 414 | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 126 | *result = PARSE_SUCCESS; | 545 | 414 | return static_cast<T>(negative ? -val : val); | 546 | 414 | } | 547 | 4.05k | } | 548 | 21 | *result = PARSE_SUCCESS; | 549 | 21 | return static_cast<T>(negative ? -val : val); | 550 | 603 | } |
_ZN5doris12StringParser22string_to_int_internalImLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 1.84k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 1.84k | if (UNLIKELY(len <= 0)) { | 479 | 0 | *result = PARSE_FAILURE; | 480 | 0 | return 0; | 481 | 0 | } | 482 | | | 483 | 1.84k | using UnsignedT = MakeUnsignedT<T>; | 484 | 1.84k | UnsignedT val = 0; | 485 | 1.84k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 1.84k | bool negative = false; | 487 | 1.84k | int i = 0; | 488 | 1.84k | switch (*s) { | 489 | 357 | case '-': | 490 | 357 | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 357 | max_val += std::numeric_limits<T>::is_signed; | 493 | 357 | [[fallthrough]]; | 494 | 357 | case '+': | 495 | 357 | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 357 | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 1.84k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 1.84k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 1.27k | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 1.27k | return static_cast<T>(negative ? -val : val); | 507 | 1.27k | } | 508 | | | 509 | 567 | const T max_div_10 = max_val / 10; | 510 | 567 | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 567 | int first = i; | 513 | 6.69k | for (; i < len; ++i) { | 514 | 6.67k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 6.30k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 6.30k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 168 | *result = PARSE_OVERFLOW; | 519 | 168 | return negative ? -max_val : max_val; | 520 | 168 | } | 521 | 6.13k | val = val * 10 + digit; | 522 | 6.13k | } else { | 523 | | if constexpr (enable_strict_mode) { | 524 | | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | | *result = PARSE_FAILURE; | 527 | | return 0; | 528 | | } | 529 | 378 | } else { | 530 | | // Save original position where non-digit was found | 531 | 378 | int remaining_len = len - i; | 532 | 378 | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | 378 | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | 378 | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | 378 | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | 252 | *result = PARSE_FAILURE; | 540 | 252 | return 0; | 541 | 252 | } | 542 | 378 | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 126 | *result = PARSE_SUCCESS; | 545 | 378 | return static_cast<T>(negative ? -val : val); | 546 | 378 | } | 547 | 6.67k | } | 548 | 21 | *result = PARSE_SUCCESS; | 549 | 21 | return static_cast<T>(negative ? -val : val); | 550 | 567 | } |
_ZN5doris12StringParser22string_to_int_internalIN4wide7integerILm256EiEELb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 4 | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 4 | if (UNLIKELY(len <= 0)) { | 479 | 0 | *result = PARSE_FAILURE; | 480 | 0 | return 0; | 481 | 0 | } | 482 | | | 483 | 4 | using UnsignedT = MakeUnsignedT<T>; | 484 | 4 | UnsignedT val = 0; | 485 | 4 | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 4 | bool negative = false; | 487 | 4 | int i = 0; | 488 | 4 | switch (*s) { | 489 | 0 | case '-': | 490 | 0 | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 0 | max_val += std::numeric_limits<T>::is_signed; | 493 | 0 | [[fallthrough]]; | 494 | 0 | case '+': | 495 | 0 | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 0 | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 4 | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 4 | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 4 | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 4 | return static_cast<T>(negative ? -val : val); | 507 | 4 | } | 508 | | | 509 | 0 | const T max_div_10 = max_val / 10; | 510 | 0 | const T max_mod_10 = max_val % 10; | 511 | |
| 512 | 0 | int first = i; | 513 | 0 | for (; i < len; ++i) { | 514 | 0 | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 0 | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 0 | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 0 | *result = PARSE_OVERFLOW; | 519 | 0 | return negative ? -max_val : max_val; | 520 | 0 | } | 521 | 0 | val = val * 10 + digit; | 522 | 0 | } else { | 523 | | if constexpr (enable_strict_mode) { | 524 | | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | | *result = PARSE_FAILURE; | 527 | | return 0; | 528 | | } | 529 | 0 | } else { | 530 | | // Save original position where non-digit was found | 531 | 0 | int remaining_len = len - i; | 532 | 0 | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | 0 | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | 0 | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | 0 | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | 0 | *result = PARSE_FAILURE; | 540 | 0 | return 0; | 541 | 0 | } | 542 | 0 | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 0 | *result = PARSE_SUCCESS; | 545 | 0 | return static_cast<T>(negative ? -val : val); | 546 | 0 | } | 547 | 0 | } | 548 | 0 | *result = PARSE_SUCCESS; | 549 | 0 | return static_cast<T>(negative ? -val : val); | 550 | 0 | } |
_ZN5doris12StringParser22string_to_int_internalIoLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 477 | 844 | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 478 | 844 | if (UNLIKELY(len <= 0)) { | 479 | 0 | *result = PARSE_FAILURE; | 480 | 0 | return 0; | 481 | 0 | } | 482 | | | 483 | 844 | using UnsignedT = MakeUnsignedT<T>; | 484 | 844 | UnsignedT val = 0; | 485 | 844 | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 486 | 844 | bool negative = false; | 487 | 844 | int i = 0; | 488 | 844 | switch (*s) { | 489 | 357 | case '-': | 490 | 357 | negative = true; | 491 | | // Only signed types have an extra value in their negative range. | 492 | 357 | max_val += std::numeric_limits<T>::is_signed; | 493 | 357 | [[fallthrough]]; | 494 | 357 | case '+': | 495 | 357 | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 357 | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 844 | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 844 | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 0 | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 0 | return static_cast<T>(negative ? -val : val); | 507 | 0 | } | 508 | | | 509 | 844 | const T max_div_10 = max_val / 10; | 510 | 844 | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 844 | int first = i; | 513 | 12.3k | for (; i < len; ++i) { | 514 | 12.3k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 11.6k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 11.6k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 168 | *result = PARSE_OVERFLOW; | 519 | 168 | return negative ? -max_val : max_val; | 520 | 168 | } | 521 | 11.5k | val = val * 10 + digit; | 522 | 11.5k | } else { | 523 | | if constexpr (enable_strict_mode) { | 524 | | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | | *result = PARSE_FAILURE; | 527 | | return 0; | 528 | | } | 529 | 637 | } else { | 530 | | // Save original position where non-digit was found | 531 | 637 | int remaining_len = len - i; | 532 | 637 | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | 637 | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | 637 | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | 637 | !is_float_suffix(remaining_s, remaining_len))))) { | 537 | | // Reject the string because either the first char was not a digit, | 538 | | // or the remaining chars are not all whitespace | 539 | 378 | *result = PARSE_FAILURE; | 540 | 378 | return 0; | 541 | 378 | } | 542 | 637 | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 259 | *result = PARSE_SUCCESS; | 545 | 637 | return static_cast<T>(negative ? -val : val); | 546 | 637 | } | 547 | 12.3k | } | 548 | 39 | *result = PARSE_SUCCESS; | 549 | 39 | return static_cast<T>(negative ? -val : val); | 550 | 844 | } |
|
551 | | |
552 | | template <typename T> |
553 | | T StringParser::string_to_unsigned_int_internal(const char* __restrict s, int len, |
554 | 3.81k | ParseResult* result) { |
555 | 3.81k | if (UNLIKELY(len <= 0)) { |
556 | 0 | *result = PARSE_FAILURE; |
557 | 0 | return 0; |
558 | 0 | } |
559 | | |
560 | 3.81k | T val = 0; |
561 | 3.81k | T max_val = std::numeric_limits<T>::max(); |
562 | 3.81k | int i = 0; |
563 | | |
564 | 3.81k | using signedT = MakeSignedT<T>; |
565 | | // This is the fast path where the string cannot overflow. |
566 | 3.81k | if (LIKELY(len - i < NumberTraits::max_ascii_len<signedT>())) { |
567 | 3.71k | val = string_to_int_no_overflow<T>(s + i, len - i, result); |
568 | 3.71k | return val; |
569 | 3.71k | } |
570 | | |
571 | 99 | const T max_div_10 = max_val / 10; |
572 | 99 | const T max_mod_10 = max_val % 10; |
573 | | |
574 | 99 | int first = i; |
575 | 2.01k | for (; i < len; ++i) { |
576 | 1.96k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { |
577 | 1.96k | T digit = s[i] - '0'; |
578 | | // This is a tricky check to see if adding this digit will cause an overflow. |
579 | 1.96k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { |
580 | 49 | *result = PARSE_OVERFLOW; |
581 | 49 | return max_val; |
582 | 49 | } |
583 | 1.91k | val = val * 10 + digit; |
584 | 1.91k | } else { |
585 | 0 | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { |
586 | | // Reject the string because either the first char was not a digit, |
587 | | // or the remaining chars are not all whitespace |
588 | 0 | *result = PARSE_FAILURE; |
589 | 0 | return 0; |
590 | 0 | } |
591 | | // Returning here is slightly faster than breaking the loop. |
592 | 0 | *result = PARSE_SUCCESS; |
593 | 0 | return val; |
594 | 0 | } |
595 | 1.96k | } |
596 | 50 | *result = PARSE_SUCCESS; |
597 | 50 | return val; |
598 | 99 | } |
599 | | |
600 | | template <typename T> |
601 | | T StringParser::string_to_int_internal(const char* __restrict s, int64_t len, int base, |
602 | 3.49k | ParseResult* result) { |
603 | 3.49k | using UnsignedT = MakeUnsignedT<T>; |
604 | 3.49k | UnsignedT val = 0; |
605 | 3.49k | UnsignedT max_val = StringParser::numeric_limits<T>(false); |
606 | 3.49k | bool negative = false; |
607 | 3.49k | if (UNLIKELY(len <= 0)) { |
608 | 19 | *result = PARSE_FAILURE; |
609 | 19 | return 0; |
610 | 19 | } |
611 | 3.47k | int i = 0; |
612 | 3.47k | switch (*s) { |
613 | 1.53k | case '-': |
614 | 1.53k | negative = true; |
615 | | // Only signed types have an extra value in their negative range. |
616 | | // Keep the unsigned magnitude limit unchanged to avoid wrapping it to zero. |
617 | 1.53k | max_val += std::numeric_limits<T>::is_signed; |
618 | 1.53k | [[fallthrough]]; |
619 | 1.53k | case '+': |
620 | 1.53k | i = 1; |
621 | 3.47k | } |
622 | | |
623 | 3.47k | const T max_div_base = max_val / base; |
624 | 3.47k | const T max_mod_base = max_val % base; |
625 | | |
626 | 3.47k | int first = i; |
627 | 16.0k | for (; i < len; ++i) { |
628 | 12.9k | T digit; |
629 | 12.9k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { |
630 | 12.5k | digit = s[i] - '0'; |
631 | 12.5k | } else if (s[i] >= 'a' && s[i] <= 'z') { |
632 | 314 | digit = (s[i] - 'a' + 10); |
633 | 314 | } else if (s[i] >= 'A' && s[i] <= 'Z') { |
634 | 24 | digit = (s[i] - 'A' + 10); |
635 | 24 | } else { |
636 | 8 | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { |
637 | | // Reject the string because either the first char was not an alpha/digit, |
638 | | // or the remaining chars are not all whitespace |
639 | 8 | *result = PARSE_FAILURE; |
640 | 8 | return 0; |
641 | 8 | } |
642 | | // skip trailing whitespace. |
643 | 0 | break; |
644 | 8 | } |
645 | | |
646 | | // Bail, if we encounter a digit that is not available in base. |
647 | 12.9k | if (digit >= base) { |
648 | 257 | break; |
649 | 257 | } |
650 | | |
651 | | // This is a tricky check to see if adding this digit will cause an overflow. |
652 | 12.6k | if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) { |
653 | 100 | *result = PARSE_OVERFLOW; |
654 | 100 | return static_cast<T>(negative ? -max_val : max_val); |
655 | 100 | } |
656 | 12.5k | val = val * base + digit; |
657 | 12.5k | } |
658 | 3.36k | *result = PARSE_SUCCESS; |
659 | 3.36k | return static_cast<T>(negative ? -val : val); |
660 | 3.47k | } |
661 | | |
662 | | template <typename T, bool enable_strict_mode> |
663 | 127M | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { |
664 | 127M | T val = 0; |
665 | 127M | if (UNLIKELY(len == 0)) { |
666 | 0 | *result = PARSE_SUCCESS; |
667 | 0 | return val; |
668 | 0 | } |
669 | | // Factor out the first char for error handling speeds up the loop. |
670 | 127M | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { |
671 | 127M | val = s[0] - '0'; |
672 | 18.4E | } else { |
673 | 18.4E | *result = PARSE_FAILURE; |
674 | 18.4E | return 0; |
675 | 18.4E | } |
676 | 530M | for (int i = 1; i < len; ++i) { |
677 | 403M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { |
678 | 403M | T digit = s[i] - '0'; |
679 | 403M | val = val * 10 + digit; |
680 | 403M | } else { |
681 | 72.3k | if constexpr (enable_strict_mode) { |
682 | 1.64k | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { |
683 | 1.29k | *result = PARSE_FAILURE; |
684 | 1.29k | return 0; |
685 | 1.29k | } |
686 | 70.6k | } else { |
687 | | // Save original position where non-digit was found |
688 | 70.6k | int remaining_len = len - i; |
689 | 70.6k | const char* remaining_s = s + i; |
690 | | // Skip trailing whitespaces from the remaining portion |
691 | 70.6k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); |
692 | 70.6k | if ((UNLIKELY(remaining_len != 0 && |
693 | 70.6k | !is_float_suffix(remaining_s, remaining_len)))) { |
694 | 10.9k | *result = PARSE_FAILURE; |
695 | 10.9k | return 0; |
696 | 10.9k | } |
697 | 70.6k | } |
698 | 60.0k | *result = PARSE_SUCCESS; |
699 | 72.3k | return val; |
700 | 72.3k | } |
701 | 403M | } |
702 | 126M | *result = PARSE_SUCCESS; |
703 | 126M | return val; |
704 | 127M | } _ZN5doris12StringParser25string_to_int_no_overflowIoLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 663 | 809k | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 664 | 809k | T val = 0; | 665 | 809k | if (UNLIKELY(len == 0)) { | 666 | 0 | *result = PARSE_SUCCESS; | 667 | 0 | return val; | 668 | 0 | } | 669 | | // Factor out the first char for error handling speeds up the loop. | 670 | 809k | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 671 | 808k | val = s[0] - '0'; | 672 | 808k | } else { | 673 | 1.34k | *result = PARSE_FAILURE; | 674 | 1.34k | return 0; | 675 | 1.34k | } | 676 | 6.58M | for (int i = 1; i < len; ++i) { | 677 | 5.79M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 678 | 5.78M | T digit = s[i] - '0'; | 679 | 5.78M | val = val * 10 + digit; | 680 | 5.78M | } else { | 681 | | if constexpr (enable_strict_mode) { | 682 | | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 683 | | *result = PARSE_FAILURE; | 684 | | return 0; | 685 | | } | 686 | 10.7k | } else { | 687 | | // Save original position where non-digit was found | 688 | 10.7k | int remaining_len = len - i; | 689 | 10.7k | const char* remaining_s = s + i; | 690 | | // Skip trailing whitespaces from the remaining portion | 691 | 10.7k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 692 | 10.7k | if ((UNLIKELY(remaining_len != 0 && | 693 | 10.7k | !is_float_suffix(remaining_s, remaining_len)))) { | 694 | 10.3k | *result = PARSE_FAILURE; | 695 | 10.3k | return 0; | 696 | 10.3k | } | 697 | 10.7k | } | 698 | 350 | *result = PARSE_SUCCESS; | 699 | 10.7k | return val; | 700 | 10.7k | } | 701 | 5.79M | } | 702 | 797k | *result = PARSE_SUCCESS; | 703 | 797k | return val; | 704 | 808k | } |
_ZN5doris12StringParser25string_to_int_no_overflowIjLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 663 | 2.13k | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 664 | 2.13k | T val = 0; | 665 | 2.13k | if (UNLIKELY(len == 0)) { | 666 | 0 | *result = PARSE_SUCCESS; | 667 | 0 | return val; | 668 | 0 | } | 669 | | // Factor out the first char for error handling speeds up the loop. | 670 | 2.13k | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 671 | 1.93k | val = s[0] - '0'; | 672 | 1.93k | } else { | 673 | 197 | *result = PARSE_FAILURE; | 674 | 197 | return 0; | 675 | 197 | } | 676 | 5.59k | for (int i = 1; i < len; ++i) { | 677 | 4.12k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 678 | 3.66k | T digit = s[i] - '0'; | 679 | 3.66k | val = val * 10 + digit; | 680 | 3.66k | } else { | 681 | 460 | if constexpr (enable_strict_mode) { | 682 | 460 | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 683 | 348 | *result = PARSE_FAILURE; | 684 | 348 | return 0; | 685 | 348 | } | 686 | | } else { | 687 | | // Save original position where non-digit was found | 688 | | int remaining_len = len - i; | 689 | | const char* remaining_s = s + i; | 690 | | // Skip trailing whitespaces from the remaining portion | 691 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 692 | | if ((UNLIKELY(remaining_len != 0 && | 693 | | !is_float_suffix(remaining_s, remaining_len)))) { | 694 | | *result = PARSE_FAILURE; | 695 | | return 0; | 696 | | } | 697 | | } | 698 | 112 | *result = PARSE_SUCCESS; | 699 | 460 | return val; | 700 | 460 | } | 701 | 4.12k | } | 702 | 1.47k | *result = PARSE_SUCCESS; | 703 | 1.47k | return val; | 704 | 1.93k | } |
_ZN5doris12StringParser25string_to_int_no_overflowIhLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 663 | 478 | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 664 | 478 | T val = 0; | 665 | 478 | if (UNLIKELY(len == 0)) { | 666 | 0 | *result = PARSE_SUCCESS; | 667 | 0 | return val; | 668 | 0 | } | 669 | | // Factor out the first char for error handling speeds up the loop. | 670 | 478 | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 671 | 452 | val = s[0] - '0'; | 672 | 452 | } else { | 673 | 26 | *result = PARSE_FAILURE; | 674 | 26 | return 0; | 675 | 26 | } | 676 | 790 | for (int i = 1; i < len; ++i) { | 677 | 340 | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 678 | 338 | T digit = s[i] - '0'; | 679 | 338 | val = val * 10 + digit; | 680 | 338 | } else { | 681 | 2 | if constexpr (enable_strict_mode) { | 682 | 2 | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 683 | 2 | *result = PARSE_FAILURE; | 684 | 2 | return 0; | 685 | 2 | } | 686 | | } else { | 687 | | // Save original position where non-digit was found | 688 | | int remaining_len = len - i; | 689 | | const char* remaining_s = s + i; | 690 | | // Skip trailing whitespaces from the remaining portion | 691 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 692 | | if ((UNLIKELY(remaining_len != 0 && | 693 | | !is_float_suffix(remaining_s, remaining_len)))) { | 694 | | *result = PARSE_FAILURE; | 695 | | return 0; | 696 | | } | 697 | | } | 698 | 0 | *result = PARSE_SUCCESS; | 699 | 2 | return val; | 700 | 2 | } | 701 | 340 | } | 702 | 450 | *result = PARSE_SUCCESS; | 703 | 450 | return val; | 704 | 452 | } |
_ZN5doris12StringParser25string_to_int_no_overflowItLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 663 | 712 | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 664 | 712 | T val = 0; | 665 | 712 | if (UNLIKELY(len == 0)) { | 666 | 0 | *result = PARSE_SUCCESS; | 667 | 0 | return val; | 668 | 0 | } | 669 | | // Factor out the first char for error handling speeds up the loop. | 670 | 712 | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 671 | 674 | val = s[0] - '0'; | 672 | 674 | } else { | 673 | 38 | *result = PARSE_FAILURE; | 674 | 38 | return 0; | 675 | 38 | } | 676 | 1.72k | for (int i = 1; i < len; ++i) { | 677 | 1.18k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 678 | 1.05k | T digit = s[i] - '0'; | 679 | 1.05k | val = val * 10 + digit; | 680 | 1.05k | } else { | 681 | 132 | if constexpr (enable_strict_mode) { | 682 | 132 | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 683 | 90 | *result = PARSE_FAILURE; | 684 | 90 | return 0; | 685 | 90 | } | 686 | | } else { | 687 | | // Save original position where non-digit was found | 688 | | int remaining_len = len - i; | 689 | | const char* remaining_s = s + i; | 690 | | // Skip trailing whitespaces from the remaining portion | 691 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 692 | | if ((UNLIKELY(remaining_len != 0 && | 693 | | !is_float_suffix(remaining_s, remaining_len)))) { | 694 | | *result = PARSE_FAILURE; | 695 | | return 0; | 696 | | } | 697 | | } | 698 | 42 | *result = PARSE_SUCCESS; | 699 | 132 | return val; | 700 | 132 | } | 701 | 1.18k | } | 702 | 542 | *result = PARSE_SUCCESS; | 703 | 542 | return val; | 704 | 674 | } |
_ZN5doris12StringParser25string_to_int_no_overflowImLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 663 | 1.50k | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 664 | 1.50k | T val = 0; | 665 | 1.50k | if (UNLIKELY(len == 0)) { | 666 | 0 | *result = PARSE_SUCCESS; | 667 | 0 | return val; | 668 | 0 | } | 669 | | // Factor out the first char for error handling speeds up the loop. | 670 | 1.50k | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 671 | 1.16k | val = s[0] - '0'; | 672 | 1.16k | } else { | 673 | 343 | *result = PARSE_FAILURE; | 674 | 343 | return 0; | 675 | 343 | } | 676 | 4.21k | for (int i = 1; i < len; ++i) { | 677 | 3.64k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 678 | 3.04k | T digit = s[i] - '0'; | 679 | 3.04k | val = val * 10 + digit; | 680 | 3.04k | } else { | 681 | 596 | if constexpr (enable_strict_mode) { | 682 | 596 | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 683 | 456 | *result = PARSE_FAILURE; | 684 | 456 | return 0; | 685 | 456 | } | 686 | | } else { | 687 | | // Save original position where non-digit was found | 688 | | int remaining_len = len - i; | 689 | | const char* remaining_s = s + i; | 690 | | // Skip trailing whitespaces from the remaining portion | 691 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 692 | | if ((UNLIKELY(remaining_len != 0 && | 693 | | !is_float_suffix(remaining_s, remaining_len)))) { | 694 | | *result = PARSE_FAILURE; | 695 | | return 0; | 696 | | } | 697 | | } | 698 | 140 | *result = PARSE_SUCCESS; | 699 | 596 | return val; | 700 | 596 | } | 701 | 3.64k | } | 702 | 567 | *result = PARSE_SUCCESS; | 703 | 567 | return val; | 704 | 1.16k | } |
_ZN5doris12StringParser25string_to_int_no_overflowIoLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 663 | 1.23k | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 664 | 1.23k | T val = 0; | 665 | 1.23k | if (UNLIKELY(len == 0)) { | 666 | 0 | *result = PARSE_SUCCESS; | 667 | 0 | return val; | 668 | 0 | } | 669 | | // Factor out the first char for error handling speeds up the loop. | 670 | 1.23k | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 671 | 992 | val = s[0] - '0'; | 672 | 992 | } else { | 673 | 240 | *result = PARSE_FAILURE; | 674 | 240 | return 0; | 675 | 240 | } | 676 | 4.37k | for (int i = 1; i < len; ++i) { | 677 | 3.84k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 678 | 3.38k | T digit = s[i] - '0'; | 679 | 3.38k | val = val * 10 + digit; | 680 | 3.38k | } else { | 681 | 456 | if constexpr (enable_strict_mode) { | 682 | 456 | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 683 | 400 | *result = PARSE_FAILURE; | 684 | 400 | return 0; | 685 | 400 | } | 686 | | } else { | 687 | | // Save original position where non-digit was found | 688 | | int remaining_len = len - i; | 689 | | const char* remaining_s = s + i; | 690 | | // Skip trailing whitespaces from the remaining portion | 691 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 692 | | if ((UNLIKELY(remaining_len != 0 && | 693 | | !is_float_suffix(remaining_s, remaining_len)))) { | 694 | | *result = PARSE_FAILURE; | 695 | | return 0; | 696 | | } | 697 | | } | 698 | 56 | *result = PARSE_SUCCESS; | 699 | 456 | return val; | 700 | 456 | } | 701 | 3.84k | } | 702 | 536 | *result = PARSE_SUCCESS; | 703 | 536 | return val; | 704 | 992 | } |
_ZN5doris12StringParser25string_to_int_no_overflowIhLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 663 | 4.44M | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 664 | 4.44M | T val = 0; | 665 | 4.44M | if (UNLIKELY(len == 0)) { | 666 | 0 | *result = PARSE_SUCCESS; | 667 | 0 | return val; | 668 | 0 | } | 669 | | // Factor out the first char for error handling speeds up the loop. | 670 | 4.46M | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 671 | 4.46M | val = s[0] - '0'; | 672 | 18.4E | } else { | 673 | 18.4E | *result = PARSE_FAILURE; | 674 | 18.4E | return 0; | 675 | 18.4E | } | 676 | 4.92M | for (int i = 1; i < len; ++i) { | 677 | 466k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 678 | 466k | T digit = s[i] - '0'; | 679 | 466k | val = val * 10 + digit; | 680 | 466k | } else { | 681 | | if constexpr (enable_strict_mode) { | 682 | | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 683 | | *result = PARSE_FAILURE; | 684 | | return 0; | 685 | | } | 686 | 5 | } else { | 687 | | // Save original position where non-digit was found | 688 | 5 | int remaining_len = len - i; | 689 | 5 | const char* remaining_s = s + i; | 690 | | // Skip trailing whitespaces from the remaining portion | 691 | 5 | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 692 | 5 | if ((UNLIKELY(remaining_len != 0 && | 693 | 5 | !is_float_suffix(remaining_s, remaining_len)))) { | 694 | 2 | *result = PARSE_FAILURE; | 695 | 2 | return 0; | 696 | 2 | } | 697 | 5 | } | 698 | 3 | *result = PARSE_SUCCESS; | 699 | 5 | return val; | 700 | 5 | } | 701 | 466k | } | 702 | 4.46M | *result = PARSE_SUCCESS; | 703 | 4.46M | return val; | 704 | 4.46M | } |
_ZN5doris12StringParser25string_to_int_no_overflowItLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 663 | 411k | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 664 | 411k | T val = 0; | 665 | 411k | if (UNLIKELY(len == 0)) { | 666 | 0 | *result = PARSE_SUCCESS; | 667 | 0 | return val; | 668 | 0 | } | 669 | | // Factor out the first char for error handling speeds up the loop. | 670 | 411k | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 671 | 410k | val = s[0] - '0'; | 672 | 410k | } else { | 673 | 1.39k | *result = PARSE_FAILURE; | 674 | 1.39k | return 0; | 675 | 1.39k | } | 676 | 968k | for (int i = 1; i < len; ++i) { | 677 | 559k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 678 | 558k | T digit = s[i] - '0'; | 679 | 558k | val = val * 10 + digit; | 680 | 558k | } else { | 681 | | if constexpr (enable_strict_mode) { | 682 | | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 683 | | *result = PARSE_FAILURE; | 684 | | return 0; | 685 | | } | 686 | 1.01k | } else { | 687 | | // Save original position where non-digit was found | 688 | 1.01k | int remaining_len = len - i; | 689 | 1.01k | const char* remaining_s = s + i; | 690 | | // Skip trailing whitespaces from the remaining portion | 691 | 1.01k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 692 | 1.01k | if ((UNLIKELY(remaining_len != 0 && | 693 | 1.01k | !is_float_suffix(remaining_s, remaining_len)))) { | 694 | 74 | *result = PARSE_FAILURE; | 695 | 74 | return 0; | 696 | 74 | } | 697 | 1.01k | } | 698 | 940 | *result = PARSE_SUCCESS; | 699 | 1.01k | return val; | 700 | 1.01k | } | 701 | 559k | } | 702 | 409k | *result = PARSE_SUCCESS; | 703 | 409k | return val; | 704 | 410k | } |
_ZN5doris12StringParser25string_to_int_no_overflowIjLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 663 | 49.7M | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 664 | 49.7M | T val = 0; | 665 | 49.7M | if (UNLIKELY(len == 0)) { | 666 | 0 | *result = PARSE_SUCCESS; | 667 | 0 | return val; | 668 | 0 | } | 669 | | // Factor out the first char for error handling speeds up the loop. | 670 | 49.7M | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 671 | 49.7M | val = s[0] - '0'; | 672 | 18.4E | } else { | 673 | 18.4E | *result = PARSE_FAILURE; | 674 | 18.4E | return 0; | 675 | 18.4E | } | 676 | 184M | for (int i = 1; i < len; ++i) { | 677 | 135M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 678 | 134M | T digit = s[i] - '0'; | 679 | 134M | val = val * 10 + digit; | 680 | 134M | } else { | 681 | | if constexpr (enable_strict_mode) { | 682 | | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 683 | | *result = PARSE_FAILURE; | 684 | | return 0; | 685 | | } | 686 | 46.0k | } else { | 687 | | // Save original position where non-digit was found | 688 | 46.0k | int remaining_len = len - i; | 689 | 46.0k | const char* remaining_s = s + i; | 690 | | // Skip trailing whitespaces from the remaining portion | 691 | 46.0k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 692 | 46.0k | if ((UNLIKELY(remaining_len != 0 && | 693 | 46.0k | !is_float_suffix(remaining_s, remaining_len)))) { | 694 | 304 | *result = PARSE_FAILURE; | 695 | 304 | return 0; | 696 | 304 | } | 697 | 46.0k | } | 698 | 45.7k | *result = PARSE_SUCCESS; | 699 | 46.0k | return val; | 700 | 46.0k | } | 701 | 135M | } | 702 | 49.6M | *result = PARSE_SUCCESS; | 703 | 49.6M | return val; | 704 | 49.7M | } |
_ZN5doris12StringParser25string_to_int_no_overflowImLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 663 | 71.6M | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 664 | 71.6M | T val = 0; | 665 | 71.6M | if (UNLIKELY(len == 0)) { | 666 | 0 | *result = PARSE_SUCCESS; | 667 | 0 | return val; | 668 | 0 | } | 669 | | // Factor out the first char for error handling speeds up the loop. | 670 | 71.6M | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 671 | 71.6M | val = s[0] - '0'; | 672 | 18.4E | } else { | 673 | 18.4E | *result = PARSE_FAILURE; | 674 | 18.4E | return 0; | 675 | 18.4E | } | 676 | 333M | for (int i = 1; i < len; ++i) { | 677 | 261M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 678 | 261M | T digit = s[i] - '0'; | 679 | 261M | val = val * 10 + digit; | 680 | 261M | } else { | 681 | | if constexpr (enable_strict_mode) { | 682 | | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 683 | | *result = PARSE_FAILURE; | 684 | | return 0; | 685 | | } | 686 | 12.8k | } else { | 687 | | // Save original position where non-digit was found | 688 | 12.8k | int remaining_len = len - i; | 689 | 12.8k | const char* remaining_s = s + i; | 690 | | // Skip trailing whitespaces from the remaining portion | 691 | 12.8k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 692 | 12.8k | if ((UNLIKELY(remaining_len != 0 && | 693 | 12.8k | !is_float_suffix(remaining_s, remaining_len)))) { | 694 | 158 | *result = PARSE_FAILURE; | 695 | 158 | return 0; | 696 | 158 | } | 697 | 12.8k | } | 698 | 12.7k | *result = PARSE_SUCCESS; | 699 | 12.8k | return val; | 700 | 12.8k | } | 701 | 261M | } | 702 | 71.6M | *result = PARSE_SUCCESS; | 703 | 71.6M | return val; | 704 | 71.6M | } |
_ZN5doris12StringParser25string_to_int_no_overflowIN4wide7integerILm256EjEELb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 663 | 4 | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 664 | 4 | T val = 0; | 665 | 4 | if (UNLIKELY(len == 0)) { | 666 | 0 | *result = PARSE_SUCCESS; | 667 | 0 | return val; | 668 | 0 | } | 669 | | // Factor out the first char for error handling speeds up the loop. | 670 | 4 | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 671 | 4 | val = s[0] - '0'; | 672 | 4 | } else { | 673 | 0 | *result = PARSE_FAILURE; | 674 | 0 | return 0; | 675 | 0 | } | 676 | 4 | for (int i = 1; i < len; ++i) { | 677 | 0 | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 678 | 0 | T digit = s[i] - '0'; | 679 | 0 | val = val * 10 + digit; | 680 | 0 | } else { | 681 | | if constexpr (enable_strict_mode) { | 682 | | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 683 | | *result = PARSE_FAILURE; | 684 | | return 0; | 685 | | } | 686 | 0 | } else { | 687 | | // Save original position where non-digit was found | 688 | 0 | int remaining_len = len - i; | 689 | 0 | const char* remaining_s = s + i; | 690 | | // Skip trailing whitespaces from the remaining portion | 691 | 0 | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 692 | 0 | if ((UNLIKELY(remaining_len != 0 && | 693 | 0 | !is_float_suffix(remaining_s, remaining_len)))) { | 694 | 0 | *result = PARSE_FAILURE; | 695 | 0 | return 0; | 696 | 0 | } | 697 | 0 | } | 698 | 0 | *result = PARSE_SUCCESS; | 699 | 0 | return val; | 700 | 0 | } | 701 | 0 | } | 702 | 4 | *result = PARSE_SUCCESS; | 703 | 4 | return val; | 704 | 4 | } |
|
705 | | |
706 | | // at least the first char(if any) must be a digit. |
707 | | template <typename T> |
708 | | T StringParser::string_to_uint_greedy_no_overflow(const char* __restrict s, int max_len, |
709 | 303k | ParseResult* result) { |
710 | 303k | T val = 0; |
711 | 303k | if (max_len == 0) [[unlikely]] { |
712 | 197k | *result = PARSE_SUCCESS; |
713 | 197k | return val; |
714 | 197k | } |
715 | | // Factor out the first char for error handling speeds up the loop. |
716 | 106k | if (is_numeric_ascii(s[0])) [[likely]] { |
717 | 106k | val = s[0] - '0'; |
718 | 106k | } else { |
719 | 0 | *result = PARSE_FAILURE; |
720 | 0 | return 0; |
721 | 0 | } |
722 | 355k | for (int i = 1; i < max_len; ++i) { |
723 | 248k | if (is_numeric_ascii(s[i])) [[likely]] { |
724 | 248k | T digit = s[i] - '0'; |
725 | 248k | val = val * 10 + digit; |
726 | 248k | } else { |
727 | | // 123abc, return 123 |
728 | 0 | *result = PARSE_SUCCESS; |
729 | 0 | return val; |
730 | 0 | } |
731 | 248k | } |
732 | 106k | *result = PARSE_SUCCESS; |
733 | 106k | return val; |
734 | 106k | } |
735 | | |
736 | | template <typename T> |
737 | 3.47M | T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) { |
738 | 3.47M | int i = 0; |
739 | | // skip leading spaces |
740 | 3.47M | for (; i < len; ++i) { |
741 | 3.43M | if (!is_whitespace_ascii(s[i])) { |
742 | 3.43M | break; |
743 | 3.43M | } |
744 | 3.43M | } |
745 | | |
746 | | // skip back spaces |
747 | 3.47M | int j = len - 1; |
748 | 18.4E | for (; j >= i; j--) { |
749 | 3.43M | if (!is_whitespace_ascii(s[j])) { |
750 | 3.43M | break; |
751 | 3.43M | } |
752 | 3.43M | } |
753 | | |
754 | | // skip leading '+', from_chars can handle '-' |
755 | 3.47M | if (i < len && s[i] == '+') { |
756 | 7.08k | i++; |
757 | | // ++ or +- are not valid, but the first + is already skipped, |
758 | | // if don't check here, from_chars will succeed. |
759 | | // |
760 | | // New version of fast_float supports a new flag called 'chars_format::allow_leading_plus' |
761 | | // which may avoid this extra check here. |
762 | | // e.g.: |
763 | | // fast_float::chars_format format = |
764 | | // fast_float::chars_format::general | fast_float::chars_format::allow_leading_plus; |
765 | | // auto res = fast_float::from_chars(s + i, s + j + 1, val, format); |
766 | 7.08k | if (i < len && (s[i] == '+' || s[i] == '-')) { |
767 | 20 | *result = PARSE_FAILURE; |
768 | 20 | return 0; |
769 | 20 | } |
770 | 7.08k | } |
771 | 3.47M | if (UNLIKELY(i > j)) { |
772 | 38.5k | *result = PARSE_FAILURE; |
773 | 38.5k | return 0; |
774 | 38.5k | } |
775 | | |
776 | | // Use double here to not lose precision while accumulating the result |
777 | 3.43M | double val = 0; |
778 | 3.43M | auto res = fast_float::from_chars(s + i, s + j + 1, val); |
779 | | |
780 | 3.43M | if (res.ptr == s + j + 1) { |
781 | 3.43M | *result = PARSE_SUCCESS; |
782 | 3.43M | return val; |
783 | 3.43M | } else { |
784 | 1.93k | *result = PARSE_FAILURE; |
785 | 1.93k | } |
786 | 1.93k | return 0; |
787 | 3.43M | } _ZN5doris12StringParser24string_to_float_internalIdEET_PKciPNS0_11ParseResultE Line | Count | Source | 737 | 2.68M | T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) { | 738 | 2.68M | int i = 0; | 739 | | // skip leading spaces | 740 | 2.68M | for (; i < len; ++i) { | 741 | 2.65M | if (!is_whitespace_ascii(s[i])) { | 742 | 2.64M | break; | 743 | 2.64M | } | 744 | 2.65M | } | 745 | | | 746 | | // skip back spaces | 747 | 2.68M | int j = len - 1; | 748 | 18.4E | for (; j >= i; j--) { | 749 | 2.65M | if (!is_whitespace_ascii(s[j])) { | 750 | 2.65M | break; | 751 | 2.65M | } | 752 | 2.64M | } | 753 | | | 754 | | // skip leading '+', from_chars can handle '-' | 755 | 2.68M | if (i < len && s[i] == '+') { | 756 | 3.54k | i++; | 757 | | // ++ or +- are not valid, but the first + is already skipped, | 758 | | // if don't check here, from_chars will succeed. | 759 | | // | 760 | | // New version of fast_float supports a new flag called 'chars_format::allow_leading_plus' | 761 | | // which may avoid this extra check here. | 762 | | // e.g.: | 763 | | // fast_float::chars_format format = | 764 | | // fast_float::chars_format::general | fast_float::chars_format::allow_leading_plus; | 765 | | // auto res = fast_float::from_chars(s + i, s + j + 1, val, format); | 766 | 3.54k | if (i < len && (s[i] == '+' || s[i] == '-')) { | 767 | 10 | *result = PARSE_FAILURE; | 768 | 10 | return 0; | 769 | 10 | } | 770 | 3.54k | } | 771 | 2.68M | if (UNLIKELY(i > j)) { | 772 | 33.3k | *result = PARSE_FAILURE; | 773 | 33.3k | return 0; | 774 | 33.3k | } | 775 | | | 776 | | // Use double here to not lose precision while accumulating the result | 777 | 2.65M | double val = 0; | 778 | 2.65M | auto res = fast_float::from_chars(s + i, s + j + 1, val); | 779 | | | 780 | 2.65M | if (res.ptr == s + j + 1) { | 781 | 2.65M | *result = PARSE_SUCCESS; | 782 | 2.65M | return val; | 783 | 18.4E | } else { | 784 | 18.4E | *result = PARSE_FAILURE; | 785 | 18.4E | } | 786 | 18.4E | return 0; | 787 | 2.65M | } |
_ZN5doris12StringParser24string_to_float_internalIfEET_PKciPNS0_11ParseResultE Line | Count | Source | 737 | 786k | T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) { | 738 | 786k | int i = 0; | 739 | | // skip leading spaces | 740 | 786k | for (; i < len; ++i) { | 741 | 781k | if (!is_whitespace_ascii(s[i])) { | 742 | 781k | break; | 743 | 781k | } | 744 | 781k | } | 745 | | | 746 | | // skip back spaces | 747 | 786k | int j = len - 1; | 748 | 18.4E | for (; j >= i; j--) { | 749 | 781k | if (!is_whitespace_ascii(s[j])) { | 750 | 781k | break; | 751 | 781k | } | 752 | 781k | } | 753 | | | 754 | | // skip leading '+', from_chars can handle '-' | 755 | 786k | if (i < len && s[i] == '+') { | 756 | 3.54k | i++; | 757 | | // ++ or +- are not valid, but the first + is already skipped, | 758 | | // if don't check here, from_chars will succeed. | 759 | | // | 760 | | // New version of fast_float supports a new flag called 'chars_format::allow_leading_plus' | 761 | | // which may avoid this extra check here. | 762 | | // e.g.: | 763 | | // fast_float::chars_format format = | 764 | | // fast_float::chars_format::general | fast_float::chars_format::allow_leading_plus; | 765 | | // auto res = fast_float::from_chars(s + i, s + j + 1, val, format); | 766 | 3.54k | if (i < len && (s[i] == '+' || s[i] == '-')) { | 767 | 10 | *result = PARSE_FAILURE; | 768 | 10 | return 0; | 769 | 10 | } | 770 | 3.54k | } | 771 | 786k | if (UNLIKELY(i > j)) { | 772 | 5.15k | *result = PARSE_FAILURE; | 773 | 5.15k | return 0; | 774 | 5.15k | } | 775 | | | 776 | | // Use double here to not lose precision while accumulating the result | 777 | 781k | double val = 0; | 778 | 781k | auto res = fast_float::from_chars(s + i, s + j + 1, val); | 779 | | | 780 | 781k | if (res.ptr == s + j + 1) { | 781 | 779k | *result = PARSE_SUCCESS; | 782 | 779k | return val; | 783 | 779k | } else { | 784 | 2.40k | *result = PARSE_FAILURE; | 785 | 2.40k | } | 786 | 2.40k | return 0; | 787 | 781k | } |
|
788 | | |
789 | | inline bool StringParser::string_to_bool_internal(const char* __restrict s, int len, |
790 | 596k | ParseResult* result) { |
791 | 596k | *result = PARSE_SUCCESS; |
792 | | |
793 | 596k | if (len == 1) { |
794 | 540k | if (s[0] == '1' || s[0] == 't' || s[0] == 'T') { |
795 | 276k | return true; |
796 | 276k | } |
797 | 263k | if (s[0] == '0' || s[0] == 'f' || s[0] == 'F') { |
798 | 262k | return false; |
799 | 262k | } |
800 | 1.38k | *result = PARSE_FAILURE; |
801 | 1.38k | return false; |
802 | 263k | } |
803 | | |
804 | 55.9k | if (len == 2) { |
805 | 993 | if ((s[0] == 'o' || s[0] == 'O') && (s[1] == 'n' || s[1] == 'N')) { |
806 | 10 | return true; |
807 | 10 | } |
808 | 983 | if ((s[0] == 'n' || s[0] == 'N') && (s[1] == 'o' || s[1] == 'O')) { |
809 | 9 | return false; |
810 | 9 | } |
811 | 983 | } |
812 | | |
813 | 55.9k | if (len == 3) { |
814 | 75 | if ((s[0] == 'y' || s[0] == 'Y') && (s[1] == 'e' || s[1] == 'E') && |
815 | 75 | (s[2] == 's' || s[2] == 'S')) { |
816 | 10 | return true; |
817 | 10 | } |
818 | 65 | if ((s[0] == 'o' || s[0] == 'O') && (s[1] == 'f' || s[1] == 'F') && |
819 | 65 | (s[2] == 'f' || s[2] == 'F')) { |
820 | 9 | return false; |
821 | 9 | } |
822 | 65 | } |
823 | | |
824 | 55.9k | if (len == 4 && (s[0] == 't' || s[0] == 'T') && (s[1] == 'r' || s[1] == 'R') && |
825 | 55.9k | (s[2] == 'u' || s[2] == 'U') && (s[3] == 'e' || s[3] == 'E')) { |
826 | 24.9k | return true; |
827 | 24.9k | } |
828 | | |
829 | 30.9k | if (len == 5 && (s[0] == 'f' || s[0] == 'F') && (s[1] == 'a' || s[1] == 'A') && |
830 | 30.9k | (s[2] == 'l' || s[2] == 'L') && (s[3] == 's' || s[3] == 'S') && |
831 | 30.9k | (s[4] == 'e' || s[4] == 'E')) { |
832 | 24.8k | return false; |
833 | 24.8k | } |
834 | | |
835 | | // No valid boolean value found |
836 | 6.16k | *result = PARSE_FAILURE; |
837 | 6.16k | return false; |
838 | 30.9k | } |
839 | | #include "common/compile_check_avoid_end.h" |
840 | | } // end namespace doris |