Coverage Report

Created: 2025-06-06 13:44

/root/doris/be/src/util/string_parser.hpp
Line
Count
Source (jump to first uncovered line)
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
27
#include <cstdlib>
28
// IWYU pragma: no_include <bits/std_abs.h>
29
#include <cmath> // IWYU pragma: keep
30
#include <cstdint>
31
#include <limits>
32
#include <map>
33
#include <string>
34
#include <system_error>
35
#include <type_traits>
36
#include <utility>
37
38
#include "common/compiler_util.h" // IWYU pragma: keep
39
#include "common/status.h"
40
#include "runtime/large_int_value.h"
41
#include "runtime/primitive_type.h"
42
#include "vec/common/int_exp.h"
43
#include "vec/common/string_utils/string_utils.h"
44
#include "vec/core/extended_types.h"
45
#include "vec/core/wide_integer.h"
46
#include "vec/data_types/data_type_decimal.h"
47
#include "vec/data_types/number_traits.h"
48
49
namespace doris {
50
namespace vectorized {
51
template <DecimalNativeTypeConcept T>
52
struct Decimal;
53
} // namespace vectorized
54
55
// Utility functions for doing atoi/atof on non-null terminated strings.  On micro benchmarks,
56
// this is significantly faster than libc (atoi/strtol and atof/strtod).
57
//
58
// Strings with leading and trailing whitespaces are accepted.
59
// Branching is heavily optimized for the non-whitespace successful case.
60
// All the StringTo* functions first parse the input string assuming it has no leading whitespace.
61
// If that first attempt was unsuccessful, these functions retry the parsing after removing
62
// whitespace. Therefore, strings with whitespace take a perf hit on branch mis-prediction.
63
//
64
// For overflows, we are following the mysql behavior, to cap values at the max/min value for that
65
// data type.  This is different from hive, which returns NULL for overflow slots for int types
66
// and inf/-inf for float types.
67
//
68
// Things we tried that did not work:
69
//  - lookup table for converting character to digit
70
// Improvements (TODO):
71
//  - Validate input using _sidd_compare_ranges
72
//  - Since we know the length, we can parallelize this: i.e. result = 100*s[0] + 10*s[1] + s[2]
73
class StringParser {
74
public:
75
    enum ParseResult { PARSE_SUCCESS = 0, PARSE_FAILURE, PARSE_OVERFLOW, PARSE_UNDERFLOW };
76
77
    template <typename T>
78
631k
    static T numeric_limits(bool negative) {
79
631k
        if constexpr (std::is_same_v<T, __int128>) {
80
583k
            return negative ? MIN_INT128 : MAX_INT128;
81
583k
        } else {
82
583k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
583k
        }
84
631k
    }
_ZN5doris12StringParser14numeric_limitsIaEET_b
Line
Count
Source
78
328k
    static T numeric_limits(bool negative) {
79
328k
        if constexpr (std::is_same_v<T, __int128>) {
80
328k
            return negative ? MIN_INT128 : MAX_INT128;
81
328k
        } else {
82
328k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
328k
        }
84
328k
    }
_ZN5doris12StringParser14numeric_limitsInEET_b
Line
Count
Source
78
47.7k
    static T numeric_limits(bool negative) {
79
47.7k
        if constexpr (std::is_same_v<T, __int128>) {
80
47.7k
            return negative ? MIN_INT128 : MAX_INT128;
81
47.7k
        } else {
82
47.7k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
47.7k
        }
84
47.7k
    }
_ZN5doris12StringParser14numeric_limitsIlEET_b
Line
Count
Source
78
89.0k
    static T numeric_limits(bool negative) {
79
89.0k
        if constexpr (std::is_same_v<T, __int128>) {
80
89.0k
            return negative ? MIN_INT128 : MAX_INT128;
81
89.0k
        } else {
82
89.0k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
89.0k
        }
84
89.0k
    }
_ZN5doris12StringParser14numeric_limitsIsEET_b
Line
Count
Source
78
77.7k
    static T numeric_limits(bool negative) {
79
77.7k
        if constexpr (std::is_same_v<T, __int128>) {
80
77.7k
            return negative ? MIN_INT128 : MAX_INT128;
81
77.7k
        } else {
82
77.7k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
77.7k
        }
84
77.7k
    }
_ZN5doris12StringParser14numeric_limitsIiEET_b
Line
Count
Source
78
68.3k
    static T numeric_limits(bool negative) {
79
68.3k
        if constexpr (std::is_same_v<T, __int128>) {
80
68.3k
            return negative ? MIN_INT128 : MAX_INT128;
81
68.3k
        } else {
82
68.3k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
68.3k
        }
84
68.3k
    }
_ZN5doris12StringParser14numeric_limitsIhEET_b
Line
Count
Source
78
19.6k
    static T numeric_limits(bool negative) {
79
19.6k
        if constexpr (std::is_same_v<T, __int128>) {
80
19.6k
            return negative ? MIN_INT128 : MAX_INT128;
81
19.6k
        } else {
82
19.6k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
19.6k
        }
84
19.6k
    }
Unexecuted instantiation: _ZN5doris12StringParser14numeric_limitsIjEET_b
_ZN5doris12StringParser14numeric_limitsImEET_b
Line
Count
Source
78
21
    static T numeric_limits(bool negative) {
79
21
        if constexpr (std::is_same_v<T, __int128>) {
80
21
            return negative ? MIN_INT128 : MAX_INT128;
81
21
        } else {
82
21
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
21
        }
84
21
    }
_ZN5doris12StringParser14numeric_limitsIoEET_b
Line
Count
Source
78
4
    static T numeric_limits(bool negative) {
79
4
        if constexpr (std::is_same_v<T, __int128>) {
80
4
            return negative ? MIN_INT128 : MAX_INT128;
81
4
        } else {
82
4
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
4
        }
84
4
    }
_ZN5doris12StringParser14numeric_limitsIN4wide7integerILm256EiEEEET_b
Line
Count
Source
78
4
    static T numeric_limits(bool negative) {
79
4
        if constexpr (std::is_same_v<T, __int128>) {
80
4
            return negative ? MIN_INT128 : MAX_INT128;
81
4
        } else {
82
4
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
4
        }
84
4
    }
85
86
    template <typename T>
87
548k
    static T get_scale_multiplier(int scale) {
88
548k
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
89
548k
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
90
548k
                      "You can only instantiate as int32_t, int64_t, __int128.");
91
548k
        if constexpr (std::is_same_v<T, int32_t>) {
92
432k
            return common::exp10_i32(scale);
93
432k
        } else if constexpr (std::is_same_v<T, int64_t>) {
94
324k
            return common::exp10_i64(scale);
95
324k
        } else if constexpr (std::is_same_v<T, __int128>) {
96
216k
            return common::exp10_i128(scale);
97
216k
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
98
216k
            return common::exp10_i256(scale);
99
216k
        }
100
548k
    }
_ZN5doris12StringParser20get_scale_multiplierIiEET_i
Line
Count
Source
87
116k
    static T get_scale_multiplier(int scale) {
88
116k
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
89
116k
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
90
116k
                      "You can only instantiate as int32_t, int64_t, __int128.");
91
116k
        if constexpr (std::is_same_v<T, int32_t>) {
92
116k
            return common::exp10_i32(scale);
93
116k
        } else if constexpr (std::is_same_v<T, int64_t>) {
94
116k
            return common::exp10_i64(scale);
95
116k
        } else if constexpr (std::is_same_v<T, __int128>) {
96
116k
            return common::exp10_i128(scale);
97
116k
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
98
116k
            return common::exp10_i256(scale);
99
116k
        }
100
116k
    }
_ZN5doris12StringParser20get_scale_multiplierIlEET_i
Line
Count
Source
87
108k
    static T get_scale_multiplier(int scale) {
88
108k
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
89
108k
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
90
108k
                      "You can only instantiate as int32_t, int64_t, __int128.");
91
108k
        if constexpr (std::is_same_v<T, int32_t>) {
92
108k
            return common::exp10_i32(scale);
93
108k
        } else if constexpr (std::is_same_v<T, int64_t>) {
94
108k
            return common::exp10_i64(scale);
95
108k
        } else if constexpr (std::is_same_v<T, __int128>) {
96
108k
            return common::exp10_i128(scale);
97
108k
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
98
108k
            return common::exp10_i256(scale);
99
108k
        }
100
108k
    }
_ZN5doris12StringParser20get_scale_multiplierInEET_i
Line
Count
Source
87
107k
    static T get_scale_multiplier(int scale) {
88
107k
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
89
107k
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
90
107k
                      "You can only instantiate as int32_t, int64_t, __int128.");
91
107k
        if constexpr (std::is_same_v<T, int32_t>) {
92
107k
            return common::exp10_i32(scale);
93
107k
        } else if constexpr (std::is_same_v<T, int64_t>) {
94
107k
            return common::exp10_i64(scale);
95
107k
        } else if constexpr (std::is_same_v<T, __int128>) {
96
107k
            return common::exp10_i128(scale);
97
107k
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
98
107k
            return common::exp10_i256(scale);
99
107k
        }
100
107k
    }
_ZN5doris12StringParser20get_scale_multiplierIN4wide7integerILm256EiEEEET_i
Line
Count
Source
87
216k
    static T get_scale_multiplier(int scale) {
88
216k
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
89
216k
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
90
216k
                      "You can only instantiate as int32_t, int64_t, __int128.");
91
216k
        if constexpr (std::is_same_v<T, int32_t>) {
92
216k
            return common::exp10_i32(scale);
93
216k
        } else if constexpr (std::is_same_v<T, int64_t>) {
94
216k
            return common::exp10_i64(scale);
95
216k
        } else if constexpr (std::is_same_v<T, __int128>) {
96
216k
            return common::exp10_i128(scale);
97
216k
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
98
216k
            return common::exp10_i256(scale);
99
216k
        }
100
216k
    }
101
102
    // This is considerably faster than glibc's implementation (25x).
103
    // In the case of overflow, the max/min value for the data type will be returned.
104
    // Assumes s represents a decimal number.
105
    template <typename T>
106
370k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
370k
        T ans = string_to_int_internal<T>(s, len, result);
108
370k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
312k
            return ans;
110
312k
        }
111
112
57.6k
        int i = skip_leading_whitespace(s, len);
113
57.6k
        return string_to_int_internal<T>(s + i, len - i, result);
114
370k
    }
_ZN5doris12StringParser13string_to_intInEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
46.5k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
46.5k
        T ans = string_to_int_internal<T>(s, len, result);
108
46.5k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
45.2k
            return ans;
110
45.2k
        }
111
112
1.23k
        int i = skip_leading_whitespace(s, len);
113
1.23k
        return string_to_int_internal<T>(s + i, len - i, result);
114
46.5k
    }
_ZN5doris12StringParser13string_to_intIlEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
83.4k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
83.4k
        T ans = string_to_int_internal<T>(s, len, result);
108
83.4k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
78.9k
            return ans;
110
78.9k
        }
111
112
4.52k
        int i = skip_leading_whitespace(s, len);
113
4.52k
        return string_to_int_internal<T>(s + i, len - i, result);
114
83.4k
    }
_ZN5doris12StringParser13string_to_intIaEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
97.4k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
97.4k
        T ans = string_to_int_internal<T>(s, len, result);
108
97.4k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
68.2k
            return ans;
110
68.2k
        }
111
112
29.2k
        int i = skip_leading_whitespace(s, len);
113
29.2k
        return string_to_int_internal<T>(s + i, len - i, result);
114
97.4k
    }
_ZN5doris12StringParser13string_to_intIsEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
68.3k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
68.3k
        T ans = string_to_int_internal<T>(s, len, result);
108
68.3k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
59.9k
            return ans;
110
59.9k
        }
111
112
8.33k
        int i = skip_leading_whitespace(s, len);
113
8.33k
        return string_to_int_internal<T>(s + i, len - i, result);
114
68.3k
    }
_ZN5doris12StringParser13string_to_intIiEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
63.0k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
63.0k
        T ans = string_to_int_internal<T>(s, len, result);
108
63.0k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
56.6k
            return ans;
110
56.6k
        }
111
112
6.40k
        int i = skip_leading_whitespace(s, len);
113
6.40k
        return string_to_int_internal<T>(s + i, len - i, result);
114
63.0k
    }
_ZN5doris12StringParser13string_to_intIhEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
11.7k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
11.7k
        T ans = string_to_int_internal<T>(s, len, result);
108
11.7k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
3.83k
            return ans;
110
3.83k
        }
111
112
7.90k
        int i = skip_leading_whitespace(s, len);
113
7.90k
        return string_to_int_internal<T>(s + i, len - i, result);
114
11.7k
    }
Unexecuted instantiation: _ZN5doris12StringParser13string_to_intIjEET_PKcmPNS0_11ParseResultE
_ZN5doris12StringParser13string_to_intImEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
20
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
20
        T ans = string_to_int_internal<T>(s, len, result);
108
20
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
20
            return ans;
110
20
        }
111
112
0
        int i = skip_leading_whitespace(s, len);
113
0
        return string_to_int_internal<T>(s + i, len - i, result);
114
20
    }
_ZN5doris12StringParser13string_to_intIoEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
4
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
4
        T ans = string_to_int_internal<T>(s, len, result);
108
4
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
4
            return ans;
110
4
        }
111
112
0
        int i = skip_leading_whitespace(s, len);
113
0
        return string_to_int_internal<T>(s + i, len - i, result);
114
4
    }
_ZN5doris12StringParser13string_to_intIN4wide7integerILm256EiEEEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
4
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
4
        T ans = string_to_int_internal<T>(s, len, result);
108
4
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
4
            return ans;
110
4
        }
111
112
0
        int i = skip_leading_whitespace(s, len);
113
0
        return string_to_int_internal<T>(s + i, len - i, result);
114
4
    }
115
116
    // This is considerably faster than glibc's implementation.
117
    // In the case of overflow, the max/min value for the data type will be returned.
118
    // Assumes s represents a decimal number.
119
    template <typename T>
120
1.57k
    static inline T string_to_unsigned_int(const char* __restrict s, int len, ParseResult* result) {
121
1.57k
        T ans = string_to_unsigned_int_internal<T>(s, len, result);
122
1.57k
        if (LIKELY(*result == PARSE_SUCCESS)) {
123
276
            return ans;
124
276
        }
125
126
1.29k
        int i = skip_leading_whitespace(s, len);
127
1.29k
        return string_to_unsigned_int_internal<T>(s + i, len - i, result);
128
1.57k
    }
_ZN5doris12StringParser22string_to_unsigned_intImEET_PKciPNS0_11ParseResultE
Line
Count
Source
120
541
    static inline T string_to_unsigned_int(const char* __restrict s, int len, ParseResult* result) {
121
541
        T ans = string_to_unsigned_int_internal<T>(s, len, result);
122
541
        if (LIKELY(*result == PARSE_SUCCESS)) {
123
213
            return ans;
124
213
        }
125
126
328
        int i = skip_leading_whitespace(s, len);
127
328
        return string_to_unsigned_int_internal<T>(s + i, len - i, result);
128
541
    }
_ZN5doris12StringParser22string_to_unsigned_intIhEET_PKciPNS0_11ParseResultE
Line
Count
Source
120
343
    static inline T string_to_unsigned_int(const char* __restrict s, int len, ParseResult* result) {
121
343
        T ans = string_to_unsigned_int_internal<T>(s, len, result);
122
343
        if (LIKELY(*result == PARSE_SUCCESS)) {
123
21
            return ans;
124
21
        }
125
126
322
        int i = skip_leading_whitespace(s, len);
127
322
        return string_to_unsigned_int_internal<T>(s + i, len - i, result);
128
343
    }
_ZN5doris12StringParser22string_to_unsigned_intItEET_PKciPNS0_11ParseResultE
Line
Count
Source
120
343
    static inline T string_to_unsigned_int(const char* __restrict s, int len, ParseResult* result) {
121
343
        T ans = string_to_unsigned_int_internal<T>(s, len, result);
122
343
        if (LIKELY(*result == PARSE_SUCCESS)) {
123
21
            return ans;
124
21
        }
125
126
322
        int i = skip_leading_whitespace(s, len);
127
322
        return string_to_unsigned_int_internal<T>(s + i, len - i, result);
128
343
    }
