diff options
author | tedvessenes@gmail.com <tedvessenes@gmail.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-18 17:16:27 +0000 |
---|---|---|
committer | tedvessenes@gmail.com <tedvessenes@gmail.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-18 17:16:27 +0000 |
commit | ce87e24351c86bc40b32dca51fe2ebbabc603b1a (patch) | |
tree | 8184c32b18bbda29760be2e1678ad4a6fd7fd539 | |
parent | 71fdf77a393f95290a872bb3916f21c0bf2b4dbc (diff) | |
download | chromium_src-ce87e24351c86bc40b32dca51fe2ebbabc603b1a.zip chromium_src-ce87e24351c86bc40b32dca51fe2ebbabc603b1a.tar.gz chromium_src-ce87e24351c86bc40b32dca51fe2ebbabc603b1a.tar.bz2 |
Standardize StringToInt{,64} interface.
These changes address issue #106655. All variants of StringToInt have been
converted to use the StringPiece class. One instance of conversion, in
chrome/browser/history/text_database.cc, required copying an underlying
string. This is because the string type in question could use 8 or 16
bit characters depending on the OS type, and because StringPiece is not
implemented as a template, the code cannot specify whether to create a
StringPiece or StringPiece16. This should be remedied in a future CL.
R=erikwright@chromium.org
BUG=106655
TEST=
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=114929
Review URL: http://codereview.chromium.org/8921006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@114944 0039d316-1c4b-4281-b951-d872f2087c98
23 files changed, 156 insertions, 298 deletions
@@ -141,6 +141,7 @@ Naveen Bobbili <qghc36@motorola.com> Ravi Phaneendra Kasibhatla <ravi.kasibhatla@motorola.com> Rosen Dash <nqk836@motorola.com> Parag Radke <nrqv63@motorola.com> +Ted Vessenes <tedvessenes@gmail.com> Yair Yogev <progame@gmail.com> Chandra Shekar Vallala <brk376@motorola.com> Patrasciuc Sorin Cristian <cristian.patrasciuc@gmail.com> diff --git a/base/mac/mac_util.mm b/base/mac/mac_util.mm index 66fbf11..2ba1a30 100644 --- a/base/mac/mac_util.mm +++ b/base/mac/mac_util.mm @@ -14,6 +14,7 @@ #include "base/mac/scoped_cftyperef.h" #include "base/memory/scoped_nsobject.h" #include "base/string_number_conversions.h" +#include "base/string_piece.h" #include "base/sys_string_conversions.h" namespace base { @@ -521,7 +522,9 @@ int DarwinMajorVersionInternal() { int darwin_major_version = 0; char* dot = strchr(uname_info.release, '.'); if (dot) { - if (!base::StringToInt(uname_info.release, dot, &darwin_major_version)) { + if (!base::StringToInt(base::StringPiece(uname_info.release, + dot - uname_info.release), + &darwin_major_version)) { dot = NULL; } } diff --git a/base/string_number_conversions.cc b/base/string_number_conversions.cc index 54eca17..1d82c5d 100644 --- a/base/string_number_conversions.cc +++ b/base/string_number_conversions.cc @@ -292,24 +292,6 @@ class BaseIteratorRangeToNumberTraits { static const int kBase = BASE; }; -typedef BaseIteratorRangeToNumberTraits<std::string::const_iterator, int, 10> - IteratorRangeToIntTraits; -typedef BaseIteratorRangeToNumberTraits<string16::const_iterator, int, 10> - WideIteratorRangeToIntTraits; -typedef BaseIteratorRangeToNumberTraits<std::string::const_iterator, int64, 10> - IteratorRangeToInt64Traits; -typedef BaseIteratorRangeToNumberTraits<string16::const_iterator, int64, 10> - WideIteratorRangeToInt64Traits; - -typedef BaseIteratorRangeToNumberTraits<const char*, int, 10> - CharBufferToIntTraits; -typedef BaseIteratorRangeToNumberTraits<const char16*, int, 10> - WideCharBufferToIntTraits; -typedef BaseIteratorRangeToNumberTraits<const char*, int64, 10> - CharBufferToInt64Traits; -typedef BaseIteratorRangeToNumberTraits<const char16*, int64, 10> - WideCharBufferToInt64Traits; - template<typename ITERATOR> class BaseHexIteratorRangeToIntTraits : public BaseIteratorRangeToNumberTraits<ITERATOR, int, 16> { @@ -320,10 +302,8 @@ class BaseHexIteratorRangeToIntTraits } }; -typedef BaseHexIteratorRangeToIntTraits<std::string::const_iterator> +typedef BaseHexIteratorRangeToIntTraits<StringPiece::const_iterator> HexIteratorRangeToIntTraits; -typedef BaseHexIteratorRangeToIntTraits<const char*> - HexCharBufferToIntTraits; template<typename STR> bool HexStringToBytesT(const STR& input, std::vector<uint8>* output) { @@ -342,6 +322,30 @@ bool HexStringToBytesT(const STR& input, std::vector<uint8>* output) { return true; } +template <typename VALUE, int BASE> +class StringPieceToNumberTraits + : public BaseIteratorRangeToNumberTraits<StringPiece::const_iterator, + VALUE, + BASE> {}; + +template <typename VALUE> +bool StringToIntImpl(const StringPiece& input, VALUE* output) { + return IteratorRangeToNumber<StringPieceToNumberTraits<VALUE, 10> >::Invoke( + input.begin(), input.end(), output); +} + +template <typename VALUE, int BASE> +class StringPiece16ToNumberTraits + : public BaseIteratorRangeToNumberTraits<StringPiece16::const_iterator, + VALUE, + BASE> {}; + +template <typename VALUE> +bool String16ToIntImpl(const StringPiece16& input, VALUE* output) { + return IteratorRangeToNumber<StringPiece16ToNumberTraits<VALUE, 10> >::Invoke( + input.begin(), input.end(), output); +} + } // namespace std::string IntToString(int value) { @@ -390,91 +394,22 @@ std::string DoubleToString(double value) { return std::string(buffer); } -bool StringToInt(const std::string& input, int* output) { - return IteratorRangeToNumber<IteratorRangeToIntTraits>::Invoke(input.begin(), - input.end(), - output); -} - -bool StringToInt(std::string::const_iterator begin, - std::string::const_iterator end, - int* output) { - return IteratorRangeToNumber<IteratorRangeToIntTraits>::Invoke(begin, - end, - output); -} - -#if !defined(STD_STRING_ITERATOR_IS_CHAR_POINTER) -bool StringToInt(const char* begin, const char* end, int* output) { - return IteratorRangeToNumber<CharBufferToIntTraits>::Invoke(begin, - end, - output); -} -#endif - -bool StringToInt(const string16& input, int* output) { - return IteratorRangeToNumber<WideIteratorRangeToIntTraits>::Invoke( - input.begin(), input.end(), output); -} - -bool StringToInt(string16::const_iterator begin, - string16::const_iterator end, - int* output) { - return IteratorRangeToNumber<WideIteratorRangeToIntTraits>::Invoke(begin, - end, - output); -} - -#if !defined(BASE_STRING16_ITERATOR_IS_CHAR16_POINTER) -bool StringToInt(const char16* begin, const char16* end, int* output) { - return IteratorRangeToNumber<WideCharBufferToIntTraits>::Invoke(begin, - end, - output); -} -#endif - -bool StringToInt64(const std::string& input, int64* output) { - return IteratorRangeToNumber<IteratorRangeToInt64Traits>::Invoke( - input.begin(), input.end(), output); +bool StringToInt(const StringPiece& input, int* output) { + return StringToIntImpl(input, output); } -bool StringToInt64(std::string::const_iterator begin, - std::string::const_iterator end, - int64* output) { - return IteratorRangeToNumber<IteratorRangeToInt64Traits>::Invoke(begin, - end, - output); +bool StringToInt(const StringPiece16& input, int* output) { + return String16ToIntImpl(input, output); } -#if !defined(STD_STRING_ITERATOR_IS_CHAR_POINTER) -bool StringToInt64(const char* begin, const char* end, int64* output) { - return IteratorRangeToNumber<CharBufferToInt64Traits>::Invoke(begin, - end, - output); +bool StringToInt64(const StringPiece& input, int64* output) { + return StringToIntImpl(input, output); } -#endif -bool StringToInt64(const string16& input, int64* output) { - return IteratorRangeToNumber<WideIteratorRangeToInt64Traits>::Invoke( - input.begin(), input.end(), output); +bool StringToInt64(const StringPiece16& input, int64* output) { + return String16ToIntImpl(input, output); } -bool StringToInt64(string16::const_iterator begin, - string16::const_iterator end, - int64* output) { - return IteratorRangeToNumber<WideIteratorRangeToInt64Traits>::Invoke(begin, - end, - output); -} - -#if !defined(BASE_STRING16_ITERATOR_IS_CHAR16_POINTER) -bool StringToInt64(const char16* begin, const char16* end, int64* output) { - return IteratorRangeToNumber<WideCharBufferToInt64Traits>::Invoke(begin, - end, - output); -} -#endif - bool StringToDouble(const std::string& input, double* output) { errno = 0; // Thread-safe? It is on at least Mac, Linux, and Windows. char* endptr = NULL; @@ -517,27 +452,11 @@ std::string HexEncode(const void* bytes, size_t size) { return ret; } -bool HexStringToInt(const std::string& input, int* output) { +bool HexStringToInt(const StringPiece& input, int* output) { return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke( input.begin(), input.end(), output); } -bool HexStringToInt(std::string::const_iterator begin, - std::string::const_iterator end, - int* output) { - return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke(begin, - end, - output); -} - -#if !defined(STD_STRING_ITERATOR_IS_CHAR_POINTER) -bool HexStringToInt(const char* begin, const char* end, int* output) { - return IteratorRangeToNumber<HexCharBufferToIntTraits>::Invoke(begin, - end, - output); -} -#endif - bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { return HexStringToBytesT(input, output); } diff --git a/base/string_number_conversions.h b/base/string_number_conversions.h index 6cf6a76..8dc7942 100644 --- a/base/string_number_conversions.h +++ b/base/string_number_conversions.h @@ -10,6 +10,7 @@ #include "base/base_export.h" #include "base/basictypes.h" +#include "base/string_piece.h" #include "base/string16.h" // ---------------------------------------------------------------------------- @@ -58,32 +59,10 @@ BASE_EXPORT std::string DoubleToString(double value); // - No characters parseable as a number at the beginning of the string. // |*output| will be set to 0. // - Empty string. |*output| will be set to 0. -BASE_EXPORT bool StringToInt(const std::string& input, int* output); -BASE_EXPORT bool StringToInt(std::string::const_iterator begin, - std::string::const_iterator end, - int* output); -BASE_EXPORT bool StringToInt(const char* begin, const char* end, int* output); - -BASE_EXPORT bool StringToInt(const string16& input, int* output); -BASE_EXPORT bool StringToInt(string16::const_iterator begin, - string16::const_iterator end, - int* output); -BASE_EXPORT bool StringToInt(const char16* begin, const char16* end, - int* output); - -BASE_EXPORT bool StringToInt64(const std::string& input, int64* output); -BASE_EXPORT bool StringToInt64(std::string::const_iterator begin, - std::string::const_iterator end, - int64* output); -BASE_EXPORT bool StringToInt64(const char* begin, const char* end, - int64* output); - -BASE_EXPORT bool StringToInt64(const string16& input, int64* output); -BASE_EXPORT bool StringToInt64(string16::const_iterator begin, - string16::const_iterator end, - int64* output); -BASE_EXPORT bool StringToInt64(const char16* begin, const char16* end, - int64* output); +BASE_EXPORT bool StringToInt(const StringPiece& input, int* output); +BASE_EXPORT bool StringToInt(const StringPiece16& input, int* output); +BASE_EXPORT bool StringToInt64(const StringPiece& input, int64* output); +BASE_EXPORT bool StringToInt64(const StringPiece16& input, int64* output); // For floating-point conversions, only conversions of input strings in decimal // form are defined to work. Behavior with strings representing floating-point @@ -104,12 +83,7 @@ BASE_EXPORT bool StringToDouble(const std::string& input, double* output); BASE_EXPORT std::string HexEncode(const void* bytes, size_t size); // Best effort conversion, see StringToInt above for restrictions. -BASE_EXPORT bool HexStringToInt(const std::string& input, int* output); -BASE_EXPORT bool HexStringToInt(std::string::const_iterator begin, - std::string::const_iterator end, - int* output); -BASE_EXPORT bool HexStringToInt(const char* begin, const char* end, - int* output); +BASE_EXPORT bool HexStringToInt(const StringPiece& input, int* output); // Similar to the previous functions, except that output is a vector of bytes. // |*output| will contain as many bytes as were successfully parsed prior to the diff --git a/base/string_number_conversions_unittest.cc b/base/string_number_conversions_unittest.cc index 1e5ff14..438db07 100644 --- a/base/string_number_conversions_unittest.cc +++ b/base/string_number_conversions_unittest.cc @@ -105,34 +105,14 @@ TEST(StringNumberConversionsTest, StringToInt) { }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { - const char* ascii_chars = cases[i].input.c_str(); int output = 0; EXPECT_EQ(cases[i].success, StringToInt(cases[i].input, &output)); EXPECT_EQ(cases[i].output, output); - output = 0; - EXPECT_EQ(cases[i].success, StringToInt(cases[i].input.begin(), - cases[i].input.end(), - &output)); - EXPECT_EQ(cases[i].output, output); - output = 0; - EXPECT_EQ(cases[i].success, StringToInt( - ascii_chars, ascii_chars + cases[i].input.length(), &output)); - EXPECT_EQ(cases[i].output, output); string16 utf16_input = UTF8ToUTF16(cases[i].input); - const char16* utf16_chars = utf16_input.c_str(); output = 0; EXPECT_EQ(cases[i].success, StringToInt(utf16_input, &output)); EXPECT_EQ(cases[i].output, output); - output = 0; - EXPECT_EQ(cases[i].success, StringToInt(utf16_input.begin(), - utf16_input.end(), - &output)); - EXPECT_EQ(cases[i].output, output); - output = 0; - EXPECT_EQ(cases[i].success, StringToInt( - utf16_chars, utf16_chars + utf16_input.length(), &output)); - EXPECT_EQ(cases[i].output, output); } // One additional test to verify that conversion of numbers in strings with @@ -143,26 +123,11 @@ TEST(StringNumberConversionsTest, StringToInt) { int output; EXPECT_FALSE(StringToInt(input_string, &output)); EXPECT_EQ(6, output); - output = 0; - EXPECT_FALSE(StringToInt(input_string.begin(), input_string.end(), &output)); - EXPECT_EQ(6, output); - output = 0; - EXPECT_FALSE(StringToInt(input, input + arraysize(input), &output)); - EXPECT_EQ(6, output); string16 utf16_input = UTF8ToUTF16(input_string); - const char16* utf16_chars = utf16_input.c_str(); output = 0; EXPECT_FALSE(StringToInt(utf16_input, &output)); EXPECT_EQ(6, output); - output = 0; - EXPECT_FALSE(StringToInt(utf16_input.begin(), utf16_input.end(), &output)); - EXPECT_EQ(6, output); - output = 0; - EXPECT_FALSE(StringToInt(utf16_chars, - utf16_chars + utf16_input.length(), - &output)); - EXPECT_EQ(6, output); output = 0; const char16 negative_wide_input[] = { 0xFF4D, '4', '2', 0}; @@ -210,34 +175,14 @@ TEST(StringNumberConversionsTest, StringToInt64) { }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { - const char* ascii_chars = cases[i].input.c_str(); int64 output = 0; EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input, &output)); EXPECT_EQ(cases[i].output, output); - output = 0; - EXPECT_EQ(cases[i].success, StringToInt64(cases[i].input.begin(), - cases[i].input.end(), - &output)); - EXPECT_EQ(cases[i].output, output); - output = 0; - EXPECT_EQ(cases[i].success, StringToInt64( - ascii_chars, ascii_chars + cases[i].input.length(), &output)); - EXPECT_EQ(cases[i].output, output); string16 utf16_input = UTF8ToUTF16(cases[i].input); - const char16* utf16_chars = utf16_input.c_str(); output = 0; EXPECT_EQ(cases[i].success, StringToInt64(utf16_input, &output)); EXPECT_EQ(cases[i].output, output); - output = 0; - EXPECT_EQ(cases[i].success, StringToInt64(utf16_input.begin(), - utf16_input.end(), - &output)); - EXPECT_EQ(cases[i].output, output); - output = 0; - EXPECT_EQ(cases[i].success, StringToInt64( - utf16_chars, utf16_chars + utf16_input.length(), &output)); - EXPECT_EQ(cases[i].output, output); } // One additional test to verify that conversion of numbers in strings with @@ -248,28 +193,11 @@ TEST(StringNumberConversionsTest, StringToInt64) { int64 output; EXPECT_FALSE(StringToInt64(input_string, &output)); EXPECT_EQ(6, output); - output = 0; - EXPECT_FALSE(StringToInt64(input_string.begin(), - input_string.end(), - &output)); - EXPECT_EQ(6, output); - output = 0; - EXPECT_FALSE(StringToInt64(input, input + arraysize(input), &output)); - EXPECT_EQ(6, output); string16 utf16_input = UTF8ToUTF16(input_string); - const char16* utf16_chars = utf16_input.c_str(); output = 0; EXPECT_FALSE(StringToInt64(utf16_input, &output)); EXPECT_EQ(6, output); - output = 0; - EXPECT_FALSE(StringToInt64(utf16_input.begin(), utf16_input.end(), &output)); - EXPECT_EQ(6, output); - output = 0; - EXPECT_FALSE(StringToInt64(utf16_chars, - utf16_chars + utf16_input.length(), - &output)); - EXPECT_EQ(6, output); } TEST(StringNumberConversionsTest, HexStringToInt) { @@ -310,19 +238,9 @@ TEST(StringNumberConversionsTest, HexStringToInt) { }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { - const char* ascii_chars = cases[i].input.c_str(); int output = 0; EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input, &output)); EXPECT_EQ(cases[i].output, output); - output = 0; - EXPECT_EQ(cases[i].success, HexStringToInt(cases[i].input.begin(), - cases[i].input.end(), - &output)); - EXPECT_EQ(cases[i].output, output); - output = 0; - EXPECT_EQ(cases[i].success, HexStringToInt( - ascii_chars, ascii_chars + cases[i].input.length(), &output)); - EXPECT_EQ(cases[i].output, output); } // One additional test to verify that conversion of numbers in strings with // embedded NUL characters. The NUL and extra data after it should be @@ -332,14 +250,6 @@ TEST(StringNumberConversionsTest, HexStringToInt) { int output; EXPECT_FALSE(HexStringToInt(input_string, &output)); EXPECT_EQ(0xc0ffee, output); - output = 0; - EXPECT_FALSE(HexStringToInt(input_string.begin(), - input_string.end(), - &output)); - EXPECT_EQ(0xc0ffee, output); - output = 0; - EXPECT_FALSE(HexStringToInt(input, input + arraysize(input), &output)); - EXPECT_EQ(0xc0ffee, output); } TEST(StringNumberConversionsTest, HexStringToBytes) { diff --git a/base/string_piece.h b/base/string_piece.h index 278c7b6..5e420c1 100644 --- a/base/string_piece.h +++ b/base/string_piece.h @@ -60,6 +60,10 @@ class BASE_EXPORT StringPiece { : ptr_(str.data()), length_(str.size()) { } StringPiece(const char* offset, size_type len) : ptr_(offset), length_(len) { } + StringPiece(const std::string::const_iterator& begin, + const std::string::const_iterator& end) + : ptr_((end > begin) ? &(*begin) : NULL), + length_((end > begin) ? (size_type)(end - begin) : 0) { } // data() may return a pointer to a buffer with embedded NULs, and the // returned buffer may or may not be null terminated. Therefore it is @@ -197,6 +201,10 @@ class BASE_EXPORT StringPiece16 { : ptr_(str.data()), length_(str.size()) { } StringPiece16(const char16* offset, size_type len) : ptr_(offset), length_(len) { } + StringPiece16(const string16::const_iterator& begin, + const string16::const_iterator& end) + : ptr_((end > begin) ? &(*begin) : NULL), + length_((end > begin) ? (size_type)(end - begin) : 0) { } // data() may return a pointer to a buffer with embedded NULs, and the // returned buffer may or may not be null terminated. Therefore it is diff --git a/chrome/browser/autocomplete_history_manager.cc b/chrome/browser/autocomplete_history_manager.cc index 4006b29..5265666 100644 --- a/chrome/browser/autocomplete_history_manager.cc +++ b/chrome/browser/autocomplete_history_manager.cc @@ -19,6 +19,7 @@ #include "content/browser/tab_contents/tab_contents.h" #include "webkit/forms/form_data.h" +using base::StringPiece16; using webkit::forms::FormData; using webkit::forms::FormField; @@ -63,26 +64,32 @@ bool IsSSN(const string16& text) { return false; int area; - if (!base::StringToInt(number_string.begin(), - number_string.begin() + 3, - &area)) + if (!base::StringToInt(StringPiece16(number_string.begin(), + number_string.begin() + 3), + &area)) { return false; + } if (area < 1 || area == 666 || - area >= 900) + area >= 900) { return false; + } int group; - if (!base::StringToInt(number_string.begin() + 3, - number_string.begin() + 5, - &group) || group == 0) + if (!base::StringToInt(StringPiece16(number_string.begin() + 3, + number_string.begin() + 5), + &group) + || group == 0) { return false; + } int serial; - if (!base::StringToInt(number_string.begin() + 5, - number_string.begin() + 9, - &serial) || serial == 0) + if (!base::StringToInt(StringPiece16(number_string.begin() + 5, + number_string.begin() + 9), + &serial) + || serial == 0) { return false; + } return true; } diff --git a/chrome/browser/component_updater/component_updater_service.cc b/chrome/browser/component_updater/component_updater_service.cc index 6e2d98d..699dbc5 100644 --- a/chrome/browser/component_updater/component_updater_service.cc +++ b/chrome/browser/component_updater/component_updater_service.cc @@ -15,6 +15,7 @@ #include "base/memory/scoped_ptr.h" #include "base/stl_util.h" #include "base/string_number_conversions.h" +#include "base/string_piece.h" #include "base/string_util.h" #include "base/stringprintf.h" #include "base/timer.h" @@ -74,10 +75,13 @@ static std::string HexStringToID(const std::string& hexstr) { std::string id; for (size_t i = 0; i < hexstr.size(); ++i) { int val; - if (base::HexStringToInt(hexstr.begin() + i, hexstr.begin() + i + 1, &val)) + if (base::HexStringToInt(base::StringPiece(hexstr.begin() + i, + hexstr.begin() + i + 1), + &val)) { id.append(1, val + 'a'); - else + } else { id.append(1, 'a'); + } } DCHECK(Extension::IdIsValid(id)); return id; diff --git a/chrome/browser/history/text_database.cc b/chrome/browser/history/text_database.cc index bb70fc7..c5da03c 100644 --- a/chrome/browser/history/text_database.cc +++ b/chrome/browser/history/text_database.cc @@ -111,9 +111,14 @@ TextDatabase::DBIdent TextDatabase::FileNameToID(const FilePath& file_path) { return 0; } + // TODO: Once StringPiece supports a templated interface over the + // underlying string type, use it here instead of substr, since that + // will avoid needless string copies. StringPiece cannot be used + // right now because FilePath::StringType could use either 8 or 16 bit + // characters, depending on the OS. int year, month; - base::StringToInt(suffix.begin(), suffix.begin() + 4, &year); - base::StringToInt(suffix.begin() + 5, suffix.begin() + 7, &month); + base::StringToInt(suffix.substr(0, 4), &year); + base::StringToInt(suffix.substr(5, 2), &month); return year * 100 + month; } diff --git a/chrome/browser/profiles/profile_info_cache.cc b/chrome/browser/profiles/profile_info_cache.cc index d71e788..4b11f54 100644 --- a/chrome/browser/profiles/profile_info_cache.cc +++ b/chrome/browser/profiles/profile_info_cache.cc @@ -13,6 +13,7 @@ #include "base/rand_util.h" #include "base/stl_util.h" #include "base/string_number_conversions.h" +#include "base/string_piece.h" #include "base/stringprintf.h" #include "base/utf_string_conversions.h" #include "base/values.h" @@ -681,8 +682,9 @@ bool ProfileInfoCache::IsDefaultAvatarIconUrl(const std::string& url, return false; int int_value = -1; - if (base::StringToInt(url.begin() + strlen(kDefaultUrlPrefix), - url.end(), + if (base::StringToInt(base::StringPiece(url.begin() + + strlen(kDefaultUrlPrefix), + url.end()), &int_value)) { if (int_value < 0 || int_value >= static_cast<int>(kDefaultAvatarIconsCount)) diff --git a/chrome/browser/safe_browsing/filter_false_positive_perftest.cc b/chrome/browser/safe_browsing/filter_false_positive_perftest.cc index be48a4e..22412cb2 100644 --- a/chrome/browser/safe_browsing/filter_false_positive_perftest.cc +++ b/chrome/browser/safe_browsing/filter_false_positive_perftest.cc @@ -64,6 +64,7 @@ #include "base/path_service.h" #include "base/rand_util.h" #include "base/string_number_conversions.h" +#include "base/string_piece.h" #include "base/string_util.h" #include "base/time.h" #include "chrome/browser/safe_browsing/bloom_filter.h" @@ -242,7 +243,8 @@ void CalculateBloomFilterFalsePositives( if (use_weights) { std::string::size_type pos = url.find_last_of(","); if (pos != std::string::npos) { - base::StringToInt(url.begin() + pos + 1, url.end(), &weight); + base::StringToInt(base::StringPiece(url.begin() + pos + 1, url.end()), + &weight); url = url.substr(0, pos); } } diff --git a/chrome/browser/safe_browsing/safe_browsing_blocking_page.cc b/chrome/browser/safe_browsing/safe_browsing_blocking_page.cc index 976c414..05dfb39 100644 --- a/chrome/browser/safe_browsing/safe_browsing_blocking_page.cc +++ b/chrome/browser/safe_browsing/safe_browsing_blocking_page.cc @@ -12,6 +12,7 @@ #include "base/i18n/rtl.h" #include "base/lazy_instance.h" #include "base/string_number_conversions.h" +#include "base/string_piece.h" #include "base/stringprintf.h" #include "base/utf_string_conversions.h" #include "base/values.h" @@ -467,8 +468,9 @@ void SafeBrowsingBlockingPage::CommandReceived(const std::string& cmd) { size_t colon_index = command.find(':'); if (colon_index != std::string::npos) { DCHECK(colon_index < command.size() - 1); - bool result = base::StringToInt(command.begin() + colon_index + 1, - command.end(), + bool result = base::StringToInt(base::StringPiece(command.begin() + + colon_index + 1, + command.end()), &element_index); command = command.substr(0, colon_index); DCHECK(result); diff --git a/chrome/common/extensions/extension.cc b/chrome/common/extensions/extension.cc index 4b790f0..9c1623a 100644 --- a/chrome/common/extensions/extension.cc +++ b/chrome/common/extensions/extension.cc @@ -17,6 +17,7 @@ #include "base/stl_util.h" #include "base/string16.h" #include "base/string_number_conversions.h" +#include "base/string_piece.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "base/values.h" @@ -79,10 +80,13 @@ const char kDefaultContentSecurityPolicy[] = static void ConvertHexadecimalToIDAlphabet(std::string* id) { for (size_t i = 0; i < id->size(); ++i) { int val; - if (base::HexStringToInt(id->begin() + i, id->begin() + i + 1, &val)) + if (base::HexStringToInt(base::StringPiece(id->begin() + i, + id->begin() + i + 1), + &val)) { (*id)[i] = val + 'a'; - else + } else { (*id)[i] = 'a'; + } } } diff --git a/chrome/test/perf/page_cycler_test.cc b/chrome/test/perf/page_cycler_test.cc index 31401ea..e1f284e 100644 --- a/chrome/test/perf/page_cycler_test.cc +++ b/chrome/test/perf/page_cycler_test.cc @@ -9,6 +9,7 @@ #include "base/path_service.h" #include "base/process_util.h" #include "base/string_number_conversions.h" +#include "base/string_piece.h" #include "base/sys_string_conversions.h" #include "base/test/test_timeouts.h" #include "base/utf_string_conversions.h" @@ -331,8 +332,8 @@ static bool HasDatabaseErrors(const std::string timings) { new_pos = timings.find(',', pos); if (new_pos == std::string::npos) new_pos = timings.length(); - if (!base::StringToInt(timings.begin() + pos, - timings.begin() + new_pos, + if (!base::StringToInt(base::StringPiece(timings.begin() + pos, + timings.begin() + new_pos), &time)) { LOG(ERROR) << "Invalid time reported: " << time_str; return true; diff --git a/chrome_frame/test/test_server.cc b/chrome_frame/test/test_server.cc index f2d7de3..cddcd2c 100644 --- a/chrome_frame/test/test_server.cc +++ b/chrome_frame/test/test_server.cc @@ -9,6 +9,7 @@ #include "base/bind.h" #include "base/logging.h" #include "base/string_number_conversions.h" +#include "base/string_piece.h" #include "base/string_util.h" #include "base/stringprintf.h" #include "base/utf_string_conversions.h" @@ -49,8 +50,8 @@ void Request::ParseHeaders(const std::string& headers) { while (it.GetNext()) { if (LowerCaseEqualsASCII(it.name(), "content-length")) { int int_content_length; - base::StringToInt(it.values_begin(), - it.values_end(), + base::StringToInt(base::StringPiece(it.values_begin(), + it.values_end()), &int_content_length); content_length_ = int_content_length; break; diff --git a/chrome_frame/utils.cc b/chrome_frame/utils.cc index 9003109..8610f83 100644 --- a/chrome_frame/utils.cc +++ b/chrome_frame/utils.cc @@ -16,6 +16,7 @@ #include "base/logging.h" #include "base/path_service.h" #include "base/string_number_conversions.h" +#include "base/string_piece.h" #include "base/string_tokenizer.h" #include "base/string_util.h" #include "base/stringprintf.h" @@ -1554,7 +1555,9 @@ int GetXUaCompatibleDirective(const std::string& directive, char delimiter) { } int header_ie_version = 0; - if (!base::StringToInt(filter_begin + 2, filter_end, &header_ie_version) || + if (!base::StringToInt(base::StringPiece(filter_begin + 2, + filter_end), + &header_ie_version) || header_ie_version == 0) { // ensure it's not a sequence of 0's continue; } diff --git a/net/base/net_util.cc b/net/base/net_util.cc index 7ae4363d..180a905 100644 --- a/net/base/net_util.cc +++ b/net/base/net_util.cc @@ -1985,8 +1985,8 @@ void SetExplicitlyAllowedPorts(const std::string& allowed_ports) { if (i == size || allowed_ports[i] == kComma) { if (i > last) { int port; - base::StringToInt(allowed_ports.begin() + last, - allowed_ports.begin() + i, + base::StringToInt(base::StringPiece(allowed_ports.begin() + last, + allowed_ports.begin() + i), &port); ports.insert(port); } diff --git a/net/base/x509_cert_types.cc b/net/base/x509_cert_types.cc index 6beb3ec..7578ccd 100644 --- a/net/base/x509_cert_types.cc +++ b/net/base/x509_cert_types.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -20,7 +20,7 @@ namespace { // untouched otherwise. Returns the parsed integer. int ParseIntAndAdvance(const char** field, size_t field_len, bool* ok) { int result = 0; - *ok &= base::StringToInt(*field, *field + field_len, &result); + *ok &= base::StringToInt(base::StringPiece(*field, field_len), &result); *field += field_len; return result; } diff --git a/net/ftp/ftp_ctrl_response_buffer.cc b/net/ftp/ftp_ctrl_response_buffer.cc index 4aeef1f..670c70d 100644 --- a/net/ftp/ftp_ctrl_response_buffer.cc +++ b/net/ftp/ftp_ctrl_response_buffer.cc @@ -1,11 +1,12 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this -// source code is governed by a BSD-style license that can be found in the -// LICENSE file. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. #include "net/ftp/ftp_ctrl_response_buffer.h" #include "base/logging.h" #include "base/string_number_conversions.h" +#include "base/string_piece.h" //#include "base/string_util.h" #include "net/base/net_errors.h" @@ -91,7 +92,8 @@ FtpCtrlResponseBuffer::ParsedLine FtpCtrlResponseBuffer::ParseLine( ParsedLine result; if (line.length() >= 3) { - if (base::StringToInt(line.begin(), line.begin() + 3, &result.status_code)) + if (base::StringToInt(base::StringPiece(line.begin(), line.begin() + 3), + &result.status_code)) result.has_status_code = (100 <= result.status_code && result.status_code <= 599); if (result.has_status_code && line.length() >= 4 && line[3] == ' ') { diff --git a/net/ftp/ftp_util.cc b/net/ftp/ftp_util.cc index 553d513..f6bdf2f 100644 --- a/net/ftp/ftp_util.cc +++ b/net/ftp/ftp_util.cc @@ -12,6 +12,7 @@ #include "base/logging.h" #include "base/memory/singleton.h" #include "base/string_number_conversions.h" +#include "base/string_piece.h" #include "base/string_split.h" #include "base/string_tokenizer.h" #include "base/string_util.h" @@ -21,6 +22,8 @@ #include "unicode/dtfmtsym.h" #include "unicode/uchar.h" +using base::StringPiece16; + // For examples of Unix<->VMS path conversions, see the unit test file. On VMS // a path looks differently depending on whether it's a file or directory. @@ -211,26 +214,26 @@ bool FtpUtil::LsDateListingToTime(const string16& month, const string16& day, if (!base::StringToInt(rest, &time_exploded.year)) { // Maybe it's time. Does it look like time (HH:MM)? if (rest.length() == 5 && rest[2] == ':') { - if (!base::StringToInt(rest.begin(), - rest.begin() + 2, - &time_exploded.hour)) + if (!base::StringToInt(StringPiece16(rest.begin(), rest.begin() + 2), + &time_exploded.hour)) { return false; + } - if (!base::StringToInt(rest.begin() + 3, - rest.begin() + 5, - &time_exploded.minute)) + if (!base::StringToInt(StringPiece16(rest.begin() + 3, rest.begin() + 5), + &time_exploded.minute)) { return false; + } } else if (rest.length() == 4 && rest[1] == ':') { // Sometimes it's just H:MM. - if (!base::StringToInt(rest.begin(), - rest.begin() + 1, - &time_exploded.hour)) + if (!base::StringToInt(StringPiece16(rest.begin(), rest.begin() + 1), + &time_exploded.hour)) { return false; + } - if (!base::StringToInt(rest.begin() + 2, - rest.begin() + 4, - &time_exploded.minute)) + if (!base::StringToInt(StringPiece16(rest.begin() + 2, rest.begin() + 4), + &time_exploded.minute)) { return false; + } } else { return false; } diff --git a/net/http/http_chunked_decoder.cc b/net/http/http_chunked_decoder.cc index d5b16dd..87f54640 100644 --- a/net/http/http_chunked_decoder.cc +++ b/net/http/http_chunked_decoder.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -187,12 +187,14 @@ bool HttpChunkedDecoder::ParseChunkSize(const char* start, int len, int* out) { // Be more restrictive than HexStringToInt; // don't allow inputs with leading "-", "+", "0x", "0X" - if (base::StringPiece(start, len).find_first_not_of("0123456789abcdefABCDEF") - != base::StringPiece::npos) + base::StringPiece chunk_size(start, len); + if (chunk_size.find_first_not_of("0123456789abcdefABCDEF") + != base::StringPiece::npos) { return false; + } int parsed_number; - bool ok = base::HexStringToInt(start, start + len, &parsed_number); + bool ok = base::HexStringToInt(chunk_size, &parsed_number); if (ok && parsed_number >= 0) { *out = parsed_number; return true; diff --git a/net/http/http_response_headers.cc b/net/http/http_response_headers.cc index f249db8..98bd052 100644 --- a/net/http/http_response_headers.cc +++ b/net/http/http_response_headers.cc @@ -15,11 +15,13 @@ #include "base/metrics/histogram.h" #include "base/pickle.h" #include "base/string_number_conversions.h" +#include "base/string_piece.h" #include "base/string_util.h" #include "base/time.h" #include "net/base/escape.h" #include "net/http/http_util.h" +using base::StringPiece; using base::Time; using base::TimeDelta; @@ -698,7 +700,7 @@ void HttpResponseHeaders::ParseStatusLine( raw_headers_.push_back(' '); raw_headers_.append(code, p); raw_headers_.push_back(' '); - base::StringToInt(code, p, &response_code_); + base::StringToInt(StringPiece(code, p), &response_code_); // Skip whitespace. while (*p == ' ') @@ -1071,8 +1073,8 @@ bool HttpResponseHeaders::GetMaxAgeValue(TimeDelta* result) const { value.begin() + kMaxAgePrefixLen, kMaxAgePrefix)) { int64 seconds; - base::StringToInt64(value.begin() + kMaxAgePrefixLen, - value.end(), + base::StringToInt64(StringPiece(value.begin() + kMaxAgePrefixLen, + value.end()), &seconds); *result = TimeDelta::FromSeconds(seconds); return true; @@ -1250,8 +1252,8 @@ bool HttpResponseHeaders::GetContentRange(int64* first_byte_position, byte_range_resp_spec.begin() + minus_position; HttpUtil::TrimLWS(&first_byte_pos_begin, &first_byte_pos_end); - bool ok = base::StringToInt64(first_byte_pos_begin, - first_byte_pos_end, + bool ok = base::StringToInt64(StringPiece(first_byte_pos_begin, + first_byte_pos_end), first_byte_position); // Obtain last-byte-pos. @@ -1261,8 +1263,8 @@ bool HttpResponseHeaders::GetContentRange(int64* first_byte_position, byte_range_resp_spec.end(); HttpUtil::TrimLWS(&last_byte_pos_begin, &last_byte_pos_end); - ok &= base::StringToInt64(last_byte_pos_begin, - last_byte_pos_end, + ok &= base::StringToInt64(StringPiece(last_byte_pos_begin, + last_byte_pos_end), last_byte_position); if (!ok) { *first_byte_position = *last_byte_position = -1; @@ -1286,8 +1288,8 @@ bool HttpResponseHeaders::GetContentRange(int64* first_byte_position, if (LowerCaseEqualsASCII(instance_length_begin, instance_length_end, "*")) { return false; - } else if (!base::StringToInt64(instance_length_begin, - instance_length_end, + } else if (!base::StringToInt64(StringPiece(instance_length_begin, + instance_length_end), instance_length)) { *instance_length = -1; return false; diff --git a/net/proxy/proxy_bypass_rules.cc b/net/proxy/proxy_bypass_rules.cc index 3d622a8..1c0fd61 100644 --- a/net/proxy/proxy_bypass_rules.cc +++ b/net/proxy/proxy_bypass_rules.cc @@ -7,6 +7,7 @@ #include "base/stl_util.h" #include "base/stringprintf.h" #include "base/string_number_conversions.h" +#include "base/string_piece.h" #include "base/string_tokenizer.h" #include "base/string_util.h" #include "net/base/net_util.h" @@ -315,7 +316,9 @@ bool ProxyBypassRules::AddRuleFromStringInternal( host = raw; port = -1; if (pos_colon != std::string::npos) { - if (!base::StringToInt(raw.begin() + pos_colon + 1, raw.end(), &port) || + if (!base::StringToInt(base::StringPiece(raw.begin() + pos_colon + 1, + raw.end()), + &port) || (port < 0 || port > 0xFFFF)) { return false; // Port was invalid. } |