Coverage Report

Created: 2026-05-09 01:43

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/compiler_util.h" // IWYU pragma: keep
40
#include "common/status.h"
41
#include "core/data_type/number_traits.h"
42
#include "core/data_type/primitive_type.h"
43
#include "core/extended_types.h"
44
#include "core/value/large_int_value.h"
45
#include "exec/common/int_exp.h"
46
#include "exec/common/string_utils/string_utils.h"
47
48
namespace doris {
49
#include "common/compile_check_avoid_begin.h"
50
template <DecimalNativeTypeConcept T>
51
struct Decimal;
52
53
// they rely on the template parameter `IS_STRICT`. in strict mode, it will set error code and otherwise it will not.
54
#ifndef SET_PARAMS_RET_FALSE_IFN
55
#define SET_PARAMS_RET_FALSE_IFN(stmt, ...)                           \
56
1.43M
    do {                                                              \
57
1.43M
        if (!(stmt)) [[unlikely]] {                                   \
58
36.5k
            if constexpr (IsStrict) {                                 \
59
174
                params.status = Status::InvalidArgument(__VA_ARGS__); \
60
174
            }                                                         \
61
36.5k
            return false;                                             \
62
36.5k
        }                                                             \
63
1.43M
    } while (false)
64
#endif
65
66
#ifndef SET_PARAMS_RET_FALSE_FROM_EXCEPTION
67
#define SET_PARAMS_RET_FALSE_FROM_EXCEPTION(stmt) \
68
160
    do {                                          \
69
160
        try {                                     \
70
160
            { stmt; }                             \
71
160
        } catch (const doris::Exception& e) {     \
72
15
            if constexpr (IsStrict) {             \
73
2
                params.status = e.to_status();    \
74
2
            }                                     \
75
15
            return false;                         \
76
15
        }                                         \
77
160
    } while (false)
78
#endif
79
80
// skip leading and trailing ascii whitespaces,
81
// return the pointer to the first non-whitespace char,
82
// and update the len to the new length, which does not include
83
// leading and trailing whitespaces
84
template <typename T>
85
6.04M
inline const char* skip_ascii_whitespaces(const char* s, T& len) {
86
6.51M
    while (len > 0 && is_whitespace_ascii(*s)) {
87
469k
        ++s;
88
469k
        --len;
89
469k
    }
90
91
6.50M
    while (len > 0 && is_whitespace_ascii(s[len - 1])) {
92
465k
        --len;
93
465k
    }
94
95
6.04M
    return s;
96
6.04M
}
_ZN5doris22skip_ascii_whitespacesImEEPKcS2_RT_
Line
Count
Source
85
6.01M
inline const char* skip_ascii_whitespaces(const char* s, T& len) {
86
6.40M
    while (len > 0 && is_whitespace_ascii(*s)) {
87
393k
        ++s;
88
393k
        --len;
89
393k
    }
90
91
6.40M
    while (len > 0 && is_whitespace_ascii(s[len - 1])) {
92
390k
        --len;
93
390k
    }
94
95
6.01M
    return s;
96
6.01M
}
_ZN5doris22skip_ascii_whitespacesIiEEPKcS2_RT_
Line
Count
Source
85
1.37k
inline const char* skip_ascii_whitespaces(const char* s, T& len) {
86
4.90k
    while (len > 0 && is_whitespace_ascii(*s)) {
87
3.52k
        ++s;
88
3.52k
        --len;
89
3.52k
    }
90
91
4.90k
    while (len > 0 && is_whitespace_ascii(s[len - 1])) {
92
3.52k
        --len;
93
3.52k
    }
94
95
1.37k
    return s;
96
1.37k
}
_ZN5doris22skip_ascii_whitespacesIlEEPKcS2_RT_
Line
Count
Source
85
27.8k
inline const char* skip_ascii_whitespaces(const char* s, T& len) {
86
100k
    while (len > 0 && is_whitespace_ascii(*s)) {
87
72.4k
        ++s;
88
72.4k
        --len;
89
72.4k
    }
90
91
99.8k
    while (len > 0 && is_whitespace_ascii(s[len - 1])) {
92
72.0k
        --len;
93
72.0k
    }
94
95
27.8k
    return s;
96
27.8k
}
97
98
template <typename T>
99
54.8k
inline const char* skip_leading_whitespace(const char* __restrict s, T& len) {
100
158k
    while (len > 0 && is_whitespace_ascii(*s)) {
101
103k
        ++s;
102
103k
        --len;
103
103k
    }
104
105
54.8k
    return s;
106
54.8k
}
107
108
// skip trailing ascii whitespaces,
109
// return the pointer to the first char,
110
// and update the len to the new length, which does not include
111
// trailing whitespaces
112
template <typename T>
113
125k
inline const char* skip_trailing_whitespaces(const char* s, T& len) {
114
241k
    while (len > 0 && is_whitespace_ascii(s[len - 1])) {
115
115k
        --len;
116
115k
    }
117
118
125k
    return s;
119
125k
}
120
121
template <bool (*Pred)(char)>
122
60.5k
bool range_suite(const char* s, const char* end) {
123
60.5k
    return std::ranges::all_of(s, end, Pred);
124
60.5k
}
_ZN5doris11range_suiteIXadL_Z16is_numeric_asciicEEEEbPKcS2_
Line
Count
Source
122
58.2k
bool range_suite(const char* s, const char* end) {
123
58.2k
    return std::ranges::all_of(s, end, Pred);
124
58.2k
}
_ZN5doris11range_suiteIXadL_Z19is_whitespace_asciicEEEEbPKcS2_
Line
Count
Source
122
2.34k
bool range_suite(const char* s, const char* end) {
123
2.34k
    return std::ranges::all_of(s, end, Pred);
124
2.34k
}
125
126
inline auto is_digit_range = range_suite<is_numeric_ascii>;
127
inline auto is_space_range = range_suite<is_whitespace_ascii>;
128
129
// combine in_bound and range_suite is ok. won't lead to duplicated calculation.
130
57.2k
inline bool in_bound(const char* s, const char* end, size_t offset) {
131
57.2k
    if (s + offset >= end) [[unlikely]] {
132
3.39k
        return false;
133
3.39k
    }
134
53.8k
    return true;
135
57.2k
}
136
137
// LEN = 0 means any length(include zero). LEN = 1 means only one character. so on. LEN = -x means x or more.
138
// if need result, use StringRef{origin_s, s} outside
139
template <int LEN, bool (*Pred)(char)>
140
1.60M
bool skip_qualified_char(const char*& s, const char* end) {
141
1.60M
    if constexpr (LEN == 0) {
142
        // Consume any length of characters that match the predicate.
143
4.06M
        while (s != end && Pred(*s)) {
144
2.53M
            ++s;
145
2.53M
        }
146
1.52M
    } else if constexpr (LEN > 0) {
147
        // Consume exactly LEN characters that match the predicate.
148
132k
        for (int i = 0; i < LEN; ++i, ++s) {
149
71.6k
            if (s == end || !Pred(*s)) [[unlikely]] {
150
10.8k
                return false;
151
10.8k
            }
152
71.6k
        }
153
71.6k
    } else { // LEN < 0
154
        // Consume at least -LEN characters that match the predicate.
155
174
        int count = 0;
156
1.32k
        while (s != end && Pred(*s)) {
157
1.14k
            ++s;
158
1.14k
            ++count;
159
1.14k
        }
160
174
        if (count < -LEN) [[unlikely]] {
161
0
            return false;
162
0
        }
163
174
    }
164
61.0k
    return true;
165
1.60M
}
_ZN5doris19skip_qualified_charILi0EXadL_Z19is_whitespace_asciicEEEEbRPKcS2_
Line
Count
Source
140
526k
bool skip_qualified_char(const char*& s, const char* end) {
141
526k
    if constexpr (LEN == 0) {
142
        // Consume any length of characters that match the predicate.
143
529k
        while (s != end && Pred(*s)) {
144
3.02k
            ++s;
145
3.02k
        }
146
    } else if constexpr (LEN > 0) {
147
        // Consume exactly LEN characters that match the predicate.
148
        for (int i = 0; i < LEN; ++i, ++s) {
149
            if (s == end || !Pred(*s)) [[unlikely]] {
150
                return false;
151
            }
152
        }
153
    } else { // LEN < 0
154
        // Consume at least -LEN characters that match the predicate.
155
        int count = 0;
156
        while (s != end && Pred(*s)) {
157
            ++s;
158
            ++count;
159
        }
160
        if (count < -LEN) [[unlikely]] {
161
            return false;
162
        }
163
    }
164
526k
    return true;
165
526k
}
_ZN5doris19skip_qualified_charILi0EXadL_Z16is_numeric_asciicEEEEbRPKcS2_
Line
Count
Source
140
1.00M
bool skip_qualified_char(const char*& s, const char* end) {
141
1.00M
    if constexpr (LEN == 0) {
142
        // Consume any length of characters that match the predicate.
143
3.53M
        while (s != end && Pred(*s)) {
144
2.53M
            ++s;
145
2.53M
        }
146
    } else if constexpr (LEN > 0) {
147
        // Consume exactly LEN characters that match the predicate.
148
        for (int i = 0; i < LEN; ++i, ++s) {
149
            if (s == end || !Pred(*s)) [[unlikely]] {
150
                return false;
151
            }
152
        }
153
    } else { // LEN < 0
154
        // Consume at least -LEN characters that match the predicate.
155
        int count = 0;
156
        while (s != end && Pred(*s)) {
157
            ++s;
158
            ++count;
159
        }
160
        if (count < -LEN) [[unlikely]] {
161
            return false;
162
        }
163
    }
164
1.00M
    return true;
165
1.00M
}
_ZN5doris19skip_qualified_charILin1EXadL_Z23is_not_whitespace_asciicEEEEbRPKcS2_
Line
Count
Source
140
174
bool skip_qualified_char(const char*& s, const char* end) {
141
    if constexpr (LEN == 0) {
142
        // Consume any length of characters that match the predicate.
143
        while (s != end && Pred(*s)) {
144
            ++s;
145
        }
146
    } else if constexpr (LEN > 0) {
147
        // Consume exactly LEN characters that match the predicate.
148
        for (int i = 0; i < LEN; ++i, ++s) {
149
            if (s == end || !Pred(*s)) [[unlikely]] {
150
                return false;
151
            }
152
        }
153
174
    } else { // LEN < 0
154
        // Consume at least -LEN characters that match the predicate.
155
174
        int count = 0;
156
1.32k
        while (s != end && Pred(*s)) {
157
1.14k
            ++s;
158
1.14k
            ++count;
159
1.14k
        }
160
174
        if (count < -LEN) [[unlikely]] {
161
0
            return false;
162
0
        }
163
174
    }
164
174
    return true;
165
174
}
Unexecuted instantiation: _ZN5doris19skip_qualified_charILi1EXadL_Z14is_slash_asciicEEEEbRPKcS2_
_ZN5doris19skip_qualified_charILi1EXadL_Z12is_non_alnumcEEEEbRPKcS2_
Line
Count
Source
140
35.5k
bool skip_qualified_char(const char*& s, const char* end) {
141
    if constexpr (LEN == 0) {
142
        // Consume any length of characters that match the predicate.
143
        while (s != end && Pred(*s)) {
144
            ++s;
145
        }
146
35.5k
    } else if constexpr (LEN > 0) {
147
        // Consume exactly LEN characters that match the predicate.
148
60.3k
        for (int i = 0; i < LEN; ++i, ++s) {
149
35.5k
            if (s == end || !Pred(*s)) [[unlikely]] {
150
10.6k
                return false;
151
10.6k
            }
152
35.5k
        }
153
    } else { // LEN < 0
154
        // Consume at least -LEN characters that match the predicate.
155
        int count = 0;
156
        while (s != end && Pred(*s)) {
157
            ++s;
158
            ++count;
159
        }
160
        if (count < -LEN) [[unlikely]] {
161
            return false;
162
        }
163
    }
164
24.8k
    return true;
165
35.5k
}
_ZN5doris19skip_qualified_charILi1EXadL_ZNS_12is_delimiterEcEEEEbRPKcS2_
Line
Count
Source
140
2.50k
bool skip_qualified_char(const char*& s, const char* end) {
141
    if constexpr (LEN == 0) {
142
        // Consume any length of characters that match the predicate.
143
        while (s != end && Pred(*s)) {
144
            ++s;
145
        }
146
2.50k
    } else if constexpr (LEN > 0) {
147
        // Consume exactly LEN characters that match the predicate.
148
4.96k
        for (int i = 0; i < LEN; ++i, ++s) {
149
2.50k
            if (s == end || !Pred(*s)) [[unlikely]] {
150
47
                return false;
151
47
            }
152
2.50k
        }
153
    } else { // LEN < 0
154
        // Consume at least -LEN characters that match the predicate.
155
        int count = 0;
156
        while (s != end && Pred(*s)) {
157
            ++s;
158
            ++count;
159
        }
160
        if (count < -LEN) [[unlikely]] {
161
            return false;
162
        }
163
    }
164
2.45k
    return true;
165
2.50k
}
_ZN5doris19skip_qualified_charILi1EXadL_ZNS_11is_date_sepEcEEEEbRPKcS2_
Line
Count
Source
140
33.5k
bool skip_qualified_char(const char*& s, const char* end) {
141
    if constexpr (LEN == 0) {
142
        // Consume any length of characters that match the predicate.
143
        while (s != end && Pred(*s)) {
144
            ++s;
145
        }
146
33.5k
    } else if constexpr (LEN > 0) {
147
        // Consume exactly LEN characters that match the predicate.
148
67.0k
        for (int i = 0; i < LEN; ++i, ++s) {
149
33.5k
            if (s == end || !Pred(*s)) [[unlikely]] {
150
42
                return false;
151
42
            }
152
33.5k
        }
153
    } else { // LEN < 0
154
        // Consume at least -LEN characters that match the predicate.
155
        int count = 0;
156
        while (s != end && Pred(*s)) {
157
            ++s;
158
            ++count;
159
        }
160
        if (count < -LEN) [[unlikely]] {
161
            return false;
162
        }
163
    }
164
33.5k
    return true;
165
33.5k
}
_ZN5doris19skip_qualified_charILi1EXadL_ZNS_8is_colonEcEEEEbRPKcS2_
Line
Count
Source
140
142
bool skip_qualified_char(const char*& s, const char* end) {
141
    if constexpr (LEN == 0) {
142
        // Consume any length of characters that match the predicate.
143
        while (s != end && Pred(*s)) {
144
            ++s;
145
        }
146
142
    } else if constexpr (LEN > 0) {
147
        // Consume exactly LEN characters that match the predicate.
148
260
        for (int i = 0; i < LEN; ++i, ++s) {
149
142
            if (s == end || !Pred(*s)) [[unlikely]] {
150
24
                return false;
151
24
            }
152
142
        }
153
    } else { // LEN < 0
154
        // Consume at least -LEN characters that match the predicate.
155
        int count = 0;
156
        while (s != end && Pred(*s)) {
157
            ++s;
158
            ++count;
159
        }
160
        if (count < -LEN) [[unlikely]] {
161
            return false;
162
        }
163
    }
164
118
    return true;
165
142
}
166
167
inline auto skip_any_whitespace = skip_qualified_char<0, is_whitespace_ascii>;
168
inline auto skip_any_digit = skip_qualified_char<0, is_numeric_ascii>;
169
inline auto skip_tz_name_part = skip_qualified_char<-1, is_not_whitespace_ascii>;
170
inline auto skip_one_slash = skip_qualified_char<1, is_slash_ascii>;
171
inline auto skip_one_non_alnum = skip_qualified_char<1, is_non_alnum>;
172
173
2.50k
inline bool is_delimiter(char c) {
174
2.50k
    return c == ' ' || c == 'T' || c == ':';
175
2.50k
}
176
inline auto consume_one_delimiter = skip_qualified_char<1, is_delimiter>;
177
178
55.4k
inline bool is_date_sep(char c) {
179
55.4k
    return c == '-' || c == '/';
180
55.4k
}
181
inline auto consume_one_date_sep = skip_qualified_char<1, is_date_sep>;
182
183
142
inline bool is_colon(char c) {
184
142
    return c == ':';
185
142
}
186
inline auto consume_one_colon = skip_qualified_char<1, is_colon>;
187
188
// only consume a string of digit, not include sign.
189
// when has MAX_LEN > 0, do greedy match but at most MAX_LEN.
190
// LEN = 0 means any length, otherwise(must > 0) it means exactly LEN digits.
191
template <typename T, int LEN = 0, int MAX_LEN = -1>
192
20
bool consume_digit(const char*& s, const char* end, T& out) {
193
20
    static_assert(LEN >= 0);
194
    if constexpr (MAX_LEN > 0) {
195
        out = 0;
196
        for (int i = 0; i < MAX_LEN; ++i, ++s) {
197
            if (s == end || !is_numeric_ascii(*s)) {
198
                if (i < LEN) [[unlikely]] {
199
                    return false;
200
                }
201
                break; // stop consuming if we have consumed enough digits.
202
            }
203
            out = out * 10 + (*s - '0');
204
        }
205
    } else if constexpr (LEN == 0) {
206
        // Consume any length of digits.
207
        out = 0;
208
        while (s != end && is_numeric_ascii(*s)) {
209
            out = out * 10 + (*s - '0');
210
            ++s;
211
        }
212
20
    } else if constexpr (LEN > 0) {
213
        // Consume exactly LEN digits.
214
20
        out = 0;
215
85
        for (int i = 0; i < LEN; ++i, ++s) {
216
65
            if (s == end || !is_numeric_ascii(*s)) [[unlikely]] {
217
0
                return false;
218
0
            }
219
65
            out = out * 10 + (*s - '0');
220
65
        }
221
20
    }
222
20
    return true;
223
20
}
_ZN5doris13consume_digitIjLi4ELin1EEEbRPKcS2_RT_
Line
Count
Source
192
15
bool consume_digit(const char*& s, const char* end, T& out) {
193
15
    static_assert(LEN >= 0);
194
    if constexpr (MAX_LEN > 0) {
195
        out = 0;
196
        for (int i = 0; i < MAX_LEN; ++i, ++s) {
197
            if (s == end || !is_numeric_ascii(*s)) {
198
                if (i < LEN) [[unlikely]] {
199
                    return false;
200
                }
201
                break; // stop consuming if we have consumed enough digits.
202
            }
203
            out = out * 10 + (*s - '0');
204
        }
205
    } else if constexpr (LEN == 0) {
206
        // Consume any length of digits.
207
        out = 0;
208
        while (s != end && is_numeric_ascii(*s)) {
209
            out = out * 10 + (*s - '0');
210
            ++s;
211
        }
212
15
    } else if constexpr (LEN > 0) {
213
        // Consume exactly LEN digits.
214
15
        out = 0;
215
75
        for (int i = 0; i < LEN; ++i, ++s) {
216
60
            if (s == end || !is_numeric_ascii(*s)) [[unlikely]] {
217
0
                return false;
218
0
            }
219
60
            out = out * 10 + (*s - '0');
220
60
        }
221
15
    }
222
15
    return true;
223
15
}
_ZN5doris13consume_digitIjLi1ELin1EEEbRPKcS2_RT_
Line
Count
Source
192
5
bool consume_digit(const char*& s, const char* end, T& out) {
193
5
    static_assert(LEN >= 0);
194
    if constexpr (MAX_LEN > 0) {
195
        out = 0;
196
        for (int i = 0; i < MAX_LEN; ++i, ++s) {
197
            if (s == end || !is_numeric_ascii(*s)) {
198
                if (i < LEN) [[unlikely]] {
199
                    return false;
200
                }
201
                break; // stop consuming if we have consumed enough digits.
202
            }
203
            out = out * 10 + (*s - '0');
204
        }
205
    } else if constexpr (LEN == 0) {
206
        // Consume any length of digits.
207
        out = 0;
208
        while (s != end && is_numeric_ascii(*s)) {
209
            out = out * 10 + (*s - '0');
210
            ++s;
211
        }
212
5
    } else if constexpr (LEN > 0) {
213
        // Consume exactly LEN digits.
214
5
        out = 0;
215
10
        for (int i = 0; i < LEN; ++i, ++s) {
216
5
            if (s == end || !is_numeric_ascii(*s)) [[unlikely]] {
217
0
                return false;
218
0
            }
219
5
            out = out * 10 + (*s - '0');
220
5
        }
221
5
    }
222
5
    return true;
223
5
}
224
225
// specialized version for 2 digits, which is used very often in date/time parsing.
226
template <>
227
113k
inline bool consume_digit<uint32_t, 2, -1>(const char*& s, const char* end, uint32_t& out) {
228
113k
    out = 0;
229
113k
    if (s == end || s + 1 == end || !is_numeric_ascii(*s) || !is_numeric_ascii(*(s + 1)))
230
18.4k
            [[unlikely]] {
231
18.4k
        return false;
232
18.4k
    }
233
94.8k
    out = (s[0] - '0') * 10 + (s[1] - '0');
234
94.8k
    s += 2; // consume 2 digits
235
94.8k
    return true;
236
113k
}
237
238
// specialized version for 1 or 2 digits, which is used very often in date/time parsing.
239
template <>
240
61.1k
inline bool consume_digit<uint32_t, 1, 2>(const char*& s, const char* end, uint32_t& out) {
241
61.1k
    out = 0;
242
61.1k
    if (s == end || !is_numeric_ascii(*s)) [[unlikely]] {
243
479
        return false;
244
60.6k
    } else if (s + 1 != end && is_numeric_ascii(*(s + 1))) {
245
        // consume 2 digits
246
43.9k
        out = (*s - '0') * 10 + (*(s + 1) - '0');
247
43.9k
        s += 2;
248
43.9k
    } else {
249
        // consume 1 digit
250
16.6k
        out = *s - '0';
251
16.6k
        ++s;
252
16.6k
    }
253
60.6k
    return true;
254
61.1k
}
255
256
template <bool (*Pred)(char)>
257
156
uint32_t count_valid_length(const char* s, const char* end) {
258
156
    DCHECK(s <= end) << "s: " << s << ", end: " << end;
259
156
    uint32_t count = 0;
260
473
    while (s != end && Pred(*s)) {
261
317
        ++count;
262
317
        ++s;
263
317
    }
264
156
    return count;
265
156
}
266
267
inline auto count_digits = count_valid_length<is_numeric_ascii>;
268
269
144
inline PURE std::string combine_tz_offset(char sign, uint32_t hour_offset, uint32_t minute_offset) {
270
144
    std::string result(6, '0');
271
144
    result[0] = sign;
272
144
    result[1] = '0' + (hour_offset / 10);
273
144
    result[2] = '0' + (hour_offset % 10);
274
144
    result[3] = ':';
275
144
    result[4] = '0' + (minute_offset / 10);
276
144
    result[5] = '0' + (minute_offset % 10);
277
144
    DCHECK_EQ(result.size(), 6);
278
144
    return result;
279
144
}
280
281
// Utility functions for doing atoi/atof on non-null terminated strings.  On micro benchmarks,
282
// this is significantly faster than libc (atoi/strtol and atof/strtod).
283
//
284
// Strings with leading and trailing whitespaces are accepted.
285
// Branching is heavily optimized for the non-whitespace successful case.
286
// All the StringTo* functions first parse the input string assuming it has no leading whitespace.
287
// If that first attempt was unsuccessful, these functions retry the parsing after removing
288
// whitespace. Therefore, strings with whitespace take a perf hit on branch mis-prediction.
289
//
290
// For overflows, we are following the mysql behavior, to cap values at the max/min value for that
291
// data type.  This is different from hive, which returns NULL for overflow slots for int types
292
// and inf/-inf for float types.
293
//
294
// Things we tried that did not work:
295
//  - lookup table for converting character to digit
296
// Improvements (TODO):
297
//  - Validate input using _simd_compare_ranges
298
//  - Since we know the length, we can parallelize this: i.e. result = 100*s[0] + 10*s[1] + s[2]
299
class StringParser {
300
public:
301
    enum ParseResult { PARSE_SUCCESS = 0, PARSE_FAILURE, PARSE_OVERFLOW, PARSE_UNDERFLOW };
302
303
    template <typename T>
304
22.8M
    static T numeric_limits(bool negative) {
305
22.8M
        if constexpr (std::is_same_v<T, __int128>) {
306
49.5k
            return negative ? MIN_INT128 : MAX_INT128;
307
22.8M
        } else {
308
22.8M
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
309
22.8M
        }
310
22.8M
    }
_ZN5doris12StringParser14numeric_limitsInEET_b
Line
Count
Source
304
49.5k
    static T numeric_limits(bool negative) {
305
49.5k
        if constexpr (std::is_same_v<T, __int128>) {
306
49.5k
            return negative ? MIN_INT128 : MAX_INT128;
307
        } else {
308
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
309
        }
310
49.5k
    }
_ZN5doris12StringParser14numeric_limitsIaEET_b
Line
Count
Source
304
2.86M
    static T numeric_limits(bool negative) {
305
        if constexpr (std::is_same_v<T, __int128>) {
306
            return negative ? MIN_INT128 : MAX_INT128;
307
2.86M
        } else {
308
2.86M
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
309
2.86M
        }
310
2.86M
    }
_ZN5doris12StringParser14numeric_limitsIsEET_b
Line
Count
Source
304
202k
    static T numeric_limits(bool negative) {
305
        if constexpr (std::is_same_v<T, __int128>) {
306
            return negative ? MIN_INT128 : MAX_INT128;
307
202k
        } else {
308
202k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
309
202k
        }
310
202k
    }
_ZN5doris12StringParser14numeric_limitsIiEET_b
Line
Count
Source
304
3.73M
    static T numeric_limits(bool negative) {
305
        if constexpr (std::is_same_v<T, __int128>) {
306
            return negative ? MIN_INT128 : MAX_INT128;
307
3.73M
        } else {
308
3.73M
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
309
3.73M
        }
310
3.73M
    }
_ZN5doris12StringParser14numeric_limitsIlEET_b
Line
Count
Source
304
16.0M
    static T numeric_limits(bool negative) {
305
        if constexpr (std::is_same_v<T, __int128>) {
306
            return negative ? MIN_INT128 : MAX_INT128;
307
16.0M
        } else {
308
16.0M
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
309
16.0M
        }
310
16.0M
    }
_ZN5doris12StringParser14numeric_limitsIjEET_b
Line
Count
Source
304
148
    static T numeric_limits(bool negative) {
305
        if constexpr (std::is_same_v<T, __int128>) {
306
            return negative ? MIN_INT128 : MAX_INT128;
307
148
        } else {
308
148
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
309
148
        }
310
148
    }
_ZN5doris12StringParser14numeric_limitsImEET_b
Line
Count
Source
304
29
    static T numeric_limits(bool negative) {
305
        if constexpr (std::is_same_v<T, __int128>) {
306
            return negative ? MIN_INT128 : MAX_INT128;
307
29
        } else {
308
29
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
309
29
        }
310
29
    }
_ZN5doris12StringParser14numeric_limitsIN4wide7integerILm256EiEEEET_b
Line
Count
Source
304
4
    static T numeric_limits(bool negative) {
305
        if constexpr (std::is_same_v<T, __int128>) {
306
            return negative ? MIN_INT128 : MAX_INT128;
307
4
        } else {
308
4
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
309
4
        }
310
4
    }
_ZN5doris12StringParser14numeric_limitsIoEET_b
Line
Count
Source
304
4
    static T numeric_limits(bool negative) {
305
        if constexpr (std::is_same_v<T, __int128>) {
306
            return negative ? MIN_INT128 : MAX_INT128;
307
4
        } else {
308
4
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
309
4
        }
310
4
    }
311
312
    template <typename T>
313
7.79M
    static T get_scale_multiplier(int scale) {
314
7.79M
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
315
7.79M
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
316
7.79M
                      "You can only instantiate as int32_t, int64_t, __int128.");
317
7.79M
        if constexpr (std::is_same_v<T, int32_t>) {
318
3.57M
            return common::exp10_i32(scale);
319
3.57M
        } else if constexpr (std::is_same_v<T, int64_t>) {
320
2.40M
            return common::exp10_i64(scale);
321
2.40M
        } else if constexpr (std::is_same_v<T, __int128>) {
322
1.49M
            return common::exp10_i128(scale);
323
1.49M
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
324
320k
            return common::exp10_i256(scale);
325
320k
        }
326
7.79M
    }
_ZN5doris12StringParser20get_scale_multiplierIiEET_i
Line
Count
Source
313
3.57M
    static T get_scale_multiplier(int scale) {
314
3.57M
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
315
3.57M
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
316
3.57M
                      "You can only instantiate as int32_t, int64_t, __int128.");
317
3.57M
        if constexpr (std::is_same_v<T, int32_t>) {
318
3.57M
            return common::exp10_i32(scale);
319
        } else if constexpr (std::is_same_v<T, int64_t>) {
320
            return common::exp10_i64(scale);
321
        } else if constexpr (std::is_same_v<T, __int128>) {
322
            return common::exp10_i128(scale);
323
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
324
            return common::exp10_i256(scale);
325
        }
326
3.57M
    }