_ZN5doris12StringParser22string_to_unsigned_intIjEET_PKciPNS0_11ParseResultE
Line
Count
Source
120
343
    static inline T string_to_unsigned_int(const char* __restrict s, int len, ParseResult* result) {
121
343
        T ans = string_to_unsigned_int_internal<T>(s, len, result);
122
343
        if (LIKELY(*result == PARSE_SUCCESS)) {
123
21
            return ans;
124
21
        }
125
126
322
        int i = skip_leading_whitespace(s, len);
127
322
        return string_to_unsigned_int_internal<T>(s + i, len - i, result);
128
343
    }
129
130
    // Convert a string s representing a number in given base into a decimal number.
131
    template <typename T>
132
    static inline T string_to_int(const char* __restrict s, int64_t len, int base,
133
27.8k
                                  ParseResult* result) {
134
27.8k
        T ans = string_to_int_internal<T>(s, len, base, result);
135
27.8k
        if (LIKELY(*result == PARSE_SUCCESS)) {
136
2.06k
            return ans;
137
2.06k
        }
138
139
25.7k
        int i = skip_leading_whitespace(s, len);
140
25.7k
        return string_to_int_internal<T>(s + i, len - i, base, result);
141
27.8k
    }
_ZN5doris12StringParser13string_to_intIaEET_PKcliPNS0_11ParseResultE
Line
Count
Source
133
26.4k
                                  ParseResult* result) {
134
26.4k
        T ans = string_to_int_internal<T>(s, len, base, result);
135
26.4k
        if (LIKELY(*result == PARSE_SUCCESS)) {
136
1.91k
            return ans;
137
1.91k
        }
138
139
24.5k
        int i = skip_leading_whitespace(s, len);
140
24.5k
        return string_to_int_internal<T>(s + i, len - i, base, result);
141
26.4k
    }
_ZN5doris12StringParser13string_to_intIsEET_PKcliPNS0_11ParseResultE
Line
Count
Source
133
490
                                  ParseResult* result) {
134
490
        T ans = string_to_int_internal<T>(s, len, base, result);
135
490
        if (LIKELY(*result == PARSE_SUCCESS)) {
136
56
            return ans;
137
56
        }
138
139
434
        int i = skip_leading_whitespace(s, len);
140
434
        return string_to_int_internal<T>(s + i, len - i, base, result);
141
490
    }
_ZN5doris12StringParser13string_to_intIiEET_PKcliPNS0_11ParseResultE
Line
Count
Source
133
441
                                  ParseResult* result) {
134
441
        T ans = string_to_int_internal<T>(s, len, base, result);
135
441
        if (LIKELY(*result == PARSE_SUCCESS)) {
136
49
            return ans;
137
49
        }
138
139
392
        int i = skip_leading_whitespace(s, len);
140
392
        return string_to_int_internal<T>(s + i, len - i, base, result);
141
441
    }
_ZN5doris12StringParser13string_to_intIlEET_PKcliPNS0_11ParseResultE
Line
Count
Source
133
441
                                  ParseResult* result) {
134
441
        T ans = string_to_int_internal<T>(s, len, base, result);
135
441
        if (LIKELY(*result == PARSE_SUCCESS)) {
136
49
            return ans;
137
49
        }
138
139
392
        int i = skip_leading_whitespace(s, len);
140
392
        return string_to_int_internal<T>(s + i, len - i, base, result);
141
441
    }
_ZN5doris12StringParser13string_to_intImEET_PKcliPNS0_11ParseResultE
Line
Count
Source
133
1
                                  ParseResult* result) {
134
1
        T ans = string_to_int_internal<T>(s, len, base, result);
135
1
        if (LIKELY(*result == PARSE_SUCCESS)) {
136
1
            return ans;
137
1
        }
138
139
0
        int i = skip_leading_whitespace(s, len);
140
0
        return string_to_int_internal<T>(s + i, len - i, base, result);
141
1
    }
142
143
    template <typename T>
144
136k
    static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) {
145
136k
        return string_to_float_internal<T>(s, len, result);
146
136k
    }
_ZN5doris12StringParser15string_to_floatIdEET_PKcmPNS0_11ParseResultE
Line
Count
Source
144
73.0k
    static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) {
145
73.0k
        return string_to_float_internal<T>(s, len, result);
146
73.0k
    }
_ZN5doris12StringParser15string_to_floatIfEET_PKcmPNS0_11ParseResultE
Line
Count
Source
144
63.1k
    static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) {
145
63.1k
        return string_to_float_internal<T>(s, len, result);
146
63.1k
    }
147
148
    // Parses a string for 'true' or 'false', case insensitive.
149
8.19k
    static inline bool string_to_bool(const char* __restrict s, int len, ParseResult* result) {
150
8.19k
        bool ans = string_to_bool_internal(s, len, result);
151
8.19k
        if (LIKELY(*result == PARSE_SUCCESS)) {
152
7.16k
            return ans;
153
7.16k
        }
154
155
1.03k
        int i = skip_leading_whitespace(s, len);
156
1.03k
        return string_to_bool_internal(s + i, len - i, result);
157
8.19k
    }
158
159
    template <PrimitiveType P>
160
    static inline typename PrimitiveTypeTraits<P>::CppType::NativeType string_to_decimal(
161
            const char* __restrict s, int len, int type_precision, int type_scale,
162
            ParseResult* result);
163
164
    template <typename T>
165
    static Status split_string_to_map(const std::string& base, const T element_separator,
166
                                      const T key_value_separator,
167
                                      std::map<std::string, std::string>* result) {
168
        int key_pos = 0;
169
        int key_end;
170
        int val_pos;
171
        int val_end;
172
173
        while ((key_end = base.find(key_value_separator, key_pos)) != std::string::npos) {
174
            if ((val_pos = base.find_first_not_of(key_value_separator, key_end)) ==
175
                std::string::npos) {
176
                break;
177
            }
178
            if ((val_end = base.find(element_separator, val_pos)) == std::string::npos) {
179
                val_end = base.size();
180
            }
181
            result->insert(std::make_pair(base.substr(key_pos, key_end - key_pos),
182
                                          base.substr(val_pos, val_end - val_pos)));
183
            key_pos = val_end;
184
            if (key_pos != std::string::npos) {
185
                ++key_pos;
186
            }
187
        }
188
189
        return Status::OK();
190
    }
191
192
private:
193
    // This is considerably faster than glibc's implementation.
194
    // In the case of overflow, the max/min value for the data type will be returned.
195
    // Assumes s represents a decimal number.
196
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
197
    template <typename T>
198
    static inline T string_to_int_internal(const char* __restrict s, int len, ParseResult* result);
199
200
    // This is considerably faster than glibc's implementation.
201
    // In the case of overflow, the max/min value for the data type will be returned.
202
    // Assumes s represents a decimal number.
203
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
204
    template <typename T>
205
    static inline T string_to_unsigned_int_internal(const char* __restrict s, int len,
206
                                                    ParseResult* result);
207
208
    // Convert a string s representing a number in given base into a decimal number.
209
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
210
    template <typename T>
211
    static inline T string_to_int_internal(const char* __restrict s, int64_t len, int base,
212
                                           ParseResult* result);
213
214
    // Converts an ascii string to an integer of type T assuming it cannot overflow
215
    // and the number is positive.
216
    // Leading whitespace is not allowed. Trailing whitespace will be skipped.
217
    template <typename T>
218
    static inline T string_to_int_no_overflow(const char* __restrict s, int len,
219
                                              ParseResult* result);
220
221
    // This is considerably faster than glibc's implementation (>100x why???)
222
    // No special case handling needs to be done for overflows, the floating point spec
223
    // already does it and will cap the values to -inf/inf
224
    // To avoid inaccurate conversions this function falls back to strtod for
225
    // scientific notation.
226
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
227
    // TODO: Investigate using intrinsics to speed up the slow strtod path.
228
    template <typename T>
229
    static inline T string_to_float_internal(const char* __restrict s, int len,
230
                                             ParseResult* result);
231
232
    // parses a string for 'true' or 'false', case insensitive
233
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
234
    static inline bool string_to_bool_internal(const char* __restrict s, int len,
235
                                               ParseResult* result);
236
237
    // Returns true if s only contains whitespace.
238
36.7k
    static inline bool is_all_whitespace(const char* __restrict s, int len) {
239
118k
        for (int i = 0; i < len; ++i) {
240
83.4k
            if (!LIKELY(is_whitespace(s[i]))) {
241
1.96k
                return false;
242
1.96k
            }
243
83.4k
        }
244
34.8k
        return true;
245
36.7k
    }
246
247
    // For strings like "3.0", "3.123", and "3.", can parse them as 3.
248
1.68k
    static inline bool is_float_suffix(const char* __restrict s, int len) {
249
1.68k
        return (s[0] == '.' && is_all_digit(s + 1, len - 1));
250
1.68k
    }
251
252
858
    static inline bool is_all_digit(const char* __restrict s, int len) {
253
1.86k
        for (int i = 0; i < len; ++i) {
254
1.01k
            if (!LIKELY(s[i] >= '0' && s[i] <= '9')) {
255
11
                return false;
256
11
            }
257
1.01k
        }
258
847
        return true;
259
858
    }
260
261
    // Returns the position of the first non-whitespace character in s.
262
85.7k
    static inline int skip_leading_whitespace(const char* __restrict s, int len) {
263
85.7k
        int i = 0;
264
246k
        while (i < len && is_whitespace(s[i])) {
265
160k
            ++i;
266
160k
        }
267
85.7k
        return i;
268
85.7k
    }
269
270
    // Our own definition of "isspace" that optimize on the ' ' branch.
271
3.95M
    static inline bool is_whitespace(const char& c) {
272
3.95M
        return LIKELY(c == ' ') ||
273
3.95M
               UNLIKELY(c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r');
274
3.95M
    }
275
276
}; // end of class StringParser
277
278
template <typename T>
279
565k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
565k
    if (UNLIKELY(len <= 0)) {
281
2.29k
        *result = PARSE_FAILURE;
282
2.29k
        return 0;
283
2.29k
    }
284
285
563k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
563k
    UnsignedT val = 0;
287
563k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
563k
    bool negative = false;
289
563k
    int i = 0;
290
563k
    switch (*s) {
291
108k
    case '-':
292
108k
        negative = true;
293
108k
        max_val += 1;
294
108k
        [[fallthrough]];
295
181k
    case '+':
296
181k
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
181k
        if (UNLIKELY(len == 1)) {
299
2
            *result = PARSE_FAILURE;
300
2
            return 0;
301
2
        }
302
563k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
563k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
387k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
387k
        return static_cast<T>(negative ? -val : val);
308
387k
    }
309
310
176k
    const T max_div_10 = max_val / 10;
311
176k
    const T max_mod_10 = max_val % 10;
312
313
176k
    int first = i;
314
1.64M
    for (; i < len; ++i) {
315
1.57M
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
1.51M
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
1.51M
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
45.9k
                *result = PARSE_OVERFLOW;
320
45.9k
                return negative ? -max_val : max_val;
321
45.9k
            }
322
1.47M
            val = val * 10 + digit;
323
1.47M
        } else {
324
55.9k
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
55.9k
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
42.9k
                *result = PARSE_FAILURE;
329
42.9k
                return 0;
330
42.9k
            }
331
            // Returning here is slightly faster than breaking the loop.
332
13.0k
            *result = PARSE_SUCCESS;
333
13.0k
            return static_cast<T>(negative ? -val : val);
334
55.9k
        }
335
1.57M
    }
336
74.1k
    *result = PARSE_SUCCESS;
337
74.1k
    return static_cast<T>(negative ? -val : val);
338
176k
}
_ZN5doris12StringParser22string_to_int_internalIaEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
264k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
264k
    if (UNLIKELY(len <= 0)) {
281
206
        *result = PARSE_FAILURE;
282
206
        return 0;
283
206
    }
284
285
264k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
264k
    UnsignedT val = 0;
287
264k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
264k
    bool negative = false;
289
264k
    int i = 0;
290
264k
    switch (*s) {
291
28.3k
    case '-':
292
28.3k
        negative = true;
293
28.3k
        max_val += 1;
294
28.3k
        [[fallthrough]];
295
99.3k
    case '+':
296
99.3k
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
99.3k
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
264k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
264k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
199k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
199k
        return static_cast<T>(negative ? -val : val);
308
199k
    }
309
310
64.2k
    const T max_div_10 = max_val / 10;
311
64.2k
    const T max_mod_10 = max_val % 10;
312
313
64.2k
    int first = i;
314
154k
    for (; i < len; ++i) {
315
147k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
111k
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
111k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
20.9k
                *result = PARSE_OVERFLOW;
320
20.9k
                return negative ? -max_val : max_val;
321
20.9k
            }
322
90.4k
            val = val * 10 + digit;
323
90.4k
        } else {
324
36.0k
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
36.0k
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
24.5k
                *result = PARSE_FAILURE;
329
24.5k
                return 0;
330
24.5k
            }
331
            // Returning here is slightly faster than breaking the loop.
332
11.5k
            *result = PARSE_SUCCESS;
333
11.5k
            return static_cast<T>(negative ? -val : val);
334
36.0k
        }
335
147k
    }
336
7.16k
    *result = PARSE_SUCCESS;
337
7.16k
    return static_cast<T>(negative ? -val : val);
338
64.2k
}
_ZN5doris12StringParser22string_to_int_internalInEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
47.7k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
47.7k
    if (UNLIKELY(len <= 0)) {
281
36
        *result = PARSE_FAILURE;
282
36
        return 0;
283
36
    }
284
285
47.7k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
47.7k
    UnsignedT val = 0;
287
47.7k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
47.7k
    bool negative = false;
289
47.7k
    int i = 0;
290
47.7k
    switch (*s) {
291
3.51k
    case '-':
292
3.51k
        negative = true;
293
3.51k
        max_val += 1;
294
3.51k
        [[fallthrough]];
295
3.99k
    case '+':
296
3.99k
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
3.99k
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
47.7k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
47.7k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
43.4k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
43.4k
        return static_cast<T>(negative ? -val : val);
308
43.4k
    }
309
310
4.31k
    const T max_div_10 = max_val / 10;
311
4.31k
    const T max_mod_10 = max_val % 10;
312
313
4.31k
    int first = i;
314
161k
    for (; i < len; ++i) {
315
157k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
157k
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
157k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
6
                *result = PARSE_OVERFLOW;
320
6
                return negative ? -max_val : max_val;
321
6
            }
322
157k
            val = val * 10 + digit;
323
157k
        } else {
324
522
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
522
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
314
                *result = PARSE_FAILURE;
329
314
                return 0;
330
314
            }
331
            // Returning here is slightly faster than breaking the loop.
332
208
            *result = PARSE_SUCCESS;
333
208
            return static_cast<T>(negative ? -val : val);
334
522
        }
335
157k
    }
336
3.78k
    *result = PARSE_SUCCESS;
337
3.78k
    return static_cast<T>(negative ? -val : val);
338
4.31k
}
_ZN5doris12StringParser22string_to_int_internalIlEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
88.0k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
88.0k
    if (UNLIKELY(len <= 0)) {
281
4
        *result = PARSE_FAILURE;
282
4
        return 0;
283
4
    }
284
285
88.0k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
88.0k
    UnsignedT val = 0;
287
88.0k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
88.0k
    bool negative = false;
289
88.0k
    int i = 0;
290
88.0k
    switch (*s) {
291
51.6k
    case '-':
292
51.6k
        negative = true;
293
51.6k
        max_val += 1;
294
51.6k
        [[fallthrough]];
295
52.2k
    case '+':
296
52.2k
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
52.2k
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
88.0k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
88.0k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
33.6k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
33.6k
        return static_cast<T>(negative ? -val : val);
308
33.6k
    }
309
310
54.3k
    const T max_div_10 = max_val / 10;
311
54.3k
    const T max_mod_10 = max_val % 10;
312
313
54.3k
    int first = i;
314
1.06M
    for (; i < len; ++i) {
315
1.01M
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
1.01M
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
1.01M
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
4.95k
                *result = PARSE_OVERFLOW;
320
4.95k
                return negative ? -max_val : max_val;
321
4.95k
            }
322
1.00M
            val = val * 10 + digit;
