Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:24

0001 // Copyright 2018 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_STRINGS_INTERNAL_CHARCONV_PARSE_H_
0016 #define ABSL_STRINGS_INTERNAL_CHARCONV_PARSE_H_
0017 
0018 #include <cstdint>
0019 
0020 #include "absl/base/config.h"
0021 #include "absl/strings/charconv.h"
0022 
0023 namespace absl {
0024 ABSL_NAMESPACE_BEGIN
0025 namespace strings_internal {
0026 
0027 // Enum indicating whether a parsed float is a number or special value.
0028 enum class FloatType { kNumber, kInfinity, kNan };
0029 
0030 // The decomposed parts of a parsed `float` or `double`.
0031 struct ParsedFloat {
0032   // Representation of the parsed mantissa, with the decimal point adjusted to
0033   // make it an integer.
0034   //
0035   // During decimal scanning, this contains 19 significant digits worth of
0036   // mantissa value.  If digits beyond this point are found, they
0037   // are truncated, and if any of these dropped digits are nonzero, then
0038   // `mantissa` is inexact, and the full mantissa is stored in [subrange_begin,
0039   // subrange_end).
0040   //
0041   // During hexadecimal scanning, this contains 15 significant hex digits worth
0042   // of mantissa value.  Digits beyond this point are sticky -- they are
0043   // truncated, but if any dropped digits are nonzero, the low bit of mantissa
0044   // will be set.  (This allows for precise rounding, and avoids the need
0045   // to store the full mantissa in [subrange_begin, subrange_end).)
0046   uint64_t mantissa = 0;
0047 
0048   // Floating point expontent.  This reflects any decimal point adjustments and
0049   // any truncated digits from the mantissa.  The absolute value of the parsed
0050   // number is represented by mantissa * (base ** exponent), where base==10 for
0051   // decimal floats, and base==2 for hexadecimal floats.
0052   int exponent = 0;
0053 
0054   // The literal exponent value scanned from the input, or 0 if none was
0055   // present.  This does not reflect any adjustments applied to mantissa.
0056   int literal_exponent = 0;
0057 
0058   // The type of number scanned.
0059   FloatType type = FloatType::kNumber;
0060 
0061   // When non-null, [subrange_begin, subrange_end) marks a range of characters
0062   // that require further processing.  The meaning is dependent on float type.
0063   // If type == kNumber and this is set, this is a "wide input": the input
0064   // mantissa contained more than 19 digits.  The range contains the full
0065   // mantissa.  It plus `literal_exponent` need to be examined to find the best
0066   // floating point match.
0067   // If type == kNan and this is set, the range marks the contents of a
0068   // matched parenthesized character region after the NaN.
0069   const char* subrange_begin = nullptr;
0070   const char* subrange_end = nullptr;
0071 
0072   // One-past-the-end of the successfully parsed region, or nullptr if no
0073   // matching pattern was found.
0074   const char* end = nullptr;
0075 };
0076 
0077 // Read the floating point number in the provided range, and populate
0078 // ParsedFloat accordingly.
0079 //
0080 // format_flags is a bitmask value specifying what patterns this API will match.
0081 // `scientific` and `fixed`  are honored per std::from_chars rules
0082 // ([utility.from.chars], C++17): if exactly one of these bits is set, then an
0083 // exponent is required, or dislallowed, respectively.
0084 //
0085 // Template parameter `base` must be either 10 or 16.  For base 16, a "0x" is
0086 // *not* consumed.  The `hex` bit from format_flags is ignored by ParseFloat.
0087 template <int base>
0088 ParsedFloat ParseFloat(const char* begin, const char* end,
0089                        absl::chars_format format_flags);
0090 
0091 extern template ParsedFloat ParseFloat<10>(const char* begin, const char* end,
0092                                            absl::chars_format format_flags);
0093 extern template ParsedFloat ParseFloat<16>(const char* begin, const char* end,
0094                                            absl::chars_format format_flags);
0095 
0096 }  // namespace strings_internal
0097 ABSL_NAMESPACE_END
0098 }  // namespace absl
0099 #endif  // ABSL_STRINGS_INTERNAL_CHARCONV_PARSE_H_