_ZN5doris12StringParser20get_scale_multiplierIlEET_i
Line
Count
Source
313
2.40M
    static T get_scale_multiplier(int scale) {
314
2.40M
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
315
2.40M
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
316
2.40M
                      "You can only instantiate as int32_t, int64_t, __int128.");
317
        if constexpr (std::is_same_v<T, int32_t>) {
318
            return common::exp10_i32(scale);
319
2.40M
        } else if constexpr (std::is_same_v<T, int64_t>) {
320
2.40M
            return common::exp10_i64(scale);
321
        } else if constexpr (std::is_same_v<T, __int128>) {
322
            return common::exp10_i128(scale);
323
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
324
            return common::exp10_i256(scale);
325
        }
326
2.40M
    }
_ZN5doris12StringParser20get_scale_multiplierInEET_i
Line
Count
Source
313
1.49M
    static T get_scale_multiplier(int scale) {
314
1.49M
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
315
1.49M
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
316
1.49M
                      "You can only instantiate as int32_t, int64_t, __int128.");
317
        if constexpr (std::is_same_v<T, int32_t>) {
318
            return common::exp10_i32(scale);
319
        } else if constexpr (std::is_same_v<T, int64_t>) {
320
            return common::exp10_i64(scale);
321
1.49M
        } else if constexpr (std::is_same_v<T, __int128>) {
322
1.49M
            return common::exp10_i128(scale);
323
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
324
            return common::exp10_i256(scale);
325
        }
