diff --git a/webrtc/base/BUILD.gn b/webrtc/base/BUILD.gn index 7e02a3a063..c7a099e5e0 100644 --- a/webrtc/base/BUILD.gn +++ b/webrtc/base/BUILD.gn @@ -156,6 +156,8 @@ rtc_static_library("rtc_base_approved") { "safe_conversions_impl.h", "sanitizer.h", "scoped_ref_ptr.h", + "string_to_number.cc", + "string_to_number.h", "stringencode.cc", "stringencode.h", "stringutils.cc", @@ -780,6 +782,7 @@ if (rtc_include_tests) { "ratetracker_unittest.cc", "refcountedobject_unittest.cc", "safe_compare_unittest.cc", + "string_to_number_unittest.cc", "stringencode_unittest.cc", "stringutils_unittest.cc", "swap_queue_unittest.cc", diff --git a/webrtc/base/string_to_number.cc b/webrtc/base/string_to_number.cc new file mode 100644 index 0000000000..f8809268a2 --- /dev/null +++ b/webrtc/base/string_to_number.cc @@ -0,0 +1,49 @@ +/* + * Copyright 2017 The WebRTC Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include + +#include "webrtc/base/string_to_number.h" + +namespace rtc { +namespace string_to_number_internal { + +rtc::Optional ParseSigned(const char* str, int base) { + RTC_DCHECK(str); + if (isdigit(str[0]) || str[0] == '-') { + char* end = nullptr; + errno = 0; + const signed_type value = std::strtoll(str, &end, base); + if (end && *end == '\0' && errno == 0) { + return rtc::Optional(value); + } + } + return rtc::Optional(); +} + +rtc::Optional ParseUnsigned(const char* str, int base) { + RTC_DCHECK(str); + if (isdigit(str[0]) || str[0] == '-') { + // 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)) { + return rtc::Optional(value); + } + } + return rtc::Optional(); +} + +} // namespace string_to_number_internal +} // namespace rtc diff --git a/webrtc/base/string_to_number.h b/webrtc/base/string_to_number.h new file mode 100644 index 0000000000..8306f7cf89 --- /dev/null +++ b/webrtc/base/string_to_number.h @@ -0,0 +1,101 @@ +/* + * Copyright 2017 The WebRTC Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_BASE_STRING_TO_NUMBER_H_ +#define WEBRTC_BASE_STRING_TO_NUMBER_H_ + +#include +#include + +#include "webrtc/base/optional.h" + +namespace rtc { + +// This file declares a family of functions to parse integers from strings. +// The standard C library functions either fail to indicate errors (atoi, etc.) +// or are a hassle to work with (strtol, sscanf, etc.). The standard C++ library +// functions (std::stoi, etc.) indicate errors by throwing exceptions, which +// are disabled in WebRTC. +// +// Integers are parsed using one of the following functions: +// rtc::Optional StringToNumber(const char* str, int base = 10); +// rtc::Optional StringToNumber(const std::string& str, +// int base = 10); +// +// These functions parse a value from the beginning of a string into one of the +// fundamental integer types, or returns an empty Optional if parsing +// failed. Values outside of the range supported by the type will be +// rejected. The strings must begin with a digit or a minus sign. No leading +// space nor trailing contents are allowed. +// By setting base to 0, one of octal, decimal or hexadecimal will be +// detected from the string's prefix (0, nothing or 0x, respectively). +// If non-zero, base can be set to a value between 2 and 36 inclusively. +// +// If desired, this interface could be extended with support for floating-point +// types. + +namespace string_to_number_internal { +// These must be (unsigned) long long, to match the signature of strto(u)ll. +using unsigned_type = unsigned long long; // NOLINT(runtime/int) +using signed_type = long long; // NOLINT(runtime/int) + +rtc::Optional ParseSigned(const char* str, int base); +rtc::Optional ParseUnsigned(const char* str, int base); +} // namespace string_to_number_internal + +template +typename std::enable_if::value && std::is_signed::value, + rtc::Optional>::type +StringToNumber(const char* str, int base = 10) { + using string_to_number_internal::signed_type; + static_assert( + std::numeric_limits::max() <= + std::numeric_limits::max() && + std::numeric_limits::lowest() >= + std::numeric_limits::lowest(), + "StringToNumber only supports signed integers as large as long long int"); + rtc::Optional value = + string_to_number_internal::ParseSigned(str, base); + if (value && *value >= std::numeric_limits::lowest() && + *value <= std::numeric_limits::max()) { + return rtc::Optional(static_cast(*value)); + } + return rtc::Optional(); +} + +template +typename std::enable_if::value && + std::is_unsigned::value, + rtc::Optional>::type +StringToNumber(const char* str, int base = 10) { + using string_to_number_internal::unsigned_type; + static_assert(std::numeric_limits::max() <= + std::numeric_limits::max(), + "StringToNumber only supports unsigned integers as large as " + "unsigned long long int"); + rtc::Optional value = + string_to_number_internal::ParseUnsigned(str, base); + if (value && *value <= std::numeric_limits::max()) { + return rtc::Optional(static_cast(*value)); + } + return rtc::Optional(); +} + +// The std::string overloads only exists if there is a matching const char* +// version. +template +auto StringToNumber(const std::string& str, int base = 10) + -> decltype(StringToNumber(str.c_str(), base)) { + return StringToNumber(str.c_str(), base); +} + +} // namespace rtc + +#endif // WEBRTC_BASE_STRING_TO_NUMBER_H_ diff --git a/webrtc/base/string_to_number_unittest.cc b/webrtc/base/string_to_number_unittest.cc new file mode 100644 index 0000000000..00ebd4cae4 --- /dev/null +++ b/webrtc/base/string_to_number_unittest.cc @@ -0,0 +1,115 @@ +/* + * Copyright 2017 The WebRTC Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/base/string_to_number.h" + +#include +#include +#include + +#include "webrtc/base/gunit.h" + +namespace rtc { + +namespace { +// clang-format off +using IntegerTypes = + ::testing::Types; +// clang-format on + +template +class BasicNumberTest : public ::testing::Test {}; + +TYPED_TEST_CASE_P(BasicNumberTest); + +TYPED_TEST_P(BasicNumberTest, TestValidNumbers) { + using T = TypeParam; + constexpr T min_value = std::numeric_limits::lowest(); + constexpr T max_value = std::numeric_limits::max(); + const std::string min_string = std::to_string(min_value); + const std::string max_string = std::to_string(max_value); + EXPECT_EQ(min_value, StringToNumber(min_string)); + EXPECT_EQ(min_value, StringToNumber(min_string.c_str())); + EXPECT_EQ(max_value, StringToNumber(max_string)); + EXPECT_EQ(max_value, StringToNumber(max_string.c_str())); + EXPECT_EQ(0, StringToNumber("0")); + EXPECT_EQ(0, StringToNumber("-0")); + EXPECT_EQ(0, StringToNumber(std::string("-0000000000000"))); +} + +TYPED_TEST_P(BasicNumberTest, TestInvalidNumbers) { + using T = TypeParam; + // Value ranges aren't strictly enforced in this test, since that would either + // require doctoring specific strings for each data type, which is a hassle + // across platforms, or to be able to do addition of values larger than the + // largest type, which is another hassle. + constexpr T min_value = std::numeric_limits::lowest(); + constexpr T max_value = std::numeric_limits::max(); + // If the type supports negative values, make the large negative value + // approximately ten times larger. If the type is unsigned, just use -2. + const std::string too_low_string = + (min_value == 0) ? "-2" : (std::to_string(min_value) + "1"); + // Make the large value approximately ten times larger than the maximum. + const std::string too_large_string = std::to_string(max_value) + "1"; + EXPECT_EQ(rtc::Optional(), StringToNumber(too_low_string)); + EXPECT_EQ(rtc::Optional(), StringToNumber(too_low_string.c_str())); + EXPECT_EQ(rtc::Optional(), StringToNumber(too_large_string)); + EXPECT_EQ(rtc::Optional(), StringToNumber(too_large_string.c_str())); +} + +TYPED_TEST_P(BasicNumberTest, TestInvalidInputs) { + using T = TypeParam; + const char kInvalidCharArray[] = "Invalid string containing 47"; + const char kPlusMinusCharArray[] = "+-100"; + const char kNumberFollowedByCruft[] = "640x480"; + EXPECT_EQ(rtc::Optional(), StringToNumber(kInvalidCharArray)); + EXPECT_EQ(rtc::Optional(), + StringToNumber(std::string(kInvalidCharArray))); + EXPECT_EQ(rtc::Optional(), StringToNumber(kPlusMinusCharArray)); + EXPECT_EQ(rtc::Optional(), + StringToNumber(std::string(kPlusMinusCharArray))); + EXPECT_EQ(rtc::Optional(), StringToNumber(kNumberFollowedByCruft)); + EXPECT_EQ(rtc::Optional(), + StringToNumber(std::string(kNumberFollowedByCruft))); + EXPECT_EQ(rtc::Optional(), StringToNumber(" 5")); + EXPECT_EQ(rtc::Optional(), StringToNumber(" - 5")); + EXPECT_EQ(rtc::Optional(), StringToNumber("- 5")); + EXPECT_EQ(rtc::Optional(), StringToNumber(" -5")); + EXPECT_EQ(rtc::Optional(), StringToNumber("5 ")); +} + +REGISTER_TYPED_TEST_CASE_P(BasicNumberTest, + TestValidNumbers, + TestInvalidNumbers, + TestInvalidInputs); + +} // namespace + +INSTANTIATE_TYPED_TEST_CASE_P(StringToNumberTest_Integers, + BasicNumberTest, + IntegerTypes); + +TEST(StringToNumberTest, TestSpecificValues) { + EXPECT_EQ(rtc::Optional(), StringToNumber("256")); + EXPECT_EQ(rtc::Optional(), StringToNumber("-256")); + EXPECT_EQ(rtc::Optional(), StringToNumber("256")); + EXPECT_EQ(rtc::Optional(), StringToNumber("-256")); +} + +} // namespace rtc