Coverage Report

Created: 2026-09-17 10:05

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