326
1.49M
    }
_ZN5doris12StringParser20get_scale_multiplierIN4wide7integerILm256EiEEEET_i
Line
Count
Source
313
320k
    static T get_scale_multiplier(int scale) {
314
320k
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
315
320k
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
316
320k
                      "You can only instantiate as int32_t, int64_t, __int128.");
317
        if constexpr (std::is_same_v<T, int32_t>) {
318
            return common::exp10_i32(scale);
319
        } else if constexpr (std::is_same_v<T, int64_t>) {
320
            return common::exp10_i64(scale);
321
        } else if constexpr (std::is_same_v<T, __int128>) {
322
            return common::exp10_i128(scale);
323
320k
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
324
320k
            return common::exp10_i256(scale);
325
320k
        }
326
320k
    }
327
328
    // This is considerably faster than glibc's implementation (25x).
329
    // Assumes s represents a decimal number.
330
    template <typename T, bool enable_strict_mode = false>
331
22.7M
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
22.7M
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
22.7M
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
22.7M
            return ans;
335
22.7M
        }
336
54.2k
        s = skip_leading_whitespace(s, len);
337
54.2k
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
22.7M
    }
_ZN5doris12StringParser13string_to_intInLb0EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
46.4k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
46.4k
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
46.4k
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
45.1k
            return ans;
335
45.1k
        }
336
1.33k
        s = skip_leading_whitespace(s, len);
337
1.33k
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
46.4k
    }
_ZN5doris12StringParser13string_to_intIaLb1EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
1.52k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
1.52k
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
1.52k
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
608
            return ans;
335
608
        }
336
912
        s = skip_leading_whitespace(s, len);
337
912
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
1.52k
    }
_ZN5doris12StringParser13string_to_intIsLb1EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
1.56k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
1.56k
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
1.56k
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
668
            return ans;
335
668
        }
336
896
        s = skip_leading_whitespace(s, len);
337
896
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
1.56k
    }
_ZN5doris12StringParser13string_to_intIiLb1EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
1.62k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
1.62k
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
1.62k
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
748
            return ans;
335
748
        }
336
880
        s = skip_leading_whitespace(s, len);
337
880
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
1.62k
    }
_ZN5doris12StringParser13string_to_intIlLb1EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
1.48k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
1.48k
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
1.48k
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
622
            return ans;
335
622
        }
336
867
        s = skip_leading_whitespace(s, len);
337
867
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
1.48k
    }
_ZN5doris12StringParser13string_to_intInLb1EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
936
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
936
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
936
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
88
            return ans;
335
88
        }
336
848
        s = skip_leading_whitespace(s, len);
337
848
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
936
    }
_ZN5doris12StringParser13string_to_intIaLb0EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
2.79M
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
2.79M
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
2.79M
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
2.76M
            return ans;
335
2.76M
        }
336
29.4k
        s = skip_leading_whitespace(s, len);
337
29.4k
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
2.79M
    }
_ZN5doris12StringParser13string_to_intIsLb0EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
191k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
191k
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
191k
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
183k
            return ans;
335
183k
        }
336
8.13k
        s = skip_leading_whitespace(s, len);
337
8.13k
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
191k
    }
_ZN5doris12StringParser13string_to_intIiLb0EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
3.72M
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
3.72M
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
3.72M
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
3.71M
            return ans;
335
3.71M
        }
336
7.19k
        s = skip_leading_whitespace(s, len);
337
7.19k
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
3.72M
    }
_ZN5doris12StringParser13string_to_intIlLb0EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
15.9M
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
15.9M
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
15.9M
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
15.9M
            return ans;
335
15.9M
        }
336
3.74k
        s = skip_leading_whitespace(s, len);
337
3.74k
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
15.9M
    }
_ZN5doris12StringParser13string_to_intIjLb0EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
1
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
1
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
1
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
1
            return ans;
335
1
        }
336
0
        s = skip_leading_whitespace(s, len);
337
0
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
1
    }
_ZN5doris12StringParser13string_to_intImLb0EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
28
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
28
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
28
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
28
            return ans;
335
28
        }
336
0
        s = skip_leading_whitespace(s, len);
337
0
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
28
    }
_ZN5doris12StringParser13string_to_intIN4wide7integerILm256EiEELb0EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
4
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
4
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
4
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
4
            return ans;
335
4
        }
336
0
        s = skip_leading_whitespace(s, len);
337
0
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
4
    }
_ZN5doris12StringParser13string_to_intIoLb0EEET_PKcmPNS0_11ParseResultE
Line
Count
Source
331
4
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
332
4
        T ans = string_to_int_internal<T, enable_strict_mode>(s, len, result);
333
4
        if (LIKELY(*result == PARSE_SUCCESS)) {
334
4
            return ans;
335
4
        }
336
0
        s = skip_leading_whitespace(s, len);
337
0
        return string_to_int_internal<T, enable_strict_mode>(s, len, result);
338
4
    }
339
340
    // This is considerably faster than glibc's implementation.
341
    // In the case of overflow, the max/min value for the data type will be returned.
342
    // Assumes s represents a decimal number.
343
    template <typename T>
344
343
    static inline T string_to_unsigned_int(const char* __restrict s, int len, ParseResult* result) {
345
343
        s = skip_ascii_whitespaces(s, len);
346
343
        return string_to_unsigned_int_internal<T>(s, len, result);
347
343
    }
348
349
    // Convert a string s representing a number in given base into a decimal number.
350
    template <typename T>
351
    static inline T string_to_int(const char* __restrict s, int64_t len, int base,
352
1
                                  ParseResult* result) {
353
1
        s = skip_ascii_whitespaces(s, len);
354
1
        return string_to_int_internal<T>(s, len, base, result);
355
1
    }
356
357
    template <typename T>
358
1.62M
    static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) {
359
1.62M
        s = skip_ascii_whitespaces(s, len);
360
1.62M
        return string_to_float_internal<T>(s, len, result);
361
1.62M
    }
_ZN5doris12StringParser15string_to_floatIdEET_PKcmPNS0_11ParseResultE
Line
Count
Source
358
942k
    static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) {
359
942k
        s = skip_ascii_whitespaces(s, len);
360
942k
        return string_to_float_internal<T>(s, len, result);
361
942k
    }
_ZN5doris12StringParser15string_to_floatIfEET_PKcmPNS0_11ParseResultE
Line
Count
Source
358
682k
    static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) {
359
682k
        s = skip_ascii_whitespaces(s, len);
360
682k
        return string_to_float_internal<T>(s, len, result);
361
682k
    }
362
363
    // Parses a string for 'true' or 'false', case insensitive.
364
628k
    static inline bool string_to_bool(const char* __restrict s, size_t len, ParseResult* result) {
365
628k
        s = skip_ascii_whitespaces(s, len);
366
628k
        return string_to_bool_internal(s, len, result);
367
628k
    }
368
369
    template <PrimitiveType P>
370
    static typename PrimitiveTypeTraits<P>::CppType::NativeType string_to_decimal(
371
            const char* __restrict s, size_t len, int type_precision, int type_scale,
372
            ParseResult* result);
373
374
    template <typename T>
375
    static Status split_string_to_map(const std::string& base, const T element_separator,
376
                                      const T key_value_separator,
377
                                      std::map<std::string, std::string>* result) {
378
        int key_pos = 0;
379
        int key_end;
380
        int val_pos;
381
        int val_end;
382
383
        while ((key_end = base.find(key_value_separator, key_pos)) != std::string::npos) {
384
            if ((val_pos = base.find_first_not_of(key_value_separator, key_end)) ==
385
                std::string::npos) {
386
                break;
387
            }
388
            if ((val_end = base.find(element_separator, val_pos)) == std::string::npos) {
389
                val_end = base.size();
390
            }
391
            result->insert(std::make_pair(base.substr(key_pos, key_end - key_pos),
392
                                          base.substr(val_pos, val_end - val_pos)));
393
            key_pos = val_end;
394
            if (key_pos != std::string::npos) {
395
                ++key_pos;
396
            }
397
        }
398
399
        return Status::OK();
400
    }
401
402
    // This is considerably faster than glibc's implementation.
403
    // In the case of overflow, the max/min value for the data type will be returned.
404
    // Assumes s represents a decimal number.
405
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
406
    template <typename T, bool enable_strict_mode = false>
407
    static inline T string_to_int_internal(const char* __restrict s, int len, ParseResult* result);
408
409
    // This is considerably faster than glibc's implementation.
410
    // In the case of overflow, the max/min value for the data type will be returned.
411
    // Assumes s represents a decimal number.
412
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
413
    template <typename T>
414
    static inline T string_to_unsigned_int_internal(const char* __restrict s, int len,
415
                                                    ParseResult* result);
416
417
    // Convert a string s representing a number in given base into a decimal number.
418
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
419
    template <typename T>
420
    static inline T string_to_int_internal(const char* __restrict s, int64_t len, int base,
421
                                           ParseResult* result);
422
423
    // Converts an ascii string to an integer of type T assuming it cannot overflow
424
    // and the number is positive.
425
    // Leading whitespace is not allowed. Trailing whitespace will be skipped.
426
    template <typename T, bool enable_strict_mode = false>
427
    static inline T string_to_int_no_overflow(const char* __restrict s, int len,
428
                                              ParseResult* result);
429
430
    // zero length, or at least one legal digit. at most consume MAX_LEN digits and stop. or stop when next
431
    // char is not a digit.
432
    template <typename T>
433
    static inline T string_to_uint_greedy_no_overflow(const char* __restrict s, int max_len,
434
                                                      ParseResult* result);
435
436
    // This is considerably faster than glibc's implementation (>100x why???)
437
    // No special case handling needs to be done for overflows, the floating point spec
438
    // already does it and will cap the values to -inf/inf
439
    // To avoid inaccurate conversions this function falls back to strtod for
440
    // scientific notation.
441
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
442
    // TODO: Investigate using intrinsics to speed up the slow strtod path.
443
    template <typename T>
444
    static inline T string_to_float_internal(const char* __restrict s, int len,
445
                                             ParseResult* result);
446
447
    // parses a string for 'true' or 'false', case insensitive
448
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
449
    static inline bool string_to_bool_internal(const char* __restrict s, int len,
450
                                               ParseResult* result);
451
452
    // Returns true if s only contains whitespace.
453
3.54k
    static inline bool is_all_whitespace(const char* __restrict s, int len) {
454
6.44k
        for (int i = 0; i < len; ++i) {
455
6.00k
            if (!LIKELY(is_whitespace_ascii(s[i]))) {
456
3.10k
                return false;
457
3.10k
            }
458
6.00k
        }
459
440
        return true;
460
3.54k
    }
