You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/string_to_number.h"
  11. #include <ctype.h>
  12. #include <cerrno>
  13. #include <cstdlib>
  14. #include "rtc_base/checks.h"
  15. namespace rtc {
  16. namespace string_to_number_internal {
  17. absl::optional<signed_type> ParseSigned(const char* str, int base) {
  18. RTC_DCHECK(str);
  19. if (isdigit(str[0]) || str[0] == '-') {
  20. char* end = nullptr;
  21. errno = 0;
  22. const signed_type value = std::strtoll(str, &end, base);
  23. if (end && *end == '\0' && errno == 0) {
  24. return value;
  25. }
  26. }
  27. return absl::nullopt;
  28. }
  29. absl::optional<unsigned_type> ParseUnsigned(const char* str, int base) {
  30. RTC_DCHECK(str);
  31. if (isdigit(str[0]) || str[0] == '-') {
  32. // Explicitly discard negative values. std::strtoull parsing causes unsigned
  33. // wraparound. We cannot just reject values that start with -, though, since
  34. // -0 is perfectly fine, as is -0000000000000000000000000000000.
  35. const bool is_negative = str[0] == '-';
  36. char* end = nullptr;
  37. errno = 0;
  38. const unsigned_type value = std::strtoull(str, &end, base);
  39. if (end && *end == '\0' && errno == 0 && (value == 0 || !is_negative)) {
  40. return value;
  41. }
  42. }
  43. return absl::nullopt;
  44. }
  45. template <typename T>
  46. T StrToT(const char* str, char** str_end);
  47. template <>
  48. inline float StrToT(const char* str, char** str_end) {
  49. return std::strtof(str, str_end);
  50. }
  51. template <>
  52. inline double StrToT(const char* str, char** str_end) {
  53. return std::strtod(str, str_end);
  54. }
  55. template <>
  56. inline long double StrToT(const char* str, char** str_end) {
  57. return std::strtold(str, str_end);
  58. }
  59. template <typename T>
  60. absl::optional<T> ParseFloatingPoint(const char* str) {
  61. RTC_DCHECK(str);
  62. if (*str == '\0')
  63. return absl::nullopt;
  64. char* end = nullptr;
  65. errno = 0;
  66. const T value = StrToT<T>(str, &end);
  67. if (end && *end == '\0' && errno == 0) {
  68. return value;
  69. }
  70. return absl::nullopt;
  71. }
  72. template absl::optional<float> ParseFloatingPoint(const char* str);
  73. template absl::optional<double> ParseFloatingPoint(const char* str);
  74. template absl::optional<long double> ParseFloatingPoint(const char* str);
  75. } // namespace string_to_number_internal
  76. } // namespace rtc