summaryrefslogtreecommitdiffstats
path: root/base/strings
diff options
context:
space:
mode:
authorbrettw <brettw@chromium.org>2015-08-06 15:54:16 -0700
committerCommit bot <commit-bot@chromium.org>2015-08-06 22:55:04 +0000
commitc15100ce1266e52a77f2974ffe9dfb833f5e58b3 (patch)
tree9cf77374c512e02e4a1401db68a952d3107e0aec /base/strings
parentedfd2506bf1b5b270168fbab8b114e879abbb828 (diff)
downloadchromium_src-c15100ce1266e52a77f2974ffe9dfb833f5e58b3.zip
chromium_src-c15100ce1266e52a77f2974ffe9dfb833f5e58b3.tar.gz
chromium_src-c15100ce1266e52a77f2974ffe9dfb833f5e58b3.tar.bz2
Replace StringToLowerASCII with base::ToLowerASCII
Standardize on using string pieces and returning strings. Remove in-place version (this was only used in a couple places and they were not performance-critical). De-templatize the character versions of ToUpperASCII/ToLowerASCII. This would lead to bizarre errors if you pass other things (like a string). This is so little code, it's now just duplicated. I renamed StringToLowerASCII to just be ToLowerASCII so you can pass whatever you want to ToLowerASCII and it does the right thing. This seems simpler to me. This replaces all calls of StringToUpperASCII to the new form. The lowercase version is more common and will be done in a separate pass. Review URL: https://codereview.chromium.org/1280473002 Cr-Commit-Position: refs/heads/master@{#342219}
Diffstat (limited to 'base/strings')
-rw-r--r--base/strings/string_util.cc38
-rw-r--r--base/strings/string_util.h34
-rw-r--r--base/strings/string_util_unittest.cc36
3 files changed, 74 insertions, 34 deletions
diff --git a/base/strings/string_util.cc b/base/strings/string_util.cc
index 725d86d..19c38d5 100644
--- a/base/strings/string_util.cc
+++ b/base/strings/string_util.cc
@@ -148,6 +148,44 @@ bool IsWprintfFormatPortable(const wchar_t* format) {
return true;
}
+namespace {
+
+template<typename StringType>
+StringType ToLowerASCIIImpl(BasicStringPiece<StringType> str) {
+ StringType ret;
+ ret.reserve(str.size());
+ for (size_t i = 0; i < str.size(); i++)
+ ret.push_back(ToLowerASCII(str[i]));
+ return ret;
+}
+
+template<typename StringType>
+StringType ToUpperASCIIImpl(BasicStringPiece<StringType> str) {
+ StringType ret;
+ ret.reserve(str.size());
+ for (size_t i = 0; i < str.size(); i++)
+ ret.push_back(ToUpperASCII(str[i]));
+ return ret;
+}
+
+} // namespace
+
+std::string ToLowerASCII(StringPiece str) {
+ return ToLowerASCIIImpl<std::string>(str);
+}
+
+string16 ToLowerASCII(StringPiece16 str) {
+ return ToLowerASCIIImpl<string16>(str);
+}
+
+std::string ToUpperASCII(StringPiece str) {
+ return ToUpperASCIIImpl<std::string>(str);
+}
+
+string16 ToUpperASCII(StringPiece16 str) {
+ return ToUpperASCIIImpl<string16>(str);
+}
+
template<class StringType>
int CompareCaseInsensitiveASCIIT(BasicStringPiece<StringType> a,
BasicStringPiece<StringType> b) {
diff --git a/base/strings/string_util.h b/base/strings/string_util.h
index 01dc3fc..3ec74a5 100644
--- a/base/strings/string_util.h
+++ b/base/strings/string_util.h
@@ -93,16 +93,30 @@ BASE_EXPORT bool IsWprintfFormatPortable(const wchar_t* format);
// ASCII-specific tolower. The standard library's tolower is locale sensitive,
// so we don't want to use it here.
-template <class Char> inline Char ToLowerASCII(Char c) {
+inline char ToLowerASCII(char c) {
+ return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
+}
+inline char16 ToLowerASCII(char16 c) {
return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
}
// ASCII-specific toupper. The standard library's toupper is locale sensitive,
// so we don't want to use it here.
-template <class Char> inline Char ToUpperASCII(Char c) {
+inline char ToUpperASCII(char c) {
+ return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
+}
+inline char16 ToUpperASCII(char16 c) {
return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c;
}
+// Converts the given string to it's ASCII-lowercase equivalent.
+BASE_EXPORT std::string ToLowerASCII(StringPiece str);
+BASE_EXPORT string16 ToLowerASCII(StringPiece16 str);
+
+// Converts the given string to it's ASCII-uppercase equivalent.
+BASE_EXPORT std::string ToUpperASCII(StringPiece str);
+BASE_EXPORT string16 ToUpperASCII(StringPiece16 str);
+
// Functor for case-insensitive ASCII comparisons for STL algorithms like
// std::search.
//
@@ -291,11 +305,13 @@ BASE_EXPORT bool IsStringASCII(const std::wstring& str);
// Converts the elements of the given string. This version uses a pointer to
// clearly differentiate it from the non-pointer variant.
+// TODO(brettw) remove this. Callers should use base::ToLowerASCII above.
template <class str> inline void StringToLowerASCII(str* s) {
for (typename str::iterator i = s->begin(); i != s->end(); ++i)
*i = ToLowerASCII(*i);
}
+// TODO(brettw) remove this. Callers should use base::ToLowerASCII above.
template <class str> inline str StringToLowerASCII(const str& s) {
// for std::string and std::wstring
str output(s);
@@ -303,20 +319,6 @@ template <class str> inline str StringToLowerASCII(const str& s) {
return output;
}
-// Converts the elements of the given string. This version uses a pointer to
-// clearly differentiate it from the non-pointer variant.
-template <class str> inline void StringToUpperASCII(str* s) {
- for (typename str::iterator i = s->begin(); i != s->end(); ++i)
- *i = ToUpperASCII(*i);
-}
-
-template <class str> inline str StringToUpperASCII(const str& s) {
- // for std::string and std::wstring
- str output(s);
- StringToUpperASCII(&output);
- return output;
-}
-
// Compare the lower-case form of the given string against the given
// previously-lower-cased ASCII string (typically a constant).
BASE_EXPORT bool LowerCaseEqualsASCII(StringPiece str,
diff --git a/base/strings/string_util_unittest.cc b/base/strings/string_util_unittest.cc
index eb6cd7e..187e49e 100644
--- a/base/strings/string_util_unittest.cc
+++ b/base/strings/string_util_unittest.cc
@@ -503,30 +503,30 @@ TEST(StringUtilTest, ConvertASCII) {
EXPECT_EQ(0, string_with_nul.compare(narrow_with_nul));
}
+TEST(StringUtilTest, ToLowerASCII) {
+ EXPECT_EQ('c', ToLowerASCII('C'));
+ EXPECT_EQ('c', ToLowerASCII('c'));
+ EXPECT_EQ('2', ToLowerASCII('2'));
+
+ EXPECT_EQ(static_cast<char16>('c'), ToLowerASCII(static_cast<char16>('C')));
+ EXPECT_EQ(static_cast<char16>('c'), ToLowerASCII(static_cast<char16>('c')));
+ EXPECT_EQ(static_cast<char16>('2'), ToLowerASCII(static_cast<char16>('2')));
+
+ EXPECT_EQ("cc2", ToLowerASCII("Cc2"));
+ EXPECT_EQ(ASCIIToUTF16("cc2"), ToLowerASCII(ASCIIToUTF16("Cc2")));
+}
+
TEST(StringUtilTest, ToUpperASCII) {
EXPECT_EQ('C', ToUpperASCII('C'));
EXPECT_EQ('C', ToUpperASCII('c'));
EXPECT_EQ('2', ToUpperASCII('2'));
- EXPECT_EQ(L'C', ToUpperASCII(L'C'));
- EXPECT_EQ(L'C', ToUpperASCII(L'c'));
- EXPECT_EQ(L'2', ToUpperASCII(L'2'));
-
- std::string in_place_a("Cc2");
- StringToUpperASCII(&in_place_a);
- EXPECT_EQ("CC2", in_place_a);
-
- std::wstring in_place_w(L"Cc2");
- StringToUpperASCII(&in_place_w);
- EXPECT_EQ(L"CC2", in_place_w);
-
- std::string original_a("Cc2");
- std::string upper_a = StringToUpperASCII(original_a);
- EXPECT_EQ("CC2", upper_a);
+ EXPECT_EQ(static_cast<char16>('C'), ToUpperASCII(static_cast<char16>('C')));
+ EXPECT_EQ(static_cast<char16>('C'), ToUpperASCII(static_cast<char16>('c')));
+ EXPECT_EQ(static_cast<char16>('2'), ToUpperASCII(static_cast<char16>('2')));
- std::wstring original_w(L"Cc2");
- std::wstring upper_w = StringToUpperASCII(original_w);
- EXPECT_EQ(L"CC2", upper_w);
+ EXPECT_EQ("CC2", ToUpperASCII("Cc2"));
+ EXPECT_EQ(ASCIIToUTF16("CC2"), ToUpperASCII(ASCIIToUTF16("Cc2")));
}
TEST(StringUtilTest, LowerCaseEqualsASCII) {