xref: /aosp_15_r20/external/abseil-cpp/absl/strings/internal/charconv_parse.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2018 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_STRINGS_INTERNAL_CHARCONV_PARSE_H_
16*9356374aSAndroid Build Coastguard Worker #define ABSL_STRINGS_INTERNAL_CHARCONV_PARSE_H_
17*9356374aSAndroid Build Coastguard Worker 
18*9356374aSAndroid Build Coastguard Worker #include <cstdint>
19*9356374aSAndroid Build Coastguard Worker 
20*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
21*9356374aSAndroid Build Coastguard Worker #include "absl/strings/charconv.h"
22*9356374aSAndroid Build Coastguard Worker 
23*9356374aSAndroid Build Coastguard Worker namespace absl {
24*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
25*9356374aSAndroid Build Coastguard Worker namespace strings_internal {
26*9356374aSAndroid Build Coastguard Worker 
27*9356374aSAndroid Build Coastguard Worker // Enum indicating whether a parsed float is a number or special value.
28*9356374aSAndroid Build Coastguard Worker enum class FloatType { kNumber, kInfinity, kNan };
29*9356374aSAndroid Build Coastguard Worker 
30*9356374aSAndroid Build Coastguard Worker // The decomposed parts of a parsed `float` or `double`.
31*9356374aSAndroid Build Coastguard Worker struct ParsedFloat {
32*9356374aSAndroid Build Coastguard Worker   // Representation of the parsed mantissa, with the decimal point adjusted to
33*9356374aSAndroid Build Coastguard Worker   // make it an integer.
34*9356374aSAndroid Build Coastguard Worker   //
35*9356374aSAndroid Build Coastguard Worker   // During decimal scanning, this contains 19 significant digits worth of
36*9356374aSAndroid Build Coastguard Worker   // mantissa value.  If digits beyond this point are found, they
37*9356374aSAndroid Build Coastguard Worker   // are truncated, and if any of these dropped digits are nonzero, then
38*9356374aSAndroid Build Coastguard Worker   // `mantissa` is inexact, and the full mantissa is stored in [subrange_begin,
39*9356374aSAndroid Build Coastguard Worker   // subrange_end).
40*9356374aSAndroid Build Coastguard Worker   //
41*9356374aSAndroid Build Coastguard Worker   // During hexadecimal scanning, this contains 15 significant hex digits worth
42*9356374aSAndroid Build Coastguard Worker   // of mantissa value.  Digits beyond this point are sticky -- they are
43*9356374aSAndroid Build Coastguard Worker   // truncated, but if any dropped digits are nonzero, the low bit of mantissa
44*9356374aSAndroid Build Coastguard Worker   // will be set.  (This allows for precise rounding, and avoids the need
45*9356374aSAndroid Build Coastguard Worker   // to store the full mantissa in [subrange_begin, subrange_end).)
46*9356374aSAndroid Build Coastguard Worker   uint64_t mantissa = 0;
47*9356374aSAndroid Build Coastguard Worker 
48*9356374aSAndroid Build Coastguard Worker   // Floating point expontent.  This reflects any decimal point adjustments and
49*9356374aSAndroid Build Coastguard Worker   // any truncated digits from the mantissa.  The absolute value of the parsed
50*9356374aSAndroid Build Coastguard Worker   // number is represented by mantissa * (base ** exponent), where base==10 for
51*9356374aSAndroid Build Coastguard Worker   // decimal floats, and base==2 for hexadecimal floats.
52*9356374aSAndroid Build Coastguard Worker   int exponent = 0;
53*9356374aSAndroid Build Coastguard Worker 
54*9356374aSAndroid Build Coastguard Worker   // The literal exponent value scanned from the input, or 0 if none was
55*9356374aSAndroid Build Coastguard Worker   // present.  This does not reflect any adjustments applied to mantissa.
56*9356374aSAndroid Build Coastguard Worker   int literal_exponent = 0;
57*9356374aSAndroid Build Coastguard Worker 
58*9356374aSAndroid Build Coastguard Worker   // The type of number scanned.
59*9356374aSAndroid Build Coastguard Worker   FloatType type = FloatType::kNumber;
60*9356374aSAndroid Build Coastguard Worker 
61*9356374aSAndroid Build Coastguard Worker   // When non-null, [subrange_begin, subrange_end) marks a range of characters
62*9356374aSAndroid Build Coastguard Worker   // that require further processing.  The meaning is dependent on float type.
63*9356374aSAndroid Build Coastguard Worker   // If type == kNumber and this is set, this is a "wide input": the input
64*9356374aSAndroid Build Coastguard Worker   // mantissa contained more than 19 digits.  The range contains the full
65*9356374aSAndroid Build Coastguard Worker   // mantissa.  It plus `literal_exponent` need to be examined to find the best
66*9356374aSAndroid Build Coastguard Worker   // floating point match.
67*9356374aSAndroid Build Coastguard Worker   // If type == kNan and this is set, the range marks the contents of a
68*9356374aSAndroid Build Coastguard Worker   // matched parenthesized character region after the NaN.
69*9356374aSAndroid Build Coastguard Worker   const char* subrange_begin = nullptr;
70*9356374aSAndroid Build Coastguard Worker   const char* subrange_end = nullptr;
71*9356374aSAndroid Build Coastguard Worker 
72*9356374aSAndroid Build Coastguard Worker   // One-past-the-end of the successfully parsed region, or nullptr if no
73*9356374aSAndroid Build Coastguard Worker   // matching pattern was found.
74*9356374aSAndroid Build Coastguard Worker   const char* end = nullptr;
75*9356374aSAndroid Build Coastguard Worker };
76*9356374aSAndroid Build Coastguard Worker 
77*9356374aSAndroid Build Coastguard Worker // Read the floating point number in the provided range, and populate
78*9356374aSAndroid Build Coastguard Worker // ParsedFloat accordingly.
79*9356374aSAndroid Build Coastguard Worker //
80*9356374aSAndroid Build Coastguard Worker // format_flags is a bitmask value specifying what patterns this API will match.
81*9356374aSAndroid Build Coastguard Worker // `scientific` and `fixed`  are honored per std::from_chars rules
82*9356374aSAndroid Build Coastguard Worker // ([utility.from.chars], C++17): if exactly one of these bits is set, then an
83*9356374aSAndroid Build Coastguard Worker // exponent is required, or dislallowed, respectively.
84*9356374aSAndroid Build Coastguard Worker //
85*9356374aSAndroid Build Coastguard Worker // Template parameter `base` must be either 10 or 16.  For base 16, a "0x" is
86*9356374aSAndroid Build Coastguard Worker // *not* consumed.  The `hex` bit from format_flags is ignored by ParseFloat.
87*9356374aSAndroid Build Coastguard Worker template <int base>
88*9356374aSAndroid Build Coastguard Worker ParsedFloat ParseFloat(const char* begin, const char* end,
89*9356374aSAndroid Build Coastguard Worker                        absl::chars_format format_flags);
90*9356374aSAndroid Build Coastguard Worker 
91*9356374aSAndroid Build Coastguard Worker extern template ParsedFloat ParseFloat<10>(const char* begin, const char* end,
92*9356374aSAndroid Build Coastguard Worker                                            absl::chars_format format_flags);
93*9356374aSAndroid Build Coastguard Worker extern template ParsedFloat ParseFloat<16>(const char* begin, const char* end,
94*9356374aSAndroid Build Coastguard Worker                                            absl::chars_format format_flags);
95*9356374aSAndroid Build Coastguard Worker 
96*9356374aSAndroid Build Coastguard Worker }  // namespace strings_internal
97*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
98*9356374aSAndroid Build Coastguard Worker }  // namespace absl
99*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_STRINGS_INTERNAL_CHARCONV_PARSE_H_
100