From c15100ce1266e52a77f2974ffe9dfb833f5e58b3 Mon Sep 17 00:00:00 2001 From: brettw Date: Thu, 6 Aug 2015 15:54:16 -0700 Subject: 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} --- base/strings/string_util.cc | 38 ++++++++++++++++++++++++++++++++++++ base/strings/string_util.h | 34 +++++++++++++++++--------------- base/strings/string_util_unittest.cc | 36 +++++++++++++++++----------------- 3 files changed, 74 insertions(+), 34 deletions(-) (limited to 'base/strings') 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 +StringType ToLowerASCIIImpl(BasicStringPiece 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 +StringType ToUpperASCIIImpl(BasicStringPiece 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(str); +} + +string16 ToLowerASCII(StringPiece16 str) { + return ToLowerASCIIImpl(str); +} + +std::string ToUpperASCII(StringPiece str) { + return ToUpperASCIIImpl(str); +} + +string16 ToUpperASCII(StringPiece16 str) { + return ToUpperASCIIImpl(str); +} + template int CompareCaseInsensitiveASCIIT(BasicStringPiece a, BasicStringPiece 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 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 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 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 inline str StringToLowerASCII(const str& s) { // for std::string and std::wstring str output(s); @@ -303,20 +319,6 @@ template 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 inline void StringToUpperASCII(str* s) { - for (typename str::iterator i = s->begin(); i != s->end(); ++i) - *i = ToUpperASCII(*i); -} - -template 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('c'), ToLowerASCII(static_cast('C'))); + EXPECT_EQ(static_cast('c'), ToLowerASCII(static_cast('c'))); + EXPECT_EQ(static_cast('2'), ToLowerASCII(static_cast('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('C'), ToUpperASCII(static_cast('C'))); + EXPECT_EQ(static_cast('C'), ToUpperASCII(static_cast('c'))); + EXPECT_EQ(static_cast('2'), ToUpperASCII(static_cast('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) { -- cgit v1.1