Adopt absl::string_view in rtc_base/string_to_number.*

Bug: webrtc:13579
Change-Id: I7e1a8ad6c493c9c0ac20c493d2467f0a2eda26d9
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/256720
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Commit-Queue: Ali Tofigh <alito@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36340}
This commit is contained in:
Ali Tofigh
2022-03-25 14:28:27 +01:00
committed by WebRTC LUCI CQ
parent 3cdd653d66
commit 3d6c6556b4
3 changed files with 63 additions and 37 deletions

View File

@ -20,30 +20,41 @@
namespace rtc {
namespace string_to_number_internal {
absl::optional<signed_type> ParseSigned(const char* str, int base) {
RTC_DCHECK(str);
absl::optional<signed_type> ParseSigned(absl::string_view str, int base) {
if (str.empty())
return absl::nullopt;
if (isdigit(str[0]) || str[0] == '-') {
std::string str_str = std::string(str);
char* end = nullptr;
errno = 0;
const signed_type value = std::strtoll(str, &end, base);
if (end && *end == '\0' && errno == 0) {
const signed_type value = std::strtoll(str_str.c_str(), &end, base);
// Check for errors and also make sure that there were no embedded nuls in
// the input string.
if (end == str_str.c_str() + str_str.size() && errno == 0) {
return value;
}
}
return absl::nullopt;
}
absl::optional<unsigned_type> ParseUnsigned(const char* str, int base) {
RTC_DCHECK(str);
absl::optional<unsigned_type> ParseUnsigned(absl::string_view str, int base) {
if (str.empty())
return absl::nullopt;
if (isdigit(str[0]) || str[0] == '-') {
std::string str_str = std::string(str);
// Explicitly discard negative values. std::strtoull parsing causes unsigned
// wraparound. We cannot just reject values that start with -, though, since
// -0 is perfectly fine, as is -0000000000000000000000000000000.
const bool is_negative = str[0] == '-';
char* end = nullptr;
errno = 0;
const unsigned_type value = std::strtoull(str, &end, base);
if (end && *end == '\0' && errno == 0 && (value == 0 || !is_negative)) {
const unsigned_type value = std::strtoull(str_str.c_str(), &end, base);
// Check for errors and also make sure that there were no embedded nuls in
// the input string.
if (end == str_str.c_str() + str_str.size() && errno == 0 &&
(value == 0 || !is_negative)) {
return value;
}
}
@ -69,22 +80,25 @@ inline long double StrToT(const char* str, char** str_end) {
}
template <typename T>
absl::optional<T> ParseFloatingPoint(const char* str) {
RTC_DCHECK(str);
if (*str == '\0')
absl::optional<T> ParseFloatingPoint(absl::string_view str) {
if (str.empty())
return absl::nullopt;
if (str[0] == '\0')
return absl::nullopt;
std::string str_str = std::string(str);
char* end = nullptr;
errno = 0;
const T value = StrToT<T>(str, &end);
if (end && *end == '\0' && errno == 0) {
const T value = StrToT<T>(str_str.c_str(), &end);
if (end == str_str.c_str() + str_str.size() && errno == 0) {
return value;
}
return absl::nullopt;
}
template absl::optional<float> ParseFloatingPoint(const char* str);
template absl::optional<double> ParseFloatingPoint(const char* str);
template absl::optional<long double> ParseFloatingPoint(const char* str);
template absl::optional<float> ParseFloatingPoint(absl::string_view str);
template absl::optional<double> ParseFloatingPoint(absl::string_view str);
template absl::optional<long double> ParseFloatingPoint(absl::string_view str);
} // namespace string_to_number_internal
} // namespace rtc