323
1.00M
        } else {
324
1.45k
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
1.45k
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
1.15k
                *result = PARSE_FAILURE;
329
1.15k
                return 0;
330
1.15k
            }
331
            // Returning here is slightly faster than breaking the loop.
332
292
            *result = PARSE_SUCCESS;
333
292
            return static_cast<T>(negative ? -val : val);
334
1.45k
        }
335
1.01M
    }
336
47.9k
    *result = PARSE_SUCCESS;
337
47.9k
    return static_cast<T>(negative ? -val : val);
338
54.3k
}
_ZN5doris12StringParser22string_to_int_internalIsEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
76.6k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
76.6k
    if (UNLIKELY(len <= 0)) {
281
0
        *result = PARSE_FAILURE;
282
0
        return 0;
283
0
    }
284
285
76.6k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
76.6k
    UnsignedT val = 0;
287
76.6k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
76.6k
    bool negative = false;
289
76.6k
    int i = 0;
290
76.6k
    switch (*s) {
291
13.1k
    case '-':
292
13.1k
        negative = true;
293
13.1k
        max_val += 1;
294
13.1k
        [[fallthrough]];
295
13.6k
    case '+':
296
13.6k
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
13.6k
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
76.6k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
76.6k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
52.5k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
52.5k
        return static_cast<T>(negative ? -val : val);
308
52.5k
    }
309
310
24.1k
    const T max_div_10 = max_val / 10;
311
24.1k
    const T max_mod_10 = max_val % 10;
312
313
24.1k
    int first = i;
314
125k
    for (; i < len; ++i) {
315
116k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
114k
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
114k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
13.0k
                *result = PARSE_OVERFLOW;
320
13.0k
                return negative ? -max_val : max_val;
321
13.0k
            }
322
101k
            val = val * 10 + digit;
323
101k
        } else {
324
1.91k
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
1.91k
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
1.27k
                *result = PARSE_FAILURE;
329
1.27k
                return 0;
330
1.27k
            }
331
            // Returning here is slightly faster than breaking the loop.
332
638
            *result = PARSE_SUCCESS;
333
638
            return static_cast<T>(negative ? -val : val);
334
1.91k
        }
335
116k
    }
336
9.16k
    *result = PARSE_SUCCESS;
337
9.16k
    return static_cast<T>(negative ? -val : val);
338
24.1k
}
_ZN5doris12StringParser22string_to_int_internalIiEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
69.4k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
69.4k
    if (UNLIKELY(len <= 0)) {
281
2.04k
        *result = PARSE_FAILURE;
282
2.04k
        return 0;
283
2.04k
    }
284
285
67.4k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
67.4k
    UnsignedT val = 0;
287
67.4k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
67.4k
    bool negative = false;
289
67.4k
    int i = 0;
290
67.4k
    switch (*s) {
291
10.4k
    case '-':
292
10.4k
        negative = true;
293
10.4k
        max_val += 1;
294
10.4k
        [[fallthrough]];
295
11.0k
    case '+':
296
11.0k
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
11.0k
        if (UNLIKELY(len == 1)) {
299
2
            *result = PARSE_FAILURE;
300
2
            return 0;
301
2
        }
302
67.4k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
67.4k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
54.1k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
54.1k
        return static_cast<T>(negative ? -val : val);
308
54.1k
    }
309
310
13.2k
    const T max_div_10 = max_val / 10;
311
13.2k
    const T max_mod_10 = max_val % 10;
312
313
13.2k
    int first = i;
314
127k
    for (; i < len; ++i) {
315
121k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
120k
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
120k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
5.54k
                *result = PARSE_OVERFLOW;
320
5.54k
                return negative ? -max_val : max_val;
321
5.54k
            }
322
114k
            val = val * 10 + digit;
323
114k
        } else {
324
1.67k
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
1.67k
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
1.32k
                *result = PARSE_FAILURE;
329
1.32k
                return 0;
330
1.32k
            }
331
            // Returning here is slightly faster than breaking the loop.
332
348
            *result = PARSE_SUCCESS;
333
348
            return static_cast<T>(negative ? -val : val);
334
1.67k
        }
335
121k
    }
336
6.07k
    *result = PARSE_SUCCESS;
337
6.07k
    return static_cast<T>(negative ? -val : val);
338
13.2k
}
_ZN5doris12StringParser22string_to_int_internalIhEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
19.6k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
19.6k
    if (UNLIKELY(len <= 0)) {
281
4
        *result = PARSE_FAILURE;
282
4
        return 0;
283
4
    }
284
285
19.6k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
19.6k
    UnsignedT val = 0;
287
19.6k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
19.6k
    bool negative = false;
289
19.6k
    int i = 0;
290
19.6k
    switch (*s) {
291
848
    case '-':
292
848
        negative = true;
293
848
        max_val += 1;
294
848
        [[fallthrough]];
295
848
    case '+':
296
848
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
848
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
19.6k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
19.6k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
3.84k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
3.84k
        return static_cast<T>(negative ? -val : val);
308
3.84k
    }
309
310
15.7k
    const T max_div_10 = max_val / 10;
311
15.7k
    const T max_mod_10 = max_val % 10;
312
313
15.7k
    int first = i;
314
17.9k
    for (; i < len; ++i) {
315
17.9k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
3.63k
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
3.63k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
1.44k
                *result = PARSE_OVERFLOW;
320
1.44k
                return negative ? -max_val : max_val;
321
1.44k
            }
322
2.19k
            val = val * 10 + digit;
323
14.3k
        } else {
324
14.3k
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
14.3k
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
14.3k
                *result = PARSE_FAILURE;
329
14.3k
                return 0;
330
14.3k
            }
331
            // Returning here is slightly faster than breaking the loop.
332
1
            *result = PARSE_SUCCESS;
333
1
            return static_cast<T>(negative ? -val : val);
334
14.3k
        }
335
17.9k
    }
336
12
    *result = PARSE_SUCCESS;
337
12
    return static_cast<T>(negative ? -val : val);
338
15.7k
}
Unexecuted instantiation: _ZN5doris12StringParser22string_to_int_internalIjEET_PKciPNS0_11ParseResultE
_ZN5doris12StringParser22string_to_int_internalImEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
20
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
20
    if (UNLIKELY(len <= 0)) {
281
0
        *result = PARSE_FAILURE;
282
0
        return 0;
283
0
    }
284
285
20
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
20
    UnsignedT val = 0;
287
20
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
20
    bool negative = false;
289
20
    int i = 0;
290
20
    switch (*s) {
291
0
    case '-':
292
0
        negative = true;
293
0
        max_val += 1;
294
0
        [[fallthrough]];
295
0
    case '+':
296
0
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
0
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
20
    }
303
304
    // This is the fast path where the string cannot overflow.
305
20
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
20
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
20
        return static_cast<T>(negative ? -val : val);
308
20
    }
309
310
0
    const T max_div_10 = max_val / 10;
311
0
    const T max_mod_10 = max_val % 10;
312
313
0
    int first = i;
314
0
    for (; i < len; ++i) {
315
0
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
0
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
0
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
0
                *result = PARSE_OVERFLOW;
320
0
                return negative ? -max_val : max_val;
321
0
            }
322
0
            val = val * 10 + digit;
323
0
        } else {
324
0
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
0
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
0
                *result = PARSE_FAILURE;
329
0
                return 0;
330
0
            }
331
            // Returning here is slightly faster than breaking the loop.
332
0
            *result = PARSE_SUCCESS;
333
0
            return static_cast<T>(negative ? -val : val);
334
0
        }
335
0
    }
336
0
    *result = PARSE_SUCCESS;
337
0
    return static_cast<T>(negative ? -val : val);
338
0
}
_ZN5doris12StringParser22string_to_int_internalIoEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
4
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
4
    if (UNLIKELY(len <= 0)) {
281
0
        *result = PARSE_FAILURE;
282
0
        return 0;
283
0
    }
284
285
4
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
4
    UnsignedT val = 0;
287
4
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
4
    bool negative = false;
289
4
    int i = 0;
290
4
    switch (*s) {
291
0
    case '-':
292
0
        negative = true;
293
0
        max_val += 1;
294
0
        [[fallthrough]];
295
0
    case '+':
296
0
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
0
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
4
    }
303
304
    // This is the fast path where the string cannot overflow.
305
4
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
0
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
0
        return static_cast<T>(negative ? -val : val);
308
0
    }
309
310
4
    const T max_div_10 = max_val / 10;
311
4
    const T max_mod_10 = max_val % 10;
312
313
4
    int first = i;
314
84
    for (; i < len; ++i) {
315
80
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
80
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
80
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
0
                *result = PARSE_OVERFLOW;
320
0
                return negative ? -max_val : max_val;
321
0
            }
322
80
            val = val * 10 + digit;
323
80
        } else {
324
0
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
0
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
0
                *result = PARSE_FAILURE;
329
0
                return 0;
330
0
            }
331
            // Returning here is slightly faster than breaking the loop.
332
0
            *result = PARSE_SUCCESS;
333
0
            return static_cast<T>(negative ? -val : val);
334
0
        }
335
80
    }
336
4
    *result = PARSE_SUCCESS;
337
4
    return static_cast<T>(negative ? -val : val);
338
4
}
_ZN5doris12StringParser22string_to_int_internalIN4wide7integerILm256EiEEEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
4
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
4
    if (UNLIKELY(len <= 0)) {
281
0
        *result = PARSE_FAILURE;
282
0
        return 0;
283
0
    }
284
285
4
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
4
    UnsignedT val = 0;
287
4
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
4
    bool negative = false;
289
4
    int i = 0;
290
4
    switch (*s) {
291
0
    case '-':
292
0
        negative = true;
293
0
        max_val += 1;
294
0
        [[fallthrough]];
295
0
    case '+':
296
0
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
0
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
4
    }
303
304
    // This is the fast path where the string cannot overflow.
305
4
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
4
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
4
        return static_cast<T>(negative ? -val : val);
308
4
    }
309
310
0
    const T max_div_10 = max_val / 10;
311
0
    const T max_mod_10 = max_val % 10;
312
313
0
    int first = i;
314
0
    for (; i < len; ++i) {
315
0
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
0
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
0
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
0
                *result = PARSE_OVERFLOW;
320
0
                return negative ? -max_val : max_val;
321
0
            }
322
0
            val = val * 10 + digit;
323
0
        } else {
324
0
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
0
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
0
                *result = PARSE_FAILURE;
329
0
                return 0;
330
0
            }
331
            // Returning here is slightly faster than breaking the loop.
332
0
            *result = PARSE_SUCCESS;
333
0
            return static_cast<T>(negative ? -val : val);
334
0
        }
335
0
    }
336
0
    *result = PARSE_SUCCESS;
337
0
    return static_cast<T>(negative ? -val : val);
338
0
}
339
340
template <typename T>
341
T StringParser::string_to_unsigned_int_internal(const char* __restrict s, int len,
342
2.86k
                                                ParseResult* result) {
343
2.86k
    if (UNLIKELY(len <= 0)) {
344
0
        *result = PARSE_FAILURE;
345
0
        return 0;
346
0
    }
347
348
2.86k
    T val = 0;
349
2.86k
    T max_val = std::numeric_limits<T>::max();
350
2.86k
    int i = 0;
351
352
2.86k
    typedef typename std::make_signed<T>::type signedT;
353
    // This is the fast path where the string cannot overflow.
354
2.86k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<signedT>())) {
355
1.08k
        val = string_to_int_no_overflow<T>(s + i, len - i, result);
356
1.08k
        return val;
357
1.08k
    }
358
359
1.78k
    const T max_div_10 = max_val / 10;
360
1.78k
    const T max_mod_10 = max_val % 10;
361
362
1.78k
    int first = i;
363
6.54k
    for (; i < len; ++i) {
364
6.49k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
365
4.99k
            T digit = s[i] - '0';
366
            // This is a tricky check to see if adding this digit will cause an overflow.
367
4.99k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
368
224
                *result = PARSE_OVERFLOW;
369
224
                return max_val;
370
224
            }
371
4.76k
            val = val * 10 + digit;
372
4.76k
        } else {
373
1.50k
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
374
                // Reject the string because either the first char was not a digit,
375
                // or the remaining chars are not all whitespace
376
1.13k
                *result = PARSE_FAILURE;
377
1.13k
                return 0;
378
1.13k
            }
379
            // Returning here is slightly faster than breaking the loop.
380
378
            *result = PARSE_SUCCESS;
381
378
            return val;
382
1.50k
        }
383
6.49k
    }
384
49
    *result = PARSE_SUCCESS;
385
49
    return val;
386
1.78k
}
_ZN5doris12StringParser31string_to_unsigned_int_internalImEET_PKciPNS0_11ParseResultE
Line
Count
Source
342
869
                                                ParseResult* result) {
343
869
    if (UNLIKELY(len <= 0)) {
344
0
        *result = PARSE_FAILURE;
345
0
        return 0;
346
0
    }
347
348
869
    T val = 0;
349
869
    T max_val = std::numeric_limits<T>::max();
350
869
    int i = 0;
351
352
869
    typedef typename std::make_signed<T>::type signedT;
353
    // This is the fast path where the string cannot overflow.
354
869
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<signedT>())) {
355
644
        val = string_to_int_no_overflow<T>(s + i, len - i, result);
356
644
        return val;
357
644
    }
358
359
225
    const T max_div_10 = max_val / 10;
360
225
    const T max_mod_10 = max_val % 10;
361
362
225
    int first = i;
363
2.26k
    for (; i < len; ++i) {
364
2.26k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
365
2.10k
            T digit = s[i] - '0';
366
            // This is a tricky check to see if adding this digit will cause an overflow.
367
2.10k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
368
56
                *result = PARSE_OVERFLOW;
369
56
                return max_val;
370
56
            }
371
2.04k
            val = val * 10 + digit;
372
2.04k
        } else {
373
162
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
374
                // Reject the string because either the first char was not a digit,
375
                // or the remaining chars are not all whitespace
376
120
                *result = PARSE_FAILURE;
377
120
                return 0;
378
120
            }
379
            // Returning here is slightly faster than breaking the loop.
380
42
            *result = PARSE_SUCCESS;
381
42
            return val;
382
162
        }
383
2.26k
    }
384
7
    *result = PARSE_SUCCESS;
385
7
    return val;
386
225
}
_ZN5doris12StringParser31string_to_unsigned_int_internalIhEET_PKciPNS0_11ParseResultE
Line
Count
Source
342
665
                                                ParseResult* result) {
343
665
    if (UNLIKELY(len <= 0)) {
344
0
        *result = PARSE_FAILURE;
345
0
        return 0;
346
0
    }
347
348
665
    T val = 0;
349
665
    T max_val = std::numeric_limits<T>::max();
350
665
    int i = 0;
351
352
665
    typedef typename std::make_signed<T>::type signedT;
353
    // This is the fast path where the string cannot overflow.
354
665
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<signedT>())) {
355
16
        val = string_to_int_no_overflow<T>(s + i, len - i, result);
356
16
        return val;
357
16
    }
358
359
649
    const T max_div_10 = max_val / 10;
360
649
    const T max_mod_10 = max_val % 10;
361
362
649
    int first = i;
363
1.20k
    for (; i < len; ++i) {
364
1.18k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
365
609
            T digit = s[i] - '0';
366
            // This is a tricky check to see if adding this digit will cause an overflow.
367
609
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
368
56
                *result = PARSE_OVERFLOW;
369
56
                return max_val;
370
56
            }
371
553
            val = val * 10 + digit;
372
572
        } else {
373
572
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
374
                // Reject the string because either the first char was not a digit,
375
                // or the remaining chars are not all whitespace
376
446
                *result = PARSE_FAILURE;
377
446
                return 0;
378
446
            }
379
            // Returning here is slightly faster than breaking the loop.
380
126
            *result = PARSE_SUCCESS;
381
126
            return val;
382
572
        }
383
1.18k
    }
384
21
    *result = PARSE_SUCCESS;
385
21
    return val;
