string_to_number.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2017 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_STRING_TO_NUMBER_H_
  11. #define RTC_BASE_STRING_TO_NUMBER_H_
  12. #include <limits>
  13. #include <string>
  14. #include <type_traits>
  15. #include "absl/types/optional.h"
  16. namespace rtc {
  17. // This file declares a family of functions to parse integers from strings.
  18. // The standard C library functions either fail to indicate errors (atoi, etc.)
  19. // or are a hassle to work with (strtol, sscanf, etc.). The standard C++ library
  20. // functions (std::stoi, etc.) indicate errors by throwing exceptions, which
  21. // are disabled in WebRTC.
  22. //
  23. // Integers are parsed using one of the following functions:
  24. // absl::optional<int-type> StringToNumber(const char* str, int base = 10);
  25. // absl::optional<int-type> StringToNumber(const std::string& str,
  26. // int base = 10);
  27. //
  28. // These functions parse a value from the beginning of a string into one of the
  29. // fundamental integer types, or returns an empty Optional if parsing
  30. // failed. Values outside of the range supported by the type will be
  31. // rejected. The strings must begin with a digit or a minus sign. No leading
  32. // space nor trailing contents are allowed.
  33. // By setting base to 0, one of octal, decimal or hexadecimal will be
  34. // detected from the string's prefix (0, nothing or 0x, respectively).
  35. // If non-zero, base can be set to a value between 2 and 36 inclusively.
  36. //
  37. // If desired, this interface could be extended with support for floating-point
  38. // types.
  39. namespace string_to_number_internal {
  40. // These must be (unsigned) long long, to match the signature of strto(u)ll.
  41. using unsigned_type = unsigned long long; // NOLINT(runtime/int)
  42. using signed_type = long long; // NOLINT(runtime/int)
  43. absl::optional<signed_type> ParseSigned(const char* str, int base);
  44. absl::optional<unsigned_type> ParseUnsigned(const char* str, int base);
  45. template <typename T>
  46. absl::optional<T> ParseFloatingPoint(const char* str);
  47. } // namespace string_to_number_internal
  48. template <typename T>
  49. typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value,
  50. absl::optional<T>>::type
  51. StringToNumber(const char* str, int base = 10) {
  52. using string_to_number_internal::signed_type;
  53. static_assert(
  54. std::numeric_limits<T>::max() <=
  55. std::numeric_limits<signed_type>::max() &&
  56. std::numeric_limits<T>::lowest() >=
  57. std::numeric_limits<signed_type>::lowest(),
  58. "StringToNumber only supports signed integers as large as long long int");
  59. absl::optional<signed_type> value =
  60. string_to_number_internal::ParseSigned(str, base);
  61. if (value && *value >= std::numeric_limits<T>::lowest() &&
  62. *value <= std::numeric_limits<T>::max()) {
  63. return static_cast<T>(*value);
  64. }
  65. return absl::nullopt;
  66. }
  67. template <typename T>
  68. typename std::enable_if<std::is_integral<T>::value &&
  69. std::is_unsigned<T>::value,
  70. absl::optional<T>>::type
  71. StringToNumber(const char* str, int base = 10) {
  72. using string_to_number_internal::unsigned_type;
  73. static_assert(std::numeric_limits<T>::max() <=
  74. std::numeric_limits<unsigned_type>::max(),
  75. "StringToNumber only supports unsigned integers as large as "
  76. "unsigned long long int");
  77. absl::optional<unsigned_type> value =
  78. string_to_number_internal::ParseUnsigned(str, base);
  79. if (value && *value <= std::numeric_limits<T>::max()) {
  80. return static_cast<T>(*value);
  81. }
  82. return absl::nullopt;
  83. }
  84. template <typename T>
  85. typename std::enable_if<std::is_floating_point<T>::value,
  86. absl::optional<T>>::type
  87. StringToNumber(const char* str, int base = 10) {
  88. static_assert(
  89. std::numeric_limits<T>::max() <= std::numeric_limits<long double>::max(),
  90. "StringToNumber only supports floating-point numbers as large "
  91. "as long double");
  92. return string_to_number_internal::ParseFloatingPoint<T>(str);
  93. }
  94. // The std::string overloads only exists if there is a matching const char*
  95. // version.
  96. template <typename T>
  97. auto StringToNumber(const std::string& str, int base = 10)
  98. -> decltype(StringToNumber<T>(str.c_str(), base)) {
  99. return StringToNumber<T>(str.c_str(), base);
  100. }
  101. } // namespace rtc
  102. #endif // RTC_BASE_STRING_TO_NUMBER_H_