461
462
    // For strings like "3.0", "3.123", and "3.", can parse them as 3.
463
83.9k
    static inline bool is_float_suffix(const char* __restrict s, int len) {
464
83.9k
        return (s[0] == '.' && is_all_digit(s + 1, len - 1));
465
83.9k
    }
466
467
82.8k
    static inline bool is_all_digit(const char* __restrict s, int len) {
468
165k
        for (int i = 0; i < len; ++i) {
469
83.2k
            if (!LIKELY(s[i] >= '0' && s[i] <= '9')) {
470
155
                return false;
471
155
            }
472
83.2k
        }
473
82.6k
        return true;
474
82.8k
    }
475
}; // end of class StringParser
476
477
template <typename T, bool enable_strict_mode>
478
22.8M
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
22.8M
    if (UNLIKELY(len <= 0)) {
480
2.46k
        *result = PARSE_FAILURE;
481
2.46k
        return 0;
482
2.46k
    }
483
484
22.8M
    using UnsignedT = MakeUnsignedT<T>;
485
22.8M
    UnsignedT val = 0;
486
22.8M
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
22.8M
    bool negative = false;
488
22.8M
    int i = 0;
489
22.8M
    switch (*s) {
490
1.63M
    case '-':
491
1.63M
        negative = true;
492
1.63M
        max_val += 1;
493
1.63M
        [[fallthrough]];
494
1.63M
    case '+':
495
1.63M
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
1.63M
        if (UNLIKELY(len == 1)) {
498
8
            *result = PARSE_FAILURE;
499
8
            return 0;
500
8
        }
501
22.8M
    }
502
503
    // This is the fast path where the string cannot overflow.
504
22.8M
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
22.0M
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
22.0M
        return static_cast<T>(negative ? -val : val);
507
22.0M
    }
508
509
811k
    const T max_div_10 = max_val / 10;
510
811k
    const T max_mod_10 = max_val % 10;
511
512
811k
    int first = i;
513
9.78M
    for (; i < len; ++i) {
514
9.07M
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
9.02M
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
9.02M
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
48.4k
                *result = PARSE_OVERFLOW;
519
48.4k
                return negative ? -max_val : max_val;
520
48.4k
            }
521
8.97M
            val = val * 10 + digit;
522
8.97M
        } else {
523
46.8k
            if constexpr (enable_strict_mode) {
524
4.08k
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
3.78k
                    *result = PARSE_FAILURE;
527
3.78k
                    return 0;
528
3.78k
                }
529
42.7k
            } else {
530
                // Save original position where non-digit was found
531
42.7k
                int remaining_len = len - i;
532
42.7k
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
42.7k
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
42.7k
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
42.7k
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
29.9k
                    *result = PARSE_FAILURE;
540
29.9k
                    return 0;
541
29.9k
                }
542
42.7k
            }
543
            // Returning here is slightly faster than breaking the loop.
544
13.1k
            *result = PARSE_SUCCESS;
545
46.8k
            return static_cast<T>(negative ? -val : val);
546
46.8k
        }
547
9.07M
    }
548
716k
    *result = PARSE_SUCCESS;
549
716k
    return static_cast<T>(negative ? -val : val);
550
811k
}
_ZN5doris12StringParser22string_to_int_internalInLb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
47.8k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
47.8k
    if (UNLIKELY(len <= 0)) {
480
44
        *result = PARSE_FAILURE;
481
44
        return 0;
482
44
    }
483
484
47.7k
    using UnsignedT = MakeUnsignedT<T>;
485
47.7k
    UnsignedT val = 0;
486
47.7k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
47.7k
    bool negative = false;
488
47.7k
    int i = 0;
489
47.7k
    switch (*s) {
490
3.58k
    case '-':
491
3.58k
        negative = true;
492
3.58k
        max_val += 1;
493
3.58k
        [[fallthrough]];
494
3.87k
    case '+':
495
3.87k
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
3.87k
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
47.7k
    }
502
503
    // This is the fast path where the string cannot overflow.
504
47.7k
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
43.0k
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
43.0k
        return static_cast<T>(negative ? -val : val);
507
43.0k
    }
508
509
4.67k
    const T max_div_10 = max_val / 10;
510
4.67k
    const T max_mod_10 = max_val % 10;
511
512
4.67k
    int first = i;
513
173k
    for (; i < len; ++i) {
514
170k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
169k
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
169k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
512
                *result = PARSE_OVERFLOW;
519
512
                return negative ? -max_val : max_val;
520
512
            }
521
169k
            val = val * 10 + digit;
522
169k
        } else {
523
            if constexpr (enable_strict_mode) {
524
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
                    *result = PARSE_FAILURE;
527
                    return 0;
528
                }
529
536
            } else {
530
                // Save original position where non-digit was found
531
536
                int remaining_len = len - i;
532
536
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
536
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
536
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
536
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
376
                    *result = PARSE_FAILURE;
540
376
                    return 0;
541
376
                }
542
536
            }
543
            // Returning here is slightly faster than breaking the loop.
544
160
            *result = PARSE_SUCCESS;
545
536
            return static_cast<T>(negative ? -val : val);
546
536
        }
547
170k
    }
548
3.62k
    *result = PARSE_SUCCESS;
549
3.62k
    return static_cast<T>(negative ? -val : val);
550
4.67k
}
_ZN5doris12StringParser22string_to_int_internalIaLb1EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
2.43k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
2.43k
    if (UNLIKELY(len <= 0)) {
480
8
        *result = PARSE_FAILURE;
481
8
        return 0;
482
8
    }
483
484
2.42k
    using UnsignedT = MakeUnsignedT<T>;
485
2.42k
    UnsignedT val = 0;
486
2.42k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
2.42k
    bool negative = false;
488
2.42k
    int i = 0;
489
2.42k
    switch (*s) {
490
632
    case '-':
491
632
        negative = true;
492
632
        max_val += 1;
493
632
        [[fallthrough]];
494
988
    case '+':
495
988
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
988
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
2.42k
    }
502
503
    // This is the fast path where the string cannot overflow.
504
2.42k
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
568
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
568
        return static_cast<T>(negative ? -val : val);
507
568
    }
508
509
1.85k
    const T max_div_10 = max_val / 10;
510
1.85k
    const T max_mod_10 = max_val % 10;
511
512
1.85k
    int first = i;
513
6.58k
    for (; i < len; ++i) {
514
6.51k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
5.32k
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
5.32k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
600
                *result = PARSE_OVERFLOW;
519
600
                return negative ? -max_val : max_val;
520
600
            }
521
4.72k
            val = val * 10 + digit;
522
4.72k
        } else {
523
1.18k
            if constexpr (enable_strict_mode) {
524
1.18k
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
1.10k
                    *result = PARSE_FAILURE;
527
1.10k
                    return 0;
528
1.10k
                }
529
            } else {
530
                // Save original position where non-digit was found
531
                int remaining_len = len - i;
532
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
                    *result = PARSE_FAILURE;
540
                    return 0;
541
                }
542
            }
543
            // Returning here is slightly faster than breaking the loop.
544
88
            *result = PARSE_SUCCESS;
545
1.18k
            return static_cast<T>(negative ? -val : val);
546
1.18k
        }
547
6.51k
    }
548
68
    *result = PARSE_SUCCESS;
549
68
    return static_cast<T>(negative ? -val : val);
550
1.85k
}
_ZN5doris12StringParser22string_to_int_internalIsLb1EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
2.46k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
2.46k
    if (UNLIKELY(len <= 0)) {
480
8
        *result = PARSE_FAILURE;
481
8
        return 0;
482
8
    }
483
484
2.45k
    using UnsignedT = MakeUnsignedT<T>;
485
2.45k
    UnsignedT val = 0;
486
2.45k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
2.45k
    bool negative = false;
488
2.45k
    int i = 0;
489
2.45k
    switch (*s) {
490
620
    case '-':
491
620
        negative = true;
492
620
        max_val += 1;
493
620
        [[fallthrough]];
494
970
    case '+':
495
970
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
970
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
2.45k
    }
502
503
    // This is the fast path where the string cannot overflow.
504
2.45k
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
748
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
748
        return static_cast<T>(negative ? -val : val);
507
748
    }
508
509
1.70k
    const T max_div_10 = max_val / 10;
510
1.70k
    const T max_mod_10 = max_val % 10;
511
512
1.70k
    int first = i;
513
7.87k
    for (; i < len; ++i) {
514
7.83k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
6.74k
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
6.74k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
576
                *result = PARSE_OVERFLOW;
519
576
                return negative ? -max_val : max_val;
520
576
            }
521
6.17k
            val = val * 10 + digit;
522
6.17k
        } else {
523
1.08k
            if constexpr (enable_strict_mode) {
524
1.08k
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
1.00k
                    *result = PARSE_FAILURE;
527
1.00k
                    return 0;
528
1.00k
                }
529
            } else {
530
                // Save original position where non-digit was found
531
                int remaining_len = len - i;
532
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
                    *result = PARSE_FAILURE;
540
                    return 0;
541
                }
542
            }
543
            // Returning here is slightly faster than breaking the loop.
544
88
            *result = PARSE_SUCCESS;
545
1.08k
            return static_cast<T>(negative ? -val : val);
546
1.08k
        }
547
7.83k
    }
548
40
    *result = PARSE_SUCCESS;
549
40
    return static_cast<T>(negative ? -val : val);
550
1.70k
}
_ZN5doris12StringParser22string_to_int_internalIiLb1EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
2.50k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
2.50k
    if (UNLIKELY(len <= 0)) {
480
8
        *result = PARSE_FAILURE;
481
8
        return 0;
482
8
    }
483
484
2.50k
    using UnsignedT = MakeUnsignedT<T>;
485
2.50k
    UnsignedT val = 0;
486
2.50k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
2.50k
    bool negative = false;
488
2.50k
    int i = 0;
489
2.50k
    switch (*s) {
490
608
    case '-':
491
608
        negative = true;
492
608
        max_val += 1;
493
608
        [[fallthrough]];
494
952
    case '+':
495
952
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
952
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
2.50k
    }
502
503
    // This is the fast path where the string cannot overflow.
504
2.50k
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
1.12k
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
1.12k
        return static_cast<T>(negative ? -val : val);
507
1.12k
    }
508
509
1.37k
    const T max_div_10 = max_val / 10;
510
1.37k
    const T max_mod_10 = max_val % 10;
511
512
1.37k
    int first = i;
513
10.7k
    for (; i < len; ++i) {
514
10.6k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
9.90k
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
9.90k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
552
                *result = PARSE_OVERFLOW;
519
552
                return negative ? -max_val : max_val;
520
552
            }
521
9.34k
            val = val * 10 + digit;
522
9.34k
        } else {
523
795
            if constexpr (enable_strict_mode) {
524
795
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
735
                    *result = PARSE_FAILURE;
527
735
                    return 0;
528
735
                }
529
            } else {
530
                // Save original position where non-digit was found
531
                int remaining_len = len - i;
532
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
                    *result = PARSE_FAILURE;
540
                    return 0;
541
                }
542
            }
543
            // Returning here is slightly faster than breaking the loop.
544
60
            *result = PARSE_SUCCESS;
545
795
            return static_cast<T>(negative ? -val : val);
546
795
        }
547
10.6k
    }
548
32
    *result = PARSE_SUCCESS;
549
32
    return static_cast<T>(negative ? -val : val);
550
1.37k
}
_ZN5doris12StringParser22string_to_int_internalIlLb1EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
2.35k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
2.35k
    if (UNLIKELY(len <= 0)) {
480
10
        *result = PARSE_FAILURE;
481
10
        return 0;
482
10
    }
483
484
2.34k
    using UnsignedT = MakeUnsignedT<T>;
485
2.34k
    UnsignedT val = 0;
486
2.34k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
2.34k
    bool negative = false;
488
2.34k
    int i = 0;
489
2.34k
    switch (*s) {
490
596
    case '-':
491
596
        negative = true;
492
596
        max_val += 1;
493
596
        [[fallthrough]];
494
934
    case '+':
495
934
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
934
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
2.34k
    }
502
503
    // This is the fast path where the string cannot overflow.
504
2.34k
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
1.26k
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
1.26k
        return static_cast<T>(negative ? -val : val);
507
1.26k
    }
508
509
1.08k
    const T max_div_10 = max_val / 10;
510
1.08k
    const T max_mod_10 = max_val % 10;
511
512
1.08k
    int first = i;
513
16.9k
    for (; i < len; ++i) {
514
16.8k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
16.3k
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
16.3k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
528
                *result = PARSE_OVERFLOW;
519
528
                return negative ? -max_val : max_val;
520
528
            }
521
15.8k
            val = val * 10 + digit;
522
15.8k
        } else {
523
523
            if constexpr (enable_strict_mode) {
524
523
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
491
                    *result = PARSE_FAILURE;
527
491
                    return 0;
528
491
                }
529
            } else {
530
                // Save original position where non-digit was found
531
                int remaining_len = len - i;
532
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
                    *result = PARSE_FAILURE;
540
                    return 0;
541
                }
542
            }
543
            // Returning here is slightly faster than breaking the loop.
544
32
            *result = PARSE_SUCCESS;
545
523
            return static_cast<T>(negative ? -val : val);
546
523
        }
547
16.8k
    }
548
32
    *result = PARSE_SUCCESS;
549
32
    return static_cast<T>(negative ? -val : val);
550
1.08k
}
_ZN5doris12StringParser22string_to_int_internalInLb1EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
1.78k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
1.78k
    if (UNLIKELY(len <= 0)) {
480
8
        *result = PARSE_FAILURE;
481
8
        return 0;
482
8
    }
483
484
1.77k
    using UnsignedT = MakeUnsignedT<T>;
