00001 // Copyright 2018 The Abseil Authors. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); 00004 // you may not use this file except in compliance with the License. 00005 // You may obtain a copy of the License at 00006 // 00007 // https://www.apache.org/licenses/LICENSE-2.0 00008 // 00009 // Unless required by applicable law or agreed to in writing, software 00010 // distributed under the License is distributed on an "AS IS" BASIS, 00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 // See the License for the specific language governing permissions and 00013 // limitations under the License. 00014 00015 #ifndef ABSL_STRINGS_CHARCONV_H_ 00016 #define ABSL_STRINGS_CHARCONV_H_ 00017 00018 #include <system_error> // NOLINT(build/c++11) 00019 00020 namespace absl { 00021 00022 // Workalike compatibilty version of std::chars_format from C++17. 00023 // 00024 // This is an bitfield enumerator which can be passed to absl::from_chars to 00025 // configure the string-to-float conversion. 00026 enum class chars_format { 00027 scientific = 1, 00028 fixed = 2, 00029 hex = 4, 00030 general = fixed | scientific, 00031 }; 00032 00033 // The return result of a string-to-number conversion. 00034 // 00035 // `ec` will be set to `invalid_argument` if a well-formed number was not found 00036 // at the start of the input range, `result_out_of_range` if a well-formed 00037 // number was found, but it was out of the representable range of the requested 00038 // type, or to std::errc() otherwise. 00039 // 00040 // If a well-formed number was found, `ptr` is set to one past the sequence of 00041 // characters that were successfully parsed. If none was found, `ptr` is set 00042 // to the `first` argument to from_chars. 00043 struct from_chars_result { 00044 const char* ptr; 00045 std::errc ec; 00046 }; 00047 00048 // Workalike compatibilty version of std::from_chars from C++17. Currently 00049 // this only supports the `double` and `float` types. 00050 // 00051 // This interface incorporates the proposed resolutions for library issues 00052 // DR 3080 and DR 3081. If these are adopted with different wording, 00053 // Abseil's behavior will change to match the standard. (The behavior most 00054 // likely to change is for DR 3081, which says what `value` will be set to in 00055 // the case of overflow and underflow. Code that wants to avoid possible 00056 // breaking changes in this area should not depend on `value` when the returned 00057 // from_chars_result indicates a range error.) 00058 // 00059 // Searches the range [first, last) for the longest matching pattern beginning 00060 // at `first` that represents a floating point number. If one is found, store 00061 // the result in `value`. 00062 // 00063 // The matching pattern format is almost the same as that of strtod(), except 00064 // that C locale is not respected, and an initial '+' character in the input 00065 // range will never be matched. 00066 // 00067 // If `fmt` is set, it must be one of the enumerator values of the chars_format. 00068 // (This is despite the fact that chars_format is a bitmask type.) If set to 00069 // `scientific`, a matching number must contain an exponent. If set to `fixed`, 00070 // then an exponent will never match. (For example, the string "1e5" will be 00071 // parsed as "1".) If set to `hex`, then a hexadecimal float is parsed in the 00072 // format that strtod() accepts, except that a "0x" prefix is NOT matched. 00073 // (In particular, in `hex` mode, the input "0xff" results in the largest 00074 // matching pattern "0".) 00075 absl::from_chars_result from_chars(const char* first, const char* last, 00076 double& value, // NOLINT 00077 chars_format fmt = chars_format::general); 00078 00079 absl::from_chars_result from_chars(const char* first, const char* last, 00080 float& value, // NOLINT 00081 chars_format fmt = chars_format::general); 00082 00083 // std::chars_format is specified as a bitmask type, which means the following 00084 // operations must be provided: 00085 inline constexpr chars_format operator&(chars_format lhs, chars_format rhs) { 00086 return static_cast<chars_format>(static_cast<int>(lhs) & 00087 static_cast<int>(rhs)); 00088 } 00089 inline constexpr chars_format operator|(chars_format lhs, chars_format rhs) { 00090 return static_cast<chars_format>(static_cast<int>(lhs) | 00091 static_cast<int>(rhs)); 00092 } 00093 inline constexpr chars_format operator^(chars_format lhs, chars_format rhs) { 00094 return static_cast<chars_format>(static_cast<int>(lhs) ^ 00095 static_cast<int>(rhs)); 00096 } 00097 inline constexpr chars_format operator~(chars_format arg) { 00098 return static_cast<chars_format>(~static_cast<int>(arg)); 00099 } 00100 inline chars_format& operator&=(chars_format& lhs, chars_format rhs) { 00101 lhs = lhs & rhs; 00102 return lhs; 00103 } 00104 inline chars_format& operator|=(chars_format& lhs, chars_format rhs) { 00105 lhs = lhs | rhs; 00106 return lhs; 00107 } 00108 inline chars_format& operator^=(chars_format& lhs, chars_format rhs) { 00109 lhs = lhs ^ rhs; 00110 return lhs; 00111 } 00112 00113 } // namespace absl 00114 00115 #endif // ABSL_STRINGS_CHARCONV_H_