tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

charconv.h (5145B)


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