charconv.h 5.0 KB

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