386
649
}
_ZN5doris12StringParser31string_to_unsigned_int_internalItEET_PKciPNS0_11ParseResultE
Line
Count
Source
342
665
                                                ParseResult* result) {
343
665
    if (UNLIKELY(len <= 0)) {
344
0
        *result = PARSE_FAILURE;
345
0
        return 0;
346
0
    }
347
348
665
    T val = 0;
349
665
    T max_val = std::numeric_limits<T>::max();
350
665
    int i = 0;
351
352
665
    typedef typename std::make_signed<T>::type signedT;
353
    // This is the fast path where the string cannot overflow.
354
665
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<signedT>())) {
355
31
        val = string_to_int_no_overflow<T>(s + i, len - i, result);
356
31
        return val;
357
31
    }
358
359
634
    const T max_div_10 = max_val / 10;
360
634
    const T max_mod_10 = max_val % 10;
361
362
634
    int first = i;
363
1.47k
    for (; i < len; ++i) {
364
1.46k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
365
896
            T digit = s[i] - '0';
366
            // This is a tricky check to see if adding this digit will cause an overflow.
367
896
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
368
56
                *result = PARSE_OVERFLOW;
369
56
                return max_val;
370
56
            }
371
840
            val = val * 10 + digit;
372
840
        } else {
373
564
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
374
                // Reject the string because either the first char was not a digit,
375
                // or the remaining chars are not all whitespace
376
438
                *result = PARSE_FAILURE;
377
438
                return 0;
378
438
            }
379
            // Returning here is slightly faster than breaking the loop.
380
126
            *result = PARSE_SUCCESS;
381
126
            return val;
382
564
        }
383
1.46k
    }
384
14
    *result = PARSE_SUCCESS;
385
14
    return val;
386
634
}
_ZN5doris12StringParser31string_to_unsigned_int_internalIjEET_PKciPNS0_11ParseResultE
Line
Count
Source
342
665
                                                ParseResult* result) {
343
665
    if (UNLIKELY(len <= 0)) {
344
0
        *result = PARSE_FAILURE;
345
0
        return 0;
346
0
    }
347
348
665
    T val = 0;
349
665
    T max_val = std::numeric_limits<T>::max();
350
665
    int i = 0;
351
352
665
    typedef typename std::make_signed<T>::type signedT;
353
    // This is the fast path where the string cannot overflow.
354
665
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<signedT>())) {
355
392
        val = string_to_int_no_overflow<T>(s + i, len - i, result);
356
392
        return val;
357
392
    }
358
359
273
    const T max_div_10 = max_val / 10;
360
273
    const T max_mod_10 = max_val % 10;
361
362
273
    int first = i;
363
1.60k
    for (; i < len; ++i) {
364
1.59k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
365
1.38k
            T digit = s[i] - '0';
366
            // This is a tricky check to see if adding this digit will cause an overflow.
367
1.38k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
368
56
                *result = PARSE_OVERFLOW;
369
56
                return max_val;
370
56
            }
371
1.33k
            val = val * 10 + digit;
372
1.33k
        } else {
373
210
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
374
                // Reject the string because either the first char was not a digit,
375
                // or the remaining chars are not all whitespace
376
126
                *result = PARSE_FAILURE;
377
126
                return 0;
378
126
            }
379
            // Returning here is slightly faster than breaking the loop.
380
84
            *result = PARSE_SUCCESS;
381
84
            return val;
382
210
        }
383
1.59k
    }
384
7
    *result = PARSE_SUCCESS;
385
7
    return val;
386
273
}
387
388
template <typename T>
389
T StringParser::string_to_int_internal(const char* __restrict s, int64_t len, int base,
390
53.6k
                                       ParseResult* result) {
391
53.6k
    typedef typename std::make_unsigned<T>::type UnsignedT;
392
53.6k
    UnsignedT val = 0;
393
53.6k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
394
53.6k
    bool negative = false;
395
53.6k
    if (UNLIKELY(len <= 0)) {
396
0
        *result = PARSE_FAILURE;
397
0
        return 0;
398
0
    }
399
53.6k
    int i = 0;
400
53.6k
    switch (*s) {
401
14.3k
    case '-':
402
14.3k
        negative = true;
403
14.3k
        max_val = StringParser::numeric_limits<T>(false) + 1;
404
14.3k
        [[fallthrough]];
405
14.6k
    case '+':
406
14.6k
        i = 1;
407
53.6k
    }
408
409
53.6k
    const T max_div_base = max_val / base;
410
53.6k
    const T max_mod_base = max_val % base;
411
412
53.6k
    int first = i;
413
120k
    for (; i < len; ++i) {
414
118k
        T digit;
415
118k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
416
81.6k
            digit = s[i] - '0';
417
81.6k
        } else if (s[i] >= 'a' && s[i] <= 'z') {
418
639
            digit = (s[i] - 'a' + 10);
419
36.4k
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
420
98
            digit = (s[i] - 'A' + 10);
421
36.3k
        } else {
422
36.3k
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
423
                // Reject the string because either the first char was not an alpha/digit,
424
                // or the remaining chars are not all whitespace
425
24.0k
                *result = PARSE_FAILURE;
426
24.0k
                return 0;
427
24.0k
            }
428
            // skip trailing whitespace.
429
12.2k
            break;
430
36.3k
        }
431
432
        // Bail, if we encounter a digit that is not available in base.
433
82.4k
        if (digit >= base) {
434
392
            break;
435
392
        }
436
437
        // This is a tricky check to see if adding this digit will cause an overflow.
438
82.0k
        if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) {
439
14.8k
            *result = PARSE_OVERFLOW;
440
14.8k
            return static_cast<T>(negative ? -max_val : max_val);
441
14.8k
        }
442
67.2k
        val = val * base + digit;
443
67.2k
    }
444
14.7k
    *result = PARSE_SUCCESS;
445
14.7k
    return static_cast<T>(negative ? -val : val);
446
53.6k
}
_ZN5doris12StringParser22string_to_int_internalIaEET_PKcliPNS0_11ParseResultE
Line
Count
Source
390
51.0k
                                       ParseResult* result) {
391
51.0k
    typedef typename std::make_unsigned<T>::type UnsignedT;
392
51.0k
    UnsignedT val = 0;
393
51.0k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
394
51.0k
    bool negative = false;
395
51.0k
    if (UNLIKELY(len <= 0)) {
396
0
        *result = PARSE_FAILURE;
397
0
        return 0;
398
0
    }
399
51.0k
    int i = 0;
400
51.0k
    switch (*s) {
401
13.7k
    case '-':
402
13.7k
        negative = true;
403
13.7k
        max_val = StringParser::numeric_limits<T>(false) + 1;
404
13.7k
        [[fallthrough]];
405
13.8k
    case '+':
406
13.8k
        i = 1;
407
51.0k
    }
408
409
51.0k
    const T max_div_base = max_val / base;
410
51.0k
    const T max_mod_base = max_val % base;
411
412
51.0k
    int first = i;
413
108k
    for (; i < len; ++i) {
414
107k
        T digit;
415
107k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
416
72.1k
            digit = s[i] - '0';
417
72.1k
        } else if (s[i] >= 'a' && s[i] <= 'z') {
418
539
            digit = (s[i] - 'a' + 10);
419
34.3k
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
420
98
            digit = (s[i] - 'A' + 10);
421
34.2k
        } else {
422
34.2k
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
423
                // Reject the string because either the first char was not an alpha/digit,
424
                // or the remaining chars are not all whitespace
425
22.8k
                *result = PARSE_FAILURE;
426
22.8k
                return 0;
427
22.8k
            }
428
            // skip trailing whitespace.
429
11.3k
            break;
430
34.2k
        }
431
432
        // Bail, if we encounter a digit that is not available in base.
433
72.7k
        if (digit >= base) {
434
392
            break;
435
392
        }
436
437
        // This is a tricky check to see if adding this digit will cause an overflow.
438
72.4k
        if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) {
439
14.5k
            *result = PARSE_OVERFLOW;
440
14.5k
            return static_cast<T>(negative ? -max_val : max_val);
441
14.5k
        }
442
57.8k
        val = val * base + digit;
443
57.8k
    }
444
13.6k
    *result = PARSE_SUCCESS;
445
13.6k
    return static_cast<T>(negative ? -val : val);
446
51.0k
}
_ZN5doris12StringParser22string_to_int_internalIsEET_PKcliPNS0_11ParseResultE
Line
Count
Source
390
924
                                       ParseResult* result) {
391
924
    typedef typename std::make_unsigned<T>::type UnsignedT;
392
924
    UnsignedT val = 0;
393
924
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
394
924
    bool negative = false;
395
924
    if (UNLIKELY(len <= 0)) {
396
0
        *result = PARSE_FAILURE;
397
0
        return 0;
398
0
    }
399
924
    int i = 0;
400
924
    switch (*s) {
401
203
    case '-':
402
203
        negative = true;
403
203
        max_val = StringParser::numeric_limits<T>(false) + 1;
404
203
        [[fallthrough]];
405
252
    case '+':
406
252
        i = 1;
407
924
    }
408
409
924
    const T max_div_base = max_val / base;
410
924
    const T max_mod_base = max_val % base;
411
412
924
    int first = i;
413
2.59k
    for (; i < len; ++i) {
414
2.54k
        T digit;
415
2.54k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
416
1.68k
            digit = s[i] - '0';
417
1.68k
        } else if (s[i] >= 'a' && s[i] <= 'z') {
418
98
            digit = (s[i] - 'a' + 10);
419
756
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
420
0
            digit = (s[i] - 'A' + 10);
421
756
        } else {
422
756
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
423
                // Reject the string because either the first char was not an alpha/digit,
424
                // or the remaining chars are not all whitespace
425
420
                *result = PARSE_FAILURE;
426
420
                return 0;
427
420
            }
428
            // skip trailing whitespace.
429
336
            break;
430
756
        }
431
432
        // Bail, if we encounter a digit that is not available in base.
433
1.78k
        if (digit >= base) {
434
0
            break;
435
0
        }
436
437
        // This is a tricky check to see if adding this digit will cause an overflow.
438
1.78k
        if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) {
439
112
            *result = PARSE_OVERFLOW;
440
112
            return static_cast<T>(negative ? -max_val : max_val);
441
112
        }
442
1.67k
        val = val * base + digit;
443
1.67k
    }
444
392
    *result = PARSE_SUCCESS;
445
392
    return static_cast<T>(negative ? -val : val);
446
924
}
_ZN5doris12StringParser22string_to_int_internalIiEET_PKcliPNS0_11ParseResultE
Line
Count
Source
390
833
                                       ParseResult* result) {
391
833
    typedef typename std::make_unsigned<T>::type UnsignedT;
392
833
    UnsignedT val = 0;
393
833
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
394
833
    bool negative = false;
395
833
    if (UNLIKELY(len <= 0)) {
396
0
        *result = PARSE_FAILURE;
397
0
        return 0;
398
0
    }
399
833
    int i = 0;
400
833
    switch (*s) {
401
154
    case '-':
402
154
        negative = true;
403
154
        max_val = StringParser::numeric_limits<T>(false) + 1;
404
154
        [[fallthrough]];
405
252
    case '+':
406
252
        i = 1;
407
833
    }
408
409
833
    const T max_div_base = max_val / base;
410
833
    const T max_mod_base = max_val % base;
411
412
833
    int first = i;
413
3.55k
    for (; i < len; ++i) {
414
3.50k
        T digit;
415
3.50k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
416
2.83k
            digit = s[i] - '0';
417
2.83k
        } else if (s[i] >= 'a' && s[i] <= 'z') {
418
0
            digit = (s[i] - 'a' + 10);
419
672
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
420
0
            digit = (s[i] - 'A' + 10);
421
672
        } else {
422
672
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
423
                // Reject the string because either the first char was not an alpha/digit,
424
                // or the remaining chars are not all whitespace
425
378
                *result = PARSE_FAILURE;
426
378
                return 0;
427
378
            }
428
            // skip trailing whitespace.
429
294
            break;
430
672
        }
431
432
        // Bail, if we encounter a digit that is not available in base.
433
2.83k
        if (digit >= base) {
434
0
            break;
435
0
        }
436
437
        // This is a tricky check to see if adding this digit will cause an overflow.
438
2.83k
        if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) {
439
112
            *result = PARSE_OVERFLOW;
440
112
            return static_cast<T>(negative ? -max_val : max_val);
441
112
        }
442
2.72k
        val = val * base + digit;
443
2.72k
    }
444
343
    *result = PARSE_SUCCESS;
445
343
    return static_cast<T>(negative ? -val : val);
446
833
}
_ZN5doris12StringParser22string_to_int_internalIlEET_PKcliPNS0_11ParseResultE
Line
Count
Source
390
833
                                       ParseResult* result) {
391
833
    typedef typename std::make_unsigned<T>::type UnsignedT;
392
833
    UnsignedT val = 0;
393
833
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
394
833
    bool negative = false;
395
833
    if (UNLIKELY(len <= 0)) {
396
0
        *result = PARSE_FAILURE;
397
0
        return 0;
398
0
    }
399
833
    int i = 0;
400
833
    switch (*s) {
401
203
    case '-':
402
203
        negative = true;
403
203
        max_val = StringParser::numeric_limits<T>(false) + 1;
404
203
        [[fallthrough]];
405
252
    case '+':
406
252
        i = 1;
407
833
    }
408
409
833
    const T max_div_base = max_val / base;
410
833
    const T max_mod_base = max_val % base;
411
412
833
    int first = i;
413
5.74k
    for (; i < len; ++i) {
414
5.69k
        T digit;
415
5.69k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
416
5.01k
            digit = s[i] - '0';
417
5.01k
        } else if (s[i] >= 'a' && s[i] <= 'z') {
418
0
            digit = (s[i] - 'a' + 10);
419
672
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
420
0
            digit = (s[i] - 'A' + 10);
421
672
        } else {
422
672
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
423
                // Reject the string because either the first char was not an alpha/digit,
424
                // or the remaining chars are not all whitespace
425
378
                *result = PARSE_FAILURE;
426
378
                return 0;
427
378
            }
428
            // skip trailing whitespace.
429
294
            break;
430
672
        }
431
432
        // Bail, if we encounter a digit that is not available in base.
433
5.01k
        if (digit >= base) {
434
0
            break;
435
0
        }
436
437
        // This is a tricky check to see if adding this digit will cause an overflow.
438
5.01k
        if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) {
439
112
            *result = PARSE_OVERFLOW;
440
112
            return static_cast<T>(negative ? -max_val : max_val);
441
112
        }
442
4.90k
        val = val * base + digit;
443
4.90k
    }
444
343
    *result = PARSE_SUCCESS;
445
343
    return static_cast<T>(negative ? -val : val);
446
833
}
_ZN5doris12StringParser22string_to_int_internalImEET_PKcliPNS0_11ParseResultE
Line
Count
Source
390
1
                                       ParseResult* result) {
391
1
    typedef typename std::make_unsigned<T>::type UnsignedT;
392
1
    UnsignedT val = 0;
393
1
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
394
1
    bool negative = false;
395
1
    if (UNLIKELY(len <= 0)) {
396
0
        *result = PARSE_FAILURE;
397
0
        return 0;
398
0
    }
399
1
    int i = 0;
400
1
    switch (*s) {
401
0
    case '-':
402
0
        negative = true;
403
0
        max_val = StringParser::numeric_limits<T>(false) + 1;
404
0
        [[fallthrough]];
405
0
    case '+':
406
0
        i = 1;
407
1
    }
408
409
1
    const T max_div_base = max_val / base;
410
1
    const T max_mod_base = max_val % base;
411
412
1
    int first = i;
413
3
    for (; i < len; ++i) {
414
2
        T digit;
415
2
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
416
0
            digit = s[i] - '0';
417
2
        } else if (s[i] >= 'a' && s[i] <= 'z') {
418
2
            digit = (s[i] - 'a' + 10);
419
2
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
420
0
            digit = (s[i] - 'A' + 10);
421
0
        } else {
422
0
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
423
                // Reject the string because either the first char was not an alpha/digit,
424
                // or the remaining chars are not all whitespace
425
0
                *result = PARSE_FAILURE;
426
0
                return 0;
427
0
            }
428
            // skip trailing whitespace.
429
0
            break;
430
0
        }
431
432
        // Bail, if we encounter a digit that is not available in base.
433
2
        if (digit >= base) {
434
0
            break;
435
0
        }
436
437
        // This is a tricky check to see if adding this digit will cause an overflow.
438
2
        if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) {
439
0
            *result = PARSE_OVERFLOW;
440
0
            return static_cast<T>(negative ? -max_val : max_val);
441
0
        }
