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/compiler_util.h" // IWYU pragma: keep |
40 | | #include "common/status.h" |
41 | | #include "core/data_type/number_traits.h" |
42 | | #include "core/data_type/primitive_type.h" |
43 | | #include "core/extended_types.h" |
44 | | #include "core/value/large_int_value.h" |
45 | | #include "exec/common/int_exp.h" |
46 | | #include "exec/common/string_utils/string_utils.h" |
47 | | |
48 | | namespace doris { |
49 | | #include "common/compile_check_avoid_begin.h" |
50 | | template <DecimalNativeTypeConcept T> |
51 | | struct Decimal; |
52 | | |
53 | | // they rely on the template parameter `IS_STRICT`. in strict mode, it will set error code and otherwise it will not. |
54 | | #ifndef SET_PARAMS_RET_FALSE_IFN |
55 | | #define SET_PARAMS_RET_FALSE_IFN(stmt, ...) \ |
56 | 246M | do { \ |
57 | 246M | if (!(stmt)) [[unlikely]] { \ |
58 | 899k | if constexpr (IsStrict) { \ |
59 | 1.02k | params.status = Status::InvalidArgument(__VA_ARGS__); \ |
60 | 1.02k | } \ |
61 | 899k | return false; \ |
62 | 899k | } \ |
63 | 246M | } while (false) |
64 | | #endif |
65 | | |
66 | | #ifndef SET_PARAMS_RET_FALSE_FROM_EXCEPTION |
67 | | #define SET_PARAMS_RET_FALSE_FROM_EXCEPTION(stmt) \ |
68 | 784 | do { \ |
69 | 784 | try { \ |
70 | 784 | { stmt; } \ |
71 | 784 | } catch (const doris::Exception& e) { \ |
72 | 74 | if constexpr (IsStrict) { \ |
73 | 8 | params.status = e.to_status(); \ |
74 | 8 | } \ |
75 | 74 | return false; \ |
76 | 74 | } \ |
77 | 784 | } while (false) |
78 | | #endif |
79 | | |
80 | | // skip leading and trailing ascii whitespaces, |
81 | | // return the pointer to the first non-whitespace char, |
82 | | // and update the len to the new length, which does not include |
83 | | // leading and trailing whitespaces |
84 | | template <typename T> |
85 | 22.7M | inline const char* skip_ascii_whitespaces(const char* s, T& len) { |
86 | 23.1M | while (len > 0 && is_whitespace_ascii(*s)) { |
87 | 470k | ++s; |
88 | 470k | --len; |
89 | 470k | } |
90 | | |
91 | 23.1M | while (len > 0 && is_whitespace_ascii(s[len - 1])) { |
92 | 465k | --len; |
93 | 465k | } |
94 | | |
95 | 22.7M | return s; |
96 | 22.7M | } _ZN5doris22skip_ascii_whitespacesImEEPKcS2_RT_ Line | Count | Source | 85 | 22.6M | inline const char* skip_ascii_whitespaces(const char* s, T& len) { | 86 | 23.0M | while (len > 0 && is_whitespace_ascii(*s)) { | 87 | 394k | ++s; | 88 | 394k | --len; | 89 | 394k | } | 90 | | | 91 | 23.0M | while (len > 0 && is_whitespace_ascii(s[len - 1])) { | 92 | 390k | --len; | 93 | 390k | } | 94 | | | 95 | 22.6M | return s; | 96 | 22.6M | } |
_ZN5doris22skip_ascii_whitespacesIiEEPKcS2_RT_ Line | Count | Source | 85 | 4.84k | inline const char* skip_ascii_whitespaces(const char* s, T& len) { | 86 | 8.37k | while (len > 0 && is_whitespace_ascii(*s)) { | 87 | 3.52k | ++s; | 88 | 3.52k | --len; | 89 | 3.52k | } | 90 | | | 91 | 8.37k | while (len > 0 && is_whitespace_ascii(s[len - 1])) { | 92 | 3.52k | --len; | 93 | 3.52k | } | 94 | | | 95 | 4.84k | return s; | 96 | 4.84k | } |
_ZN5doris22skip_ascii_whitespacesIlEEPKcS2_RT_ Line | Count | Source | 85 | 27.9k | inline const char* skip_ascii_whitespaces(const char* s, T& len) { | 86 | 100k | while (len > 0 && is_whitespace_ascii(*s)) { | 87 | 72.4k | ++s; | 88 | 72.4k | --len; | 89 | 72.4k | } | 90 | | | 91 | 99.9k | while (len > 0 && is_whitespace_ascii(s[len - 1])) { | 92 | 72.0k | --len; | 93 | 72.0k | } | 94 | | | 95 | 27.9k | return s; | 96 | 27.9k | } |
|
97 | | |
98 | | template <typename T> |
99 | 226k | inline const char* skip_leading_whitespace(const char* __restrict s, T& len) { |
100 | 332k | while (len > 0 && is_whitespace_ascii(*s)) { |
101 | 105k | ++s; |
102 | 105k | --len; |
103 | 105k | } |
104 | | |
105 | 226k | return s; |
106 | 226k | } |
107 | | |
108 | | // skip trailing ascii whitespaces, |
109 | | // return the pointer to the first char, |
110 | | // and update the len to the new length, which does not include |
111 | | // trailing whitespaces |
112 | | template <typename T> |
113 | 109k | inline const char* skip_trailing_whitespaces(const char* s, T& len) { |
114 | 224k | while (len > 0 && is_whitespace_ascii(s[len - 1])) { |
115 | 115k | --len; |
116 | 115k | } |
117 | | |
118 | 109k | return s; |
119 | 109k | } |
120 | | |
121 | | template <bool (*Pred)(char)> |
122 | 23.0M | bool range_suite(const char* s, const char* end) { |
123 | 23.0M | return std::ranges::all_of(s, end, Pred); |
124 | 23.0M | } _ZN5doris11range_suiteIXadL_Z16is_numeric_asciicEEEEbPKcS2_ Line | Count | Source | 122 | 23.0M | bool range_suite(const char* s, const char* end) { | 123 | 23.0M | return std::ranges::all_of(s, end, Pred); | 124 | 23.0M | } |
_ZN5doris11range_suiteIXadL_Z19is_whitespace_asciicEEEEbPKcS2_ Line | Count | Source | 122 | 2.50k | bool range_suite(const char* s, const char* end) { | 123 | 2.50k | return std::ranges::all_of(s, end, Pred); | 124 | 2.50k | } |
|
125 | | |
126 | | inline auto is_digit_range = range_suite<is_numeric_ascii>; |
127 | | inline auto is_space_range = range_suite<is_whitespace_ascii>; |
128 | | |
129 | | // combine in_bound and range_suite is ok. won't lead to duplicated calculation. |
130 | 41.6M | inline bool in_bound(const char* s, const char* end, size_t offset) { |
131 | 41.6M | if (s + offset >= end) [[unlikely]] { |
132 | 443k | return false; |
133 | 443k | } |
134 | 41.1M | return true; |
135 | 41.6M | } |
136 | | |
137 | | // LEN = 0 means any length(include zero). LEN = 1 means only one character. so on. LEN = -x means x or more. |
138 | | // if need result, use StringRef{origin_s, s} outside |
139 | | template <int LEN, bool (*Pred)(char)> |
140 | 53.9M | bool skip_qualified_char(const char*& s, const char* end) { |
141 | 53.9M | if constexpr (LEN == 0) { |
142 | | // Consume any length of characters that match the predicate. |
143 | 8.62M | while (s != end && Pred(*s)) { |
144 | 2.77M | ++s; |
145 | 2.77M | } |
146 | 48.0M | } else if constexpr (LEN > 0) { |
147 | | // Consume exactly LEN characters that match the predicate. |
148 | 96.0M | for (int i = 0; i < LEN; ++i, ++s) { |
149 | 48.0M | if (s == end || !Pred(*s)) [[unlikely]] { |
150 | 75.3k | return false; |
151 | 75.3k | } |
152 | 48.0M | } |
153 | 48.0M | } else { // LEN < 0 |
154 | | // Consume at least -LEN characters that match the predicate. |
155 | 16.3k | int count = 0; |
156 | 33.5k | while (s != end && Pred(*s)) { |
157 | 17.2k | ++s; |
158 | 17.2k | ++count; |
159 | 17.2k | } |
160 | 16.3k | if (count < -LEN) [[unlikely]] { |
161 | 0 | return false; |
162 | 0 | } |
163 | 16.3k | } |
164 | 48.0M | return true; |
165 | 53.9M | } _ZN5doris19skip_qualified_charILi0EXadL_Z19is_whitespace_asciicEEEEbRPKcS2_ Line | Count | Source | 140 | 2.90M | bool skip_qualified_char(const char*& s, const char* end) { | 141 | 2.90M | if constexpr (LEN == 0) { | 142 | | // Consume any length of characters that match the predicate. | 143 | 2.90M | while (s != end && Pred(*s)) { | 144 | 3.28k | ++s; | 145 | 3.28k | } | 146 | | } else if constexpr (LEN > 0) { | 147 | | // Consume exactly LEN characters that match the predicate. | 148 | | for (int i = 0; i < LEN; ++i, ++s) { | 149 | | if (s == end || !Pred(*s)) [[unlikely]] { | 150 | | return false; | 151 | | } | 152 | | } | 153 | | } else { // LEN < 0 | 154 | | // Consume at least -LEN characters that match the predicate. | 155 | | int count = 0; | 156 | | while (s != end && Pred(*s)) { | 157 | | ++s; | 158 | | ++count; | 159 | | } | 160 | | if (count < -LEN) [[unlikely]] { | 161 | | return false; | 162 | | } | 163 | | } | 164 | 2.90M | return true; | 165 | 2.90M | } |
_ZN5doris19skip_qualified_charILi0EXadL_Z16is_numeric_asciicEEEEbRPKcS2_ Line | Count | Source | 140 | 2.94M | bool skip_qualified_char(const char*& s, const char* end) { | 141 | 2.94M | if constexpr (LEN == 0) { | 142 | | // Consume any length of characters that match the predicate. | 143 | 5.71M | while (s != end && Pred(*s)) { | 144 | 2.76M | ++s; | 145 | 2.76M | } | 146 | | } else if constexpr (LEN > 0) { | 147 | | // Consume exactly LEN characters that match the predicate. | 148 | | for (int i = 0; i < LEN; ++i, ++s) { | 149 | | if (s == end || !Pred(*s)) [[unlikely]] { | 150 | | return false; | 151 | | } | 152 | | } | 153 | | } else { // LEN < 0 | 154 | | // Consume at least -LEN characters that match the predicate. | 155 | | int count = 0; | 156 | | while (s != end && Pred(*s)) { | 157 | | ++s; | 158 | | ++count; | 159 | | } | 160 | | if (count < -LEN) [[unlikely]] { | 161 | | return false; | 162 | | } | 163 | | } | 164 | 2.94M | return true; | 165 | 2.94M | } |
_ZN5doris19skip_qualified_charILin1EXadL_Z23is_not_whitespace_asciicEEEEbRPKcS2_ Line | Count | Source | 140 | 16.3k | bool skip_qualified_char(const char*& s, const char* end) { | 141 | | if constexpr (LEN == 0) { | 142 | | // Consume any length of characters that match the predicate. | 143 | | while (s != end && Pred(*s)) { | 144 | | ++s; | 145 | | } | 146 | | } else if constexpr (LEN > 0) { | 147 | | // Consume exactly LEN characters that match the predicate. | 148 | | for (int i = 0; i < LEN; ++i, ++s) { | 149 | | if (s == end || !Pred(*s)) [[unlikely]] { | 150 | | return false; | 151 | | } | 152 | | } | 153 | 16.3k | } else { // LEN < 0 | 154 | | // Consume at least -LEN characters that match the predicate. | 155 | 16.3k | int count = 0; | 156 | 33.5k | while (s != end && Pred(*s)) { | 157 | 17.2k | ++s; | 158 | 17.2k | ++count; | 159 | 17.2k | } | 160 | 16.3k | if (count < -LEN) [[unlikely]] { | 161 | 0 | return false; | 162 | 0 | } | 163 | 16.3k | } | 164 | 16.3k | return true; | 165 | 16.3k | } |
Unexecuted instantiation: _ZN5doris19skip_qualified_charILi1EXadL_Z14is_slash_asciicEEEEbRPKcS2_ _ZN5doris19skip_qualified_charILi1EXadL_Z12is_non_alnumcEEEEbRPKcS2_ Line | Count | Source | 140 | 100k | bool skip_qualified_char(const char*& s, const char* end) { | 141 | | if constexpr (LEN == 0) { | 142 | | // Consume any length of characters that match the predicate. | 143 | | while (s != end && Pred(*s)) { | 144 | | ++s; | 145 | | } | 146 | 100k | } else if constexpr (LEN > 0) { | 147 | | // Consume exactly LEN characters that match the predicate. | 148 | 126k | for (int i = 0; i < LEN; ++i, ++s) { | 149 | 100k | if (s == end || !Pred(*s)) [[unlikely]] { | 150 | 75.1k | return false; | 151 | 75.1k | } | 152 | 100k | } | 153 | | } else { // LEN < 0 | 154 | | // Consume at least -LEN characters that match the predicate. | 155 | | int count = 0; | 156 | | while (s != end && Pred(*s)) { | 157 | | ++s; | 158 | | ++count; | 159 | | } | 160 | | if (count < -LEN) [[unlikely]] { | 161 | | return false; | 162 | | } | 163 | | } | 164 | 25.4k | return true; | 165 | 100k | } |
_ZN5doris19skip_qualified_charILi1EXadL_ZNS_12is_delimiterEcEEEEbRPKcS2_ Line | Count | Source | 140 | 2.26M | bool skip_qualified_char(const char*& s, const char* end) { | 141 | | if constexpr (LEN == 0) { | 142 | | // Consume any length of characters that match the predicate. | 143 | | while (s != end && Pred(*s)) { | 144 | | ++s; | 145 | | } | 146 | 2.26M | } else if constexpr (LEN > 0) { | 147 | | // Consume exactly LEN characters that match the predicate. | 148 | 4.52M | for (int i = 0; i < LEN; ++i, ++s) { | 149 | 2.26M | if (s == end || !Pred(*s)) [[unlikely]] { | 150 | 89 | return false; | 151 | 89 | } | 152 | 2.26M | } | 153 | | } else { // LEN < 0 | 154 | | // Consume at least -LEN characters that match the predicate. | 155 | | int count = 0; | 156 | | while (s != end && Pred(*s)) { | 157 | | ++s; | 158 | | ++count; | 159 | | } | 160 | | if (count < -LEN) [[unlikely]] { | 161 | | return false; | 162 | | } | 163 | | } | 164 | 2.26M | return true; | 165 | 2.26M | } |
_ZN5doris19skip_qualified_charILi1EXadL_ZNS_11is_date_sepEcEEEEbRPKcS2_ Line | Count | Source | 140 | 41.1M | bool skip_qualified_char(const char*& s, const char* end) { | 141 | | if constexpr (LEN == 0) { | 142 | | // Consume any length of characters that match the predicate. | 143 | | while (s != end && Pred(*s)) { | 144 | | ++s; | 145 | | } | 146 | 41.1M | } else if constexpr (LEN > 0) { | 147 | | // Consume exactly LEN characters that match the predicate. | 148 | 82.3M | for (int i = 0; i < LEN; ++i, ++s) { | 149 | 41.1M | if (s == end || !Pred(*s)) [[unlikely]] { | 150 | 63 | return false; | 151 | 63 | } | 152 | 41.1M | } | 153 | | } else { // LEN < 0 | 154 | | // Consume at least -LEN characters that match the predicate. | 155 | | int count = 0; | 156 | | while (s != end && Pred(*s)) { | 157 | | ++s; | 158 | | ++count; | 159 | | } | 160 | | if (count < -LEN) [[unlikely]] { | 161 | | return false; | 162 | | } | 163 | | } | 164 | 41.1M | return true; | 165 | 41.1M | } |
_ZN5doris19skip_qualified_charILi1EXadL_ZNS_8is_colonEcEEEEbRPKcS2_ Line | Count | Source | 140 | 4.51M | bool skip_qualified_char(const char*& s, const char* end) { | 141 | | if constexpr (LEN == 0) { | 142 | | // Consume any length of characters that match the predicate. | 143 | | while (s != end && Pred(*s)) { | 144 | | ++s; | 145 | | } | 146 | 4.51M | } else if constexpr (LEN > 0) { | 147 | | // Consume exactly LEN characters that match the predicate. | 148 | 9.03M | for (int i = 0; i < LEN; ++i, ++s) { | 149 | 4.51M | if (s == end || !Pred(*s)) [[unlikely]] { | 150 | 54 | return false; | 151 | 54 | } | 152 | 4.51M | } | 153 | | } else { // LEN < 0 | 154 | | // Consume at least -LEN characters that match the predicate. | 155 | | int count = 0; | 156 | | while (s != end && Pred(*s)) { | 157 | | ++s; | 158 | | ++count; | 159 | | } | 160 | | if (count < -LEN) [[unlikely]] { | 161 | | return false; | 162 | | } | 163 | | } | 164 | 4.51M | return true; | 165 | 4.51M | } |
|
166 | | |
167 | | inline auto skip_any_whitespace = skip_qualified_char<0, is_whitespace_ascii>; |
168 | | inline auto skip_any_digit = skip_qualified_char<0, is_numeric_ascii>; |
169 | | inline auto skip_tz_name_part = skip_qualified_char<-1, is_not_whitespace_ascii>; |
170 | | inline auto skip_one_slash = skip_qualified_char<1, is_slash_ascii>; |
171 | | inline auto skip_one_non_alnum = skip_qualified_char<1, is_non_alnum>; |
172 | | |
173 | 2.26M | inline bool is_delimiter(char c) { |
174 | 2.26M | return c == ' ' || c == 'T' || c == ':'; |
175 | 2.26M | } |
176 | | inline auto consume_one_delimiter = skip_qualified_char<1, is_delimiter>; |
177 | | |
178 | 61.6M | inline bool is_date_sep(char c) { |
179 | 61.6M | return c == '-' || c == '/'; |
180 | 61.6M | } |
181 | | inline auto consume_one_date_sep = skip_qualified_char<1, is_date_sep>; |
182 | | |
183 | 4.51M | inline bool is_colon(char c) { |
184 | 4.51M | return c == ':'; |
185 | 4.51M | } |
186 | | inline auto consume_one_colon = skip_qualified_char<1, is_colon>; |
187 | | |
188 | | // only consume a string of digit, not include sign. |
189 | | // when has MAX_LEN > 0, do greedy match but at most MAX_LEN. |
190 | | // LEN = 0 means any length, otherwise(must > 0) it means exactly LEN digits. |
191 | | template <typename T, int LEN = 0, int MAX_LEN = -1> |
192 | 32.7k | bool consume_digit(const char*& s, const char* end, T& out) { |
193 | 32.7k | static_assert(LEN >= 0); |
194 | | if constexpr (MAX_LEN > 0) { |
195 | | out = 0; |
196 | | for (int i = 0; i < MAX_LEN; ++i, ++s) { |
197 | | if (s == end || !is_numeric_ascii(*s)) { |
198 | | if (i < LEN) [[unlikely]] { |
199 | | return false; |
200 | | } |
201 | | break; // stop consuming if we have consumed enough digits. |
202 | | } |
203 | | out = out * 10 + (*s - '0'); |
204 | | } |
205 | | } else if constexpr (LEN == 0) { |
206 | | // Consume any length of digits. |
207 | | out = 0; |
208 | | while (s != end && is_numeric_ascii(*s)) { |
209 | | out = out * 10 + (*s - '0'); |
210 | | ++s; |
211 | | } |
212 | 32.7k | } else if constexpr (LEN > 0) { |
213 | | // Consume exactly LEN digits. |
214 | 32.7k | out = 0; |
215 | 163k | for (int i = 0; i < LEN; ++i, ++s) { |
216 | 131k | if (s == end || !is_numeric_ascii(*s)) [[unlikely]] { |
217 | 0 | return false; |
218 | 0 | } |
219 | 131k | out = out * 10 + (*s - '0'); |
220 | 131k | } |
221 | 32.7k | } |
222 | 32.7k | return true; |
223 | 32.7k | } _ZN5doris13consume_digitIjLi4ELin1EEEbRPKcS2_RT_ Line | Count | Source | 192 | 32.7k | bool consume_digit(const char*& s, const char* end, T& out) { | 193 | 32.7k | static_assert(LEN >= 0); | 194 | | if constexpr (MAX_LEN > 0) { | 195 | | out = 0; | 196 | | for (int i = 0; i < MAX_LEN; ++i, ++s) { | 197 | | if (s == end || !is_numeric_ascii(*s)) { | 198 | | if (i < LEN) [[unlikely]] { | 199 | | return false; | 200 | | } | 201 | | break; // stop consuming if we have consumed enough digits. | 202 | | } | 203 | | out = out * 10 + (*s - '0'); | 204 | | } | 205 | | } else if constexpr (LEN == 0) { | 206 | | // Consume any length of digits. | 207 | | out = 0; | 208 | | while (s != end && is_numeric_ascii(*s)) { | 209 | | out = out * 10 + (*s - '0'); | 210 | | ++s; | 211 | | } | 212 | 32.7k | } else if constexpr (LEN > 0) { | 213 | | // Consume exactly LEN digits. | 214 | 32.7k | out = 0; | 215 | 163k | for (int i = 0; i < LEN; ++i, ++s) { | 216 | 131k | if (s == end || !is_numeric_ascii(*s)) [[unlikely]] { | 217 | 0 | return false; | 218 | 0 | } | 219 | 131k | out = out * 10 + (*s - '0'); | 220 | 131k | } | 221 | 32.7k | } | 222 | 32.7k | return true; | 223 | 32.7k | } |
_ZN5doris13consume_digitIjLi1ELin1EEEbRPKcS2_RT_ Line | Count | Source | 192 | 11 | bool consume_digit(const char*& s, const char* end, T& out) { | 193 | 11 | static_assert(LEN >= 0); | 194 | | if constexpr (MAX_LEN > 0) { | 195 | | out = 0; | 196 | | for (int i = 0; i < MAX_LEN; ++i, ++s) { | 197 | | if (s == end || !is_numeric_ascii(*s)) { | 198 | | if (i < LEN) [[unlikely]] { | 199 | | return false; | 200 | | } | 201 | | break; // stop consuming if we have consumed enough digits. | 202 | | } | 203 | | out = out * 10 + (*s - '0'); | 204 | | } | 205 | | } else if constexpr (LEN == 0) { | 206 | | // Consume any length of digits. | 207 | | out = 0; | 208 | | while (s != end && is_numeric_ascii(*s)) { | 209 | | out = out * 10 + (*s - '0'); | 210 | | ++s; | 211 | | } | 212 | 11 | } else if constexpr (LEN > 0) { | 213 | | // Consume exactly LEN digits. | 214 | 11 | out = 0; | 215 | 22 | for (int i = 0; i < LEN; ++i, ++s) { | 216 | 11 | if (s == end || !is_numeric_ascii(*s)) [[unlikely]] { | 217 | 0 | return false; | 218 | 0 | } | 219 | 11 | out = out * 10 + (*s - '0'); | 220 | 11 | } | 221 | 11 | } | 222 | 11 | return true; | 223 | 11 | } |
|
224 | | |
225 | | // specialized version for 2 digits, which is used very often in date/time parsing. |
226 | | template <> |
227 | 41.8M | inline bool consume_digit<uint32_t, 2, -1>(const char*& s, const char* end, uint32_t& out) { |
228 | 41.8M | out = 0; |
229 | 41.8M | if (s == end || s + 1 == end || !is_numeric_ascii(*s) || !is_numeric_ascii(*(s + 1))) |
230 | 36.6k | [[unlikely]] { |
231 | 36.6k | return false; |
232 | 36.6k | } |
233 | 41.8M | out = (s[0] - '0') * 10 + (s[1] - '0'); |
234 | 41.8M | s += 2; // consume 2 digits |
235 | 41.8M | return true; |
236 | 41.8M | } |
237 | | |
238 | | // specialized version for 1 or 2 digits, which is used very often in date/time parsing. |
239 | | template <> |
240 | 48.0M | inline bool consume_digit<uint32_t, 1, 2>(const char*& s, const char* end, uint32_t& out) { |
241 | 48.0M | out = 0; |
242 | 48.0M | if (s == end || !is_numeric_ascii(*s)) [[unlikely]] { |
243 | 520 | return false; |
244 | 48.0M | } else if (s + 1 != end && is_numeric_ascii(*(s + 1))) { |
245 | | // consume 2 digits |
246 | 47.9M | out = (*s - '0') * 10 + (*(s + 1) - '0'); |
247 | 47.9M | s += 2; |
248 | 47.9M | } else { |
249 | | // consume 1 digit |
250 | 106k | out = *s - '0'; |
251 | 106k | ++s; |
252 | 106k | } |
253 | 48.0M | return true; |
254 | 48.0M | } |
255 | | |
256 | | template <bool (*Pred)(char)> |
257 | 129k | uint32_t count_valid_length(const char* s, const char* end) { |
258 | 18.4E | DCHECK(s <= end) << "s: " << s << ", end: " << end; |
259 | 129k | uint32_t count = 0; |
260 | 388k | while (s != end && Pred(*s)) { |
261 | 258k | ++count; |
262 | 258k | ++s; |
263 | 258k | } |
264 | 129k | return count; |
265 | 129k | } |
266 | | |
267 | | inline auto count_digits = count_valid_length<is_numeric_ascii>; |
268 | | |
269 | 129k | inline PURE std::string combine_tz_offset(char sign, uint32_t hour_offset, uint32_t minute_offset) { |
270 | 129k | std::string result(6, '0'); |
271 | 129k | result[0] = sign; |
272 | 129k | result[1] = '0' + (hour_offset / 10); |
273 | 129k | result[2] = '0' + (hour_offset % 10); |
274 | 129k | result[3] = ':'; |
275 | 129k | result[4] = '0' + (minute_offset / 10); |
276 | 129k | result[5] = '0' + (minute_offset % 10); |
277 | 129k | DCHECK_EQ(result.size(), 6); |
278 | 129k | return result; |
279 | 129k | } |
280 | | |
281 | | // Utility functions for doing atoi/atof on non-null terminated strings. On micro benchmarks, |
282 | | // this is significantly faster than libc (atoi/strtol and atof/strtod). |
283 | | // |
284 | | // Strings with leading and trailing whitespaces are accepted. |
285 | | // Branching is heavily optimized for the non-whitespace successful case. |
286 | | // All the StringTo* functions first parse the input string assuming it has no leading whitespace. |
287 | | // If that first attempt was unsuccessful, these functions retry the parsing after removing |
288 | | // whitespace. Therefore, strings with whitespace take a perf hit on branch mis-prediction. |
289 | | // |
290 | | // For overflows, we are following the mysql behavior, to cap values at the max/min value for that |
291 | | // data type. This is different from hive, which returns NULL for overflow slots for int types |
292 | | // and inf/-inf for float types. |
293 | | // |
294 | | // Things we tried that did not work: |
295 | | // - lookup table for converting character to digit |
296 | | // Improvements (TODO): |
297 | | // - Validate input using _simd_compare_ranges |
298 | | // - Since we know the length, we can parallelize this: i.e. result = 100*s[0] + 10*s[1] + s[2] |
299 | | class StringParser { |
300 | | public: |
301 | | enum ParseResult { PARSE_SUCCESS = 0, PARSE_FAILURE, PARSE_OVERFLOW, PARSE_UNDERFLOW }; |
302 | | |
303 | | template <typename T> |
304 | 140M | static T numeric_limits(bool negative) { |
305 | 140M | if constexpr (std::is_same_v<T, __int128>) { |
306 | 1.03M | return negative ? MIN_INT128 : MAX_INT128; |
307 | 139M | } else { |
308 | 139M | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); |
309 | 139M | } |
310 | 140M | } _ZN5doris12StringParser14numeric_limitsInEET_b Line | Count | Source | 304 | 1.03M | static T numeric_limits(bool negative) { | 305 | 1.03M | if constexpr (std::is_same_v<T, __int128>) { | 306 | 1.03M | return negative ? MIN_INT128 : MAX_INT128; | 307 | | } else { | 308 | | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 309 | | } | 310 | 1.03M | } |
_ZN5doris12StringParser14numeric_limitsIaEET_b Line | Count | Source | 304 | 6.25M | static T numeric_limits(bool negative) { | 305 | | if constexpr (std::is_same_v<T, __int128>) { | 306 | | return negative ? MIN_INT128 : MAX_INT128; | 307 | 6.25M | } else { | 308 | 6.25M | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 309 | 6.25M | } | 310 | 6.25M | } |
_ZN5doris12StringParser14numeric_limitsIsEET_b Line | Count | Source | 304 | 1.07M | static T numeric_limits(bool negative) { | 305 | | if constexpr (std::is_same_v<T, __int128>) { | 306 | | return negative ? MIN_INT128 : MAX_INT128; | 307 | 1.07M | } else { | 308 | 1.07M | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 309 | 1.07M | } | 310 | 1.07M | } |
_ZN5doris12StringParser14numeric_limitsIiEET_b Line | Count | Source | 304 | 52.2M | static T numeric_limits(bool negative) { | 305 | | if constexpr (std::is_same_v<T, __int128>) { | 306 | | return negative ? MIN_INT128 : MAX_INT128; | 307 | 52.2M | } else { | 308 | 52.2M | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 309 | 52.2M | } | 310 | 52.2M | } |
_ZN5doris12StringParser14numeric_limitsIlEET_b Line | Count | Source | 304 | 79.9M | static T numeric_limits(bool negative) { | 305 | | if constexpr (std::is_same_v<T, __int128>) { | 306 | | return negative ? MIN_INT128 : MAX_INT128; | 307 | 79.9M | } else { | 308 | 79.9M | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 309 | 79.9M | } | 310 | 79.9M | } |
_ZN5doris12StringParser14numeric_limitsIjEET_b Line | Count | Source | 304 | 743 | static T numeric_limits(bool negative) { | 305 | | if constexpr (std::is_same_v<T, __int128>) { | 306 | | return negative ? MIN_INT128 : MAX_INT128; | 307 | 743 | } else { | 308 | 743 | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 309 | 743 | } | 310 | 743 | } |
_ZN5doris12StringParser14numeric_limitsImEET_b Line | Count | Source | 304 | 3.86k | static T numeric_limits(bool negative) { | 305 | | if constexpr (std::is_same_v<T, __int128>) { | 306 | | return negative ? MIN_INT128 : MAX_INT128; | 307 | 3.86k | } else { | 308 | 3.86k | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 309 | 3.86k | } | 310 | 3.86k | } |
_ZN5doris12StringParser14numeric_limitsIN4wide7integerILm256EiEEEET_b Line | Count | Source | 304 | 4 | static T numeric_limits(bool negative) { | 305 | | if constexpr (std::is_same_v<T, __int128>) { | 306 | | return negative ? MIN_INT128 : MAX_INT128; | 307 | 4 | } else { | 308 | 4 | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 309 | 4 | } | 310 | 4 | } |
_ZN5doris12StringParser14numeric_limitsIoEET_b Line | Count | Source | 304 | 4 | static T numeric_limits(bool negative) { | 305 | | if constexpr (std::is_same_v<T, __int128>) { | 306 | | return negative ? MIN_INT128 : MAX_INT128; | 307 | 4 | } else { | 308 | 4 | return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); | 309 | 4 | } | 310 | 4 | } |
|
311 | | |
312 | | template <typename T> |
313 | 39.3M | static T get_scale_multiplier(int scale) { |
314 | 39.3M | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || |
315 | 39.3M | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, |
316 | 39.3M | "You can only instantiate as int32_t, int64_t, __int128."); |
317 | 39.3M | if constexpr (std::is_same_v<T, int32_t>) { |
318 | 698k | return common::exp10_i32(scale); |
319 | 33.8M | } else if constexpr (std::is_same_v<T, int64_t>) { |
320 | 33.8M | return common::exp10_i64(scale); |
321 | 33.8M | } else if constexpr (std::is_same_v<T, __int128>) { |
322 | 4.40M | return common::exp10_i128(scale); |
323 | 4.40M | } else if constexpr (std::is_same_v<T, wide::Int256>) { |
324 | 362k | return common::exp10_i256(scale); |
325 | 362k | } |
326 | 39.3M | } _ZN5doris12StringParser20get_scale_multiplierIiEET_i Line | Count | Source | 313 | 698k | static T get_scale_multiplier(int scale) { | 314 | 698k | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || | 315 | 698k | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, | 316 | 698k | "You can only instantiate as int32_t, int64_t, __int128."); | 317 | 698k | if constexpr (std::is_same_v<T, int32_t>) { | 318 | 698k | return common::exp10_i32(scale); | 319 | | } else if constexpr (std::is_same_v<T, int64_t>) { | 320 | | return common::exp10_i64(scale); | 321 | | } else if constexpr (std::is_same_v<T, __int128>) { | 322 | | return common::exp10_i128(scale); | 323 | | } else if constexpr (std::is_same_v<T, wide::Int256>) { | 324 | | return common::exp10_i256(scale); | 325 | | } | 326 | 698k | } |
_ZN5doris12StringParser20get_scale_multiplierIlEET_i Line | Count | Source | 313 | 33.8M | static T get_scale_multiplier(int scale) { | 314 | 33.8M | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || | 315 | 33.8M | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, | 316 | 33.8M | "You can only instantiate as int32_t, int64_t, __int128."); | 317 | | if constexpr (std::is_same_v<T, int32_t>) { | 318 | | return common::exp10_i32(scale); | 319 | 33.8M | } else if constexpr (std::is_same_v<T, int64_t>) { | 320 | 33.8M | return common::exp10_i64(scale); | 321 | | } else if constexpr (std::is_same_v<T, __int128>) { | 322 | | return common::exp10_i128(scale); | 323 | | } else if constexpr (std::is_same_v<T, wide::Int256>) { | 324 | | return common::exp10_i256(scale); | 325 | | } | 326 | 33.8M | } |
_ZN5doris12StringParser20get_scale_multiplierInEET_i Line | Count | Source | 313 | 4.40M | static T get_scale_multiplier(int scale) { | 314 | 4.40M | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || | 315 | 4.40M | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, | 316 | 4.40M | "You can only instantiate as int32_t, int64_t, __int128."); | 317 | | if constexpr (std::is_same_v<T, int32_t>) { | 318 | | return common::exp10_i32(scale); | 319 | | } else if constexpr (std::is_same_v<T, int64_t>) { | 320 | | return common::exp10_i64(scale); | 321 | 4.40M | } else if constexpr (std::is_same_v<T, __int128>) { | 322 | 4.40M | return common::exp10_i128(scale); | 323 | | } else if constexpr (std::is_same_v<T, wide::Int256>) { | 324 | | return common::exp10_i256(scale); | 325 | | } | 326 | 4.40M | } |
_ZN5doris12StringParser20get_scale_multiplierIN4wide7integerILm256EiEEEET_i Line | Count | Source | 313 | 362k | static T get_scale_multiplier(int scale) { | 314 | 362k | static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> || | 315 | 362k | std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>, | 316 | 362k | "You can only instantiate as int32_t, int64_t, __int128."); | 317 | | if constexpr (std::is_same_v<T, int32_t>) { | 318 | | return common::exp10_i32(scale); | 319 | | } else if constexpr (std::is_same_v<T, int64_t>) { | 320 | | return common::exp10_i64(scale); | 321 | | } else if constexpr (std::is_same_v<T, __int128>) { | 322 | | return common::exp10_i128(scale); | 323 | 362k | } else if constexpr (std::is_same_v<T, wide::Int256>) { | 324 | 362k | return common::exp10_i256(scale); | 325 | 362k | } | 326 | 362k | } |
|
327 | | |
328 | | // This is considerably faster than glibc's implementation (25x). |
329 | | // Assumes s represents a decimal number. |
330 | | template <typename T, bool enable_strict_mode = false> |
331 | 140M | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { |
332 | 140M | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); |
333 | 140M | if (LIKELY(*result == PARSE_SUCCESS)) { |
334 | 140M | return ans; |
335 | 140M | } |
336 | 345k | s = skip_leading_whitespace(s, len); |
337 | 345k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); |
338 | 140M | } _ZN5doris12StringParser13string_to_intInLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 331 | 1.10M | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 332 | 1.10M | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 333 | 1.10M | if (LIKELY(*result == PARSE_SUCCESS)) { | 334 | 994k | return ans; | 335 | 994k | } | 336 | 106k | s = skip_leading_whitespace(s, len); | 337 | 106k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 338 | 1.10M | } |
_ZN5doris12StringParser13string_to_intIaLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 331 | 6.17M | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 332 | 6.17M | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 333 | 6.17M | if (LIKELY(*result == PARSE_SUCCESS)) { | 334 | 6.12M | return ans; | 335 | 6.12M | } | 336 | 49.7k | s = skip_leading_whitespace(s, len); | 337 | 49.7k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 338 | 6.17M | } |
_ZN5doris12StringParser13string_to_intIsLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 331 | 1.06M | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 332 | 1.06M | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 333 | 1.06M | if (LIKELY(*result == PARSE_SUCCESS)) { | 334 | 1.04M | return ans; | 335 | 1.04M | } | 336 | 19.6k | s = skip_leading_whitespace(s, len); | 337 | 19.6k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 338 | 1.06M | } |
_ZN5doris12StringParser13string_to_intIiLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 331 | 52.2M | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 332 | 52.2M | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 333 | 52.3M | if (LIKELY(*result == PARSE_SUCCESS)) { | 334 | 52.3M | return ans; | 335 | 52.3M | } | 336 | 18.4E | s = skip_leading_whitespace(s, len); | 337 | 18.4E | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 338 | 52.2M | } |
_ZN5doris12StringParser13string_to_intIlLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 331 | 80.1M | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 332 | 80.1M | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 333 | 80.1M | if (LIKELY(*result == PARSE_SUCCESS)) { | 334 | 79.9M | return ans; | 335 | 79.9M | } | 336 | 174k | s = skip_leading_whitespace(s, len); | 337 | 174k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 338 | 80.1M | } |
_ZN5doris12StringParser13string_to_intIaLb1EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 331 | 11.4k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 332 | 11.4k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 333 | 11.4k | if (LIKELY(*result == PARSE_SUCCESS)) { | 334 | 10.4k | return ans; | 335 | 10.4k | } | 336 | 912 | s = skip_leading_whitespace(s, len); | 337 | 912 | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 338 | 11.4k | } |
_ZN5doris12StringParser13string_to_intIsLb1EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 331 | 11.3k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 332 | 11.3k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 333 | 11.3k | if (LIKELY(*result == PARSE_SUCCESS)) { | 334 | 10.5k | return ans; | 335 | 10.5k | } | 336 | 896 | s = skip_leading_whitespace(s, len); | 337 | 896 | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 338 | 11.3k | } |
_ZN5doris12StringParser13string_to_intIiLb1EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 331 | 13.0k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 332 | 13.0k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 333 | 13.0k | if (LIKELY(*result == PARSE_SUCCESS)) { | 334 | 12.1k | return ans; | 335 | 12.1k | } | 336 | 903 | s = skip_leading_whitespace(s, len); | 337 | 903 | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 338 | 13.0k | } |
_ZN5doris12StringParser13string_to_intIlLb1EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 331 | 11.8k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 332 | 11.8k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 333 | 11.8k | if (LIKELY(*result == PARSE_SUCCESS)) { | 334 | 10.7k | return ans; | 335 | 10.7k | } | 336 | 1.15k | s = skip_leading_whitespace(s, len); | 337 | 1.15k | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 338 | 11.8k | } |
_ZN5doris12StringParser13string_to_intInLb1EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 331 | 11.6k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 332 | 11.6k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 333 | 11.6k | if (LIKELY(*result == PARSE_SUCCESS)) { | 334 | 10.8k | return ans; | 335 | 10.8k | } | 336 | 848 | s = skip_leading_whitespace(s, len); | 337 | 848 | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 338 | 11.6k | } |
Unexecuted instantiation: _ZN5doris12StringParser13string_to_intIjLb0EEET_PKcmPNS0_11ParseResultE _ZN5doris12StringParser13string_to_intImLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 331 | 3.78k | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 332 | 3.78k | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 333 | 3.78k | if (LIKELY(*result == PARSE_SUCCESS)) { | 334 | 3.78k | return ans; | 335 | 3.78k | } | 336 | 18.4E | s = skip_leading_whitespace(s, len); | 337 | 18.4E | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 338 | 3.78k | } |
_ZN5doris12StringParser13string_to_intIN4wide7integerILm256EiEELb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 331 | 4 | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 332 | 4 | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 333 | 4 | if (LIKELY(*result == PARSE_SUCCESS)) { | 334 | 4 | return ans; | 335 | 4 | } | 336 | 0 | s = skip_leading_whitespace(s, len); | 337 | 0 | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 338 | 4 | } |
_ZN5doris12StringParser13string_to_intIoLb0EEET_PKcmPNS0_11ParseResultE Line | Count | Source | 331 | 4 | static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) { | 332 | 4 | T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result); | 333 | 4 | if (LIKELY(*result == PARSE_SUCCESS)) { | 334 | 4 | return ans; | 335 | 4 | } | 336 | 0 | s = skip_leading_whitespace(s, len); | 337 | 0 | return string_to_int_internal<T, enable_strict_mode>(s, len, result); | 338 | 4 | } |
|
339 | | |
340 | | // This is considerably faster than glibc's implementation. |
341 | | // In the case of overflow, the max/min value for the data type will be returned. |
342 | | // Assumes s represents a decimal number. |
343 | | template <typename T> |
344 | 3.81k | static inline T string_to_unsigned_int(const char* __restrict s, int len, ParseResult* result) { |
345 | 3.81k | s = skip_ascii_whitespaces(s, len); |
346 | 3.81k | return string_to_unsigned_int_internal<T>(s, len, result); |
347 | 3.81k | } |
348 | | |
349 | | // Convert a string s representing a number in given base into a decimal number. |
350 | | template <typename T> |
351 | | static inline T string_to_int(const char* __restrict s, int64_t len, int base, |
352 | 73 | ParseResult* result) { |
353 | 73 | s = skip_ascii_whitespaces(s, len); |
354 | 73 | return string_to_int_internal<T>(s, len, base, result); |
355 | 73 | } |
356 | | |
357 | | template <typename T> |
358 | 4.29M | static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) { |
359 | 4.29M | s = skip_ascii_whitespaces(s, len); |
360 | 4.29M | return string_to_float_internal<T>(s, len, result); |
361 | 4.29M | } _ZN5doris12StringParser15string_to_floatIfEET_PKcmPNS0_11ParseResultE Line | Count | Source | 358 | 966k | static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) { | 359 | 966k | s = skip_ascii_whitespaces(s, len); | 360 | 966k | return string_to_float_internal<T>(s, len, result); | 361 | 966k | } |
_ZN5doris12StringParser15string_to_floatIdEET_PKcmPNS0_11ParseResultE Line | Count | Source | 358 | 3.33M | static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) { | 359 | 3.33M | s = skip_ascii_whitespaces(s, len); | 360 | 3.33M | return string_to_float_internal<T>(s, len, result); | 361 | 3.33M | } |
|
362 | | |
363 | | // Parses a string for 'true' or 'false', case insensitive. |
364 | 657k | static inline bool string_to_bool(const char* __restrict s, size_t len, ParseResult* result) { |
365 | 657k | s = skip_ascii_whitespaces(s, len); |
366 | 657k | return string_to_bool_internal(s, len, result); |
367 | 657k | } |
368 | | |
369 | | template <PrimitiveType P> |
370 | | static typename PrimitiveTypeTraits<P>::CppType::NativeType string_to_decimal( |
371 | | const char* __restrict s, size_t len, int type_precision, int type_scale, |
372 | | ParseResult* result); |
373 | | |
374 | | template <typename T> |
375 | | static Status split_string_to_map(const std::string& base, const T element_separator, |
376 | | const T key_value_separator, |
377 | | std::map<std::string, std::string>* result) { |
378 | | int key_pos = 0; |
379 | | int key_end; |
380 | | int val_pos; |
381 | | int val_end; |
382 | | |
383 | | while ((key_end = base.find(key_value_separator, key_pos)) != std::string::npos) { |
384 | | if ((val_pos = base.find_first_not_of(key_value_separator, key_end)) == |
385 | | std::string::npos) { |
386 | | break; |
387 | | } |
388 | | if ((val_end = base.find(element_separator, val_pos)) == std::string::npos) { |
389 | | val_end = base.size(); |
390 | | } |
391 | | result->insert(std::make_pair(base.substr(key_pos, key_end - key_pos), |
392 | | base.substr(val_pos, val_end - val_pos))); |
393 | | key_pos = val_end; |
394 | | if (key_pos != std::string::npos) { |
395 | | ++key_pos; |
396 | | } |
397 | | } |
398 | | |
399 | | return Status::OK(); |
400 | | } |
401 | | |
402 | | // This is considerably faster than glibc's implementation. |
403 | | // In the case of overflow, the max/min value for the data type will be returned. |
404 | | // Assumes s represents a decimal number. |
405 | | // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed. |
406 | | template <typename T, bool enable_strict_mode = false> |
407 | | static inline T string_to_int_internal(const char* __restrict s, int len, ParseResult* result); |
408 | | |
409 | | // This is considerably faster than glibc's implementation. |
410 | | // In the case of overflow, the max/min value for the data type will be returned. |
411 | | // Assumes s represents a decimal number. |
412 | | // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed. |
413 | | template <typename T> |
414 | | static inline T string_to_unsigned_int_internal(const char* __restrict s, int len, |
415 | | ParseResult* result); |
416 | | |
417 | | // Convert a string s representing a number in given base into a decimal number. |
418 | | // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed. |
419 | | template <typename T> |
420 | | static inline T string_to_int_internal(const char* __restrict s, int64_t len, int base, |
421 | | ParseResult* result); |
422 | | |
423 | | // Converts an ascii string to an integer of type T assuming it cannot overflow |
424 | | // and the number is positive. |
425 | | // Leading whitespace is not allowed. Trailing whitespace will be skipped. |
426 | | template <typename T, bool enable_strict_mode = false> |
427 | | static inline T string_to_int_no_overflow(const char* __restrict s, int len, |
428 | | ParseResult* result); |
429 | | |
430 | | // zero length, or at least one legal digit. at most consume MAX_LEN digits and stop. or stop when next |
431 | | // char is not a digit. |
432 | | template <typename T> |
433 | | static inline T string_to_uint_greedy_no_overflow(const char* __restrict s, int max_len, |
434 | | ParseResult* result); |
435 | | |
436 | | // This is considerably faster than glibc's implementation (>100x why???) |
437 | | // No special case handling needs to be done for overflows, the floating point spec |
438 | | // already does it and will cap the values to -inf/inf |
439 | | // To avoid inaccurate conversions this function falls back to strtod for |
440 | | // scientific notation. |
441 | | // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed. |
442 | | // TODO: Investigate using intrinsics to speed up the slow strtod path. |
443 | | template <typename T> |
444 | | static inline T string_to_float_internal(const char* __restrict s, int len, |
445 | | ParseResult* result); |
446 | | |
447 | | // parses a string for 'true' or 'false', case insensitive |
448 | | // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed. |
449 | | static inline bool string_to_bool_internal(const char* __restrict s, int len, |
450 | | ParseResult* result); |
451 | | |
452 | | // Returns true if s only contains whitespace. |
453 | 3.81k | static inline bool is_all_whitespace(const char* __restrict s, int len) { |
454 | 6.71k | for (int i = 0; i < len; ++i) { |
455 | 6.27k | if (!LIKELY(is_whitespace_ascii(s[i]))) { |
456 | 3.37k | return false; |
457 | 3.37k | } |
458 | 6.27k | } |
459 | 440 | return true; |
460 | 3.81k | } |
461 | | |
462 | | // For strings like "3.0", "3.123", and "3.", can parse them as 3. |
463 | 66.8k | static inline bool is_float_suffix(const char* __restrict s, int len) { |
464 | 66.8k | return (s[0] == '.' && is_all_digit(s + 1, len - 1)); |
465 | 66.8k | } |
466 | | |
467 | 45.6k | static inline bool is_all_digit(const char* __restrict s, int len) { |
468 | 91.6k | for (int i = 0; i < len; ++i) { |
469 | 46.1k | if (!LIKELY(s[i] >= '0' && s[i] <= '9')) { |
470 | 179 | return false; |
471 | 179 | } |
472 | 46.1k | } |
473 | 45.4k | return true; |
474 | 45.6k | } |
475 | | }; // end of class StringParser |
476 | | |
477 | | template <typename T, bool enable_strict_mode> |
478 | 140M | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { |
479 | 140M | if (UNLIKELY(len <= 0)) { |
480 | 282k | *result = PARSE_FAILURE; |
481 | 282k | return 0; |
482 | 282k | } |
483 | | |
484 | 140M | using UnsignedT = MakeUnsignedT<T>; |
485 | 140M | UnsignedT val = 0; |
486 | 140M | UnsignedT max_val = StringParser::numeric_limits<T>(false); |
487 | 140M | bool negative = false; |
488 | 140M | int i = 0; |
489 | 140M | switch (*s) { |
490 | 3.06M | case '-': |
491 | 3.06M | negative = true; |
492 | 3.06M | max_val += 1; |
493 | 3.06M | [[fallthrough]]; |
494 | 3.06M | case '+': |
495 | 3.06M | ++i; |
496 | | // only one '+'/'-' char, so could return failure directly |
497 | 3.06M | if (UNLIKELY(len == 1)) { |
498 | 2 | *result = PARSE_FAILURE; |
499 | 2 | return 0; |
500 | 2 | } |
501 | 140M | } |
502 | | |
503 | | // This is the fast path where the string cannot overflow. |
504 | 140M | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { |
505 | 139M | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); |
506 | 139M | return static_cast<T>(negative ? -val : val); |
507 | 139M | } |
508 | | |
509 | 1.39M | const T max_div_10 = max_val / 10; |
510 | 1.39M | const T max_mod_10 = max_val % 10; |
511 | | |
512 | 1.39M | int first = i; |
513 | 12.2M | for (; i < len; ++i) { |
514 | 11.0M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { |
515 | 10.9M | T digit = s[i] - '0'; |
516 | | // This is a tricky check to see if adding this digit will cause an overflow. |
517 | 10.9M | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { |
518 | 81.6k | *result = PARSE_OVERFLOW; |
519 | 81.6k | return negative ? -max_val : max_val; |
520 | 81.6k | } |
521 | 10.8M | val = val * 10 + digit; |
522 | 10.8M | } else { |
523 | 47.4k | if constexpr (enable_strict_mode) { |
524 | 4.15k | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { |
525 | | // Reject the string because the remaining chars are not all whitespace |
526 | 3.85k | *result = PARSE_FAILURE; |
527 | 3.85k | return 0; |
528 | 3.85k | } |
529 | 43.3k | } else { |
530 | | // Save original position where non-digit was found |
531 | 43.3k | int remaining_len = len - i; |
532 | 43.3k | const char* remaining_s = s + i; |
533 | | // Skip trailing whitespaces from the remaining portion |
534 | 43.3k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); |
535 | 43.3k | if ((UNLIKELY(i == first || (remaining_len != 0 && |
536 | 43.3k | !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 | 30.4k | *result = PARSE_FAILURE; |
540 | 30.4k | return 0; |
541 | 30.4k | } |
542 | 43.3k | } |
543 | | // Returning here is slightly faster than breaking the loop. |
544 | 13.2k | *result = PARSE_SUCCESS; |
545 | 47.4k | return static_cast<T>(negative ? -val : val); |
546 | 47.4k | } |
547 | 11.0M | } |
548 | 1.26M | *result = PARSE_SUCCESS; |
549 | 1.26M | return static_cast<T>(negative ? -val : val); |
550 | 1.39M | } _ZN5doris12StringParser22string_to_int_internalInLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 478 | 1.20M | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 1.20M | if (UNLIKELY(len <= 0)) { | 480 | 190k | *result = PARSE_FAILURE; | 481 | 190k | return 0; | 482 | 190k | } | 483 | | | 484 | 1.01M | using UnsignedT = MakeUnsignedT<T>; | 485 | 1.01M | UnsignedT val = 0; | 486 | 1.01M | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 1.01M | bool negative = false; | 488 | 1.01M | int i = 0; | 489 | 1.01M | switch (*s) { | 490 | 344k | case '-': | 491 | 344k | negative = true; | 492 | 344k | max_val += 1; | 493 | 344k | [[fallthrough]]; | 494 | 344k | case '+': | 495 | 344k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 344k | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 1.01M | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 1.01M | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 1.01M | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 1.01M | return static_cast<T>(negative ? -val : val); | 507 | 1.01M | } | 508 | | | 509 | 4.99k | const T max_div_10 = max_val / 10; | 510 | 4.99k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 4.99k | int first = i; | 513 | 186k | for (; i < len; ++i) { | 514 | 182k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 181k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 181k | 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 | 181k | val = val * 10 + digit; | 522 | 181k | } 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 | 524 | } else { | 530 | | // Save original position where non-digit was found | 531 | 524 | int remaining_len = len - i; | 532 | 524 | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | 524 | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | 524 | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | 524 | !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 | 524 | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 148 | *result = PARSE_SUCCESS; | 545 | 524 | return static_cast<T>(negative ? -val : val); | 546 | 524 | } | 547 | 182k | } | 548 | 3.95k | *result = PARSE_SUCCESS; | 549 | 3.95k | return static_cast<T>(negative ? -val : val); | 550 | 4.99k | } |
_ZN5doris12StringParser22string_to_int_internalIaLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 478 | 6.23M | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 6.23M | if (UNLIKELY(len <= 0)) { | 480 | 28.0k | *result = PARSE_FAILURE; | 481 | 28.0k | return 0; | 482 | 28.0k | } | 483 | | | 484 | 6.20M | using UnsignedT = MakeUnsignedT<T>; | 485 | 6.20M | UnsignedT val = 0; | 486 | 6.20M | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 6.20M | bool negative = false; | 488 | 6.20M | int i = 0; | 489 | 6.20M | switch (*s) { | 490 | 387k | case '-': | 491 | 387k | negative = true; | 492 | 387k | max_val += 1; | 493 | 387k | [[fallthrough]]; | 494 | 388k | case '+': | 495 | 388k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 388k | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 6.20M | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 6.20M | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 5.95M | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 5.95M | return static_cast<T>(negative ? -val : val); | 507 | 5.95M | } | 508 | | | 509 | 249k | const T max_div_10 = max_val / 10; | 510 | 249k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 249k | int first = i; | 513 | 874k | for (; i < len; ++i) { | 514 | 708k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 671k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 671k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 46.4k | *result = PARSE_OVERFLOW; | 519 | 46.4k | return negative ? -max_val : max_val; | 520 | 46.4k | } | 521 | 624k | val = val * 10 + digit; | 522 | 624k | } 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 | 708k | } | 548 | 166k | *result = PARSE_SUCCESS; | 549 | 166k | return static_cast<T>(negative ? -val : val); | 550 | 249k | } |
_ZN5doris12StringParser22string_to_int_internalIsLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 478 | 1.08M | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 1.08M | if (UNLIKELY(len <= 0)) { | 480 | 14.6k | *result = PARSE_FAILURE; | 481 | 14.6k | return 0; | 482 | 14.6k | } | 483 | | | 484 | 1.06M | using UnsignedT = MakeUnsignedT<T>; | 485 | 1.06M | UnsignedT val = 0; | 486 | 1.06M | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 1.06M | bool negative = false; | 488 | 1.06M | int i = 0; | 489 | 1.06M | switch (*s) { | 490 | 396k | case '-': | 491 | 396k | negative = true; | 492 | 396k | max_val += 1; | 493 | 396k | [[fallthrough]]; | 494 | 396k | case '+': | 495 | 396k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 396k | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 1.06M | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 1.06M | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 498k | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 498k | return static_cast<T>(negative ? -val : val); | 507 | 498k | } | 508 | | | 509 | 567k | const T max_div_10 = max_val / 10; | 510 | 567k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 567k | int first = i; | 513 | 3.37M | for (; i < len; ++i) { | 514 | 2.83M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 2.83M | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 2.83M | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 20.5k | *result = PARSE_OVERFLOW; | 519 | 20.5k | return negative ? -max_val : max_val; | 520 | 20.5k | } | 521 | 2.81M | val = val * 10 + digit; | 522 | 2.81M | } 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.36k | } else { | 530 | | // Save original position where non-digit was found | 531 | 2.36k | int remaining_len = len - i; | 532 | 2.36k | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | 2.36k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | 2.36k | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | 2.36k | !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.36k | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 574 | *result = PARSE_SUCCESS; | 545 | 2.36k | return static_cast<T>(negative ? -val : val); | 546 | 2.36k | } | 547 | 2.83M | } | 548 | 544k | *result = PARSE_SUCCESS; | 549 | 544k | return static_cast<T>(negative ? -val : val); | 550 | 567k | } |
_ZN5doris12StringParser22string_to_int_internalIiLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 478 | 52.3M | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 52.3M | if (UNLIKELY(len <= 0)) { | 480 | 22.9k | *result = PARSE_FAILURE; | 481 | 22.9k | return 0; | 482 | 22.9k | } | 483 | | | 484 | 52.2M | using UnsignedT = MakeUnsignedT<T>; | 485 | 52.2M | UnsignedT val = 0; | 486 | 52.2M | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 52.2M | bool negative = false; | 488 | 52.2M | int i = 0; | 489 | 52.2M | switch (*s) { | 490 | 861k | case '-': | 491 | 861k | negative = true; | 492 | 861k | max_val += 1; | 493 | 861k | [[fallthrough]]; | 494 | 861k | case '+': | 495 | 861k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 861k | if (UNLIKELY(len == 1)) { | 498 | 2 | *result = PARSE_FAILURE; | 499 | 2 | return 0; | 500 | 2 | } | 501 | 52.2M | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 52.2M | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 52.0M | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 52.0M | return static_cast<T>(negative ? -val : val); | 507 | 52.0M | } | 508 | | | 509 | 200k | const T max_div_10 = max_val / 10; | 510 | 200k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 200k | int first = i; | 513 | 1.84M | for (; i < len; ++i) { | 514 | 1.65M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 1.65M | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 1.65M | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 5.81k | *result = PARSE_OVERFLOW; | 519 | 5.81k | return negative ? -max_val : max_val; | 520 | 5.81k | } | 521 | 1.64M | val = val * 10 + digit; | 522 | 1.64M | } 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.54k | *result = PARSE_FAILURE; | 540 | 1.54k | return 0; | 541 | 1.54k | } | 542 | 1.93k | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 387 | *result = PARSE_SUCCESS; | 545 | 1.93k | return static_cast<T>(negative ? -val : val); | 546 | 1.93k | } | 547 | 1.65M | } | 548 | 192k | *result = PARSE_SUCCESS; | 549 | 192k | return static_cast<T>(negative ? -val : val); | 550 | 200k | } |
_ZN5doris12StringParser22string_to_int_internalIlLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 478 | 80.0M | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 80.0M | if (UNLIKELY(len <= 0)) { | 480 | 26.3k | *result = PARSE_FAILURE; | 481 | 26.3k | return 0; | 482 | 26.3k | } | 483 | | | 484 | 79.9M | using UnsignedT = MakeUnsignedT<T>; | 485 | 79.9M | UnsignedT val = 0; | 486 | 79.9M | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 79.9M | bool negative = false; | 488 | 79.9M | int i = 0; | 489 | 79.9M | switch (*s) { | 490 | 1.05M | case '-': | 491 | 1.05M | negative = true; | 492 | 1.05M | max_val += 1; | 493 | 1.05M | [[fallthrough]]; | 494 | 1.05M | case '+': | 495 | 1.05M | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 1.05M | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 79.9M | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 79.9M | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 79.5M | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 79.5M | return static_cast<T>(negative ? -val : val); | 507 | 79.5M | } | 508 | | | 509 | 351k | const T max_div_10 = max_val / 10; | 510 | 351k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 351k | int first = i; | 513 | 5.78M | for (; i < len; ++i) { | 514 | 5.44M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 5.44M | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 5.44M | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 5.40k | *result = PARSE_OVERFLOW; | 519 | 5.40k | return negative ? -max_val : max_val; | 520 | 5.40k | } | 521 | 5.43M | val = val * 10 + digit; | 522 | 5.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.56k | } else { | 530 | | // Save original position where non-digit was found | 531 | 1.56k | int remaining_len = len - i; | 532 | 1.56k | const char* remaining_s = s + i; | 533 | | // Skip trailing whitespaces from the remaining portion | 534 | 1.56k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 535 | 1.56k | if ((UNLIKELY(i == first || (remaining_len != 0 && | 536 | 1.56k | !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.25k | *result = PARSE_FAILURE; | 540 | 1.25k | return 0; | 541 | 1.25k | } | 542 | 1.56k | } | 543 | | // Returning here is slightly faster than breaking the loop. | 544 | 310 | *result = PARSE_SUCCESS; | 545 | 1.56k | return static_cast<T>(negative ? -val : val); | 546 | 1.56k | } | 547 | 5.44M | } | 548 | 344k | *result = PARSE_SUCCESS; | 549 | 344k | return static_cast<T>(negative ? -val : val); | 550 | 351k | } |
_ZN5doris12StringParser22string_to_int_internalIaLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 478 | 12.3k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 12.3k | if (UNLIKELY(len <= 0)) { | 480 | 8 | *result = PARSE_FAILURE; | 481 | 8 | return 0; | 482 | 8 | } | 483 | | | 484 | 12.3k | using UnsignedT = MakeUnsignedT<T>; | 485 | 12.3k | UnsignedT val = 0; | 486 | 12.3k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 12.3k | bool negative = false; | 488 | 12.3k | int i = 0; | 489 | 12.3k | switch (*s) { | 490 | 3.84k | case '-': | 491 | 3.84k | negative = true; | 492 | 3.84k | max_val += 1; | 493 | 3.84k | [[fallthrough]]; | 494 | 4.20k | case '+': | 495 | 4.20k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 4.20k | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 12.3k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 12.3k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 7.13k | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 7.13k | return static_cast<T>(negative ? -val : val); | 507 | 7.13k | } | 508 | | | 509 | 5.18k | const T max_div_10 = max_val / 10; | 510 | 5.18k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 5.18k | int first = i; | 513 | 19.8k | for (; i < len; ++i) { | 514 | 16.4k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 15.3k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 15.3k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 600 | *result = PARSE_OVERFLOW; | 519 | 600 | return negative ? -max_val : max_val; | 520 | 600 | } | 521 | 14.7k | val = val * 10 + digit; | 522 | 14.7k | } else { | 523 | 1.18k | if constexpr (enable_strict_mode) { | 524 | 1.18k | 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.10k | *result = PARSE_FAILURE; | 527 | 1.10k | return 0; | 528 | 1.10k | } | 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.18k | return static_cast<T>(negative ? -val : val); | 546 | 1.18k | } | 547 | 16.4k | } | 548 | 3.39k | *result = PARSE_SUCCESS; | 549 | 3.39k | return static_cast<T>(negative ? -val : val); | 550 | 5.18k | } |
_ZN5doris12StringParser22string_to_int_internalIsLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 478 | 12.2k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 12.2k | if (UNLIKELY(len <= 0)) { | 480 | 8 | *result = PARSE_FAILURE; | 481 | 8 | return 0; | 482 | 8 | } | 483 | | | 484 | 12.2k | using UnsignedT = MakeUnsignedT<T>; | 485 | 12.2k | UnsignedT val = 0; | 486 | 12.2k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 12.2k | bool negative = false; | 488 | 12.2k | int i = 0; | 489 | 12.2k | switch (*s) { | 490 | 4.86k | case '-': | 491 | 4.86k | negative = true; | 492 | 4.86k | max_val += 1; | 493 | 4.86k | [[fallthrough]]; | 494 | 5.21k | case '+': | 495 | 5.21k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 5.21k | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 12.2k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 12.2k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 4.35k | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 4.35k | return static_cast<T>(negative ? -val : val); | 507 | 4.35k | } | 508 | | | 509 | 7.93k | const T max_div_10 = max_val / 10; | 510 | 7.93k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 7.93k | int first = i; | 513 | 45.2k | for (; i < len; ++i) { | 514 | 38.9k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 37.8k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 37.8k | 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 | 37.3k | val = val * 10 + digit; | 522 | 37.3k | } 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 | 38.9k | } | 548 | 6.27k | *result = PARSE_SUCCESS; | 549 | 6.27k | return static_cast<T>(negative ? -val : val); | 550 | 7.93k | } |
_ZN5doris12StringParser22string_to_int_internalIiLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 478 | 13.9k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 13.9k | if (UNLIKELY(len <= 0)) { | 480 | 14 | *result = PARSE_FAILURE; | 481 | 14 | return 0; | 482 | 14 | } | 483 | | | 484 | 13.9k | using UnsignedT = MakeUnsignedT<T>; | 485 | 13.9k | UnsignedT val = 0; | 486 | 13.9k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 13.9k | bool negative = false; | 488 | 13.9k | int i = 0; | 489 | 13.9k | switch (*s) { | 490 | 4.30k | case '-': | 491 | 4.30k | negative = true; | 492 | 4.30k | max_val += 1; | 493 | 4.30k | [[fallthrough]]; | 494 | 4.65k | case '+': | 495 | 4.65k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 4.65k | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 13.9k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 13.9k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 7.76k | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 7.76k | return static_cast<T>(negative ? -val : val); | 507 | 7.76k | } | 508 | | | 509 | 6.18k | const T max_div_10 = max_val / 10; | 510 | 6.18k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 6.18k | int first = i; | 513 | 63.6k | for (; i < len; ++i) { | 514 | 58.7k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 57.9k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 57.9k | 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 | 57.4k | val = val * 10 + digit; | 522 | 57.4k | } 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 | 58.7k | } | 548 | 4.84k | *result = PARSE_SUCCESS; | 549 | 4.84k | return static_cast<T>(negative ? -val : val); | 550 | 6.18k | } |
_ZN5doris12StringParser22string_to_int_internalIlLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 478 | 13.0k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 13.0k | if (UNLIKELY(len <= 0)) { | 480 | 18 | *result = PARSE_FAILURE; | 481 | 18 | return 0; | 482 | 18 | } | 483 | | | 484 | 12.9k | using UnsignedT = MakeUnsignedT<T>; | 485 | 12.9k | UnsignedT val = 0; | 486 | 12.9k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 12.9k | bool negative = false; | 488 | 12.9k | int i = 0; | 489 | 12.9k | switch (*s) { | 490 | 2.39k | case '-': | 491 | 2.39k | negative = true; | 492 | 2.39k | max_val += 1; | 493 | 2.39k | [[fallthrough]]; | 494 | 2.84k | case '+': | 495 | 2.84k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 2.84k | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 12.9k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 12.9k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 10.6k | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 10.6k | return static_cast<T>(negative ? -val : val); | 507 | 10.6k | } | 508 | | | 509 | 2.32k | const T max_div_10 = max_val / 10; | 510 | 2.32k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 2.32k | int first = i; | 513 | 41.7k | for (; i < len; ++i) { | 514 | 40.7k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 40.1k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 40.1k | if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) { | 518 | 704 | *result = PARSE_OVERFLOW; | 519 | 704 | return negative ? -max_val : max_val; | 520 | 704 | } | 521 | 39.4k | val = val * 10 + digit; | 522 | 39.4k | } else { | 523 | 591 | if constexpr (enable_strict_mode) { | 524 | 591 | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | 559 | *result = PARSE_FAILURE; | 527 | 559 | return 0; | 528 | 559 | } | 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 | 32 | *result = PARSE_SUCCESS; | 545 | 591 | return static_cast<T>(negative ? -val : val); | 546 | 591 | } | 547 | 40.7k | } | 548 | 1.02k | *result = PARSE_SUCCESS; | 549 | 1.02k | return static_cast<T>(negative ? -val : val); | 550 | 2.32k | } |
_ZN5doris12StringParser22string_to_int_internalInLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 478 | 12.5k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 12.5k | if (UNLIKELY(len <= 0)) { | 480 | 8 | *result = PARSE_FAILURE; | 481 | 8 | return 0; | 482 | 8 | } | 483 | | | 484 | 12.5k | using UnsignedT = MakeUnsignedT<T>; | 485 | 12.5k | UnsignedT val = 0; | 486 | 12.5k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 12.5k | bool negative = false; | 488 | 12.5k | int i = 0; | 489 | 12.5k | switch (*s) { | 490 | 3.69k | case '-': | 491 | 3.69k | negative = true; | 492 | 3.69k | max_val += 1; | 493 | 3.69k | [[fallthrough]]; | 494 | 4.02k | case '+': | 495 | 4.02k | ++i; | 496 | | // only one '+'/'-' char, so could return failure directly | 497 | 4.02k | if (UNLIKELY(len == 1)) { | 498 | 0 | *result = PARSE_FAILURE; | 499 | 0 | return 0; | 500 | 0 | } | 501 | 12.5k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 12.5k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 11.4k | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 11.4k | return static_cast<T>(negative ? -val : val); | 507 | 11.4k | } | 508 | | | 509 | 1.06k | const T max_div_10 = max_val / 10; | 510 | 1.06k | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 1.06k | int first = i; | 513 | 33.0k | for (; i < len; ++i) { | 514 | 32.9k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 32.4k | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 32.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 | 31.9k | val = val * 10 + digit; | 522 | 31.9k | } else { | 523 | 488 | if constexpr (enable_strict_mode) { | 524 | 488 | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | 456 | *result = PARSE_FAILURE; | 527 | 456 | return 0; | 528 | 456 | } | 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 | 32 | *result = PARSE_SUCCESS; | 545 | 488 | return static_cast<T>(negative ? -val : val); | 546 | 488 | } | 547 | 32.9k | } | 548 | 76 | *result = PARSE_SUCCESS; | 549 | 76 | return static_cast<T>(negative ? -val : val); | 550 | 1.06k | } |
Unexecuted instantiation: _ZN5doris12StringParser22string_to_int_internalIjLb0EEET_PKciPNS0_11ParseResultE _ZN5doris12StringParser22string_to_int_internalImLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 478 | 3.78k | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 3.78k | if (UNLIKELY(len <= 0)) { | 480 | 0 | *result = PARSE_FAILURE; | 481 | 0 | return 0; | 482 | 0 | } | 483 | | | 484 | 3.78k | using UnsignedT = MakeUnsignedT<T>; | 485 | 3.78k | UnsignedT val = 0; | 486 | 3.78k | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 3.78k | bool negative = false; | 488 | 3.78k | int i = 0; | 489 | 3.78k | switch (*s) { | 490 | 0 | case '-': | 491 | 0 | negative = true; | 492 | 0 | max_val += 1; | 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 | 3.78k | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 3.78k | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 3.78k | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 3.78k | return static_cast<T>(negative ? -val : val); | 507 | 3.78k | } | 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_internalIjLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 478 | 750 | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 750 | if (UNLIKELY(len <= 0)) { | 480 | 7 | *result = PARSE_FAILURE; | 481 | 7 | return 0; | 482 | 7 | } | 483 | | | 484 | 743 | using UnsignedT = MakeUnsignedT<T>; | 485 | 743 | UnsignedT val = 0; | 486 | 743 | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 743 | bool negative = false; | 488 | 743 | int i = 0; | 489 | 743 | switch (*s) { | 490 | 0 | case '-': | 491 | 0 | negative = true; | 492 | 0 | max_val += 1; | 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 | 743 | } | 502 | | | 503 | | // This is the fast path where the string cannot overflow. | 504 | 743 | if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) { | 505 | 743 | val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result); | 506 | 743 | return static_cast<T>(negative ? -val : val); | 507 | 743 | } | 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 | 0 | if constexpr (enable_strict_mode) { | 524 | 0 | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { | 525 | | // Reject the string because the remaining chars are not all whitespace | 526 | 0 | *result = PARSE_FAILURE; | 527 | 0 | return 0; | 528 | 0 | } | 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 | 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_internalIN4wide7integerILm256EiEELb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 478 | 4 | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 4 | if (UNLIKELY(len <= 0)) { | 480 | 0 | *result = PARSE_FAILURE; | 481 | 0 | return 0; | 482 | 0 | } | 483 | | | 484 | 4 | using UnsignedT = MakeUnsignedT<T>; | 485 | 4 | UnsignedT val = 0; | 486 | 4 | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 4 | bool negative = false; | 488 | 4 | int i = 0; | 489 | 4 | switch (*s) { | 490 | 0 | case '-': | 491 | 0 | negative = true; | 492 | 0 | max_val += 1; | 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 | 478 | 4 | T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) { | 479 | 4 | if (UNLIKELY(len <= 0)) { | 480 | 0 | *result = PARSE_FAILURE; | 481 | 0 | return 0; | 482 | 0 | } | 483 | | | 484 | 4 | using UnsignedT = MakeUnsignedT<T>; | 485 | 4 | UnsignedT val = 0; | 486 | 4 | UnsignedT max_val = StringParser::numeric_limits<T>(false); | 487 | 4 | bool negative = false; | 488 | 4 | int i = 0; | 489 | 4 | switch (*s) { | 490 | 0 | case '-': | 491 | 0 | negative = true; | 492 | 0 | max_val += 1; | 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 | 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 | 4 | const T max_div_10 = max_val / 10; | 510 | 4 | const T max_mod_10 = max_val % 10; | 511 | | | 512 | 4 | int first = i; | 513 | 84 | for (; i < len; ++i) { | 514 | 80 | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 515 | 80 | T digit = s[i] - '0'; | 516 | | // This is a tricky check to see if adding this digit will cause an overflow. | 517 | 80 | 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 | 80 | val = val * 10 + digit; | 522 | 80 | } 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 | 80 | } | 548 | 4 | *result = PARSE_SUCCESS; | 549 | 4 | return static_cast<T>(negative ? -val : val); | 550 | 4 | } |
|
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 | 98 | const T max_div_10 = max_val / 10; |
572 | 98 | const T max_mod_10 = max_val % 10; |
573 | | |
574 | 98 | int first = i; |
575 | 2.00k | 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 | 49 | *result = PARSE_SUCCESS; |
597 | 49 | return val; |
598 | 98 | } |
599 | | |
600 | | template <typename T> |
601 | | T StringParser::string_to_int_internal(const char* __restrict s, int64_t len, int base, |
602 | 73 | ParseResult* result) { |
603 | 73 | using UnsignedT = MakeUnsignedT<T>; |
604 | 73 | UnsignedT val = 0; |
605 | 73 | UnsignedT max_val = StringParser::numeric_limits<T>(false); |
606 | 73 | bool negative = false; |
607 | 73 | if (UNLIKELY(len <= 0)) { |
608 | 2 | *result = PARSE_FAILURE; |
609 | 2 | return 0; |
610 | 2 | } |
611 | 71 | int i = 0; |
612 | 71 | switch (*s) { |
613 | 3 | case '-': |
614 | 3 | negative = true; |
615 | 3 | max_val = StringParser::numeric_limits<T>(false) + 1; |
616 | 3 | [[fallthrough]]; |
617 | 3 | case '+': |
618 | 3 | i = 1; |
619 | 71 | } |
620 | | |
621 | 71 | const T max_div_base = max_val / base; |
622 | 71 | const T max_mod_base = max_val % base; |
623 | | |
624 | 71 | int first = i; |
625 | 178 | for (; i < len; ++i) { |
626 | 156 | T digit; |
627 | 156 | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { |
628 | 69 | digit = s[i] - '0'; |
629 | 87 | } else if (s[i] >= 'a' && s[i] <= 'z') { |
630 | 81 | digit = (s[i] - 'a' + 10); |
631 | 81 | } else if (s[i] >= 'A' && s[i] <= 'Z') { |
632 | 6 | digit = (s[i] - 'A' + 10); |
633 | 6 | } else { |
634 | 0 | if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) { |
635 | | // Reject the string because either the first char was not an alpha/digit, |
636 | | // or the remaining chars are not all whitespace |
637 | 0 | *result = PARSE_FAILURE; |
638 | 0 | return 0; |
639 | 0 | } |
640 | | // skip trailing whitespace. |
641 | 0 | break; |
642 | 0 | } |
643 | | |
644 | | // Bail, if we encounter a digit that is not available in base. |
645 | 156 | if (digit >= base) { |
646 | 48 | break; |
647 | 48 | } |
648 | | |
649 | | // This is a tricky check to see if adding this digit will cause an overflow. |
650 | 108 | if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) { |
651 | 1 | *result = PARSE_OVERFLOW; |
652 | 1 | return static_cast<T>(negative ? -max_val : max_val); |
653 | 1 | } |
654 | 107 | val = val * base + digit; |
655 | 107 | } |
656 | 70 | *result = PARSE_SUCCESS; |
657 | 70 | return static_cast<T>(negative ? -val : val); |
658 | 71 | } |
659 | | |
660 | | template <typename T, bool enable_strict_mode> |
661 | 139M | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { |
662 | 139M | T val = 0; |
663 | 139M | if (UNLIKELY(len == 0)) { |
664 | 0 | *result = PARSE_SUCCESS; |
665 | 0 | return val; |
666 | 0 | } |
667 | | // Factor out the first char for error handling speeds up the loop. |
668 | 139M | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { |
669 | 139M | val = s[0] - '0'; |
670 | 18.4E | } else { |
671 | 18.4E | *result = PARSE_FAILURE; |
672 | 18.4E | return 0; |
673 | 18.4E | } |
674 | 569M | for (int i = 1; i < len; ++i) { |
675 | 430M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { |
676 | 430M | T digit = s[i] - '0'; |
677 | 430M | val = val * 10 + digit; |
678 | 430M | } else { |
679 | 7.12k | if constexpr (enable_strict_mode) { |
680 | 1.51k | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { |
681 | 1.37k | *result = PARSE_FAILURE; |
682 | 1.37k | return 0; |
683 | 1.37k | } |
684 | 5.60k | } else { |
685 | | // Save original position where non-digit was found |
686 | 5.60k | int remaining_len = len - i; |
687 | 5.60k | const char* remaining_s = s + i; |
688 | | // Skip trailing whitespaces from the remaining portion |
689 | 5.60k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); |
690 | 5.60k | if ((UNLIKELY(remaining_len != 0 && |
691 | 20.7k | !is_float_suffix(remaining_s, remaining_len)))) { |
692 | 20.7k | *result = PARSE_FAILURE; |
693 | 20.7k | return 0; |
694 | 20.7k | } |
695 | 5.60k | } |
696 | 18.4E | *result = PARSE_SUCCESS; |
697 | 7.12k | return val; |
698 | 7.12k | } |
699 | 430M | } |
700 | 139M | *result = PARSE_SUCCESS; |
701 | 139M | return val; |
702 | 139M | } _ZN5doris12StringParser25string_to_int_no_overflowIoLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 661 | 1.01M | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 662 | 1.01M | T val = 0; | 663 | 1.01M | if (UNLIKELY(len == 0)) { | 664 | 0 | *result = PARSE_SUCCESS; | 665 | 0 | return val; | 666 | 0 | } | 667 | | // Factor out the first char for error handling speeds up the loop. | 668 | 1.01M | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 669 | 1.01M | val = s[0] - '0'; | 670 | 1.01M | } else { | 671 | 1.32k | *result = PARSE_FAILURE; | 672 | 1.32k | return 0; | 673 | 1.32k | } | 674 | 9.61M | for (int i = 1; i < len; ++i) { | 675 | 8.62M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 676 | 8.60M | T digit = s[i] - '0'; | 677 | 8.60M | val = val * 10 + digit; | 678 | 8.60M | } else { | 679 | | if constexpr (enable_strict_mode) { | 680 | | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 681 | | *result = PARSE_FAILURE; | 682 | | return 0; | 683 | | } | 684 | 20.4k | } else { | 685 | | // Save original position where non-digit was found | 686 | 20.4k | int remaining_len = len - i; | 687 | 20.4k | const char* remaining_s = s + i; | 688 | | // Skip trailing whitespaces from the remaining portion | 689 | 20.4k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 690 | 20.4k | if ((UNLIKELY(remaining_len != 0 && | 691 | 20.4k | !is_float_suffix(remaining_s, remaining_len)))) { | 692 | 20.1k | *result = PARSE_FAILURE; | 693 | 20.1k | return 0; | 694 | 20.1k | } | 695 | 20.4k | } | 696 | 306 | *result = PARSE_SUCCESS; | 697 | 20.4k | return val; | 698 | 20.4k | } | 699 | 8.62M | } | 700 | 991k | *result = PARSE_SUCCESS; | 701 | 991k | return val; | 702 | 1.01M | } |
_ZN5doris12StringParser25string_to_int_no_overflowIhLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 661 | 5.96M | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 662 | 5.96M | T val = 0; | 663 | 5.96M | if (UNLIKELY(len == 0)) { | 664 | 0 | *result = PARSE_SUCCESS; | 665 | 0 | return val; | 666 | 0 | } | 667 | | // Factor out the first char for error handling speeds up the loop. | 668 | 5.96M | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 669 | 5.96M | val = s[0] - '0'; | 670 | 5.96M | } else { | 671 | 795 | *result = PARSE_FAILURE; | 672 | 795 | return 0; | 673 | 795 | } | 674 | 6.50M | for (int i = 1; i < len; ++i) { | 675 | 541k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 676 | 541k | T digit = s[i] - '0'; | 677 | 541k | val = val * 10 + digit; | 678 | 541k | } else { | 679 | | if constexpr (enable_strict_mode) { | 680 | | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 681 | | *result = PARSE_FAILURE; | 682 | | return 0; | 683 | | } | 684 | 4 | } else { | 685 | | // Save original position where non-digit was found | 686 | 4 | int remaining_len = len - i; | 687 | 4 | const char* remaining_s = s + i; | 688 | | // Skip trailing whitespaces from the remaining portion | 689 | 4 | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 690 | 4 | if ((UNLIKELY(remaining_len != 0 && | 691 | 4 | !is_float_suffix(remaining_s, remaining_len)))) { | 692 | 2 | *result = PARSE_FAILURE; | 693 | 2 | return 0; | 694 | 2 | } | 695 | 4 | } | 696 | 2 | *result = PARSE_SUCCESS; | 697 | 4 | return val; | 698 | 4 | } | 699 | 541k | } | 700 | 5.96M | *result = PARSE_SUCCESS; | 701 | 5.96M | return val; | 702 | 5.96M | } |
_ZN5doris12StringParser25string_to_int_no_overflowItLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 661 | 498k | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 662 | 498k | T val = 0; | 663 | 498k | if (UNLIKELY(len == 0)) { | 664 | 0 | *result = PARSE_SUCCESS; | 665 | 0 | return val; | 666 | 0 | } | 667 | | // Factor out the first char for error handling speeds up the loop. | 668 | 498k | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 669 | 497k | val = s[0] - '0'; | 670 | 497k | } else { | 671 | 1.40k | *result = PARSE_FAILURE; | 672 | 1.40k | return 0; | 673 | 1.40k | } | 674 | 1.21M | for (int i = 1; i < len; ++i) { | 675 | 717k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 676 | 716k | T digit = s[i] - '0'; | 677 | 716k | val = val * 10 + digit; | 678 | 716k | } else { | 679 | | if constexpr (enable_strict_mode) { | 680 | | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 681 | | *result = PARSE_FAILURE; | 682 | | return 0; | 683 | | } | 684 | 966 | } else { | 685 | | // Save original position where non-digit was found | 686 | 966 | int remaining_len = len - i; | 687 | 966 | const char* remaining_s = s + i; | 688 | | // Skip trailing whitespaces from the remaining portion | 689 | 966 | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 690 | 966 | if ((UNLIKELY(remaining_len != 0 && | 691 | 966 | !is_float_suffix(remaining_s, remaining_len)))) { | 692 | 74 | *result = PARSE_FAILURE; | 693 | 74 | return 0; | 694 | 74 | } | 695 | 966 | } | 696 | 892 | *result = PARSE_SUCCESS; | 697 | 966 | return val; | 698 | 966 | } | 699 | 717k | } | 700 | 496k | *result = PARSE_SUCCESS; | 701 | 496k | return val; | 702 | 497k | } |
_ZN5doris12StringParser25string_to_int_no_overflowIjLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 661 | 52.0M | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 662 | 52.0M | T val = 0; | 663 | 52.0M | if (UNLIKELY(len == 0)) { | 664 | 0 | *result = PARSE_SUCCESS; | 665 | 0 | return val; | 666 | 0 | } | 667 | | // Factor out the first char for error handling speeds up the loop. | 668 | 52.0M | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 669 | 52.0M | val = s[0] - '0'; | 670 | 18.4E | } else { | 671 | 18.4E | *result = PARSE_FAILURE; | 672 | 18.4E | return 0; | 673 | 18.4E | } | 674 | 193M | for (int i = 1; i < len; ++i) { | 675 | 141M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 676 | 141M | T digit = s[i] - '0'; | 677 | 141M | val = val * 10 + digit; | 678 | 141M | } else { | 679 | | if constexpr (enable_strict_mode) { | 680 | | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 681 | | *result = PARSE_FAILURE; | 682 | | return 0; | 683 | | } | 684 | 29.2k | } else { | 685 | | // Save original position where non-digit was found | 686 | 29.2k | int remaining_len = len - i; | 687 | 29.2k | const char* remaining_s = s + i; | 688 | | // Skip trailing whitespaces from the remaining portion | 689 | 29.2k | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 690 | 29.2k | if ((UNLIKELY(remaining_len != 0 && | 691 | 29.2k | !is_float_suffix(remaining_s, remaining_len)))) { | 692 | 324 | *result = PARSE_FAILURE; | 693 | 324 | return 0; | 694 | 324 | } | 695 | 29.2k | } | 696 | 28.9k | *result = PARSE_SUCCESS; | 697 | 29.2k | return val; | 698 | 29.2k | } | 699 | 141M | } | 700 | 52.0M | *result = PARSE_SUCCESS; | 701 | 52.0M | return val; | 702 | 52.0M | } |
_ZN5doris12StringParser25string_to_int_no_overflowImLb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 661 | 79.6M | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 662 | 79.6M | T val = 0; | 663 | 79.6M | if (UNLIKELY(len == 0)) { | 664 | 0 | *result = PARSE_SUCCESS; | 665 | 0 | return val; | 666 | 0 | } | 667 | | // Factor out the first char for error handling speeds up the loop. | 668 | 79.8M | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 669 | 79.8M | val = s[0] - '0'; | 670 | 18.4E | } else { | 671 | 18.4E | *result = PARSE_FAILURE; | 672 | 18.4E | return 0; | 673 | 18.4E | } | 674 | 359M | for (int i = 1; i < len; ++i) { | 675 | 279M | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 676 | 279M | T digit = s[i] - '0'; | 677 | 279M | val = val * 10 + digit; | 678 | 18.4E | } else { | 679 | | if constexpr (enable_strict_mode) { | 680 | | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 681 | | *result = PARSE_FAILURE; | 682 | | return 0; | 683 | | } | 684 | 18.4E | } else { | 685 | | // Save original position where non-digit was found | 686 | 18.4E | int remaining_len = len - i; | 687 | 18.4E | const char* remaining_s = s + i; | 688 | | // Skip trailing whitespaces from the remaining portion | 689 | 18.4E | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 690 | 18.4E | if ((UNLIKELY(remaining_len != 0 && | 691 | 18.4E | !is_float_suffix(remaining_s, remaining_len)))) { | 692 | 235 | *result = PARSE_FAILURE; | 693 | 235 | return 0; | 694 | 235 | } | 695 | 18.4E | } | 696 | 18.4E | *result = PARSE_SUCCESS; | 697 | 18.4E | return val; | 698 | 18.4E | } | 699 | 279M | } | 700 | 79.8M | *result = PARSE_SUCCESS; | 701 | 79.8M | return val; | 702 | 79.8M | } |
_ZN5doris12StringParser25string_to_int_no_overflowIhLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 661 | 7.13k | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 662 | 7.13k | T val = 0; | 663 | 7.13k | if (UNLIKELY(len == 0)) { | 664 | 0 | *result = PARSE_SUCCESS; | 665 | 0 | return val; | 666 | 0 | } | 667 | | // Factor out the first char for error handling speeds up the loop. | 668 | 7.13k | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 669 | 7.10k | val = s[0] - '0'; | 670 | 7.10k | } else { | 671 | 26 | *result = PARSE_FAILURE; | 672 | 26 | return 0; | 673 | 26 | } | 674 | 9.96k | for (int i = 1; i < len; ++i) { | 675 | 2.86k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 676 | 2.86k | T digit = s[i] - '0'; | 677 | 2.86k | val = val * 10 + digit; | 678 | 2.86k | } else { | 679 | 2 | if constexpr (enable_strict_mode) { | 680 | 2 | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 681 | 2 | *result = PARSE_FAILURE; | 682 | 2 | return 0; | 683 | 2 | } | 684 | | } else { | 685 | | // Save original position where non-digit was found | 686 | | int remaining_len = len - i; | 687 | | const char* remaining_s = s + i; | 688 | | // Skip trailing whitespaces from the remaining portion | 689 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 690 | | if ((UNLIKELY(remaining_len != 0 && | 691 | | !is_float_suffix(remaining_s, remaining_len)))) { | 692 | | *result = PARSE_FAILURE; | 693 | | return 0; | 694 | | } | 695 | | } | 696 | 0 | *result = PARSE_SUCCESS; | 697 | 2 | return val; | 698 | 2 | } | 699 | 2.86k | } | 700 | 7.10k | *result = PARSE_SUCCESS; | 701 | 7.10k | return val; | 702 | 7.10k | } |
_ZN5doris12StringParser25string_to_int_no_overflowItLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 661 | 4.35k | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 662 | 4.35k | T val = 0; | 663 | 4.35k | if (UNLIKELY(len == 0)) { | 664 | 0 | *result = PARSE_SUCCESS; | 665 | 0 | return val; | 666 | 0 | } | 667 | | // Factor out the first char for error handling speeds up the loop. | 668 | 4.35k | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 669 | 4.31k | val = s[0] - '0'; | 670 | 4.31k | } else { | 671 | 38 | *result = PARSE_FAILURE; | 672 | 38 | return 0; | 673 | 38 | } | 674 | 12.3k | for (int i = 1; i < len; ++i) { | 675 | 8.11k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 676 | 8.03k | T digit = s[i] - '0'; | 677 | 8.03k | val = val * 10 + digit; | 678 | 8.03k | } else { | 679 | 82 | if constexpr (enable_strict_mode) { | 680 | 82 | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 681 | 82 | *result = PARSE_FAILURE; | 682 | 82 | return 0; | 683 | 82 | } | 684 | | } else { | 685 | | // Save original position where non-digit was found | 686 | | int remaining_len = len - i; | 687 | | const char* remaining_s = s + i; | 688 | | // Skip trailing whitespaces from the remaining portion | 689 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 690 | | if ((UNLIKELY(remaining_len != 0 && | 691 | | !is_float_suffix(remaining_s, remaining_len)))) { | 692 | | *result = PARSE_FAILURE; | 693 | | return 0; | 694 | | } | 695 | | } | 696 | 0 | *result = PARSE_SUCCESS; | 697 | 82 | return val; | 698 | 82 | } | 699 | 8.11k | } | 700 | 4.23k | *result = PARSE_SUCCESS; | 701 | 4.23k | return val; | 702 | 4.31k | } |
_ZN5doris12StringParser25string_to_int_no_overflowIjLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 661 | 8.50k | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 662 | 8.50k | T val = 0; | 663 | 8.50k | if (UNLIKELY(len == 0)) { | 664 | 0 | *result = PARSE_SUCCESS; | 665 | 0 | return val; | 666 | 0 | } | 667 | | // Factor out the first char for error handling speeds up the loop. | 668 | 8.50k | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 669 | 8.38k | val = s[0] - '0'; | 670 | 8.38k | } else { | 671 | 125 | *result = PARSE_FAILURE; | 672 | 125 | return 0; | 673 | 125 | } | 674 | 43.3k | for (int i = 1; i < len; ++i) { | 675 | 35.3k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 676 | 34.9k | T digit = s[i] - '0'; | 677 | 34.9k | val = val * 10 + digit; | 678 | 34.9k | } else { | 679 | 320 | if constexpr (enable_strict_mode) { | 680 | 320 | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 681 | 292 | *result = PARSE_FAILURE; | 682 | 292 | return 0; | 683 | 292 | } | 684 | | } else { | 685 | | // Save original position where non-digit was found | 686 | | int remaining_len = len - i; | 687 | | const char* remaining_s = s + i; | 688 | | // Skip trailing whitespaces from the remaining portion | 689 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 690 | | if ((UNLIKELY(remaining_len != 0 && | 691 | | !is_float_suffix(remaining_s, remaining_len)))) { | 692 | | *result = PARSE_FAILURE; | 693 | | return 0; | 694 | | } | 695 | | } | 696 | 28 | *result = PARSE_SUCCESS; | 697 | 320 | return val; | 698 | 320 | } | 699 | 35.3k | } | 700 | 8.06k | *result = PARSE_SUCCESS; | 701 | 8.06k | return val; | 702 | 8.38k | } |
_ZN5doris12StringParser25string_to_int_no_overflowImLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 661 | 10.6k | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 662 | 10.6k | T val = 0; | 663 | 10.6k | if (UNLIKELY(len == 0)) { | 664 | 0 | *result = PARSE_SUCCESS; | 665 | 0 | return val; | 666 | 0 | } | 667 | | // Factor out the first char for error handling speeds up the loop. | 668 | 10.6k | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 669 | 10.3k | val = s[0] - '0'; | 670 | 10.3k | } else { | 671 | 333 | *result = PARSE_FAILURE; | 672 | 333 | return 0; | 673 | 333 | } | 674 | 46.1k | for (int i = 1; i < len; ++i) { | 675 | 36.4k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 676 | 35.8k | T digit = s[i] - '0'; | 677 | 35.8k | val = val * 10 + digit; | 678 | 35.8k | } else { | 679 | 656 | if constexpr (enable_strict_mode) { | 680 | 656 | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 681 | 600 | *result = PARSE_FAILURE; | 682 | 600 | return 0; | 683 | 600 | } | 684 | | } else { | 685 | | // Save original position where non-digit was found | 686 | | int remaining_len = len - i; | 687 | | const char* remaining_s = s + i; | 688 | | // Skip trailing whitespaces from the remaining portion | 689 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 690 | | if ((UNLIKELY(remaining_len != 0 && | 691 | | !is_float_suffix(remaining_s, remaining_len)))) { | 692 | | *result = PARSE_FAILURE; | 693 | | return 0; | 694 | | } | 695 | | } | 696 | 56 | *result = PARSE_SUCCESS; | 697 | 656 | return val; | 698 | 656 | } | 699 | 36.4k | } | 700 | 9.67k | *result = PARSE_SUCCESS; | 701 | 9.67k | return val; | 702 | 10.3k | } |
_ZN5doris12StringParser25string_to_int_no_overflowIoLb1EEET_PKciPNS0_11ParseResultE Line | Count | Source | 661 | 11.4k | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 662 | 11.4k | T val = 0; | 663 | 11.4k | if (UNLIKELY(len == 0)) { | 664 | 0 | *result = PARSE_SUCCESS; | 665 | 0 | return val; | 666 | 0 | } | 667 | | // Factor out the first char for error handling speeds up the loop. | 668 | 11.4k | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 669 | 11.2k | val = s[0] - '0'; | 670 | 11.2k | } else { | 671 | 240 | *result = PARSE_FAILURE; | 672 | 240 | return 0; | 673 | 240 | } | 674 | 112k | for (int i = 1; i < len; ++i) { | 675 | 101k | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 676 | 100k | T digit = s[i] - '0'; | 677 | 100k | val = val * 10 + digit; | 678 | 100k | } else { | 679 | 456 | if constexpr (enable_strict_mode) { | 680 | 456 | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 681 | 400 | *result = PARSE_FAILURE; | 682 | 400 | return 0; | 683 | 400 | } | 684 | | } else { | 685 | | // Save original position where non-digit was found | 686 | | int remaining_len = len - i; | 687 | | const char* remaining_s = s + i; | 688 | | // Skip trailing whitespaces from the remaining portion | 689 | | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 690 | | if ((UNLIKELY(remaining_len != 0 && | 691 | | !is_float_suffix(remaining_s, remaining_len)))) { | 692 | | *result = PARSE_FAILURE; | 693 | | return 0; | 694 | | } | 695 | | } | 696 | 56 | *result = PARSE_SUCCESS; | 697 | 456 | return val; | 698 | 456 | } | 699 | 101k | } | 700 | 10.7k | *result = PARSE_SUCCESS; | 701 | 10.7k | return val; | 702 | 11.2k | } |
_ZN5doris12StringParser25string_to_int_no_overflowIN4wide7integerILm256EjEELb0EEET_PKciPNS0_11ParseResultE Line | Count | Source | 661 | 4 | T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) { | 662 | 4 | T val = 0; | 663 | 4 | if (UNLIKELY(len == 0)) { | 664 | 0 | *result = PARSE_SUCCESS; | 665 | 0 | return val; | 666 | 0 | } | 667 | | // Factor out the first char for error handling speeds up the loop. | 668 | 4 | if (LIKELY(s[0] >= '0' && s[0] <= '9')) { | 669 | 4 | val = s[0] - '0'; | 670 | 4 | } else { | 671 | 0 | *result = PARSE_FAILURE; | 672 | 0 | return 0; | 673 | 0 | } | 674 | 4 | for (int i = 1; i < len; ++i) { | 675 | 0 | if (LIKELY(s[i] >= '0' && s[i] <= '9')) { | 676 | 0 | T digit = s[i] - '0'; | 677 | 0 | val = val * 10 + digit; | 678 | 0 | } else { | 679 | | if constexpr (enable_strict_mode) { | 680 | | if (UNLIKELY(!is_all_whitespace(s + i, len - i))) { | 681 | | *result = PARSE_FAILURE; | 682 | | return 0; | 683 | | } | 684 | 0 | } else { | 685 | | // Save original position where non-digit was found | 686 | 0 | int remaining_len = len - i; | 687 | 0 | const char* remaining_s = s + i; | 688 | | // Skip trailing whitespaces from the remaining portion | 689 | 0 | remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len); | 690 | 0 | if ((UNLIKELY(remaining_len != 0 && | 691 | 0 | !is_float_suffix(remaining_s, remaining_len)))) { | 692 | 0 | *result = PARSE_FAILURE; | 693 | 0 | return 0; | 694 | 0 | } | 695 | 0 | } | 696 | 0 | *result = PARSE_SUCCESS; | 697 | 0 | return val; | 698 | 0 | } | 699 | 0 | } | 700 | 4 | *result = PARSE_SUCCESS; | 701 | 4 | return val; | 702 | 4 | } |
|
703 | | |
704 | | // at least the first char(if any) must be a digit. |
705 | | template <typename T> |
706 | | T StringParser::string_to_uint_greedy_no_overflow(const char* __restrict s, int max_len, |
707 | 689k | ParseResult* result) { |
708 | 689k | T val = 0; |
709 | 689k | if (max_len == 0) [[unlikely]] { |
710 | 461k | *result = PARSE_SUCCESS; |
711 | 461k | return val; |
712 | 461k | } |
713 | | // Factor out the first char for error handling speeds up the loop. |
714 | 228k | if (is_numeric_ascii(s[0])) [[likely]] { |
715 | 228k | val = s[0] - '0'; |
716 | 18.4E | } else { |
717 | 18.4E | *result = PARSE_FAILURE; |
718 | 18.4E | return 0; |
719 | 18.4E | } |
720 | 1.07M | for (int i = 1; i < max_len; ++i) { |
721 | 848k | if (is_numeric_ascii(s[i])) [[likely]] { |
722 | 848k | T digit = s[i] - '0'; |
723 | 848k | val = val * 10 + digit; |
724 | 848k | } else { |
725 | | // 123abc, return 123 |
726 | 1 | *result = PARSE_SUCCESS; |
727 | 1 | return val; |
728 | 1 | } |
729 | 848k | } |
730 | 228k | *result = PARSE_SUCCESS; |
731 | 228k | return val; |
732 | 228k | } |
733 | | |
734 | | template <typename T> |
735 | 4.29M | T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) { |
736 | 4.29M | int i = 0; |
737 | | // skip leading spaces |
738 | 4.29M | for (; i < len; ++i) { |
739 | 4.25M | if (!is_whitespace_ascii(s[i])) { |
740 | 4.25M | break; |
741 | 4.25M | } |
742 | 4.25M | } |
743 | | |
744 | | // skip back spaces |
745 | 4.29M | int j = len - 1; |
746 | 4.29M | for (; j >= i; j--) { |
747 | 4.25M | if (!is_whitespace_ascii(s[j])) { |
748 | 4.25M | break; |
749 | 4.25M | } |
750 | 4.25M | } |
751 | | |
752 | | // skip leading '+', from_chars can handle '-' |
753 | 4.29M | if (i < len && s[i] == '+') { |
754 | 7.14k | i++; |
755 | | // ++ or +- are not valid, but the first + is already skipped, |
756 | | // if don't check here, from_chars will succeed. |
757 | | // |
758 | | // New version of fast_float supports a new flag called 'chars_format::allow_leading_plus' |
759 | | // which may avoid this extra check here. |
760 | | // e.g.: |
761 | | // fast_float::chars_format format = |
762 | | // fast_float::chars_format::general | fast_float::chars_format::allow_leading_plus; |
763 | | // auto res = fast_float::from_chars(s + i, s + j + 1, val, format); |
764 | 7.14k | if (i < len && (s[i] == '+' || s[i] == '-')) { |
765 | 60 | *result = PARSE_FAILURE; |
766 | 60 | return 0; |
767 | 60 | } |
768 | 7.14k | } |
769 | 4.29M | if (UNLIKELY(i > j)) { |
770 | 38.2k | *result = PARSE_FAILURE; |
771 | 38.2k | return 0; |
772 | 38.2k | } |
773 | | |
774 | | // Use double here to not lose precision while accumulating the result |
775 | 4.25M | double val = 0; |
776 | 4.25M | auto res = fast_float::from_chars(s + i, s + j + 1, val); |
777 | | |
778 | 4.25M | if (res.ptr == s + j + 1) { |
779 | 4.25M | *result = PARSE_SUCCESS; |
780 | 4.25M | return val; |
781 | 4.25M | } else { |
782 | 1.74k | *result = PARSE_FAILURE; |
783 | 1.74k | } |
784 | 1.74k | return 0; |
785 | 4.25M | } _ZN5doris12StringParser24string_to_float_internalIfEET_PKciPNS0_11ParseResultE Line | Count | Source | 735 | 966k | T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) { | 736 | 966k | int i = 0; | 737 | | // skip leading spaces | 738 | 966k | for (; i < len; ++i) { | 739 | 961k | if (!is_whitespace_ascii(s[i])) { | 740 | 961k | break; | 741 | 961k | } | 742 | 961k | } | 743 | | | 744 | | // skip back spaces | 745 | 966k | int j = len - 1; | 746 | 966k | for (; j >= i; j--) { | 747 | 961k | if (!is_whitespace_ascii(s[j])) { | 748 | 961k | break; | 749 | 961k | } | 750 | 961k | } | 751 | | | 752 | | // skip leading '+', from_chars can handle '-' | 753 | 966k | if (i < len && s[i] == '+') { | 754 | 3.56k | i++; | 755 | | // ++ or +- are not valid, but the first + is already skipped, | 756 | | // if don't check here, from_chars will succeed. | 757 | | // | 758 | | // New version of fast_float supports a new flag called 'chars_format::allow_leading_plus' | 759 | | // which may avoid this extra check here. | 760 | | // e.g.: | 761 | | // fast_float::chars_format format = | 762 | | // fast_float::chars_format::general | fast_float::chars_format::allow_leading_plus; | 763 | | // auto res = fast_float::from_chars(s + i, s + j + 1, val, format); | 764 | 3.56k | if (i < len && (s[i] == '+' || s[i] == '-')) { | 765 | 30 | *result = PARSE_FAILURE; | 766 | 30 | return 0; | 767 | 30 | } | 768 | 3.56k | } | 769 | 966k | if (UNLIKELY(i > j)) { | 770 | 5.20k | *result = PARSE_FAILURE; | 771 | 5.20k | return 0; | 772 | 5.20k | } | 773 | | | 774 | | // Use double here to not lose precision while accumulating the result | 775 | 961k | double val = 0; | 776 | 961k | auto res = fast_float::from_chars(s + i, s + j + 1, val); | 777 | | | 778 | 961k | if (res.ptr == s + j + 1) { | 779 | 959k | *result = PARSE_SUCCESS; | 780 | 959k | return val; | 781 | 959k | } else { | 782 | 2.37k | *result = PARSE_FAILURE; | 783 | 2.37k | } | 784 | 2.37k | return 0; | 785 | 961k | } |
_ZN5doris12StringParser24string_to_float_internalIdEET_PKciPNS0_11ParseResultE Line | Count | Source | 735 | 3.32M | T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) { | 736 | 3.32M | int i = 0; | 737 | | // skip leading spaces | 738 | 3.32M | for (; i < len; ++i) { | 739 | 3.29M | if (!is_whitespace_ascii(s[i])) { | 740 | 3.29M | break; | 741 | 3.29M | } | 742 | 3.29M | } | 743 | | | 744 | | // skip back spaces | 745 | 3.32M | int j = len - 1; | 746 | 3.33M | for (; j >= i; j--) { | 747 | 3.29M | if (!is_whitespace_ascii(s[j])) { | 748 | 3.29M | break; | 749 | 3.29M | } | 750 | 3.29M | } | 751 | | | 752 | | // skip leading '+', from_chars can handle '-' | 753 | 3.32M | if (i < len && s[i] == '+') { | 754 | 3.57k | i++; | 755 | | // ++ or +- are not valid, but the first + is already skipped, | 756 | | // if don't check here, from_chars will succeed. | 757 | | // | 758 | | // New version of fast_float supports a new flag called 'chars_format::allow_leading_plus' | 759 | | // which may avoid this extra check here. | 760 | | // e.g.: | 761 | | // fast_float::chars_format format = | 762 | | // fast_float::chars_format::general | fast_float::chars_format::allow_leading_plus; | 763 | | // auto res = fast_float::from_chars(s + i, s + j + 1, val, format); | 764 | 3.57k | if (i < len && (s[i] == '+' || s[i] == '-')) { | 765 | 30 | *result = PARSE_FAILURE; | 766 | 30 | return 0; | 767 | 30 | } | 768 | 3.57k | } | 769 | 3.32M | if (UNLIKELY(i > j)) { | 770 | 33.0k | *result = PARSE_FAILURE; | 771 | 33.0k | return 0; | 772 | 33.0k | } | 773 | | | 774 | | // Use double here to not lose precision while accumulating the result | 775 | 3.29M | double val = 0; | 776 | 3.29M | auto res = fast_float::from_chars(s + i, s + j + 1, val); | 777 | | | 778 | 3.29M | if (res.ptr == s + j + 1) { | 779 | 3.29M | *result = PARSE_SUCCESS; | 780 | 3.29M | return val; | 781 | 18.4E | } else { | 782 | 18.4E | *result = PARSE_FAILURE; | 783 | 18.4E | } | 784 | 18.4E | return 0; | 785 | 3.29M | } |
|
786 | | |
787 | | inline bool StringParser::string_to_bool_internal(const char* __restrict s, int len, |
788 | 657k | ParseResult* result) { |
789 | 657k | *result = PARSE_SUCCESS; |
790 | | |
791 | 657k | if (len == 1) { |
792 | 554k | if (s[0] == '1' || s[0] == 't' || s[0] == 'T') { |
793 | 283k | return true; |
794 | 283k | } |
795 | 271k | if (s[0] == '0' || s[0] == 'f' || s[0] == 'F') { |
796 | 270k | return false; |
797 | 270k | } |
798 | 1.41k | *result = PARSE_FAILURE; |
799 | 1.41k | return false; |
800 | 271k | } |
801 | | |
802 | 102k | if (len == 2) { |
803 | 1.03k | if ((s[0] == 'o' || s[0] == 'O') && (s[1] == 'n' || s[1] == 'N')) { |
804 | 15 | return true; |
805 | 15 | } |
806 | 1.01k | if ((s[0] == 'n' || s[0] == 'N') && (s[1] == 'o' || s[1] == 'O')) { |
807 | 14 | return false; |
808 | 14 | } |
809 | 1.01k | } |
810 | | |
811 | 102k | if (len == 3) { |
812 | 87 | if ((s[0] == 'y' || s[0] == 'Y') && (s[1] == 'e' || s[1] == 'E') && |
813 | 87 | (s[2] == 's' || s[2] == 'S')) { |
814 | 15 | return true; |
815 | 15 | } |
816 | 72 | if ((s[0] == 'o' || s[0] == 'O') && (s[1] == 'f' || s[1] == 'F') && |
817 | 72 | (s[2] == 'f' || s[2] == 'F')) { |
818 | 14 | return false; |
819 | 14 | } |
820 | 72 | } |
821 | | |
822 | 102k | if (len == 4 && (s[0] == 't' || s[0] == 'T') && (s[1] == 'r' || s[1] == 'R') && |
823 | 102k | (s[2] == 'u' || s[2] == 'U') && (s[3] == 'e' || s[3] == 'E')) { |
824 | 49.3k | return true; |
825 | 49.3k | } |
826 | | |
827 | 53.5k | if (len == 5 && (s[0] == 'f' || s[0] == 'F') && (s[1] == 'a' || s[1] == 'A') && |
828 | 53.5k | (s[2] == 'l' || s[2] == 'L') && (s[3] == 's' || s[3] == 'S') && |
829 | 53.5k | (s[4] == 'e' || s[4] == 'E')) { |
830 | 47.2k | return false; |
831 | 47.2k | } |
832 | | |
833 | | // No valid boolean value found |
834 | 6.27k | *result = PARSE_FAILURE; |
835 | 6.27k | return false; |
836 | 53.5k | } |
837 | | #include "common/compile_check_avoid_end.h" |
838 | | } // end namespace doris |