485
1.77k
    UnsignedT val = 0;
486
1.77k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
1.77k
    bool negative = false;
488
1.77k
    int i = 0;
489
1.77k
    switch (*s) {
490
584
    case '-':
491
584
        negative = true;
492
584
        max_val += 1;
493
584
        [[fallthrough]];
494
916
    case '+':
495
916
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
916
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
1.77k
    }
502
503
    // This is the fast path where the string cannot overflow.
504
1.77k
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
752
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
752
        return static_cast<T>(negative ? -val : val);
507
752
    }
508
509
1.02k
    const T max_div_10 = max_val / 10;
510
1.02k
    const T max_mod_10 = max_val % 10;
511
512
1.02k
    int first = i;
513
31.3k
    for (; i < len; ++i) {
514
31.2k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
30.7k
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
30.7k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
504
                *result = PARSE_OVERFLOW;
519
504
                return negative ? -max_val : max_val;
520
504
            }
521
30.2k
            val = val * 10 + digit;
522
30.2k
        } else {
523
488
            if constexpr (enable_strict_mode) {
524
488
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
456
                    *result = PARSE_FAILURE;
527
456
                    return 0;
528
456
                }
529
            } else {
530
                // Save original position where non-digit was found
531
                int remaining_len = len - i;
532
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
                    *result = PARSE_FAILURE;
540
                    return 0;
541
                }
542
            }
543
            // Returning here is slightly faster than breaking the loop.
544
32
            *result = PARSE_SUCCESS;
545
488
            return static_cast<T>(negative ? -val : val);
546
488
        }
547
31.2k
    }
548
32
    *result = PARSE_SUCCESS;
549
32
    return static_cast<T>(negative ? -val : val);
550
1.02k
}
_ZN5doris12StringParser22string_to_int_internalIaLb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
2.82M
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
2.82M
    if (UNLIKELY(len <= 0)) {
480
218
        *result = PARSE_FAILURE;
481
218
        return 0;
482
218
    }
483
484
2.82M
    using UnsignedT = MakeUnsignedT<T>;
485
2.82M
    UnsignedT val = 0;
486
2.82M
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
2.82M
    bool negative = false;
488
2.82M
    int i = 0;
489
2.82M
    switch (*s) {
490
104k
    case '-':
491
104k
        negative = true;
492
104k
        max_val += 1;
493
104k
        [[fallthrough]];
494
105k
    case '+':
495
105k
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
105k
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
2.82M
    }
502
503
    // This is the fast path where the string cannot overflow.
504
2.82M
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
2.63M
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
2.63M
        return static_cast<T>(negative ? -val : val);
507
2.63M
    }
508
509
187k
    const T max_div_10 = max_val / 10;
510
187k
    const T max_mod_10 = max_val % 10;
511
512
187k
    int first = i;
513
648k
    for (; i < len; ++i) {
514
517k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
481k
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
481k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
21.2k
                *result = PARSE_OVERFLOW;
519
21.2k
                return negative ? -max_val : max_val;
520
21.2k
            }
521
460k
            val = val * 10 + digit;
522
460k
        } else {
523
            if constexpr (enable_strict_mode) {
524
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
                    *result = PARSE_FAILURE;
527
                    return 0;
528
                }
529
35.9k
            } else {
530
                // Save original position where non-digit was found
531
35.9k
                int remaining_len = len - i;
532
35.9k
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
35.9k
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
35.9k
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
35.9k
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
24.5k
                    *result = PARSE_FAILURE;
540
24.5k
                    return 0;
541
24.5k
                }
542
35.9k
            }
543
            // Returning here is slightly faster than breaking the loop.
544
11.4k
            *result = PARSE_SUCCESS;
545
35.9k
            return static_cast<T>(negative ? -val : val);
546
35.9k
        }
547
517k
    }
548
130k
    *result = PARSE_SUCCESS;
549
130k
    return static_cast<T>(negative ? -val : val);
550
187k
}
_ZN5doris12StringParser22string_to_int_internalIsLb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
199k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
199k
    if (UNLIKELY(len <= 0)) {
480
32
        *result = PARSE_FAILURE;
481
32
        return 0;
482
32
    }
483
484
199k
    using UnsignedT = MakeUnsignedT<T>;
485
199k
    UnsignedT val = 0;
486
199k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
199k
    bool negative = false;
488
199k
    int i = 0;
489
199k
    switch (*s) {
490
95.0k
    case '-':
491
95.0k
        negative = true;
492
95.0k
        max_val += 1;
493
95.0k
        [[fallthrough]];
494
95.3k
    case '+':
495
95.3k
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
95.3k
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
199k
    }
502
503
    // This is the fast path where the string cannot overflow.
504
199k
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
52.6k
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
52.6k
        return static_cast<T>(negative ? -val : val);
507
52.6k
    }
508
509
146k
    const T max_div_10 = max_val / 10;
510
146k
    const T max_mod_10 = max_val % 10;
511
512
146k
    int first = i;
513
863k
    for (; i < len; ++i) {
514
731k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
729k
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
729k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
12.9k
                *result = PARSE_OVERFLOW;
519
12.9k
                return negative ? -max_val : max_val;
520
12.9k
            }
521
716k
            val = val * 10 + digit;
522
716k
        } else {
523
            if constexpr (enable_strict_mode) {
524
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
                    *result = PARSE_FAILURE;
527
                    return 0;
528
                }
529
1.90k
            } else {
530
                // Save original position where non-digit was found
531
1.90k
                int remaining_len = len - i;
532
1.90k
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
1.90k
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
1.90k
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
1.90k
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
1.29k
                    *result = PARSE_FAILURE;
540
1.29k
                    return 0;
541
1.29k
                }
542
1.90k
            }
543
            // Returning here is slightly faster than breaking the loop.
544
606
            *result = PARSE_SUCCESS;
545
1.90k
            return static_cast<T>(negative ? -val : val);
546
1.90k
        }
547
731k
    }
548
132k
    *result = PARSE_SUCCESS;
549
132k
    return static_cast<T>(negative ? -val : val);
550
146k
}
_ZN5doris12StringParser22string_to_int_internalIiLb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
3.73M
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
3.73M
    if (UNLIKELY(len <= 0)) {
480
2.09k
        *result = PARSE_FAILURE;
481
2.09k
        return 0;
482
2.09k
    }
483
484
3.73M
    using UnsignedT = MakeUnsignedT<T>;
485
3.73M
    UnsignedT val = 0;
486
3.73M
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
3.73M
    bool negative = false;
488
3.73M
    int i = 0;
489
3.73M
    switch (*s) {
490
1.07M
    case '-':
491
1.07M
        negative = true;
492
1.07M
        max_val += 1;
493
1.07M
        [[fallthrough]];
494
1.07M
    case '+':
495
1.07M
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
1.07M
        if (UNLIKELY(len == 1)) {
498
8
            *result = PARSE_FAILURE;
499
8
            return 0;
500
8
        }
501
3.73M
    }
502
503
    // This is the fast path where the string cannot overflow.
504
3.73M
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
3.59M
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
3.59M
        return static_cast<T>(negative ? -val : val);
507
3.59M
    }
508
509
137k
    const T max_div_10 = max_val / 10;
510
137k
    const T max_mod_10 = max_val % 10;
511
512
137k
    int first = i;
513
1.48M
    for (; i < len; ++i) {
514
1.35M
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
1.35M
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
1.35M
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
5.79k
                *result = PARSE_OVERFLOW;
519
5.79k
                return negative ? -max_val : max_val;
520
5.79k
            }
521
1.34M
            val = val * 10 + digit;
522
1.34M
        } else {
523
            if constexpr (enable_strict_mode) {
524
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
                    *result = PARSE_FAILURE;
527
                    return 0;
528
                }
529
2.89k
            } else {
530
                // Save original position where non-digit was found
531
2.89k
                int remaining_len = len - i;
532
2.89k
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
2.89k
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
2.89k
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
2.89k
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
2.51k
                    *result = PARSE_FAILURE;
540
2.51k
                    return 0;
541
2.51k
                }
542
2.89k
            }
543
            // Returning here is slightly faster than breaking the loop.
544
380
            *result = PARSE_SUCCESS;
545
2.89k
            return static_cast<T>(negative ? -val : val);
546
2.89k
        }
547
1.35M
    }
548
128k
    *result = PARSE_SUCCESS;
549
128k
    return static_cast<T>(negative ? -val : val);
550
137k
}
_ZN5doris12StringParser22string_to_int_internalIlLb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
16.0M
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
16.0M
    if (UNLIKELY(len <= 0)) {
480
34
        *result = PARSE_FAILURE;
481
34
        return 0;
482
34
    }
483
484
16.0M
    using UnsignedT = MakeUnsignedT<T>;
485
16.0M
    UnsignedT val = 0;
486
16.0M
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
16.0M
    bool negative = false;
488
16.0M
    int i = 0;
489
16.0M
    switch (*s) {
490
349k
    case '-':
491
349k
        negative = true;
492
349k
        max_val += 1;
493
349k
        [[fallthrough]];
494
350k
    case '+':
495
350k
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
350k
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
16.0M
    }
502
503
    // This is the fast path where the string cannot overflow.
504
16.0M
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
15.6M
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
15.6M
        return static_cast<T>(negative ? -val : val);
507
15.6M
    }
508
509
328k
    const T max_div_10 = max_val / 10;
510
328k
    const T max_mod_10 = max_val % 10;
511
512
328k
    int first = i;
513
6.54M
    for (; i < len; ++i) {
514
6.22M
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
6.22M
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
6.22M
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
5.22k
                *result = PARSE_OVERFLOW;
519
5.22k
                return negative ? -max_val : max_val;
520
5.22k
            }
521
6.21M
            val = val * 10 + digit;
522
6.21M
        } else {
523
            if constexpr (enable_strict_mode) {
524
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
                    *result = PARSE_FAILURE;
527
                    return 0;
528
                }
529
1.45k
            } else {
530
                // Save original position where non-digit was found
531
1.45k
                int remaining_len = len - i;
532
1.45k
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
1.45k
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
1.45k
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
1.45k
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
1.24k
                    *result = PARSE_FAILURE;
540
1.24k
                    return 0;
541
1.24k
                }
542
1.45k
            }
543
            // Returning here is slightly faster than breaking the loop.
544
208
            *result = PARSE_SUCCESS;
545
1.45k
            return static_cast<T>(negative ? -val : val);
546
1.45k
        }
547
6.22M
    }
548
321k
    *result = PARSE_SUCCESS;
549
321k
    return static_cast<T>(negative ? -val : val);
550
328k
}
_ZN5doris12StringParser22string_to_int_internalIjLb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
1
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
1
    if (UNLIKELY(len <= 0)) {
480
0
        *result = PARSE_FAILURE;
481
0
        return 0;
482
0
    }
483
484
1
    using UnsignedT = MakeUnsignedT<T>;
485
1
    UnsignedT val = 0;
486
1
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
1
    bool negative = false;
488
1
    int i = 0;
489
1
    switch (*s) {
490
0
    case '-':
491
0
        negative = true;
492
0
        max_val += 1;
493
0
        [[fallthrough]];
494
0
    case '+':
495
0
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
0
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
1
    }
502
503
    // This is the fast path where the string cannot overflow.
504
1
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
1
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
1
        return static_cast<T>(negative ? -val : val);
507
1
    }
508
509
0
    const T max_div_10 = max_val / 10;
510
0
    const T max_mod_10 = max_val % 10;
511
512
0
    int first = i;
513
0
    for (; i < len; ++i) {
514
0
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
0
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
0
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
0
                *result = PARSE_OVERFLOW;
519
0
                return negative ? -max_val : max_val;
520
0
            }
521
0
            val = val * 10 + digit;
522
0
        } else {
523
            if constexpr (enable_strict_mode) {
524
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
                    *result = PARSE_FAILURE;
527
                    return 0;
528
                }
529
0
            } else {
530
                // Save original position where non-digit was found
531
0
                int remaining_len = len - i;
532
0
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
0
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
0
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
0
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
0
                    *result = PARSE_FAILURE;
540
0
                    return 0;
541
0
                }
542
0
            }
543
            // Returning here is slightly faster than breaking the loop.
544
0
            *result = PARSE_SUCCESS;
545
0
            return static_cast<T>(negative ? -val : val);
546
0
        }
547
0
    }
548
0
    *result = PARSE_SUCCESS;
549
0
    return static_cast<T>(negative ? -val : val);
550
0
}
_ZN5doris12StringParser22string_to_int_internalImLb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
28
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
28
    if (UNLIKELY(len <= 0)) {
480
0
        *result = PARSE_FAILURE;
481
0
        return 0;
482
0
    }
483
484
28
    using UnsignedT = MakeUnsignedT<T>;
485
28
    UnsignedT val = 0;
486
28
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
28
    bool negative = false;
488
28
    int i = 0;
489
28
    switch (*s) {
490
0
    case '-':
491
0
        negative = true;
492
0
        max_val += 1;
493
0
        [[fallthrough]];
494
0
    case '+':
495
0
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
0
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
28
    }
502
503
    // This is the fast path where the string cannot overflow.
504
28
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
28
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
28
        return static_cast<T>(negative ? -val : val);
507
28
    }
508
509
0
    const T max_div_10 = max_val / 10;
510
0
    const T max_mod_10 = max_val % 10;
511
512
0
    int first = i;
513
0
    for (; i < len; ++i) {
514
0
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
0
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
0
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
0
                *result = PARSE_OVERFLOW;
519
0
                return negative ? -max_val : max_val;
520
0
            }
521
0
            val = val * 10 + digit;