442
2
        val = val * base + digit;
443
2
    }
444
1
    *result = PARSE_SUCCESS;
445
1
    return static_cast<T>(negative ? -val : val);
446
1
}
447
448
template <typename T>
449
388k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
388k
    T val = 0;
451
388k
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
388k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
382k
        val = s[0] - '0';
458
382k
    } else {
459
6.42k
        *result = PARSE_FAILURE;
460
6.42k
        return 0;
461
6.42k
    }
462
598k
    for (int i = 1; i < len; ++i) {
463
219k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
216k
            T digit = s[i] - '0';
465
216k
            val = val * 10 + digit;
466
216k
        } else {
467
3.01k
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
3.01k
                          !is_float_suffix(s + i, len - i)))) {
469
292
                *result = PARSE_FAILURE;
470
292
                return 0;
471
292
            }
472
2.71k
            *result = PARSE_SUCCESS;
473
2.71k
            return val;
474
3.01k
        }
475
219k
    }
476
379k
    *result = PARSE_SUCCESS;
477
379k
    return val;
478
382k
}
_ZN5doris12StringParser25string_to_int_no_overflowIhEET_PKciPNS0_11ParseResultE
Line
Count
Source
449
203k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
203k
    T val = 0;
451
203k
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
203k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
203k
        val = s[0] - '0';
458
203k
    } else {
459
496
        *result = PARSE_FAILURE;
460
496
        return 0;
461
496
    }
462
306k
    for (int i = 1; i < len; ++i) {
463
103k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
103k
            T digit = s[i] - '0';
465
103k
            val = val * 10 + digit;
466
103k
        } else {
467
60
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
60
                          !is_float_suffix(s + i, len - i)))) {
469
0
                *result = PARSE_FAILURE;
470
0
                return 0;
471
0
            }
472
60
            *result = PARSE_SUCCESS;
473
60
            return val;
474
60
        }
475
103k
    }
476
203k
    *result = PARSE_SUCCESS;
477
203k
    return val;
478
203k
}
_ZN5doris12StringParser25string_to_int_no_overflowIoEET_PKciPNS0_11ParseResultE
Line
Count
Source
449
43.4k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
43.4k
    T val = 0;
451
43.4k
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
43.4k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
42.3k
        val = s[0] - '0';
458
42.3k
    } else {
459
1.03k
        *result = PARSE_FAILURE;
460
1.03k
        return 0;
461
1.03k
    }
462
59.7k
    for (int i = 1; i < len; ++i) {
463
17.7k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
17.3k
            T digit = s[i] - '0';
465
17.3k
            val = val * 10 + digit;
466
17.3k
        } else {
467
398
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
398
                          !is_float_suffix(s + i, len - i)))) {
469
34
                *result = PARSE_FAILURE;
470
34
                return 0;
471
34
            }
472
364
            *result = PARSE_SUCCESS;
473
364
            return val;
474
398
        }
475
17.7k
    }
476
41.9k
    *result = PARSE_SUCCESS;
477
41.9k
    return val;
478
42.3k
}
_ZN5doris12StringParser25string_to_int_no_overflowImEET_PKciPNS0_11ParseResultE
Line
Count
Source
449
34.3k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
34.3k
    T val = 0;
451
34.3k
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
34.3k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
32.9k
        val = s[0] - '0';
458
32.9k
    } else {
459
1.42k
        *result = PARSE_FAILURE;
460
1.42k
        return 0;
461
1.42k
    }
462
69.2k
    for (int i = 1; i < len; ++i) {
463
37.1k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
36.3k
            T digit = s[i] - '0';
465
36.3k
            val = val * 10 + digit;
466
36.3k
        } else {
467
770
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
770
                          !is_float_suffix(s + i, len - i)))) {
469
66
                *result = PARSE_FAILURE;
470
66
                return 0;
471
66
            }
472
704
            *result = PARSE_SUCCESS;
473
704
            return val;
474
770
        }
475
37.1k
    }
476
32.1k
    *result = PARSE_SUCCESS;
477
32.1k
    return val;
478
32.9k
}
_ZN5doris12StringParser25string_to_int_no_overflowItEET_PKciPNS0_11ParseResultE
Line
Count
Source
449
52.5k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
52.5k
    T val = 0;
451
52.5k
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
52.5k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
51.7k
        val = s[0] - '0';
458
51.7k
    } else {
459
815
        *result = PARSE_FAILURE;
460
815
        return 0;
461
815
    }
462
75.3k
    for (int i = 1; i < len; ++i) {
463
24.7k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
23.6k
            T digit = s[i] - '0';
465
23.6k
            val = val * 10 + digit;
466
23.6k
        } else {
467
1.08k
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
1.08k
                          !is_float_suffix(s + i, len - i)))) {
469
52
                *result = PARSE_FAILURE;
470
52
                return 0;
471
52
            }
472
1.03k
            *result = PARSE_SUCCESS;
473
1.03k
            return val;
474
1.08k
        }
475
24.7k
    }
476
50.6k
    *result = PARSE_SUCCESS;
477
50.6k
    return val;
478
51.7k
}
_ZN5doris12StringParser25string_to_int_no_overflowIjEET_PKciPNS0_11ParseResultE
Line
Count
Source
449
54.5k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
54.5k
    T val = 0;
451
54.5k
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
54.5k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
51.8k
        val = s[0] - '0';
458
51.8k
    } else {
459
2.65k
        *result = PARSE_FAILURE;
460
2.65k
        return 0;
461
2.65k
    }
462
88.1k
    for (int i = 1; i < len; ++i) {
463
36.9k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
36.2k
            T digit = s[i] - '0';
465
36.2k
            val = val * 10 + digit;
466
36.2k
        } else {
467
700
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
700
                          !is_float_suffix(s + i, len - i)))) {
469
140
                *result = PARSE_FAILURE;
470
140
                return 0;
471
140
            }
472
560
            *result = PARSE_SUCCESS;
473
560
            return val;
474
700
        }
475
36.9k
    }
476
51.1k
    *result = PARSE_SUCCESS;
477
51.1k
    return val;
478
51.8k
}
_ZN5doris12StringParser25string_to_int_no_overflowIN4wide7integerILm256EjEEEET_PKciPNS0_11ParseResultE
Line
Count
Source
449
4
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
4
    T val = 0;
451
4
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
4
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
4
        val = s[0] - '0';
458
4
    } else {
459
0
        *result = PARSE_FAILURE;
460
0
        return 0;
461
0
    }
462
4
    for (int i = 1; i < len; ++i) {
463
0
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
0
            T digit = s[i] - '0';
465
0
            val = val * 10 + digit;
466
0
        } else {
467
0
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
0
                          !is_float_suffix(s + i, len - i)))) {
469
0
                *result = PARSE_FAILURE;
470
0
                return 0;
471
0
            }
472
0
            *result = PARSE_SUCCESS;
473
0
            return val;
474
0
        }
475
0
    }
476
4
    *result = PARSE_SUCCESS;
477
4
    return val;
478
4
}
479
480
template <typename T>
481
136k
T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) {
482
136k
    int i = 0;
483
    // skip leading spaces
484
178k
    for (; i < len; ++i) {
485
178k
        if (!is_whitespace(s[i])) {
486
136k
            break;
487
136k
        }
488
178k
    }
489
490
    // skip back spaces
491
136k
    int j = len - 1;
492
177k
    for (; j >= i; j--) {
493
177k
        if (!is_whitespace(s[j])) {
494
136k
            break;
495
136k
        }
496
177k
    }
497
498
    // skip leading '+', from_chars can handle '-'
499
136k
    if (i < len && s[i] == '+') {
500
5.29k
        i++;
501
5.29k
    }
502
136k
    if (UNLIKELY(i > j)) {
503
3
        *result = PARSE_FAILURE;
504
3
        return 0;
505
3
    }
506
507
    // Use double here to not lose precision while accumulating the result
508
136k
    double val = 0;
509
136k
    auto res = fast_float::from_chars(s + i, s + j + 1, val);
510
511
136k
    if (res.ec == std::errc() && res.ptr == s + j + 1) {
512
131k
        if (abs(val) == std::numeric_limits<T>::infinity()) {
513
898
            auto contain_inf = false;
514
1.29k
            for (int k = i; k < j + 1; k++) {
515
1.29k
                if (s[k] == 'i' || s[k] == 'I') {
516
894
                    contain_inf = true;
517
894
                    break;
518
894
                }
519
1.29k
            }
520
521
898
            *result = contain_inf ? PARSE_SUCCESS : PARSE_OVERFLOW;
522
130k
        } else {
523
130k
            *result = PARSE_SUCCESS;
524
130k
        }
525
131k
        return val;
526
131k
    } else {
527
4.73k
        *result = PARSE_FAILURE;
528
4.73k
    }
529
4.73k
    return 0;
530
136k
}
_ZN5doris12StringParser24string_to_float_internalIdEET_PKciPNS0_11ParseResultE
Line
Count
Source
481
73.0k
T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) {
482
73.0k
    int i = 0;
483
    // skip leading spaces
484
94.2k
    for (; i < len; ++i) {
485
94.2k
        if (!is_whitespace(s[i])) {
486
73.0k
            break;
487
73.0k
        }
488
94.2k
    }
489
490
    // skip back spaces
491
73.0k
    int j = len - 1;
492
93.6k
    for (; j >= i; j--) {
493
93.6k
        if (!is_whitespace(s[j])) {
494
73.0k
            break;
495
73.0k
        }
496
93.6k
    }
497
498
    // skip leading '+', from_chars can handle '-'
499
73.0k
    if (i < len && s[i] == '+') {
500
2.64k
        i++;
501
2.64k
    }
502
73.0k
    if (UNLIKELY(i > j)) {
503
3
        *result = PARSE_FAILURE;
504
3
        return 0;
505
3
    }
506
507
    // Use double here to not lose precision while accumulating the result
508
73.0k
    double val = 0;
509
73.0k
    auto res = fast_float::from_chars(s + i, s + j + 1, val);
510
511
73.0k
    if (res.ec == std::errc() && res.ptr == s + j + 1) {
512
70.4k
        if (abs(val) == std::numeric_limits<T>::infinity()) {
513
449
            auto contain_inf = false;
514
656
            for (int k = i; k < j + 1; k++) {
515
654
                if (s[k] == 'i' || s[k] == 'I') {
516
447
                    contain_inf = true;
517
447
                    break;
518
447
                }
519
654
            }
520
521
449
            *result = contain_inf ? PARSE_SUCCESS : PARSE_OVERFLOW;
522
70.0k
        } else {
523
70.0k
            *result = PARSE_SUCCESS;
524
70.0k
        }
525
70.4k
        return val;
526
70.4k
    } else {
527
2.56k
        *result = PARSE_FAILURE;
528
2.56k
    }
529
2.56k
    return 0;
530
73.0k
}
_ZN5doris12StringParser24string_to_float_internalIfEET_PKciPNS0_11ParseResultE
Line
Count
Source
481
63.1k
T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) {
482
63.1k
    int i = 0;
483
    // skip leading spaces
484
84.3k
    for (; i < len; ++i) {
485
84.3k
        if (!is_whitespace(s[i])) {
486
63.1k
            break;
487
63.1k
        }
488
84.3k
    }
489
490
    // skip back spaces
491
63.1k
    int j = len - 1;
492
83.7k
    for (; j >= i; j--) {
493
83.7k
        if (!is_whitespace(s[j])) {
494
63.1k
            break;
495
63.1k
        }
496
83.7k
    }
497
498
    // skip leading '+', from_chars can handle '-'
499
63.1k
    if (i < len && s[i] == '+') {
500
2.64k
        i++;
501
2.64k
    }
502
63.1k
    if (UNLIKELY(i > j)) {
503
0
        *result = PARSE_FAILURE;
504
0
        return 0;
505
0
    }
506
507
    // Use double here to not lose precision while accumulating the result
508
63.1k
    double val = 0;
509
63.1k
    auto res = fast_float::from_chars(s + i, s + j + 1, val);
510
511
63.1k
    if (res.ec == std::errc() && res.ptr == s + j + 1) {
512
61.0k
        if (abs(val) == std::numeric_limits<T>::infinity()) {
513
449
            auto contain_inf = false;
514
638
            for (int k = i; k < j + 1; k++) {
515
636
                if (s[k] == 'i' || s[k] == 'I') {
516
447
                    contain_inf = true;
517
447
                    break;
518
447
                }
519
636
            }
520
521
449
            *result = contain_inf ? PARSE_SUCCESS : PARSE_OVERFLOW;
522
60.5k
        } else {
523
60.5k
            *result = PARSE_SUCCESS;
524
60.5k
        }
525
61.0k
        return val;
526
61.0k
    } else {
527
2.16k
        *result = PARSE_FAILURE;
528
2.16k
    }
529
2.16k
    return 0;
530
63.1k
}
531
532
inline bool StringParser::string_to_bool_internal(const char* __restrict s, int len,
533
9.22k
                                                  ParseResult* result) {
534
9.22k
    *result = PARSE_SUCCESS;
535
536
9.22k
    if (len >= 4 && (s[0] == 't' || s[0] == 'T')) {
537
3.71k
        bool match = (s[1] == 'r' || s[1] == 'R') && (s[2] == 'u' || s[2] == 'U') &&
538
3.71k
                     (s[3] == 'e' || s[3] == 'E');
539
3.71k
        if (match && LIKELY(is_all_whitespace(s + 4, len - 4))) {
540
3.60k
            return true;
541
3.60k
        }
542
5.51k
    } else if (len >= 5 && (s[0] == 'f' || s[0] == 'F')) {
543
3.77k
        bool match = (s[1] == 'a' || s[1] == 'A') && (s[2] == 'l' || s[2] == 'L') &&
544
3.77k
                     (s[3] == 's' || s[3] == 'S') && (s[4] == 'e' || s[4] == 'E');
545
3.77k
        if (match && LIKELY(is_all_whitespace(s + 5, len - 5))) {
546
3.65k
            return false;
547
3.65k
        }
548
3.77k
    }
549
550
1.97k
    *result = PARSE_FAILURE;
551
1.97k
    return false;
552
9.22k
}
553
554
template <PrimitiveType P>
555
typename PrimitiveTypeTraits<P>::CppType::NativeType StringParser::string_to_decimal(
556
        const char* __restrict s, int len, int type_precision, int type_scale,
557
802k
        ParseResult* result) {
558
802k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
559
802k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
560
802k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
561
802k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
562
802k
                  "wide::Int256.");
563
    // Special cases:
564
    //   1) '' == Fail, an empty string fails to parse.
565
    //   2) '   #   ' == #, leading and trailing white space is ignored.
566
    //   3) '.' == 0, a single dot parses as zero (for consistency with other types).
567
    //   4) '#.' == '#', a trailing dot is ignored.
568
569
    // Ignore leading and trailing spaces.
570
1.63M
    while (len > 0 && is_whitespace(*s)) {
571
835k
        ++s;
572
835k
        --len;
573
835k
    }
574
1.63M
    while (len > 0 && is_whitespace(s[len - 1])) {
575
833k
        --len;
576
833k
    }
577
578
802k
    bool is_negative = false;
579
802k
    if (len > 0) {
580
802k
        switch (*s) {
581
315k
        case '-':
582
315k
            is_negative = true;
583
315k
            [[fallthrough]];
584
454k
        case '+':
585
454k
            ++s;
586
454k
            --len;
587
802k
        }
588
802k
    }
589
590
    // Ignore leading zeros.
591
802k
    bool found_value = false;
592
1.86M
    while (len > 0 && UNLIKELY(*s == '0')) {
593
1.06M
        found_value = true;
594
1.06M
        ++s;
595
1.06M
        --len;
596
1.06M
    }
597
598
    // Ignore leading zeros even after a dot. This allows for differentiating between
599
    // cases like 0.01e2, which would fit in a DECIMAL(1, 0), and 0.10e2, which would
600
    // overflow.
601
802k
    int scale = 0;
602
802k
    int found_dot = 0;
603
802k
    if (len > 0 && *s == '.') {
604
189k
        found_dot = 1;
605
189k
        ++s;
606
189k
        --len;
607
1.45M
        while (len > 0 && UNLIKELY(*s == '0')) {
608
1.26M
            found_value = true;
609
1.26M
            ++scale;
610
1.26M
            ++s;
611
1.26M
            --len;
612
1.26M
        }
613
189k
    }
