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_CHARCONV_H_ 16*9356374aSAndroid Build Coastguard Worker #define ABSL_STRINGS_CHARCONV_H_ 17*9356374aSAndroid Build Coastguard Worker 18*9356374aSAndroid Build Coastguard Worker #include <system_error> // NOLINT(build/c++11) 19*9356374aSAndroid Build Coastguard Worker 20*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h" 21*9356374aSAndroid Build Coastguard Worker #include "absl/base/nullability.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 26*9356374aSAndroid Build Coastguard Worker // Workalike compatibility version of std::chars_format from C++17. 27*9356374aSAndroid Build Coastguard Worker // 28*9356374aSAndroid Build Coastguard Worker // This is an bitfield enumerator which can be passed to absl::from_chars to 29*9356374aSAndroid Build Coastguard Worker // configure the string-to-float conversion. 30*9356374aSAndroid Build Coastguard Worker enum class chars_format { 31*9356374aSAndroid Build Coastguard Worker scientific = 1, 32*9356374aSAndroid Build Coastguard Worker fixed = 2, 33*9356374aSAndroid Build Coastguard Worker hex = 4, 34*9356374aSAndroid Build Coastguard Worker general = fixed | scientific, 35*9356374aSAndroid Build Coastguard Worker }; 36*9356374aSAndroid Build Coastguard Worker 37*9356374aSAndroid Build Coastguard Worker // The return result of a string-to-number conversion. 38*9356374aSAndroid Build Coastguard Worker // 39*9356374aSAndroid Build Coastguard Worker // `ec` will be set to `invalid_argument` if a well-formed number was not found 40*9356374aSAndroid Build Coastguard Worker // at the start of the input range, `result_out_of_range` if a well-formed 41*9356374aSAndroid Build Coastguard Worker // number was found, but it was out of the representable range of the requested 42*9356374aSAndroid Build Coastguard Worker // type, or to std::errc() otherwise. 43*9356374aSAndroid Build Coastguard Worker // 44*9356374aSAndroid Build Coastguard Worker // If a well-formed number was found, `ptr` is set to one past the sequence of 45*9356374aSAndroid Build Coastguard Worker // characters that were successfully parsed. If none was found, `ptr` is set 46*9356374aSAndroid Build Coastguard Worker // to the `first` argument to from_chars. 47*9356374aSAndroid Build Coastguard Worker struct from_chars_result { 48*9356374aSAndroid Build Coastguard Worker absl::Nonnull<const char*> ptr; 49*9356374aSAndroid Build Coastguard Worker std::errc ec; 50*9356374aSAndroid Build Coastguard Worker }; 51*9356374aSAndroid Build Coastguard Worker 52*9356374aSAndroid Build Coastguard Worker // Workalike compatibility version of std::from_chars from C++17. Currently 53*9356374aSAndroid Build Coastguard Worker // this only supports the `double` and `float` types. 54*9356374aSAndroid Build Coastguard Worker // 55*9356374aSAndroid Build Coastguard Worker // This interface incorporates the proposed resolutions for library issues 56*9356374aSAndroid Build Coastguard Worker // DR 3080 and DR 3081. If these are adopted with different wording, 57*9356374aSAndroid Build Coastguard Worker // Abseil's behavior will change to match the standard. (The behavior most 58*9356374aSAndroid Build Coastguard Worker // likely to change is for DR 3081, which says what `value` will be set to in 59*9356374aSAndroid Build Coastguard Worker // the case of overflow and underflow. Code that wants to avoid possible 60*9356374aSAndroid Build Coastguard Worker // breaking changes in this area should not depend on `value` when the returned 61*9356374aSAndroid Build Coastguard Worker // from_chars_result indicates a range error.) 62*9356374aSAndroid Build Coastguard Worker // 63*9356374aSAndroid Build Coastguard Worker // Searches the range [first, last) for the longest matching pattern beginning 64*9356374aSAndroid Build Coastguard Worker // at `first` that represents a floating point number. If one is found, store 65*9356374aSAndroid Build Coastguard Worker // the result in `value`. 66*9356374aSAndroid Build Coastguard Worker // 67*9356374aSAndroid Build Coastguard Worker // The matching pattern format is almost the same as that of strtod(), except 68*9356374aSAndroid Build Coastguard Worker // that (1) C locale is not respected, (2) an initial '+' character in the 69*9356374aSAndroid Build Coastguard Worker // input range will never be matched, and (3) leading whitespaces are not 70*9356374aSAndroid Build Coastguard Worker // ignored. 71*9356374aSAndroid Build Coastguard Worker // 72*9356374aSAndroid Build Coastguard Worker // If `fmt` is set, it must be one of the enumerator values of the chars_format. 73*9356374aSAndroid Build Coastguard Worker // (This is despite the fact that chars_format is a bitmask type.) If set to 74*9356374aSAndroid Build Coastguard Worker // `scientific`, a matching number must contain an exponent. If set to `fixed`, 75*9356374aSAndroid Build Coastguard Worker // then an exponent will never match. (For example, the string "1e5" will be 76*9356374aSAndroid Build Coastguard Worker // parsed as "1".) If set to `hex`, then a hexadecimal float is parsed in the 77*9356374aSAndroid Build Coastguard Worker // format that strtod() accepts, except that a "0x" prefix is NOT matched. 78*9356374aSAndroid Build Coastguard Worker // (In particular, in `hex` mode, the input "0xff" results in the largest 79*9356374aSAndroid Build Coastguard Worker // matching pattern "0".) 80*9356374aSAndroid Build Coastguard Worker absl::from_chars_result from_chars(absl::Nonnull<const char*> first, 81*9356374aSAndroid Build Coastguard Worker absl::Nonnull<const char*> last, 82*9356374aSAndroid Build Coastguard Worker double& value, // NOLINT 83*9356374aSAndroid Build Coastguard Worker chars_format fmt = chars_format::general); 84*9356374aSAndroid Build Coastguard Worker 85*9356374aSAndroid Build Coastguard Worker absl::from_chars_result from_chars(absl::Nonnull<const char*> first, 86*9356374aSAndroid Build Coastguard Worker absl::Nonnull<const char*> last, 87*9356374aSAndroid Build Coastguard Worker float& value, // NOLINT 88*9356374aSAndroid Build Coastguard Worker chars_format fmt = chars_format::general); 89*9356374aSAndroid Build Coastguard Worker 90*9356374aSAndroid Build Coastguard Worker // std::chars_format is specified as a bitmask type, which means the following 91*9356374aSAndroid Build Coastguard Worker // operations must be provided: 92*9356374aSAndroid Build Coastguard Worker inline constexpr chars_format operator&(chars_format lhs, chars_format rhs) { 93*9356374aSAndroid Build Coastguard Worker return static_cast<chars_format>(static_cast<int>(lhs) & 94*9356374aSAndroid Build Coastguard Worker static_cast<int>(rhs)); 95*9356374aSAndroid Build Coastguard Worker } 96*9356374aSAndroid Build Coastguard Worker inline constexpr chars_format operator|(chars_format lhs, chars_format rhs) { 97*9356374aSAndroid Build Coastguard Worker return static_cast<chars_format>(static_cast<int>(lhs) | 98*9356374aSAndroid Build Coastguard Worker static_cast<int>(rhs)); 99*9356374aSAndroid Build Coastguard Worker } 100*9356374aSAndroid Build Coastguard Worker inline constexpr chars_format operator^(chars_format lhs, chars_format rhs) { 101*9356374aSAndroid Build Coastguard Worker return static_cast<chars_format>(static_cast<int>(lhs) ^ 102*9356374aSAndroid Build Coastguard Worker static_cast<int>(rhs)); 103*9356374aSAndroid Build Coastguard Worker } 104*9356374aSAndroid Build Coastguard Worker inline constexpr chars_format operator~(chars_format arg) { 105*9356374aSAndroid Build Coastguard Worker return static_cast<chars_format>(~static_cast<int>(arg)); 106*9356374aSAndroid Build Coastguard Worker } 107*9356374aSAndroid Build Coastguard Worker inline chars_format& operator&=(chars_format& lhs, chars_format rhs) { 108*9356374aSAndroid Build Coastguard Worker lhs = lhs & rhs; 109*9356374aSAndroid Build Coastguard Worker return lhs; 110*9356374aSAndroid Build Coastguard Worker } 111*9356374aSAndroid Build Coastguard Worker inline chars_format& operator|=(chars_format& lhs, chars_format rhs) { 112*9356374aSAndroid Build Coastguard Worker lhs = lhs | rhs; 113*9356374aSAndroid Build Coastguard Worker return lhs; 114*9356374aSAndroid Build Coastguard Worker } 115*9356374aSAndroid Build Coastguard Worker inline chars_format& operator^=(chars_format& lhs, chars_format rhs) { 116*9356374aSAndroid Build Coastguard Worker lhs = lhs ^ rhs; 117*9356374aSAndroid Build Coastguard Worker return lhs; 118*9356374aSAndroid Build Coastguard Worker } 119*9356374aSAndroid Build Coastguard Worker 120*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END 121*9356374aSAndroid Build Coastguard Worker } // namespace absl 122*9356374aSAndroid Build Coastguard Worker 123*9356374aSAndroid Build Coastguard Worker #endif // ABSL_STRINGS_CHARCONV_H_ 124