522
0
        } else {
523
            if constexpr (enable_strict_mode) {
524
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
                    *result = PARSE_FAILURE;
527
                    return 0;
528
                }
529
0
            } else {
530
                // Save original position where non-digit was found
531
0
                int remaining_len = len - i;
532
0
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
0
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
0
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
0
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
0
                    *result = PARSE_FAILURE;
540
0
                    return 0;
541
0
                }
542
0
            }
543
            // Returning here is slightly faster than breaking the loop.
544
0
            *result = PARSE_SUCCESS;
545
0
            return static_cast<T>(negative ? -val : val);
546
0
        }
547
0
    }
548
0
    *result = PARSE_SUCCESS;
549
0
    return static_cast<T>(negative ? -val : val);
550
0
}
_ZN5doris12StringParser22string_to_int_internalIjLb1EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
149
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
149
    if (UNLIKELY(len <= 0)) {
480
2
        *result = PARSE_FAILURE;
481
2
        return 0;
482
2
    }
483
484
147
    using UnsignedT = MakeUnsignedT<T>;
485
147
    UnsignedT val = 0;
486
147
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
147
    bool negative = false;
488
147
    int i = 0;
489
147
    switch (*s) {
490
0
    case '-':
491
0
        negative = true;
492
0
        max_val += 1;
493
0
        [[fallthrough]];
494
0
    case '+':
495
0
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
0
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
147
    }
502
503
    // This is the fast path where the string cannot overflow.
504
147
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
147
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
147
        return static_cast<T>(negative ? -val : val);
507
147
    }
508
509
0
    const T max_div_10 = max_val / 10;
510
0
    const T max_mod_10 = max_val % 10;
511
512
0
    int first = i;
513
0
    for (; i < len; ++i) {
514
0
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
0
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
0
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
0
                *result = PARSE_OVERFLOW;
519
0
                return negative ? -max_val : max_val;
520
0
            }
521
0
            val = val * 10 + digit;
522
0
        } else {
523
0
            if constexpr (enable_strict_mode) {
524
0
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
0
                    *result = PARSE_FAILURE;
527
0
                    return 0;
528
0
                }
529
            } else {
530
                // Save original position where non-digit was found
531
                int remaining_len = len - i;
532
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
                    *result = PARSE_FAILURE;
540
                    return 0;
541
                }
542
            }
543
            // Returning here is slightly faster than breaking the loop.
544
0
            *result = PARSE_SUCCESS;
545
0
            return static_cast<T>(negative ? -val : val);
546
0
        }
547
0
    }
548
0
    *result = PARSE_SUCCESS;
549
0
    return static_cast<T>(negative ? -val : val);
550
0
}
_ZN5doris12StringParser22string_to_int_internalIN4wide7integerILm256EiEELb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
4
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
4
    if (UNLIKELY(len <= 0)) {
480
0
        *result = PARSE_FAILURE;
481
0
        return 0;
482
0
    }
483
484
4
    using UnsignedT = MakeUnsignedT<T>;
485
4
    UnsignedT val = 0;
486
4
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
4
    bool negative = false;
488
4
    int i = 0;
489
4
    switch (*s) {
490
0
    case '-':
491
0
        negative = true;
492
0
        max_val += 1;
493
0
        [[fallthrough]];
494
0
    case '+':
495
0
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
0
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
4
    }
502
503
    // This is the fast path where the string cannot overflow.
504
4
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
4
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
4
        return static_cast<T>(negative ? -val : val);
507
4
    }
508
509
0
    const T max_div_10 = max_val / 10;
510
0
    const T max_mod_10 = max_val % 10;
511
512
0
    int first = i;
513
0
    for (; i < len; ++i) {
514
0
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
0
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
0
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
0
                *result = PARSE_OVERFLOW;
519
0
                return negative ? -max_val : max_val;
520
0
            }
521
0
            val = val * 10 + digit;
522
0
        } else {
523
            if constexpr (enable_strict_mode) {
524
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
                    *result = PARSE_FAILURE;
527
                    return 0;
528
                }
529
0
            } else {
530
                // Save original position where non-digit was found
531
0
                int remaining_len = len - i;
532
0
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
0
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
0
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
0
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
0
                    *result = PARSE_FAILURE;
540
0
                    return 0;
541
0
                }
542
0
            }
543
            // Returning here is slightly faster than breaking the loop.
544
0
            *result = PARSE_SUCCESS;
545
0
            return static_cast<T>(negative ? -val : val);
546
0
        }
547
0
    }
548
0
    *result = PARSE_SUCCESS;
549
0
    return static_cast<T>(negative ? -val : val);
550
0
}
_ZN5doris12StringParser22string_to_int_internalIoLb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
478
4
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
479
4
    if (UNLIKELY(len <= 0)) {
480
0
        *result = PARSE_FAILURE;
481
0
        return 0;
482
0
    }
483
484
4
    using UnsignedT = MakeUnsignedT<T>;
485
4
    UnsignedT val = 0;
486
4
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
487
4
    bool negative = false;
488
4
    int i = 0;
489
4
    switch (*s) {
490
0
    case '-':
491
0
        negative = true;
492
0
        max_val += 1;
493
0
        [[fallthrough]];
494
0
    case '+':
495
0
        ++i;
496
        // only one '+'/'-' char, so could return failure directly
497
0
        if (UNLIKELY(len == 1)) {
498
0
            *result = PARSE_FAILURE;
499
0
            return 0;
500
0
        }
501
4
    }
502
503
    // This is the fast path where the string cannot overflow.
504
4
    if (LIKELY(len - i < NumberTraits::max_ascii_len<T>())) {
505
0
        val = string_to_int_no_overflow<UnsignedT, enable_strict_mode>(s + i, len - i, result);
506
0
        return static_cast<T>(negative ? -val : val);
507
0
    }
508
509
4
    const T max_div_10 = max_val / 10;
510
4
    const T max_mod_10 = max_val % 10;
511
512
4
    int first = i;
513
84
    for (; i < len; ++i) {
514
80
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
515
80
            T digit = s[i] - '0';
516
            // This is a tricky check to see if adding this digit will cause an overflow.
517
80
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
518
0
                *result = PARSE_OVERFLOW;
519
0
                return negative ? -max_val : max_val;
520
0
            }
521
80
            val = val * 10 + digit;
522
80
        } else {
523
            if constexpr (enable_strict_mode) {
524
                if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
525
                    // Reject the string because the remaining chars are not all whitespace
526
                    *result = PARSE_FAILURE;
527
                    return 0;
528
                }
529
0
            } else {
530
                // Save original position where non-digit was found
531
0
                int remaining_len = len - i;
532
0
                const char* remaining_s = s + i;
533
                // Skip trailing whitespaces from the remaining portion
534
0
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
535
0
                if ((UNLIKELY(i == first || (remaining_len != 0 &&
536
0
                                             !is_float_suffix(remaining_s, remaining_len))))) {
537
                    // Reject the string because either the first char was not a digit,
538
                    // or the remaining chars are not all whitespace
539
0
                    *result = PARSE_FAILURE;
540
0
                    return 0;
541
0
                }
542
0
            }
543
            // Returning here is slightly faster than breaking the loop.
544
0
            *result = PARSE_SUCCESS;
545
0
            return static_cast<T>(negative ? -val : val);
546
0
        }
547
80
    }
548
4
    *result = PARSE_SUCCESS;
549
4
    return static_cast<T>(negative ? -val : val);
550
4
}
551
552
template <typename T>
553
T StringParser::string_to_unsigned_int_internal(const char* __restrict s, int len,
554
343
                                                ParseResult* result) {
555
343
    if (UNLIKELY(len <= 0)) {
556
0
        *result = PARSE_FAILURE;
557
0
        return 0;
558
0
    }
559
560
343
    T val = 0;
561
343
    T max_val = std::numeric_limits<T>::max();
562
343
    int i = 0;
563
564
343
    using signedT = MakeSignedT<T>;
565
    // This is the fast path where the string cannot overflow.
566
343
    if (LIKELY(len - i < NumberTraits::max_ascii_len<signedT>())) {
567
245
        val = string_to_int_no_overflow<T>(s + i, len - i, result);
568
245
        return val;
569
245
    }
570
571
98
    const T max_div_10 = max_val / 10;
572
98
    const T max_mod_10 = max_val % 10;
573
574
98
    int first = i;
575
2.00k
    for (; i < len; ++i) {
576
1.96k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
577
1.96k
            T digit = s[i] - '0';
578
            // This is a tricky check to see if adding this digit will cause an overflow.
579
1.96k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
580
49
                *result = PARSE_OVERFLOW;
581
49
                return max_val;
582
49
            }
583
1.91k
            val = val * 10 + digit;
584
1.91k
        } else {
585
0
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
586
                // Reject the string because either the first char was not a digit,
587
                // or the remaining chars are not all whitespace
588
0
                *result = PARSE_FAILURE;
589
0
                return 0;
590
0
            }
591
            // Returning here is slightly faster than breaking the loop.
592
0
            *result = PARSE_SUCCESS;
593
0
            return val;
594
0
        }
595
1.96k
    }
596
49
    *result = PARSE_SUCCESS;
597
49
    return val;
598
98
}
599
600
template <typename T>
601
T StringParser::string_to_int_internal(const char* __restrict s, int64_t len, int base,
602
1
                                       ParseResult* result) {
603
1
    using UnsignedT = MakeUnsignedT<T>;
604
1
    UnsignedT val = 0;
605
1
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
606
1
    bool negative = false;
607
1
    if (UNLIKELY(len <= 0)) {
608
0
        *result = PARSE_FAILURE;
609
0
        return 0;
610
0
    }
611
1
    int i = 0;
612
1
    switch (*s) {
613
0
    case '-':
614
0
        negative = true;
615
0
        max_val = StringParser::numeric_limits<T>(false) + 1;
616
0
        [[fallthrough]];
617
0
    case '+':
618
0
        i = 1;
619
1
    }
620
621
1
    const T max_div_base = max_val / base;
622
1
    const T max_mod_base = max_val % base;
623
624
1
    int first = i;
625
3
    for (; i < len; ++i) {
626
2
        T digit;
627
2
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
628
0
            digit = s[i] - '0';
629
2
        } else if (s[i] >= 'a' && s[i] <= 'z') {
630
2
            digit = (s[i] - 'a' + 10);
631
2
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
632
0
            digit = (s[i] - 'A' + 10);
633
0
        } else {
634
0
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
635
                // Reject the string because either the first char was not an alpha/digit,
636
                // or the remaining chars are not all whitespace
637
0
                *result = PARSE_FAILURE;
638
0
                return 0;
639
0
            }
640
            // skip trailing whitespace.
641
0
            break;
642
0
        }
643
644
        // Bail, if we encounter a digit that is not available in base.
645
2
        if (digit >= base) {
646
0
            break;
647
0
        }
648
649
        // This is a tricky check to see if adding this digit will cause an overflow.
650
2
        if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) {
651
0
            *result = PARSE_OVERFLOW;
652
0
            return static_cast<T>(negative ? -max_val : max_val);
653
0
        }
654
2
        val = val * base + digit;
655
2
    }
656
1
    *result = PARSE_SUCCESS;
657
1
    return static_cast<T>(negative ? -val : val);
658
1
}
659
660
template <typename T, bool enable_strict_mode>
661
22.0M
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
662
22.0M
    T val = 0;
663
22.0M
    if (UNLIKELY(len == 0)) {
664
0
        *result = PARSE_SUCCESS;
665
0
        return val;
666
0
    }
667
    // Factor out the first char for error handling speeds up the loop.
668
22.0M
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
669
21.9M
        val = s[0] - '0';
670
21.9M
    } else {
671
7.11k
        *result = PARSE_FAILURE;
672
7.11k
        return 0;
673
7.11k
    }
674
85.4M
    for (int i = 1; i < len; ++i) {
675
63.5M
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
676
63.4M
            T digit = s[i] - '0';
677
63.4M
            val = val * 10 + digit;
678
63.4M
        } else {
679
83.7k
            if constexpr (enable_strict_mode) {
680
1.31k
                if (UNLIKELY(!is_all_whitespace(s + i, len - i))) {
681
1.17k
                    *result = PARSE_FAILURE;
682
1.17k
                    return 0;
683
1.17k
                }
684
82.4k
            } else {
685
                // Save original position where non-digit was found
686
82.4k
                int remaining_len = len - i;
687
82.4k
                const char* remaining_s = s + i;
688
                // Skip trailing whitespaces from the remaining portion
689
82.4k
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
690
82.4k
                if ((UNLIKELY(remaining_len != 0 &&
691
82.4k
                              !is_float_suffix(remaining_s, remaining_len)))) {
692
490
                    *result = PARSE_FAILURE;
693
490
                    return 0;
694
490
                }
695
82.4k
            }
696
82.1k
            *result = PARSE_SUCCESS;
697
83.7k
            return val;
698
83.7k
        }
699
63.5M
    }
700
21.9M
    *result = PARSE_SUCCESS;
701
21.9M
    return val;
702
21.9M
}
_ZN5doris12StringParser25string_to_int_no_overflowIoLb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
661
43.0k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
662
43.0k
    T val = 0;
663
43.0k
    if (UNLIKELY(len == 0)) {
664
0
        *result = PARSE_SUCCESS;
665
0
        return val;
666
0
    }
667
    // Factor out the first char for error handling speeds up the loop.
668
43.0k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
669
42.1k
        val = s[0] - '0';
670
42.1k
    } else {
671
914
        *result = PARSE_FAILURE;
672
914
        return 0;
673
914
    }