614
615
802k
    int precision = 0;
616
802k
    int max_digit = type_precision - type_scale;
617
802k
    int cur_digit = 0;
618
802k
    bool found_exponent = false;
619
802k
    int8_t exponent = 0;
620
802k
    T value = 0;
621
802k
    bool has_round = false;
622
17.6M
    for (int i = 0; i < len; ++i) {
623
16.9M
        const char& c = s[i];
624
16.9M
        if (LIKELY('0' <= c && c <= '9')) {
625
16.2M
            found_value = true;
626
            // Ignore digits once the type's precision limit is reached. This avoids
627
            // overflowing the underlying storage while handling a string like
628
            // 10000000000e-10 into a DECIMAL(1, 0). Adjustments for ignored digits and
629
            // an exponent will be made later.
630
16.2M
            if (LIKELY(type_precision > precision) && !has_round) {
631
16.0M
                value = (value * 10) + (c - '0'); // Benchmarks are faster with parenthesis...
632
16.0M
                ++precision;
633
16.0M
                scale += found_dot;
634
16.0M
                cur_digit = precision - scale;
635
16.0M
            } else if (!found_dot && max_digit < (precision - scale)) {
636
432
                *result = StringParser::PARSE_OVERFLOW;
637
432
                value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
638
432
                                    : vectorized::max_decimal_value<P>(type_precision);
639
432
                return value;
640
225k
            } else if (found_dot && scale >= type_scale && !has_round) {
641
                // make rounding cases
642
225k
                if (c > '4') {
643
83.2k
                    value += 1;
644
83.2k
                }
645
225k
                has_round = true;
646
225k
                continue;
647
225k
            } else if (!found_dot) {
648
0
                ++cur_digit;
649
0
            }
650
16.0M
            DCHECK(value >= 0); // For some reason //DCHECK_GE doesn't work with __int128.
651
16.0M
        } else if (c == '.' && LIKELY(!found_dot)) {
652
539k
            found_dot = 1;
653
539k
        } else if ((c == 'e' || c == 'E') && LIKELY(!found_exponent)) {
654
137k
            found_exponent = true;
655
137k
            exponent = string_to_int_internal<int8_t>(s + i + 1, len - i - 1, result);
656
137k
            if (UNLIKELY(*result != StringParser::PARSE_SUCCESS)) {
657
10
                if (*result == StringParser::PARSE_OVERFLOW && exponent < 0) {
658
0
                    *result = StringParser::PARSE_UNDERFLOW;
659
0
                }
660
10
                return 0;
661
10
            }
662
137k
            break;
663
137k
        } else {
664
344
            if (value == 0) {
665
271
                *result = StringParser::PARSE_FAILURE;
666
271
                return 0;
667
271
            }
668
            // here to handle
669
73
            *result = StringParser::PARSE_SUCCESS;
670
73
            if (type_scale >= scale) {
671
71
                value *= get_scale_multiplier<T>(type_scale - scale);
672
                // here meet non-valid character, should return the value, keep going to meet
673
                // the E/e character because we make right user-given type_precision
674
                // not max number type_precision
675
71
                if (!is_numeric_ascii(c)) {
676
71
                    if (cur_digit > type_precision) {
677
0
                        *result = StringParser::PARSE_OVERFLOW;
678
0
                        value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
679
0
                                            : vectorized::max_decimal_value<P>(type_precision);
680
0
                        return value;
681
0
                    }
682
71
                    return is_negative ? T(-value) : T(value);
683
71
                }
684
71
            }
685
686
2
            return is_negative ? T(-value) : T(value);
687
73
        }
688
16.9M
    }
689
690
    // Find the number of truncated digits before adjusting the precision for an exponent.
691
801k
    if (exponent > scale) {
692
        // Ex: 0.1e3 (which at this point would have precision == 1 and scale == 1), the
693
        //     scale must be set to 0 and the value set to 100 which means a precision of 3.
694
68.2k
        precision += exponent - scale;
695
696
68.2k
        value *= get_scale_multiplier<T>(exponent - scale);
697
68.2k
        scale = 0;
698
733k
    } else {
699
        // Ex: 100e-4, the scale must be set to 4 but no adjustment to the value is needed,
700
        //     the precision must also be set to 4 but that will be done below for the
701
        //     non-exponent case anyways.
702
733k
        scale -= exponent;
703
733k
    }
704
    // Ex: 0.001, at this point would have precision 1 and scale 3 since leading zeros
705
    //     were ignored during previous parsing.
706
801k
    if (scale > precision) {
707
76.8k
        precision = scale;
708
76.8k
    }
709
710
    // Microbenchmarks show that beyond this point, returning on parse failure is slower
711
    // than just letting the function run out.
712
13.5k
    *result = StringParser::PARSE_SUCCESS;
713
787k
    if (UNLIKELY(precision - scale > type_precision - type_scale)) {
714
11.3k
        *result = StringParser::PARSE_OVERFLOW;
715
11.3k
        if constexpr (TYPE_DECIMALV2 != P) {
716
            // decimalv3 overflow will return max min value for type precision
717
11.3k
            value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
718
11.3k
                                : vectorized::max_decimal_value<P>(type_precision);
719
11.3k
            return value;
720
11.3k
        }
721
790k
    } else if (UNLIKELY(scale > type_scale)) {
722
247k
        *result = StringParser::PARSE_UNDERFLOW;
723
247k
        int shift = scale - type_scale;
724
247k
        T divisor = get_scale_multiplier<T>(shift);
725
247k
        if (UNLIKELY(divisor == std::numeric_limits<T>::max())) {
726
0
            value = 0;
727
247k
        } else {
728
247k
            T remainder = value % divisor;
729
247k
            value /= divisor;
730
247k
            if ((remainder > 0 ? T(remainder) : T(-remainder)) >= (divisor >> 1)) {
731
121k
                value += 1;
732
121k
            }
733
247k
        }
734
247k
        DCHECK(value >= 0); // //DCHECK_GE doesn't work with __int128.
735
542k
    } else if (UNLIKELY(!found_value && !found_dot)) {
736
1
        *result = StringParser::PARSE_FAILURE;
737
1
    }
738
739
790k
    if (type_scale > scale) {
740
233k
        value *= get_scale_multiplier<T>(type_scale - scale);
741
233k
    }
742
743
790k
    return is_negative ? T(-value) : T(value);
744
787k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE28EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKciiiPNS0_11ParseResultE
Line
Count
Source
557
199k
        ParseResult* result) {
558
199k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
559
199k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
560
199k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
561
199k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
562
199k
                  "wide::Int256.");
563
    // Special cases:
564
    //   1) '' == Fail, an empty string fails to parse.
565
    //   2) '   #   ' == #, leading and trailing white space is ignored.
566
    //   3) '.' == 0, a single dot parses as zero (for consistency with other types).
567
    //   4) '#.' == '#', a trailing dot is ignored.
568
569
    // Ignore leading and trailing spaces.
570
460k
    while (len > 0 && is_whitespace(*s)) {
571
261k
        ++s;
572
261k
        --len;
573
261k
    }
574
460k
    while (len > 0 && is_whitespace(s[len - 1])) {
575
261k
        --len;
576
261k
    }
577
578
199k
    bool is_negative = false;
579
199k
    if (len > 0) {
580
199k
        switch (*s) {
581
97.4k
        case '-':
582
97.4k
            is_negative = true;
583
97.4k
            [[fallthrough]];
584
140k
        case '+':
585
140k
            ++s;
586
140k
            --len;
587
199k
        }
588
199k
    }
589
590
    // Ignore leading zeros.
591
199k
    bool found_value = false;
592
496k
    while (len > 0 && UNLIKELY(*s == '0')) {
593
296k
        found_value = true;
594
296k
        ++s;
595
296k
        --len;
596
296k
    }
597
598
    // Ignore leading zeros even after a dot. This allows for differentiating between
599
    // cases like 0.01e2, which would fit in a DECIMAL(1, 0), and 0.10e2, which would
600
    // overflow.
601
199k
    int scale = 0;
602
199k
    int found_dot = 0;
603
199k
    if (len > 0 && *s == '.') {
604
55.9k
        found_dot = 1;
605
55.9k
        ++s;
606
55.9k
        --len;
607
160k
        while (len > 0 && UNLIKELY(*s == '0')) {
608
104k
            found_value = true;
609
104k
            ++scale;
610
104k
            ++s;
611
104k
            --len;
612
104k
        }
613
55.9k
    }
614
615
199k
    int precision = 0;
616
199k
    int max_digit = type_precision - type_scale;
617
199k
    int cur_digit = 0;
618
199k
    bool found_exponent = false;
619
199k
    int8_t exponent = 0;
620
199k
    T value = 0;
621
199k
    bool has_round = false;
622
1.54M
    for (int i = 0; i < len; ++i) {
623
1.39M
        const char& c = s[i];
624
1.39M
        if (LIKELY('0' <= c && c <= '9')) {
625
1.24M
            found_value = true;
626
            // Ignore digits once the type's precision limit is reached. This avoids
627
            // overflowing the underlying storage while handling a string like
628
            // 10000000000e-10 into a DECIMAL(1, 0). Adjustments for ignored digits and
629
            // an exponent will be made later.
630
1.24M
            if (LIKELY(type_precision > precision) && !has_round) {
631
1.17M
                value = (value * 10) + (c - '0'); // Benchmarks are faster with parenthesis...
632
1.17M
                ++precision;
633
1.17M
                scale += found_dot;
634
1.17M
                cur_digit = precision - scale;
635
1.17M
            } else if (!found_dot && max_digit < (precision - scale)) {
636
432
                *result = StringParser::PARSE_OVERFLOW;
637
432
                value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
638
432
                                    : vectorized::max_decimal_value<P>(type_precision);
639
432
                return value;
640
69.1k
            } else if (found_dot && scale >= type_scale && !has_round) {
641
                // make rounding cases
642
69.1k
                if (c > '4') {
643
25.6k
                    value += 1;
644
25.6k
                }
645
69.1k
                has_round = true;
646
69.1k
                continue;
647
69.1k
            } else if (!found_dot) {
648
0
                ++cur_digit;
649
0
            }
650
1.17M
            DCHECK(value >= 0); // For some reason //DCHECK_GE doesn't work with __int128.
651
1.17M
        } else if (c == '.' && LIKELY(!found_dot)) {
652
110k
            found_dot = 1;
653
110k
        } else if ((c == 'e' || c == 'E') && LIKELY(!found_exponent)) {
654
40.9k
            found_exponent = true;
655
40.9k
            exponent = string_to_int_internal<int8_t>(s + i + 1, len - i - 1, result);
656
40.9k
            if (UNLIKELY(*result != StringParser::PARSE_SUCCESS)) {
657
0
                if (*result == StringParser::PARSE_OVERFLOW && exponent < 0) {
658
0
                    *result = StringParser::PARSE_UNDERFLOW;
659
0
                }
660
0
                return 0;
661
0
            }
662
40.9k
            break;
663
40.9k
        } else {
664
92
            if (value == 0) {
665
66
                *result = StringParser::PARSE_FAILURE;
666
66
                return 0;
667
66
            }
668
            // here to handle
669
26
            *result = StringParser::PARSE_SUCCESS;
670
26
            if (type_scale >= scale) {
671
26
                value *= get_scale_multiplier<T>(type_scale - scale);
672
                // here meet non-valid character, should return the value, keep going to meet
673
                // the E/e character because we make right user-given type_precision
674
                // not max number type_precision
675
26
                if (!is_numeric_ascii(c)) {
676
26
                    if (cur_digit > type_precision) {
677
0
                        *result = StringParser::PARSE_OVERFLOW;
678
0
                        value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
679
0
                                            : vectorized::max_decimal_value<P>(type_precision);
680
0
                        return value;
681
0
                    }
682
26
                    return is_negative ? T(-value) : T(value);
683
26
                }
684
26
            }
685
686
0
            return is_negative ? T(-value) : T(value);
687
26
        }
688
1.39M
    }
689
690
    // Find the number of truncated digits before adjusting the precision for an exponent.
691
198k
    if (exponent > scale) {
692
        // Ex: 0.1e3 (which at this point would have precision == 1 and scale == 1), the
693
        //     scale must be set to 0 and the value set to 100 which means a precision of 3.
694
0
        precision += exponent - scale;
695
696
0
        value *= get_scale_multiplier<T>(exponent - scale);
697
0
        scale = 0;
698
198k
    } else {
699
        // Ex: 100e-4, the scale must be set to 4 but no adjustment to the value is needed,
700
        //     the precision must also be set to 4 but that will be done below for the
701
        //     non-exponent case anyways.
702
198k
        scale -= exponent;
703
198k
    }
704
    // Ex: 0.001, at this point would have precision 1 and scale 3 since leading zeros
705
    //     were ignored during previous parsing.
706
198k
    if (scale > precision) {
707
19.5k
        precision = scale;
708
19.5k
    }
709
710
    // Microbenchmarks show that beyond this point, returning on parse failure is slower
711
    // than just letting the function run out.
712
198k
    *result = StringParser::PARSE_SUCCESS;
713
198k
    if (UNLIKELY(precision - scale > type_precision - type_scale)) {
714
726
        *result = StringParser::PARSE_OVERFLOW;
715
726
        if constexpr (TYPE_DECIMALV2 != P) {
716
            // decimalv3 overflow will return max min value for type precision
717
726
            value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
718
726
                                : vectorized::max_decimal_value<P>(type_precision);
719
726
            return value;
720
726
        }
721
197k
    } else if (UNLIKELY(scale > type_scale)) {
722
76.8k
        *result = StringParser::PARSE_UNDERFLOW;
723
76.8k
        int shift = scale - type_scale;
724
76.8k
        T divisor = get_scale_multiplier<T>(shift);
725
76.8k
        if (UNLIKELY(divisor == std::numeric_limits<T>::max())) {
726
0
            value = 0;
727
76.8k
        } else {
728
76.8k
            T remainder = value % divisor;
729
76.8k
            value /= divisor;
730
76.8k
            if ((remainder > 0 ? T(remainder) : T(-remainder)) >= (divisor >> 1)) {
731
38.4k
                value += 1;
732
38.4k
            }
733
76.8k
        }
734
76.8k
        DCHECK(value >= 0); // //DCHECK_GE doesn't work with __int128.
735
121k
    } else if (UNLIKELY(!found_value && !found_dot)) {
736
0
        *result = StringParser::PARSE_FAILURE;
737
0
    }
738
739
197k
    if (type_scale > scale) {
740
39.2k
        value *= get_scale_multiplier<T>(type_scale - scale);
741
39.2k
    }
742
743
197k
    return is_negative ? T(-value) : T(value);
744
198k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE29EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKciiiPNS0_11ParseResultE
Line
Count
Source
557
206k
        ParseResult* result) {
558
206k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
559
206k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
560
206k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
561
206k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
562
206k
                  "wide::Int256.");
563
    // Special cases:
564
    //   1) '' == Fail, an empty string fails to parse.
565
    //   2) '   #   ' == #, leading and trailing white space is ignored.
566
    //   3) '.' == 0, a single dot parses as zero (for consistency with other types).
567
    //   4) '#.' == '#', a trailing dot is ignored.
568
569
    // Ignore leading and trailing spaces.
570
425k
    while (len > 0 && is_whitespace(*s)) {
571
219k
        ++s;
572
219k
        --len;
573
219k
    }
574
425k
    while (len > 0 && is_whitespace(s[len - 1])) {
575
218k
        --len;
576
218k
    }
577
578
206k
    bool is_negative = false;
579
206k
    if (len > 0) {
580
206k
        switch (*s) {
581
81.2k
        case '-':
582
81.2k
            is_negative = true;
583
81.2k
            [[fallthrough]];
584
117k
        case '+':
585
117k
            ++s;
586
117k
            --len;
587
206k
        }
588
206k
    }
589
590
    // Ignore leading zeros.
591
206k
    bool found_value = false;
592
478k
    while (len > 0 && UNLIKELY(*s == '0')) {
593
271k
        found_value = true;
594
271k
        ++s;
595
271k
        --len;
596
271k
    }
