summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
Diffstat (limited to 'base')
-rw-r--r--base/mac/mac_util.mm5
-rw-r--r--base/string_number_conversions.cc149
-rw-r--r--base/string_number_conversions.h38
-rw-r--r--base/string_number_conversions_unittest.cc90
-rw-r--r--base/string_piece.h8
5 files changed, 52 insertions, 238 deletions
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