674
60.9k
    for (int i = 1; i < len; ++i) {
675
19.1k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
676
18.8k
            T digit = s[i] - '0';
677
18.8k
            val = val * 10 + digit;
678
18.8k
        } else {
679
            if constexpr (enable_strict_mode) {
680
                if (UNLIKELY(!is_all_whitespace(s + i, len - i))) {
681
                    *result = PARSE_FAILURE;
682
                    return 0;
683
                }
684
378
            } else {
685
                // Save original position where non-digit was found
686
378
                int remaining_len = len - i;
687
378
                const char* remaining_s = s + i;
688
                // Skip trailing whitespaces from the remaining portion
689
378
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
690
378
                if ((UNLIKELY(remaining_len != 0 &&
691
378
                              !is_float_suffix(remaining_s, remaining_len)))) {
692
98
                    *result = PARSE_FAILURE;
693
98
                    return 0;
694
98
                }
695
378
            }
696
280
            *result = PARSE_SUCCESS;
697
378
            return val;
698
378
        }
699
19.1k
    }
700
41.7k
    *result = PARSE_SUCCESS;
701
41.7k
    return val;
702
42.1k
}
_ZN5doris12StringParser25string_to_int_no_overflowIhLb1EEET_PKciPNS0_11ParseResultE
Line
Count
Source
661
568
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
662
568
    T val = 0;
663
568
    if (UNLIKELY(len == 0)) {
664
0
        *result = PARSE_SUCCESS;
665
0
        return val;
666
0
    }
667
    // Factor out the first char for error handling speeds up the loop.
668
568
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
669
542
        val = s[0] - '0';
670
542
    } else {
671
26
        *result = PARSE_FAILURE;
672
26
        return 0;
673
26
    }
674
542
    for (int i = 1; i < len; ++i) {
675
2
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
676
0
            T digit = s[i] - '0';
677
0
            val = val * 10 + digit;
678
2
        } else {
679
2
            if constexpr (enable_strict_mode) {
680
2
                if (UNLIKELY(!is_all_whitespace(s + i, len - i))) {
681
2
                    *result = PARSE_FAILURE;
682
2
                    return 0;
683
2
                }
684
            } else {
685
                // Save original position where non-digit was found
686
                int remaining_len = len - i;
687
                const char* remaining_s = s + i;
688
                // Skip trailing whitespaces from the remaining portion
689
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
690
                if ((UNLIKELY(remaining_len != 0 &&
691
                              !is_float_suffix(remaining_s, remaining_len)))) {
692
                    *result = PARSE_FAILURE;
693
                    return 0;
694
                }
695
            }
696
0
            *result = PARSE_SUCCESS;
697
2
            return val;
698
2
        }
699
2
    }
700
540
    *result = PARSE_SUCCESS;
701
540
    return val;
702
542
}
_ZN5doris12StringParser25string_to_int_no_overflowItLb1EEET_PKciPNS0_11ParseResultE
Line
Count
Source
661
748
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
662
748
    T val = 0;
663
748
    if (UNLIKELY(len == 0)) {
664
0
        *result = PARSE_SUCCESS;
665
0
        return val;
666
0
    }
667
    // Factor out the first char for error handling speeds up the loop.
668
748
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
669
710
        val = s[0] - '0';
670
710
    } else {
671
38
        *result = PARSE_FAILURE;
672
38
        return 0;
673
38
    }
674
786
    for (int i = 1; i < len; ++i) {
675
158
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
676
76
            T digit = s[i] - '0';
677
76
            val = val * 10 + digit;
678
82
        } else {
679
82
            if constexpr (enable_strict_mode) {
680
82
                if (UNLIKELY(!is_all_whitespace(s + i, len - i))) {
681
82
                    *result = PARSE_FAILURE;
682
82
                    return 0;
683
82
                }
684
            } else {
685
                // Save original position where non-digit was found
686
                int remaining_len = len - i;
687
                const char* remaining_s = s + i;
688
                // Skip trailing whitespaces from the remaining portion
689
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
690
                if ((UNLIKELY(remaining_len != 0 &&
691
                              !is_float_suffix(remaining_s, remaining_len)))) {
692
                    *result = PARSE_FAILURE;
693
                    return 0;
694
                }
695
            }
696
0
            *result = PARSE_SUCCESS;
697
82
            return val;
698
82
        }
699
158
    }
700
628
    *result = PARSE_SUCCESS;
701
628
    return val;
702
710
}
_ZN5doris12StringParser25string_to_int_no_overflowIjLb1EEET_PKciPNS0_11ParseResultE
Line
Count
Source
661
1.26k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
662
1.26k
    T val = 0;
663
1.26k
    if (UNLIKELY(len == 0)) {
664
0
        *result = PARSE_SUCCESS;
665
0
        return val;
666
0
    }
667
    // Factor out the first char for error handling speeds up the loop.
668
1.26k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
669
1.18k
        val = s[0] - '0';
670
1.18k
    } else {
671
85
        *result = PARSE_FAILURE;
672
85
        return 0;
673
85
    }
674
2.66k
    for (int i = 1; i < len; ++i) {
675
1.80k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
676
1.48k
            T digit = s[i] - '0';
677
1.48k
            val = val * 10 + digit;
678
1.48k
        } else {
679
320
            if constexpr (enable_strict_mode) {
680
320
                if (UNLIKELY(!is_all_whitespace(s + i, len - i))) {
681
292
                    *result = PARSE_FAILURE;
682
292
                    return 0;
683
292
                }
684
            } else {
685
                // Save original position where non-digit was found
686
                int remaining_len = len - i;
687
                const char* remaining_s = s + i;
688
                // Skip trailing whitespaces from the remaining portion
689
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
690
                if ((UNLIKELY(remaining_len != 0 &&
691
                              !is_float_suffix(remaining_s, remaining_len)))) {
692
                    *result = PARSE_FAILURE;
693
                    return 0;
694
                }
695
            }
696
28
            *result = PARSE_SUCCESS;
697
320
            return val;
698
320
        }
699
1.80k
    }
700
863
    *result = PARSE_SUCCESS;
701
863
    return val;
702
1.18k
}
_ZN5doris12StringParser25string_to_int_no_overflowImLb1EEET_PKciPNS0_11ParseResultE
Line
Count
Source
661
1.26k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
662
1.26k
    T val = 0;
663
1.26k
    if (UNLIKELY(len == 0)) {
664
0
        *result = PARSE_SUCCESS;
665
0
        return val;
666
0
    }
667
    // Factor out the first char for error handling speeds up the loop.
668
1.26k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
669
1.04k
        val = s[0] - '0';
670
1.04k
    } else {
671
217
        *result = PARSE_FAILURE;
672
217
        return 0;
673
217
    }
674
4.64k
    for (int i = 1; i < len; ++i) {
675
4.05k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
676
3.59k
            T digit = s[i] - '0';
677
3.59k
            val = val * 10 + digit;
678
3.59k
        } else {
679
456
            if constexpr (enable_strict_mode) {
680
456
                if (UNLIKELY(!is_all_whitespace(s + i, len - i))) {
681
400
                    *result = PARSE_FAILURE;
682
400
                    return 0;
683
400
                }
684
            } else {
685
                // Save original position where non-digit was found
686
                int remaining_len = len - i;
687
                const char* remaining_s = s + i;
688
                // Skip trailing whitespaces from the remaining portion
689
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
690
                if ((UNLIKELY(remaining_len != 0 &&
691
                              !is_float_suffix(remaining_s, remaining_len)))) {
692
                    *result = PARSE_FAILURE;
693
                    return 0;
694
                }
695
            }
696
56
            *result = PARSE_SUCCESS;
697
456
            return val;
698
456
        }
699
4.05k
    }
700
590
    *result = PARSE_SUCCESS;
701
590
    return val;
702
1.04k
}
_ZN5doris12StringParser25string_to_int_no_overflowIoLb1EEET_PKciPNS0_11ParseResultE
Line
Count
Source
661
752
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
662
752
    T val = 0;
663
752
    if (UNLIKELY(len == 0)) {
664
0
        *result = PARSE_SUCCESS;
665
0
        return val;
666
0
    }
667
    // Factor out the first char for error handling speeds up the loop.
668
752
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
669
512
        val = s[0] - '0';
670
512
    } else {
671
240
        *result = PARSE_FAILURE;
672
240
        return 0;
673
240
    }
674
1.49k
    for (int i = 1; i < len; ++i) {
675
1.44k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
676
986
            T digit = s[i] - '0';
677
986
            val = val * 10 + digit;
678
986
        } else {
679
456
            if constexpr (enable_strict_mode) {
680
456
                if (UNLIKELY(!is_all_whitespace(s + i, len - i))) {
681
400
                    *result = PARSE_FAILURE;
682
400
                    return 0;
683
400
                }
684
            } else {
685
                // Save original position where non-digit was found
686
                int remaining_len = len - i;
687
                const char* remaining_s = s + i;
688
                // Skip trailing whitespaces from the remaining portion
689
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
690
                if ((UNLIKELY(remaining_len != 0 &&
691
                              !is_float_suffix(remaining_s, remaining_len)))) {
692
                    *result = PARSE_FAILURE;
693
                    return 0;
694
                }
695
            }
696
56
            *result = PARSE_SUCCESS;
697
456
            return val;
698
456
        }
699
1.44k
    }
700
56
    *result = PARSE_SUCCESS;
701
56
    return val;
702
512
}
_ZN5doris12StringParser25string_to_int_no_overflowIhLb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
661
2.63M
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
662
2.63M
    T val = 0;
663
2.63M
    if (UNLIKELY(len == 0)) {
664
0
        *result = PARSE_SUCCESS;
665
0
        return val;
666
0
    }
667
    // Factor out the first char for error handling speeds up the loop.
668
2.63M
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
669
2.63M
        val = s[0] - '0';
670
2.63M
    } else {
671
582
        *result = PARSE_FAILURE;
672
582
        return 0;
673
582
    }
674
2.65M
    for (int i = 1; i < len; ++i) {
675
25.4k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
676
25.4k
            T digit = s[i] - '0';
677
25.4k
            val = val * 10 + digit;
678
25.4k
        } else {
679
            if constexpr (enable_strict_mode) {
680
                if (UNLIKELY(!is_all_whitespace(s + i, len - i))) {
681
                    *result = PARSE_FAILURE;
682
                    return 0;
683
                }
684
2
            } else {
685
                // Save original position where non-digit was found
686
2
                int remaining_len = len - i;
687
2
                const char* remaining_s = s + i;
688
                // Skip trailing whitespaces from the remaining portion
689
2
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
690
2
                if ((UNLIKELY(remaining_len != 0 &&
691
2
                              !is_float_suffix(remaining_s, remaining_len)))) {
692
2
                    *result = PARSE_FAILURE;
693
2
                    return 0;
694
2
                }
695
2
            }
696
0
            *result = PARSE_SUCCESS;
697
2
            return val;
698
2
        }
699
25.4k
    }
700
2.63M
    *result = PARSE_SUCCESS;
701
2.63M
    return val;
702
2.63M
}
_ZN5doris12StringParser25string_to_int_no_overflowItLb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
661
52.8k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
662
52.8k
    T val = 0;
663
52.8k
    if (UNLIKELY(len == 0)) {
664
0
        *result = PARSE_SUCCESS;
665
0
        return val;
666
0
    }
667
    // Factor out the first char for error handling speeds up the loop.
668
52.8k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
669
51.9k
        val = s[0] - '0';
670
51.9k
    } else {
671
918
        *result = PARSE_FAILURE;
672
918
        return 0;
673
918
    }
674
75.8k
    for (int i = 1; i < len; ++i) {
675
24.8k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
676
23.9k
            T digit = s[i] - '0';
677
23.9k
            val = val * 10 + digit;
678
23.9k
        } else {
679
            if constexpr (enable_strict_mode) {
680
                if (UNLIKELY(!is_all_whitespace(s + i, len - i))) {
681
                    *result = PARSE_FAILURE;
682
                    return 0;
683
                }
684
957
            } else {
685
                // Save original position where non-digit was found
686
957
                int remaining_len = len - i;
687
957
                const char* remaining_s = s + i;
688
                // Skip trailing whitespaces from the remaining portion
689
957
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
690
957
                if ((UNLIKELY(remaining_len != 0 &&
691
957
                              !is_float_suffix(remaining_s, remaining_len)))) {
692
74
                    *result = PARSE_FAILURE;
693
74
                    return 0;
694
74
                }
695
957
            }
696
883
            *result = PARSE_SUCCESS;
697
957
            return val;
698
957
        }
699
24.8k
    }
700
50.9k
    *result = PARSE_SUCCESS;
701
50.9k
    return val;
702
51.9k
}
_ZN5doris12StringParser25string_to_int_no_overflowIjLb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
661
3.59M
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
662
3.59M
    T val = 0;
663
3.59M
    if (UNLIKELY(len == 0)) {
664
0
        *result = PARSE_SUCCESS;
665
0
        return val;
666
0
    }
667
    // Factor out the first char for error handling speeds up the loop.
668
3.59M
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
669
3.59M
        val = s[0] - '0';
670
3.59M
    } else {
671
2.82k
        *result = PARSE_FAILURE;
672
2.82k
        return 0;
673
2.82k
    }
674
6.15M
    for (int i = 1; i < len; ++i) {
675
2.64M
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
676
2.56M
            T digit = s[i] - '0';
677
2.56M
            val = val * 10 + digit;
678
2.56M
        } else {
679
            if constexpr (enable_strict_mode) {
680
                if (UNLIKELY(!is_all_whitespace(s + i, len - i))) {
681
                    *result = PARSE_FAILURE;
682
                    return 0;
683
                }
684
80.6k
            } else {
685
                // Save original position where non-digit was found
686
80.6k
                int remaining_len = len - i;
687
80.6k
                const char* remaining_s = s + i;
688
                // Skip trailing whitespaces from the remaining portion
689
80.6k
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
690
80.6k
                if ((UNLIKELY(remaining_len != 0 &&
691
80.6k
                              !is_float_suffix(remaining_s, remaining_len)))) {
692
192
                    *result = PARSE_FAILURE;
693
192
                    return 0;
694
192
                }
695
80.6k
            }
696
80.4k
            *result = PARSE_SUCCESS;
697
80.6k
            return val;
698
80.6k
        }