597
598
    // Ignore leading zeros even after a dot. This allows for differentiating between
599
    // cases like 0.01e2, which would fit in a DECIMAL(1, 0), and 0.10e2, which would
600
    // overflow.
601
206k
    int scale = 0;
602
206k
    int found_dot = 0;
603
206k
    if (len > 0 && *s == '.') {
604
53.1k
        found_dot = 1;
605
53.1k
        ++s;
606
53.1k
        --len;
607
263k
        while (len > 0 && UNLIKELY(*s == '0')) {
608
210k
            found_value = true;
609
210k
            ++scale;
610
210k
            ++s;
611
210k
            --len;
612
210k
        }
613
53.1k
    }
614
615
206k
    int precision = 0;
616
206k
    int max_digit = type_precision - type_scale;
617
206k
    int cur_digit = 0;
618
206k
    bool found_exponent = false;
619
206k
    int8_t exponent = 0;
620
206k
    T value = 0;
621
206k
    bool has_round = false;
622
2.87M
    for (int i = 0; i < len; ++i) {
623
2.69M
        const char& c = s[i];
624
2.69M
        if (LIKELY('0' <= c && c <= '9')) {
625
2.53M
            found_value = true;
626
            // Ignore digits once the type's precision limit is reached. This avoids
627
            // overflowing the underlying storage while handling a string like
628
            // 10000000000e-10 into a DECIMAL(1, 0). Adjustments for ignored digits and
629
            // an exponent will be made later.
630
2.53M
            if (LIKELY(type_precision > precision) && !has_round) {
631
2.47M
                value = (value * 10) + (c - '0'); // Benchmarks are faster with parenthesis...
632
2.47M
                ++precision;
633
2.47M
                scale += found_dot;
634
2.47M
                cur_digit = precision - scale;
635
2.47M
            } else if (!found_dot && max_digit < (precision - scale)) {
636
0
                *result = StringParser::PARSE_OVERFLOW;
637
0
                value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
638
0
                                    : vectorized::max_decimal_value<P>(type_precision);
639
0
                return value;
640
58.8k
            } else if (found_dot && scale >= type_scale && !has_round) {
641
                // make rounding cases
642
58.8k
                if (c > '4') {
643
21.7k
                    value += 1;
644
21.7k
                }
645
58.8k
                has_round = true;
646
58.8k
                continue;
647
58.8k
            } else if (!found_dot) {
648
0
                ++cur_digit;
649
0
            }
650
2.47M
            DCHECK(value >= 0); // For some reason //DCHECK_GE doesn't work with __int128.
651
2.47M
        } else if (c == '.' && LIKELY(!found_dot)) {
652
136k
            found_dot = 1;
653
136k
        } else if ((c == 'e' || c == 'E') && LIKELY(!found_exponent)) {
654
22.9k
            found_exponent = true;
655
22.9k
            exponent = string_to_int_internal<int8_t>(s + i + 1, len - i - 1, result);
656
22.9k
            if (UNLIKELY(*result != StringParser::PARSE_SUCCESS)) {
657
0
                if (*result == StringParser::PARSE_OVERFLOW && exponent < 0) {
658
0
                    *result = StringParser::PARSE_UNDERFLOW;
659
0
                }
660
0
                return 0;
661
0
            }
662
22.9k
            break;
663
22.9k
        } else {
664
91
            if (value == 0) {
665
68
                *result = StringParser::PARSE_FAILURE;
666
68
                return 0;
667
68
            }
668
            // here to handle
669
23
            *result = StringParser::PARSE_SUCCESS;
670
23
            if (type_scale >= scale) {
671
22
                value *= get_scale_multiplier<T>(type_scale - scale);
672
                // here meet non-valid character, should return the value, keep going to meet
673
                // the E/e character because we make right user-given type_precision
674
                // not max number type_precision
675
22
                if (!is_numeric_ascii(c)) {
676
22
                    if (cur_digit > type_precision) {
677
0
                        *result = StringParser::PARSE_OVERFLOW;
678
0
                        value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
679
0
                                            : vectorized::max_decimal_value<P>(type_precision);
680
0
                        return value;
681
0
                    }
682
22
                    return is_negative ? T(-value) : T(value);
683
22
                }
684
22
            }
685
686
1
            return is_negative ? T(-value) : T(value);
687
23
        }
688
2.69M
    }
689
690
    // Find the number of truncated digits before adjusting the precision for an exponent.
691
206k
    if (exponent > scale) {
692
        // Ex: 0.1e3 (which at this point would have precision == 1 and scale == 1), the
693
        //     scale must be set to 0 and the value set to 100 which means a precision of 3.
694
1
        precision += exponent - scale;
695
696
1
        value *= get_scale_multiplier<T>(exponent - scale);
697
1
        scale = 0;
698
206k
    } else {
699
        // Ex: 100e-4, the scale must be set to 4 but no adjustment to the value is needed,
700
        //     the precision must also be set to 4 but that will be done below for the
701
        //     non-exponent case anyways.
702
206k
        scale -= exponent;
703
206k
    }
704
    // Ex: 0.001, at this point would have precision 1 and scale 3 since leading zeros
705
    //     were ignored during previous parsing.
706
206k
    if (scale > precision) {
707
20.6k
        precision = scale;
708
20.6k
    }
709
710
    // Microbenchmarks show that beyond this point, returning on parse failure is slower
711
    // than just letting the function run out.
712
206k
    *result = StringParser::PARSE_SUCCESS;
713
206k
    if (UNLIKELY(precision - scale > type_precision - type_scale)) {
714
10.5k
        *result = StringParser::PARSE_OVERFLOW;
715
10.5k
        if constexpr (TYPE_DECIMALV2 != P) {
716
            // decimalv3 overflow will return max min value for type precision
717
10.5k
            value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
718
10.5k
                                : vectorized::max_decimal_value<P>(type_precision);
719
10.5k
            return value;
720
10.5k
        }
721
195k
    } else if (UNLIKELY(scale > type_scale)) {
722
65.1k
        *result = StringParser::PARSE_UNDERFLOW;
723
65.1k
        int shift = scale - type_scale;
724
65.1k
        T divisor = get_scale_multiplier<T>(shift);
725
65.1k
        if (UNLIKELY(divisor == std::numeric_limits<T>::max())) {
726
0
            value = 0;
727
65.1k
        } else {
728
65.1k
            T remainder = value % divisor;
729
65.1k
            value /= divisor;
730
65.1k
            if ((remainder > 0 ? T(remainder) : T(-remainder)) >= (divisor >> 1)) {
731
32.0k
                value += 1;
732
32.0k
            }
733
65.1k
        }
734
65.1k
        DCHECK(value >= 0); // //DCHECK_GE doesn't work with __int128.
735
130k
    } else if (UNLIKELY(!found_value && !found_dot)) {
736
1
        *result = StringParser::PARSE_FAILURE;
737
1
    }
738
739
195k
    if (type_scale > scale) {
740
43.1k
        value *= get_scale_multiplier<T>(type_scale - scale);
741
43.1k
    }
742
743
195k
    return is_negative ? T(-value) : T(value);
744
206k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE30EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKciiiPNS0_11ParseResultE
Line
Count
Source
557
174k
        ParseResult* result) {
558
174k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
559
174k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
560
174k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
561
174k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
562
174k
                  "wide::Int256.");
563
    // Special cases:
564
    //   1) '' == Fail, an empty string fails to parse.
565
    //   2) '   #   ' == #, leading and trailing white space is ignored.
566
    //   3) '.' == 0, a single dot parses as zero (for consistency with other types).
567
    //   4) '#.' == '#', a trailing dot is ignored.
568
569
    // Ignore leading and trailing spaces.
570
351k
    while (len > 0 && is_whitespace(*s)) {
571
177k
        ++s;
572
177k
        --len;
573
177k
    }
574
350k
    while (len > 0 && is_whitespace(s[len - 1])) {
575
176k
        --len;
576
176k
    }
577
578
174k
    bool is_negative = false;
579
174k
    if (len > 0) {
580
174k
        switch (*s) {
581
67.1k
        case '-':
582
67.1k
            is_negative = true;
583
67.1k
            [[fallthrough]];
584
96.5k
        case '+':
585
96.5k
            ++s;
586
96.5k
            --len;
587
174k
        }
588
174k
    }
589
590
    // Ignore leading zeros.
591
174k
    bool found_value = false;
592
394k
    while (len > 0 && UNLIKELY(*s == '0')) {
593
220k
        found_value = true;
594
220k
        ++s;
595
220k
        --len;
596
220k
    }
597
598
    // Ignore leading zeros even after a dot. This allows for differentiating between
599
    // cases like 0.01e2, which would fit in a DECIMAL(1, 0), and 0.10e2, which would
600
    // overflow.
601
174k
    int scale = 0;
602
174k
    int found_dot = 0;
603
174k
    if (len > 0 && *s == '.') {
604
43.6k
        found_dot = 1;
605
43.6k
        ++s;
606
43.6k
        --len;
607
362k
        while (len > 0 && UNLIKELY(*s == '0')) {
608
319k
            found_value = true;
609
319k
            ++scale;
610
319k
            ++s;
611
319k
            --len;
612
319k
        }
613
43.6k
    }
614
615
174k
    int precision = 0;
616
174k
    int max_digit = type_precision - type_scale;
617
174k
    int cur_digit = 0;
618
174k
    bool found_exponent = false;
619
174k
    int8_t exponent = 0;
620
174k
    T value = 0;
621
174k
    bool has_round = false;
622
4.37M
    for (int i = 0; i < len; ++i) {
623
4.20M
        const char& c = s[i];
624
4.20M
        if (LIKELY('0' <= c && c <= '9')) {
625
4.08M
            found_value = true;
626
            // Ignore digits once the type's precision limit is reached. This avoids
627
            // overflowing the underlying storage while handling a string like
628
            // 10000000000e-10 into a DECIMAL(1, 0). Adjustments for ignored digits and
629
            // an exponent will be made later.
630
4.08M
            if (LIKELY(type_precision > precision) && !has_round) {
631
4.03M
                value = (value * 10) + (c - '0'); // Benchmarks are faster with parenthesis...
632
4.03M
                ++precision;
633
4.03M
                scale += found_dot;
634
4.03M
                cur_digit = precision - scale;
635
4.03M
            } else if (!found_dot && max_digit < (precision - scale)) {
636
0
                *result = StringParser::PARSE_OVERFLOW;
637
0
                value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
638
0
                                    : vectorized::max_decimal_value<P>(type_precision);
639
0
                return value;
640
48.6k
            } else if (found_dot && scale >= type_scale && !has_round) {
641
                // make rounding cases
642
48.6k
                if (c > '4') {
643
17.9k
                    value += 1;
644
17.9k
                }
645
48.6k
                has_round = true;
646
48.6k
                continue;
647
48.6k
            } else if (!found_dot) {
648
0
                ++cur_digit;
649
0
            }
650
4.03M
            DCHECK(value >= 0); // For some reason //DCHECK_GE doesn't work with __int128.
651
4.03M
        } else if (c == '.' && LIKELY(!found_dot)) {
652
117k
            found_dot = 1;
653
117k
        } else if ((c == 'e' || c == 'E') && LIKELY(!found_exponent)) {
654
3.25k
            found_exponent = true;
655
3.25k
            exponent = string_to_int_internal<int8_t>(s + i + 1, len - i - 1, result);
656
3.25k
            if (UNLIKELY(*result != StringParser::PARSE_SUCCESS)) {
657
0
                if (*result == StringParser::PARSE_OVERFLOW && exponent < 0) {
658
0
                    *result = StringParser::PARSE_UNDERFLOW;
659
0
                }
660
0
                return 0;
661
0
            }
662
3.25k
            break;
663
3.25k
        } else {
664
65
            if (value == 0) {
665
53
                *result = StringParser::PARSE_FAILURE;
666
53
                return 0;
667
53
            }
668
            // here to handle
669
12
            *result = StringParser::PARSE_SUCCESS;
670
12
            if (type_scale >= scale) {
671
11
                value *= get_scale_multiplier<T>(type_scale - scale);
672
                // here meet non-valid character, should return the value, keep going to meet
673
                // the E/e character because we make right user-given type_precision
674
                // not max number type_precision
675
11
                if (!is_numeric_ascii(c)) {
676
11
                    if (cur_digit > type_precision) {
677
0
                        *result = StringParser::PARSE_OVERFLOW;
678
0
                        value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
679
0
                                            : vectorized::max_decimal_value<P>(type_precision);
680
0
                        return value;
681
0
                    }
682
11
                    return is_negative ? T(-value) : T(value);
683
11
                }
684
11
            }
685
686
1
            return is_negative ? T(-value) : T(value);
687
12
        }
688
4.20M
    }
689
690
    // Find the number of truncated digits before adjusting the precision for an exponent.
691
174k
    if (exponent > scale) {
692
        // Ex: 0.1e3 (which at this point would have precision == 1 and scale == 1), the
693
        //     scale must be set to 0 and the value set to 100 which means a precision of 3.
694
0
        precision += exponent - scale;
695
696
0
        value *= get_scale_multiplier<T>(exponent - scale);
697
0
        scale = 0;
698
174k
    } else {
699
        // Ex: 100e-4, the scale must be set to 4 but no adjustment to the value is needed,
700
        //     the precision must also be set to 4 but that will be done below for the
701
        //     non-exponent case anyways.
702
174k
        scale -= exponent;
703
174k
    }
704
    // Ex: 0.001, at this point would have precision 1 and scale 3 since leading zeros
705
    //     were ignored during previous parsing.
706
174k
    if (scale > precision) {
707
20.6k
        precision = scale;
708
20.6k
    }
709
710
    // Microbenchmarks show that beyond this point, returning on parse failure is slower
711
    // than just letting the function run out.
712
174k
    *result = StringParser::PARSE_SUCCESS;
713
174k
    if (UNLIKELY(precision - scale > type_precision - type_scale)) {
714
2
        *result = StringParser::PARSE_OVERFLOW;
715
2
        if constexpr (TYPE_DECIMALV2 != P) {
716
            // decimalv3 overflow will return max min value for type precision
717
2
            value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
718
2
                                : vectorized::max_decimal_value<P>(type_precision);
719
2
            return value;
720
2
        }
721
174k
    } else if (UNLIKELY(scale > type_scale)) {
722
54.0k
        *result = StringParser::PARSE_UNDERFLOW;
723
54.0k
        int shift = scale - type_scale;
724
54.0k
        T divisor = get_scale_multiplier<T>(shift);
725
54.0k
        if (UNLIKELY(divisor == std::numeric_limits<T>::max())) {
726
0
            value = 0;
727
54.0k
        } else {
728
54.0k
            T remainder = value % divisor;
729
54.0k
            value /= divisor;
730
54.0k
            if ((remainder > 0 ? T(remainder) : T(-remainder)) >= (divisor >> 1)) {
731
25.6k
                value += 1;
732
25.6k
            }
733
54.0k
        }
734
54.0k
        DCHECK(value >= 0); // //DCHECK_GE doesn't work with __int128.
735
120k
    } else if (UNLIKELY(!found_value && !found_dot)) {
736
0
        *result = StringParser::PARSE_FAILURE;
737
0
    }
738
739
174k
    if (type_scale > scale) {
740
51.7k
        value *= get_scale_multiplier<T>(type_scale - scale);
741
51.7k
    }
742
743
174k
    return is_negative ? T(-value) : T(value);
744
174k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE35EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKciiiPNS0_11ParseResultE
Line
Count
Source
557
208k
        ParseResult* result) {
558
208k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
559
208k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
560
208k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
561
208k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
562
208k
                  "wide::Int256.");
563
    // Special cases:
564
    //   1) '' == Fail, an empty string fails to parse.
565
    //   2) '   #   ' == #, leading and trailing white space is ignored.
566
    //   3) '.' == 0, a single dot parses as zero (for consistency with other types).
567
    //   4) '#.' == '#', a trailing dot is ignored.
568
569
    // Ignore leading and trailing spaces.
570
386k
    while (len > 0 && is_whitespace(*s)) {
571
177k
        ++s;
572
177k
        --len;
573
177k
    }
574
385k
    while (len > 0 && is_whitespace(s[len - 1])) {
575
176k
        --len;
576
176k
    }
