Added integer parsing functions in base/string_to_number.h
They provide parsing of numbers from char* and std::string into any of the fundamental integer types. Values are returned in an rtc::Optional, which is left empty if parsing was unsuccessful. BUG=webrtc:5806 Review-Url: https://codereview.webrtc.org/2696003004 Cr-Commit-Position: refs/heads/master@{#17557}
This commit is contained in:
@ -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",
|
||||
|
||||
49
webrtc/base/string_to_number.cc
Normal file
49
webrtc/base/string_to_number.cc
Normal file
@ -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 <cstdlib>
|
||||
|
||||
#include "webrtc/base/string_to_number.h"
|
||||
|
||||
namespace rtc {
|
||||
namespace string_to_number_internal {
|
||||
|
||||
rtc::Optional<signed_type> 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<signed_type>(value);
|
||||
}
|
||||
}
|
||||
return rtc::Optional<signed_type>();
|
||||
}
|
||||
|
||||
rtc::Optional<unsigned_type> 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<unsigned_type>(value);
|
||||
}
|
||||
}
|
||||
return rtc::Optional<unsigned_type>();
|
||||
}
|
||||
|
||||
} // namespace string_to_number_internal
|
||||
} // namespace rtc
|
||||
101
webrtc/base/string_to_number.h
Normal file
101
webrtc/base/string_to_number.h
Normal file
@ -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 <string>
|
||||
#include <limits>
|
||||
|
||||
#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<int-type> StringToNumber(const char* str, int base = 10);
|
||||
// rtc::Optional<int-type> 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<signed_type> ParseSigned(const char* str, int base);
|
||||
rtc::Optional<unsigned_type> ParseUnsigned(const char* str, int base);
|
||||
} // namespace string_to_number_internal
|
||||
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value,
|
||||
rtc::Optional<T>>::type
|
||||
StringToNumber(const char* str, int base = 10) {
|
||||
using string_to_number_internal::signed_type;
|
||||
static_assert(
|
||||
std::numeric_limits<T>::max() <=
|
||||
std::numeric_limits<signed_type>::max() &&
|
||||
std::numeric_limits<T>::lowest() >=
|
||||
std::numeric_limits<signed_type>::lowest(),
|
||||
"StringToNumber only supports signed integers as large as long long int");
|
||||
rtc::Optional<signed_type> value =
|
||||
string_to_number_internal::ParseSigned(str, base);
|
||||
if (value && *value >= std::numeric_limits<T>::lowest() &&
|
||||
*value <= std::numeric_limits<T>::max()) {
|
||||
return rtc::Optional<T>(static_cast<T>(*value));
|
||||
}
|
||||
return rtc::Optional<T>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename std::enable_if<std::is_integral<T>::value &&
|
||||
std::is_unsigned<T>::value,
|
||||
rtc::Optional<T>>::type
|
||||
StringToNumber(const char* str, int base = 10) {
|
||||
using string_to_number_internal::unsigned_type;
|
||||
static_assert(std::numeric_limits<T>::max() <=
|
||||
std::numeric_limits<unsigned_type>::max(),
|
||||
"StringToNumber only supports unsigned integers as large as "
|
||||
"unsigned long long int");
|
||||
rtc::Optional<unsigned_type> value =
|
||||
string_to_number_internal::ParseUnsigned(str, base);
|
||||
if (value && *value <= std::numeric_limits<T>::max()) {
|
||||
return rtc::Optional<T>(static_cast<T>(*value));
|
||||
}
|
||||
return rtc::Optional<T>();
|
||||
}
|
||||
|
||||
// The std::string overloads only exists if there is a matching const char*
|
||||
// version.
|
||||
template <typename T>
|
||||
auto StringToNumber(const std::string& str, int base = 10)
|
||||
-> decltype(StringToNumber<T>(str.c_str(), base)) {
|
||||
return StringToNumber<T>(str.c_str(), base);
|
||||
}
|
||||
|
||||
} // namespace rtc
|
||||
|
||||
#endif // WEBRTC_BASE_STRING_TO_NUMBER_H_
|
||||
115
webrtc/base/string_to_number_unittest.cc
Normal file
115
webrtc/base/string_to_number_unittest.cc
Normal file
@ -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 <string>
|
||||
#include <type_traits>
|
||||
#include <limits>
|
||||
|
||||
#include "webrtc/base/gunit.h"
|
||||
|
||||
namespace rtc {
|
||||
|
||||
namespace {
|
||||
// clang-format off
|
||||
using IntegerTypes =
|
||||
::testing::Types<char,
|
||||
signed char, unsigned char, // NOLINT(runtime/int)
|
||||
short, unsigned short, // NOLINT(runtime/int)
|
||||
int, unsigned int, // NOLINT(runtime/int)
|
||||
long, unsigned long, // NOLINT(runtime/int)
|
||||
long long, unsigned long long, // NOLINT(runtime/int)
|
||||
int8_t, uint8_t,
|
||||
int16_t, uint16_t,
|
||||
int32_t, uint32_t,
|
||||
int64_t, uint64_t>;
|
||||
// clang-format on
|
||||
|
||||
template <typename T>
|
||||
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<T>::lowest();
|
||||
constexpr T max_value = std::numeric_limits<T>::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<T>(min_string));
|
||||
EXPECT_EQ(min_value, StringToNumber<T>(min_string.c_str()));
|
||||
EXPECT_EQ(max_value, StringToNumber<T>(max_string));
|
||||
EXPECT_EQ(max_value, StringToNumber<T>(max_string.c_str()));
|
||||
EXPECT_EQ(0, StringToNumber<T>("0"));
|
||||
EXPECT_EQ(0, StringToNumber<T>("-0"));
|
||||
EXPECT_EQ(0, StringToNumber<T>(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<T>::lowest();
|
||||
constexpr T max_value = std::numeric_limits<T>::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<T>(), StringToNumber<T>(too_low_string));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_low_string.c_str()));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_large_string));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(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<T>(), StringToNumber<T>(kInvalidCharArray));
|
||||
EXPECT_EQ(rtc::Optional<T>(),
|
||||
StringToNumber<T>(std::string(kInvalidCharArray)));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(kPlusMinusCharArray));
|
||||
EXPECT_EQ(rtc::Optional<T>(),
|
||||
StringToNumber<T>(std::string(kPlusMinusCharArray)));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(kNumberFollowedByCruft));
|
||||
EXPECT_EQ(rtc::Optional<T>(),
|
||||
StringToNumber<T>(std::string(kNumberFollowedByCruft)));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(" 5"));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(" - 5"));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>("- 5"));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(" -5"));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>("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<uint8_t>(), StringToNumber<uint8_t>("256"));
|
||||
EXPECT_EQ(rtc::Optional<uint8_t>(), StringToNumber<uint8_t>("-256"));
|
||||
EXPECT_EQ(rtc::Optional<int8_t>(), StringToNumber<int8_t>("256"));
|
||||
EXPECT_EQ(rtc::Optional<int8_t>(), StringToNumber<int8_t>("-256"));
|
||||
}
|
||||
|
||||
} // namespace rtc
|
||||
Reference in New Issue
Block a user