699
2.64M
    }
700
3.51M
    *result = PARSE_SUCCESS;
701
3.51M
    return val;
702
3.59M
}
_ZN5doris12StringParser25string_to_int_no_overflowImLb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
661
15.6M
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
662
15.6M
    T val = 0;
663
15.6M
    if (UNLIKELY(len == 0)) {
664
0
        *result = PARSE_SUCCESS;
665
0
        return val;
666
0
    }
667
    // Factor out the first char for error handling speeds up the loop.
668
15.6M
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
669
15.6M
        val = s[0] - '0';
670
15.6M
    } else {
671
1.27k
        *result = PARSE_FAILURE;
672
1.27k
        return 0;
673
1.27k
    }
674
76.5M
    for (int i = 1; i < len; ++i) {
675
60.8M
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
676
60.8M
            T digit = s[i] - '0';
677
60.8M
            val = val * 10 + digit;
678
60.8M
        } else {
679
            if constexpr (enable_strict_mode) {
680
                if (UNLIKELY(!is_all_whitespace(s + i, len - i))) {
681
                    *result = PARSE_FAILURE;
682
                    return 0;
683
                }
684
513
            } else {
685
                // Save original position where non-digit was found
686
513
                int remaining_len = len - i;
687
513
                const char* remaining_s = s + i;
688
                // Skip trailing whitespaces from the remaining portion
689
513
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
690
513
                if ((UNLIKELY(remaining_len != 0 &&
691
513
                              !is_float_suffix(remaining_s, remaining_len)))) {
692
124
                    *result = PARSE_FAILURE;
693
124
                    return 0;
694
124
                }
695
513
            }
696
389
            *result = PARSE_SUCCESS;
697
513
            return val;
698
513
        }
699
60.8M
    }
700
15.6M
    *result = PARSE_SUCCESS;
701
15.6M
    return val;
702
15.6M
}
_ZN5doris12StringParser25string_to_int_no_overflowIN4wide7integerILm256EjEELb0EEET_PKciPNS0_11ParseResultE
Line
Count
Source
661
4
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
662
4
    T val = 0;
663
4
    if (UNLIKELY(len == 0)) {
664
0
        *result = PARSE_SUCCESS;
665
0
        return val;
666
0
    }
667
    // Factor out the first char for error handling speeds up the loop.
668
4
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
669
4
        val = s[0] - '0';
670
4
    } else {
671
0
        *result = PARSE_FAILURE;
672
0
        return 0;
673
0
    }
674
4
    for (int i = 1; i < len; ++i) {
675
0
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
676
0
            T digit = s[i] - '0';
677
0
            val = val * 10 + digit;
678
0
        } else {
679
            if constexpr (enable_strict_mode) {
680
                if (UNLIKELY(!is_all_whitespace(s + i, len - i))) {
681
                    *result = PARSE_FAILURE;
682
                    return 0;
683
                }
684
0
            } else {
685
                // Save original position where non-digit was found
686
0
                int remaining_len = len - i;
687
0
                const char* remaining_s = s + i;
688
                // Skip trailing whitespaces from the remaining portion
689
0
                remaining_s = skip_trailing_whitespaces(remaining_s, remaining_len);
690
0
                if ((UNLIKELY(remaining_len != 0 &&
691
0
                              !is_float_suffix(remaining_s, remaining_len)))) {
692
0
                    *result = PARSE_FAILURE;
693
0
                    return 0;
694
0
                }
695
0
            }
696
0
            *result = PARSE_SUCCESS;
697
0
            return val;
698
0
        }
699
0
    }
700
4
    *result = PARSE_SUCCESS;
701
4
    return val;
702
4
}
703
704
// at least the first char(if any) must be a digit.
705
template <typename T>
706
T StringParser::string_to_uint_greedy_no_overflow(const char* __restrict s, int max_len,
707
503k
                                                  ParseResult* result) {
708
503k
    T val = 0;
709
503k
    if (max_len == 0) [[unlikely]] {
710
135k
        *result = PARSE_SUCCESS;
711
135k
        return val;
712
135k
    }
713
    // Factor out the first char for error handling speeds up the loop.
714
367k
    if (is_numeric_ascii(s[0])) [[likely]] {
715
367k
        val = s[0] - '0';
716
18.4E
    } else {
717
18.4E
        *result = PARSE_FAILURE;
718
18.4E
        return 0;
719
18.4E
    }
720
1.84M
    for (int i = 1; i < max_len; ++i) {
721
1.48M
        if (is_numeric_ascii(s[i])) [[likely]] {
722
1.48M
            T digit = s[i] - '0';
723
1.48M
            val = val * 10 + digit;
724
18.4E
        } else {
725
            // 123abc, return 123
726
18.4E
            *result = PARSE_SUCCESS;
727
18.4E
            return val;
728
18.4E
        }
729
1.48M
    }
730
368k
    *result = PARSE_SUCCESS;
731
368k
    return val;
732
367k
}
733
734
template <typename T>
735
1.62M
T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) {
736
1.62M
    int i = 0;
737
    // skip leading spaces
738
1.62M
    for (; i < len; ++i) {
739
1.62M
        if (!is_whitespace_ascii(s[i])) {
740
1.62M
            break;
741
1.62M
        }
742
1.62M
    }
743
744
    // skip back spaces
745
1.62M
    int j = len - 1;
746
1.62M
    for (; j >= i; j--) {
747
1.62M
        if (!is_whitespace_ascii(s[j])) {
748
1.62M
            break;
749
1.62M
        }
750
1.62M
    }
751
752
    // skip leading '+', from_chars can handle '-'
753
1.62M
    if (i < len && s[i] == '+') {
754
7.08k
        i++;
755
        // ++ or +- are not valid, but the first + is already skipped,
756
        // if don't check here, from_chars will succeed.
757
        //
758
        // New version of fast_float supports a new flag called 'chars_format::allow_leading_plus'
759
        // which may avoid this extra check here.
760
        // e.g.:
761
        // fast_float::chars_format format =
762
        //         fast_float::chars_format::general | fast_float::chars_format::allow_leading_plus;
763
        // auto res = fast_float::from_chars(s + i, s + j + 1, val, format);
764
7.08k
        if (i < len && (s[i] == '+' || s[i] == '-')) {
765
20
            *result = PARSE_FAILURE;
766
20
            return 0;
767
20
        }
768
7.08k
    }
769
1.62M
    if (UNLIKELY(i > j)) {
770
1.40k
        *result = PARSE_FAILURE;
771
1.40k
        return 0;
772
1.40k
    }
773
774
    // Use double here to not lose precision while accumulating the result
775
1.62M
    double val = 0;
776
1.62M
    auto res = fast_float::from_chars(s + i, s + j + 1, val);
777
778
1.62M
    if (res.ptr == s + j + 1) {
779
1.51M
        *result = PARSE_SUCCESS;
780
1.51M
        return val;
781
1.51M
    } else {
782
105k
        *result = PARSE_FAILURE;
783
105k
    }
784
105k
    return 0;
785
1.62M
}
_ZN5doris12StringParser24string_to_float_internalIdEET_PKciPNS0_11ParseResultE
Line
Count
Source
735
941k
T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) {
736
941k
    int i = 0;
737
    // skip leading spaces
738
941k
    for (; i < len; ++i) {
739
940k
        if (!is_whitespace_ascii(s[i])) {
740
940k
            break;
741
940k
        }
742
940k
    }
743
744
    // skip back spaces
745
941k
    int j = len - 1;
746
941k
    for (; j >= i; j--) {
747
940k
        if (!is_whitespace_ascii(s[j])) {
748
940k
            break;
749
940k
        }
750
940k
    }
751
752
    // skip leading '+', from_chars can handle '-'
753
941k
    if (i < len && s[i] == '+') {
754
3.54k
        i++;
755
        // ++ or +- are not valid, but the first + is already skipped,
756
        // if don't check here, from_chars will succeed.
757
        //
758
        // New version of fast_float supports a new flag called 'chars_format::allow_leading_plus'
759
        // which may avoid this extra check here.
760
        // e.g.:
761
        // fast_float::chars_format format =
762
        //         fast_float::chars_format::general | fast_float::chars_format::allow_leading_plus;
763
        // auto res = fast_float::from_chars(s + i, s + j + 1, val, format);
764
3.54k
        if (i < len && (s[i] == '+' || s[i] == '-')) {
765
10
            *result = PARSE_FAILURE;
766
10
            return 0;
767
10
        }
768
3.54k
    }
769
941k
    if (UNLIKELY(i > j)) {
770
1.39k
        *result = PARSE_FAILURE;
771
1.39k
        return 0;
772
1.39k
    }
773
774
    // Use double here to not lose precision while accumulating the result
775
940k
    double val = 0;
776
940k
    auto res = fast_float::from_chars(s + i, s + j + 1, val);
777
778
940k
    if (res.ptr == s + j + 1) {
779
834k
        *result = PARSE_SUCCESS;
780
834k
        return val;
781
834k
    } else {
782
105k
        *result = PARSE_FAILURE;
783
105k
    }
784
105k
    return 0;
785
940k
}
_ZN5doris12StringParser24string_to_float_internalIfEET_PKciPNS0_11ParseResultE
Line
Count
Source
735
682k
T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) {
736
682k
    int i = 0;
737
    // skip leading spaces
738
682k
    for (; i < len; ++i) {
739
682k
        if (!is_whitespace_ascii(s[i])) {
740
682k
            break;
741
682k
        }
742
682k
    }
743
744
    // skip back spaces
745
682k
    int j = len - 1;
746
682k
    for (; j >= i; j--) {
747
682k
        if (!is_whitespace_ascii(s[j])) {
748
681k
            break;
749
681k
        }
750
682k
    }
751
752
    // skip leading '+', from_chars can handle '-'
753
682k
    if (i < len && s[i] == '+') {
754
3.54k
        i++;
755
        // ++ or +- are not valid, but the first + is already skipped,
756
        // if don't check here, from_chars will succeed.
757
        //
758
        // New version of fast_float supports a new flag called 'chars_format::allow_leading_plus'
759
        // which may avoid this extra check here.
760
        // e.g.:
761
        // fast_float::chars_format format =
762
        //         fast_float::chars_format::general | fast_float::chars_format::allow_leading_plus;
763
        // auto res = fast_float::from_chars(s + i, s + j + 1, val, format);
764
3.54k
        if (i < len && (s[i] == '+' || s[i] == '-')) {
765
10
            *result = PARSE_FAILURE;
766
10
            return 0;
767
10
        }
768
3.54k
    }
769
682k
    if (UNLIKELY(i > j)) {
770
14
        *result = PARSE_FAILURE;
771
14
        return 0;
772
14
    }
773
774
    // Use double here to not lose precision while accumulating the result
775
682k
    double val = 0;
776
682k
    auto res = fast_float::from_chars(s + i, s + j + 1, val);
777
778
682k
    if (res.ptr == s + j + 1) {
779
681k
        *result = PARSE_SUCCESS;
780
681k
        return val;
781
681k
    } else {
782
716
        *result = PARSE_FAILURE;
783
716
    }
784
716
    return 0;
785
682k
}
786
787
inline bool StringParser::string_to_bool_internal(const char* __restrict s, int len,
788
627k
                                                  ParseResult* result) {
789
627k
    *result = PARSE_SUCCESS;
790
791
627k
    if (len == 1) {
792
618k
        if (s[0] == '1' || s[0] == 't' || s[0] == 'T') {
793
288k
            return true;
794
288k
        }
795
329k
        if (s[0] == '0' || s[0] == 'f' || s[0] == 'F') {
796
329k
            return false;
797
329k
        }
798
480
        *result = PARSE_FAILURE;
799
480
        return false;
800
329k
    }
801
802
9.83k
    if (len == 2) {
803
975
        if ((s[0] == 'o' || s[0] == 'O') && (s[1] == 'n' || s[1] == 'N')) {
804
10
            return true;
805
10
        }
806
965
        if ((s[0] == 'n' || s[0] == 'N') && (s[1] == 'o' || s[1] == 'O')) {
807
9
            return false;
808
9
        }
809
965
    }
810
811
9.81k
    if (len == 3) {
812
42
        if ((s[0] == 'y' || s[0] == 'Y') && (s[1] == 'e' || s[1] == 'E') &&
813
42
            (s[2] == 's' || s[2] == 'S')) {
814
10
            return true;
815
10
        }
816
32
        if ((s[0] == 'o' || s[0] == 'O') && (s[1] == 'f' || s[1] == 'F') &&
817
32
            (s[2] == 'f' || s[2] == 'F')) {
818
9
            return false;
819
9
        }
820
32
    }
821
822
9.79k
    if (len == 4 && (s[0] == 't' || s[0] == 'T') && (s[1] == 'r' || s[1] == 'R') &&
823
9.79k
        (s[2] == 'u' || s[2] == 'U') && (s[3] == 'e' || s[3] == 'E')) {
824
4.25k
        return true;
825
4.25k
    }
826
827
5.54k
    if (len == 5 && (s[0] == 'f' || s[0] == 'F') && (s[1] == 'a' || s[1] == 'A') &&
828
5.54k
        (s[2] == 'l' || s[2] == 'L') && (s[3] == 's' || s[3] == 'S') &&
829
5.54k
        (s[4] == 'e' || s[4] == 'E')) {
830
4.16k
        return false;
831
4.16k
    }
832
833
    // No valid boolean value found
834
1.37k
    *result = PARSE_FAILURE;
835
1.37k
    return false;
836
5.54k
}
837
#include "common/compile_check_avoid_end.h"
838
} // end namespace doris