577
578
208k
    bool is_negative = false;
579
208k
    if (len > 0) {
580
208k
        switch (*s) {
581
62.7k
        case '-':
582
62.7k
            is_negative = true;
583
62.7k
            [[fallthrough]];
584
92.1k
        case '+':
585
92.1k
            ++s;
586
92.1k
            --len;
587
208k
        }
588
208k
    }
589
590
    // Ignore leading zeros.
591
208k
    bool found_value = false;
592
448k
    while (len > 0 && UNLIKELY(*s == '0')) {
593
239k
        found_value = true;
594
239k
        ++s;
595
239k
        --len;
596
239k
    }
597
598
    // Ignore leading zeros even after a dot. This allows for differentiating between
599
    // cases like 0.01e2, which would fit in a DECIMAL(1, 0), and 0.10e2, which would
600
    // overflow.
601
208k
    int scale = 0;
602
208k
    int found_dot = 0;
603
208k
    if (len > 0 && *s == '.') {
604
34.4k
        found_dot = 1;
605
34.4k
        ++s;
606
34.4k
        --len;
607
657k
        while (len > 0 && UNLIKELY(*s == '0')) {
608
622k
            found_value = true;
609
622k
            ++scale;
610
622k
            ++s;
611
622k
            --len;
612
622k
        }
613
34.4k
    }
614
615
208k
    int precision = 0;
616
208k
    int max_digit = type_precision - type_scale;
617
208k
    int cur_digit = 0;
618
208k
    bool found_exponent = false;
619
208k
    int8_t exponent = 0;
620
208k
    T value = 0;
621
208k
    bool has_round = false;
622
8.54M
    for (int i = 0; i < len; ++i) {
623
8.40M
        const char& c = s[i];
624
8.40M
        if (LIKELY('0' <= c && c <= '9')) {
625
8.17M
            found_value = true;
626
            // Ignore digits once the type's precision limit is reached. This avoids
627
            // overflowing the underlying storage while handling a string like
628
            // 10000000000e-10 into a DECIMAL(1, 0). Adjustments for ignored digits and
629
            // an exponent will be made later.
630
8.17M
            if (LIKELY(type_precision > precision) && !has_round) {
631
8.12M
                value = (value * 10) + (c - '0'); // Benchmarks are faster with parenthesis...
632
8.12M
                ++precision;
633
8.12M
                scale += found_dot;
634
8.12M
                cur_digit = precision - scale;
635
8.12M
            } else if (!found_dot && max_digit < (precision - scale)) {
636
0
                *result = StringParser::PARSE_OVERFLOW;
637
0
                value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
638
0
                                    : vectorized::max_decimal_value<P>(type_precision);
639
0
                return value;
640
48.6k
            } else if (found_dot && scale >= type_scale && !has_round) {
641
                // make rounding cases
642
48.6k
                if (c > '4') {
643
17.9k
                    value += 1;
644
17.9k
                }
645
48.6k
                has_round = true;
646
48.6k
                continue;
647
48.6k
            } else if (!found_dot) {
648
0
                ++cur_digit;
649
0
            }
650
8.12M
            DCHECK(value >= 0); // For some reason //DCHECK_GE doesn't work with __int128.
651
8.12M
        } else if (c == '.' && LIKELY(!found_dot)) {
652
163k
            found_dot = 1;
653
163k
        } else if ((c == 'e' || c == 'E') && LIKELY(!found_exponent)) {
654
70.4k
            found_exponent = true;
655
70.4k
            exponent = string_to_int_internal<int8_t>(s + i + 1, len - i - 1, result);
656
70.4k
            if (UNLIKELY(*result != StringParser::PARSE_SUCCESS)) {
657
10
                if (*result == StringParser::PARSE_OVERFLOW && exponent < 0) {
658
0
                    *result = StringParser::PARSE_UNDERFLOW;
659
0
                }
660
10
                return 0;
661
10
            }
662
70.4k
            break;
663
70.4k
        } else {
664
80
            if (value == 0) {
665
74
                *result = StringParser::PARSE_FAILURE;
666
74
                return 0;
667
74
            }
668
            // here to handle
669
6
            *result = StringParser::PARSE_SUCCESS;
670
6
            if (type_scale >= scale) {
671
6
                value *= get_scale_multiplier<T>(type_scale - scale);
672
                // here meet non-valid character, should return the value, keep going to meet
673
                // the E/e character because we make right user-given type_precision
674
                // not max number type_precision
675
6
                if (!is_numeric_ascii(c)) {
676
6
                    if (cur_digit > type_precision) {
677
0
                        *result = StringParser::PARSE_OVERFLOW;
678
0
                        value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
679
0
                                            : vectorized::max_decimal_value<P>(type_precision);
680
0
                        return value;
681
0
                    }
682
6
                    return is_negative ? T(-value) : T(value);
683
6
                }
684
6
            }
685
686
0
            return is_negative ? T(-value) : T(value);
687
6
        }
688
8.40M
    }
689
690
    // Find the number of truncated digits before adjusting the precision for an exponent.
691
208k
    if (exponent > scale) {
692
        // Ex: 0.1e3 (which at this point would have precision == 1 and scale == 1), the
693
        //     scale must be set to 0 and the value set to 100 which means a precision of 3.
694
68.2k
        precision += exponent - scale;
695
696
68.2k
        value *= get_scale_multiplier<T>(exponent - scale);
697
68.2k
        scale = 0;
698
140k
    } else {
699
        // Ex: 100e-4, the scale must be set to 4 but no adjustment to the value is needed,
700
        //     the precision must also be set to 4 but that will be done below for the
701
        //     non-exponent case anyways.
702
140k
        scale -= exponent;
703
140k
    }
704
    // Ex: 0.001, at this point would have precision 1 and scale 3 since leading zeros
705
    //     were ignored during previous parsing.
706
208k
    if (scale > precision) {
707
15.3k
        precision = scale;
708
15.3k
    }
709
710
    // Microbenchmarks show that beyond this point, returning on parse failure is slower
711
    // than just letting the function run out.
712
208k
    *result = StringParser::PARSE_SUCCESS;
713
208k
    if (UNLIKELY(precision - scale > type_precision - type_scale)) {
714
0
        *result = StringParser::PARSE_OVERFLOW;
715
0
        if constexpr (TYPE_DECIMALV2 != P) {
716
            // decimalv3 overflow will return max min value for type precision
717
0
            value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
718
0
                                : vectorized::max_decimal_value<P>(type_precision);
719
0
            return value;
720
0
        }
721
208k
    } else if (UNLIKELY(scale > type_scale)) {
722
51.2k
        *result = StringParser::PARSE_UNDERFLOW;
723
51.2k
        int shift = scale - type_scale;
724
51.2k
        T divisor = get_scale_multiplier<T>(shift);
725
51.2k
        if (UNLIKELY(divisor == std::numeric_limits<T>::max())) {
726
0
            value = 0;
727
51.2k
        } else {
728
51.2k
            T remainder = value % divisor;
729
51.2k
            value /= divisor;
730
51.2k
            if ((remainder > 0 ? T(remainder) : T(-remainder)) >= (divisor >> 1)) {
731
25.6k
                value += 1;
732
25.6k
            }
733
51.2k
        }
734
51.2k
        DCHECK(value >= 0); // //DCHECK_GE doesn't work with __int128.
735
157k
    } else if (UNLIKELY(!found_value && !found_dot)) {
736
0
        *result = StringParser::PARSE_FAILURE;
737
0
    }
738
739
208k
    if (type_scale > scale) {
740
96.8k
        value *= get_scale_multiplier<T>(type_scale - scale);
741
96.8k
    }
742
743
208k
    return is_negative ? T(-value) : T(value);
744
208k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE20EEENS_19PrimitiveTypeTraitsIXT_EE7CppType10NativeTypeEPKciiiPNS0_11ParseResultE
Line
Count
Source
557
13.5k
        ParseResult* result) {
558
13.5k
    using T = typename PrimitiveTypeTraits<P>::CppType::NativeType;
559
13.5k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
560
13.5k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
561
13.5k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
562
13.5k
                  "wide::Int256.");
563
    // Special cases:
564
    //   1) '' == Fail, an empty string fails to parse.
565
    //   2) '   #   ' == #, leading and trailing white space is ignored.
566
    //   3) '.' == 0, a single dot parses as zero (for consistency with other types).
567
    //   4) '#.' == '#', a trailing dot is ignored.
568
569
    // Ignore leading and trailing spaces.
570
13.5k
    while (len > 0 && is_whitespace(*s)) {
571
0
        ++s;
572
0
        --len;
573
0
    }
574
13.5k
    while (len > 0 && is_whitespace(s[len - 1])) {
575
0
        --len;
576
0
    }
577
578
13.5k
    bool is_negative = false;
579
13.5k
    if (len > 0) {
580
13.5k
        switch (*s) {
581
6.68k
        case '-':
582
6.68k
            is_negative = true;
583
6.68k
            [[fallthrough]];
584
6.68k
        case '+':
585
6.68k
            ++s;
586
6.68k
            --len;
587
13.5k
        }
588
13.5k
    }
589
590
    // Ignore leading zeros.
591
13.5k
    bool found_value = false;
592
52.3k
    while (len > 0 && UNLIKELY(*s == '0')) {
593
38.8k
        found_value = true;
594
38.8k
        ++s;
595
38.8k
        --len;
596
38.8k
    }
597
598
    // Ignore leading zeros even after a dot. This allows for differentiating between
599
    // cases like 0.01e2, which would fit in a DECIMAL(1, 0), and 0.10e2, which would
600
    // overflow.
601
13.5k
    int scale = 0;
602
13.5k
    int found_dot = 0;
603
13.5k
    if (len > 0 && *s == '.') {
604
2.00k
        found_dot = 1;
605
2.00k
        ++s;
606
2.00k
        --len;
607
6.43k
        while (len > 0 && UNLIKELY(*s == '0')) {
608
4.42k
            found_value = true;
609
4.42k
            ++scale;
610
4.42k
            ++s;
611
4.42k
            --len;
612
4.42k
        }
613
2.00k
    }
614
615
13.5k
    int precision = 0;
616
13.5k
    int max_digit = type_precision - type_scale;
617
13.5k
    int cur_digit = 0;
618
13.5k
    bool found_exponent = false;
619
13.5k
    int8_t exponent = 0;
620
13.5k
    T value = 0;
621
13.5k
    bool has_round = false;
622
275k
    for (int i = 0; i < len; ++i) {
623
261k
        const char& c = s[i];
624
261k
        if (LIKELY('0' <= c && c <= '9')) {
625
250k
            found_value = true;
626
            // Ignore digits once the type's precision limit is reached. This avoids
627
            // overflowing the underlying storage while handling a string like
628
            // 10000000000e-10 into a DECIMAL(1, 0). Adjustments for ignored digits and
629
            // an exponent will be made later.
630
250k
            if (LIKELY(type_precision > precision) && !has_round) {
631
250k
                value = (value * 10) + (c - '0'); // Benchmarks are faster with parenthesis...
632
250k
                ++precision;
633
250k
                scale += found_dot;
634
250k
                cur_digit = precision - scale;
635
250k
            } else if (!found_dot && max_digit < (precision - scale)) {
636
0
                *result = StringParser::PARSE_OVERFLOW;
637
0
                value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
638
0
                                    : vectorized::max_decimal_value<P>(type_precision);
639
0
                return value;
640
8
            } else if (found_dot && scale >= type_scale && !has_round) {
641
                // make rounding cases
642
8
                if (c > '4') {
643
0
                    value += 1;
644
0
                }
645
8
                has_round = true;
646
8
                continue;
647
8
            } else if (!found_dot) {
648
0
                ++cur_digit;
649
0
            }
650
250k
            DCHECK(value >= 0); // For some reason //DCHECK_GE doesn't work with __int128.
651
250k
        } else if (c == '.' && LIKELY(!found_dot)) {
652
11.4k
            found_dot = 1;
653
11.4k
        } else if ((c == 'e' || c == 'E') && LIKELY(!found_exponent)) {
654
0
            found_exponent = true;
655
0
            exponent = string_to_int_internal<int8_t>(s + i + 1, len - i - 1, result);
656
0
            if (UNLIKELY(*result != StringParser::PARSE_SUCCESS)) {
657
0
                if (*result == StringParser::PARSE_OVERFLOW && exponent < 0) {
658
0
                    *result = StringParser::PARSE_UNDERFLOW;
659
0
                }
660
0
                return 0;
661
0
            }
662
0
            break;
663
16
        } else {
664
16
            if (value == 0) {
665
10
                *result = StringParser::PARSE_FAILURE;
666
10
                return 0;
667
10
            }
668
            // here to handle
669
6
            *result = StringParser::PARSE_SUCCESS;
670
6
            if (type_scale >= scale) {
671
6
                value *= get_scale_multiplier<T>(type_scale - scale);
672
                // here meet non-valid character, should return the value, keep going to meet
673
                // the E/e character because we make right user-given type_precision
674
                // not max number type_precision
675
6
                if (!is_numeric_ascii(c)) {
676
6
                    if (cur_digit > type_precision) {
677
0
                        *result = StringParser::PARSE_OVERFLOW;
678
0
                        value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
679
0
                                            : vectorized::max_decimal_value<P>(type_precision);
680
0
                        return value;
681
0
                    }
682
6
                    return is_negative ? T(-value) : T(value);
683
6
                }
684
6
            }
685
686
0
            return is_negative ? T(-value) : T(value);
687
6
        }
688
261k
    }
689
690
    // Find the number of truncated digits before adjusting the precision for an exponent.
691
13.5k
    if (exponent > scale) {
692
        // Ex: 0.1e3 (which at this point would have precision == 1 and scale == 1), the
693
        //     scale must be set to 0 and the value set to 100 which means a precision of 3.
694
0
        precision += exponent - scale;
695
696
0
        value *= get_scale_multiplier<T>(exponent - scale);
697
0
        scale = 0;
698
13.5k
    } else {
699
        // Ex: 100e-4, the scale must be set to 4 but no adjustment to the value is needed,
700
        //     the precision must also be set to 4 but that will be done below for the
701
        //     non-exponent case anyways.
702
13.5k
        scale -= exponent;
703
13.5k
    }
704
    // Ex: 0.001, at this point would have precision 1 and scale 3 since leading zeros
705
    //     were ignored during previous parsing.
706
13.5k
    if (scale > precision) {
707
675
        precision = scale;
708
675
    }
709
710
    // Microbenchmarks show that beyond this point, returning on parse failure is slower
711
    // than just letting the function run out.
712
13.5k
    *result = StringParser::PARSE_SUCCESS;
713
13.5k
    if (UNLIKELY(precision - scale > type_precision - type_scale)) {
714
9
        *result = StringParser::PARSE_OVERFLOW;
715
9
        if constexpr (TYPE_DECIMALV2 != P) {
716
            // decimalv3 overflow will return max min value for type precision
717
9
            value = is_negative ? vectorized::min_decimal_value<P>(type_precision)
718
9
                                : vectorized::max_decimal_value<P>(type_precision);
719
9
            return value;
720
9
        }
721
13.5k
    } else if (UNLIKELY(scale > type_scale)) {
722
17
        *result = StringParser::PARSE_UNDERFLOW;
723
17
        int shift = scale - type_scale;
724
17
        T divisor = get_scale_multiplier<T>(shift);
725
17
        if (UNLIKELY(divisor == std::numeric_limits<T>::max())) {
726
0
            value = 0;
727
17
        } else {
728
17
            T remainder = value % divisor;
729
17
            value /= divisor;
730
17
            if ((remainder > 0 ? T(remainder) : T(-remainder)) >= (divisor >> 1)) {
731
17
                value += 1;
732
17
            }
733
17
        }
734
17
        DCHECK(value >= 0); // //DCHECK_GE doesn't work with __int128.
735
13.5k
    } else if (UNLIKELY(!found_value && !found_dot)) {
736
0
        *result = StringParser::PARSE_FAILURE;
737
0
    }
738
739
13.5k
    if (type_scale > scale) {
740
1.95k
        value *= get_scale_multiplier<T>(type_scale - scale);
741
1.95k
    }
742
743
13.5k
    return is_negative ? T(-value) : T(value);
744
13.5k
}
745
746
} // end namespace doris