1// Copyright 2018 The Abseil Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef ABSL_STRINGS_CHARCONV_H_
16#define ABSL_STRINGS_CHARCONV_H_
17
18#include <system_error> // NOLINT(build/c++11)
19
20namespace absl {
21
22// Workalike compatibilty version of std::chars_format from C++17.
23//
24// This is an bitfield enumerator which can be passed to absl::from_chars to
25// configure the string-to-float conversion.
26enum class chars_format {
27 scientific = 1,
28 fixed = 2,
29 hex = 4,
30 general = fixed | scientific,
31};
32
33// The return result of a string-to-number conversion.
34//
35// `ec` will be set to `invalid_argument` if a well-formed number was not found
36// at the start of the input range, `result_out_of_range` if a well-formed
37// number was found, but it was out of the representable range of the requested
38// type, or to std::errc() otherwise.
39//
40// If a well-formed number was found, `ptr` is set to one past the sequence of
41// characters that were successfully parsed. If none was found, `ptr` is set
42// to the `first` argument to from_chars.
43struct from_chars_result {
44 const char* ptr;
45 std::errc ec;
46};
47
48// Workalike compatibilty version of std::from_chars from C++17. Currently
49// this only supports the `double` and `float` types.
50//
51// This interface incorporates the proposed resolutions for library issues
52// DR 3080 and DR 3081. If these are adopted with different wording,
53// Abseil's behavior will change to match the standard. (The behavior most
54// likely to change is for DR 3081, which says what `value` will be set to in
55// the case of overflow and underflow. Code that wants to avoid possible
56// breaking changes in this area should not depend on `value` when the returned
57// from_chars_result indicates a range error.)
58//
59// Searches the range [first, last) for the longest matching pattern beginning
60// at `first` that represents a floating point number. If one is found, store
61// the result in `value`.
62//
63// The matching pattern format is almost the same as that of strtod(), except
64// that C locale is not respected, and an initial '+' character in the input
65// range will never be matched.
66//
67// If `fmt` is set, it must be one of the enumerator values of the chars_format.
68// (This is despite the fact that chars_format is a bitmask type.) If set to
69// `scientific`, a matching number must contain an exponent. If set to `fixed`,
70// then an exponent will never match. (For example, the string "1e5" will be
71// parsed as "1".) If set to `hex`, then a hexadecimal float is parsed in the
72// format that strtod() accepts, except that a "0x" prefix is NOT matched.
73// (In particular, in `hex` mode, the input "0xff" results in the largest
74// matching pattern "0".)
75absl::from_chars_result from_chars(const char* first, const char* last,
76 double& value, // NOLINT
77 chars_format fmt = chars_format::general);
78
79absl::from_chars_result from_chars(const char* first, const char* last,
80 float& value, // NOLINT
81 chars_format fmt = chars_format::general);
82
83// std::chars_format is specified as a bitmask type, which means the following
84// operations must be provided:
85inline constexpr chars_format operator&(chars_format lhs, chars_format rhs) {
86 return static_cast<chars_format>(static_cast<int>(lhs) &
87 static_cast<int>(rhs));
88}
89inline constexpr chars_format operator|(chars_format lhs, chars_format rhs) {
90 return static_cast<chars_format>(static_cast<int>(lhs) |
91 static_cast<int>(rhs));
92}
93inline constexpr chars_format operator^(chars_format lhs, chars_format rhs) {
94 return static_cast<chars_format>(static_cast<int>(lhs) ^
95 static_cast<int>(rhs));
96}
97inline constexpr chars_format operator~(chars_format arg) {
98 return static_cast<chars_format>(~static_cast<int>(arg));
99}
100inline chars_format& operator&=(chars_format& lhs, chars_format rhs) {
101 lhs = lhs & rhs;
102 return lhs;
103}
104inline chars_format& operator|=(chars_format& lhs, chars_format rhs) {
105 lhs = lhs | rhs;
106 return lhs;
107}
108inline chars_format& operator^=(chars_format& lhs, chars_format rhs) {
109 lhs = lhs ^ rhs;
110 return lhs;
111}
112
113} // namespace absl
114
115#endif // ABSL_STRINGS_CHARCONV_H_
116