Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:41:01

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_CHARCONV_H_
0016 #define ABSL_STRINGS_CHARCONV_H_
0017 
0018 #include <system_error>  // NOLINT(build/c++11)
0019 
0020 #include "absl/base/config.h"
0021 #include "absl/base/nullability.h"
0022 
0023 namespace absl {
0024 ABSL_NAMESPACE_BEGIN
0025 
0026 // Workalike compatibility version of std::chars_format from C++17.
0027 //
0028 // This is an bitfield enumerator which can be passed to absl::from_chars to
0029 // configure the string-to-float conversion.
0030 enum class chars_format {
0031   scientific = 1,
0032   fixed = 2,
0033   hex = 4,
0034   general = fixed | scientific,
0035 };
0036 
0037 // The return result of a string-to-number conversion.
0038 //
0039 // `ec` will be set to `invalid_argument` if a well-formed number was not found
0040 // at the start of the input range, `result_out_of_range` if a well-formed
0041 // number was found, but it was out of the representable range of the requested
0042 // type, or to std::errc() otherwise.
0043 //
0044 // If a well-formed number was found, `ptr` is set to one past the sequence of
0045 // characters that were successfully parsed.  If none was found, `ptr` is set
0046 // to the `first` argument to from_chars.
0047 struct from_chars_result {
0048   absl::Nonnull<const char*> ptr;
0049   std::errc ec;
0050 };
0051 
0052 // Workalike compatibility version of std::from_chars from C++17.  Currently
0053 // this only supports the `double` and `float` types.
0054 //
0055 // This interface incorporates the proposed resolutions for library issues
0056 // DR 3080 and DR 3081.  If these are adopted with different wording,
0057 // Abseil's behavior will change to match the standard.  (The behavior most
0058 // likely to change is for DR 3081, which says what `value` will be set to in
0059 // the case of overflow and underflow.  Code that wants to avoid possible
0060 // breaking changes in this area should not depend on `value` when the returned
0061 // from_chars_result indicates a range error.)
0062 //
0063 // Searches the range [first, last) for the longest matching pattern beginning
0064 // at `first` that represents a floating point number.  If one is found, store
0065 // the result in `value`.
0066 //
0067 // The matching pattern format is almost the same as that of strtod(), except
0068 // that (1) C locale is not respected, (2) an initial '+' character in the
0069 // input range will never be matched, and (3) leading whitespaces are not
0070 // ignored.
0071 //
0072 // If `fmt` is set, it must be one of the enumerator values of the chars_format.
0073 // (This is despite the fact that chars_format is a bitmask type.)  If set to
0074 // `scientific`, a matching number must contain an exponent.  If set to `fixed`,
0075 // then an exponent will never match.  (For example, the string "1e5" will be
0076 // parsed as "1".)  If set to `hex`, then a hexadecimal float is parsed in the
0077 // format that strtod() accepts, except that a "0x" prefix is NOT matched.
0078 // (In particular, in `hex` mode, the input "0xff" results in the largest
0079 // matching pattern "0".)
0080 absl::from_chars_result from_chars(absl::Nonnull<const char*> first,
0081                                    absl::Nonnull<const char*> last,
0082                                    double& value,  // NOLINT
0083                                    chars_format fmt = chars_format::general);
0084 
0085 absl::from_chars_result from_chars(absl::Nonnull<const char*> first,
0086                                    absl::Nonnull<const char*> last,
0087                                    float& value,  // NOLINT
0088                                    chars_format fmt = chars_format::general);
0089 
0090 // std::chars_format is specified as a bitmask type, which means the following
0091 // operations must be provided:
0092 inline constexpr chars_format operator&(chars_format lhs, chars_format rhs) {
0093   return static_cast<chars_format>(static_cast<int>(lhs) &
0094                                    static_cast<int>(rhs));
0095 }
0096 inline constexpr chars_format operator|(chars_format lhs, chars_format rhs) {
0097   return static_cast<chars_format>(static_cast<int>(lhs) |
0098                                    static_cast<int>(rhs));
0099 }
0100 inline constexpr chars_format operator^(chars_format lhs, chars_format rhs) {
0101   return static_cast<chars_format>(static_cast<int>(lhs) ^
0102                                    static_cast<int>(rhs));
0103 }
0104 inline constexpr chars_format operator~(chars_format arg) {
0105   return static_cast<chars_format>(~static_cast<int>(arg));
0106 }
0107 inline chars_format& operator&=(chars_format& lhs, chars_format rhs) {
0108   lhs = lhs & rhs;
0109   return lhs;
0110 }
0111 inline chars_format& operator|=(chars_format& lhs, chars_format rhs) {
0112   lhs = lhs | rhs;
0113   return lhs;
0114 }
0115 inline chars_format& operator^=(chars_format& lhs, chars_format rhs) {
0116   lhs = lhs ^ rhs;
0117   return lhs;
0118 }
0119 
0120 ABSL_NAMESPACE_END
0121 }  // namespace absl
0122 
0123 #endif  // ABSL_STRINGS_CHARCONV_H_