Coverage Report

Created: 2026-06-12 06:08

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