diff options
author | wtc@chromium.org <wtc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-27 03:00:48 +0000 |
---|---|---|
committer | wtc@chromium.org <wtc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-27 03:00:48 +0000 |
commit | dcbffbacd7e233de6a13876ff99edf323c9bcfa6 (patch) | |
tree | cda2735e7847e944c4e0e01733cbc7b53270a137 /base | |
parent | 3ef60118b27baa091fb13a08bc2110b0afd8db82 (diff) | |
download | chromium_src-dcbffbacd7e233de6a13876ff99edf323c9bcfa6.zip chromium_src-dcbffbacd7e233de6a13876ff99edf323c9bcfa6.tar.gz chromium_src-dcbffbacd7e233de6a13876ff99edf323c9bcfa6.tar.bz2 |
Add ToUpperASCII and StringToUpperASCII.
R=darin,eroman
Review URL: http://codereview.chromium.org/28227
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10576 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/string_util.h | 25 | ||||
-rw-r--r-- | base/string_util_unittest.cc | 26 |
2 files changed, 49 insertions, 2 deletions
diff --git a/base/string_util.h b/base/string_util.h index 93b4898..b65253a 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -7,9 +7,10 @@ #ifndef BASE_STRING_UTIL_H_ #define BASE_STRING_UTIL_H_ +#include <stdarg.h> // va_list + #include <string> #include <vector> -#include <stdarg.h> // va_list #include "base/basictypes.h" #include "base/string16.h" @@ -176,7 +177,7 @@ std::wstring UTF8ToWide(const std::string& utf8); bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output); string16 WideToUTF16(const std::wstring& wide); bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output); -std::wstring UTF16ToWide(const string16& utf8); +std::wstring UTF16ToWide(const string16& utf16); bool UTF8ToUTF16(const char* src, size_t src_len, string16* output); string16 UTF8ToUTF16(const std::string& utf8); @@ -259,6 +260,26 @@ template <class str> inline str StringToLowerASCII(const str& s) { return output; } +// 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) { + return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; +} + +// 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 ASCII // string. This is useful for doing checking if an input string matches some // token, and it is optimized to avoid intermediate string copies. This API is diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc index 2b7634f..9d099ab 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -572,6 +572,32 @@ TEST(StringUtilTest, ConvertASCII) { EXPECT_EQ(0, string_with_nul.compare(narrow_with_nul)); } +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); + + std::wstring original_w(L"Cc2"); + std::wstring upper_w = StringToUpperASCII(original_w); + EXPECT_EQ(L"CC2", upper_w); +} + static const struct { const wchar_t* src_w; const char* src_a; |