diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 3 | ||||
-rw-r--r-- | base/i18n/case_conversion.cc | 34 | ||||
-rw-r--r-- | base/i18n/case_conversion.h | 27 | ||||
-rw-r--r-- | base/i18n/case_conversion_unittest.cc | 39 |
4 files changed, 103 insertions, 0 deletions
diff --git a/base/base.gyp b/base/base.gyp index 868e20e..da5d018 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -37,6 +37,8 @@ 'i18n/break_iterator.h', 'i18n/char_iterator.cc', 'i18n/char_iterator.h', + 'i18n/case_conversion.cc', + 'i18n/case_conversion.h', 'i18n/file_util_icu.cc', 'i18n/file_util_icu.h', 'i18n/icu_encoding_detection.cc', @@ -126,6 +128,7 @@ 'id_map_unittest.cc', 'i18n/break_iterator_unittest.cc', 'i18n/char_iterator_unittest.cc', + 'i18n/case_conversion_unittest.cc', 'i18n/file_util_icu_unittest.cc', 'i18n/icu_string_conversions_unittest.cc', 'i18n/rtl_unittest.cc', diff --git a/base/i18n/case_conversion.cc b/base/i18n/case_conversion.cc new file mode 100644 index 0000000..2dedade --- /dev/null +++ b/base/i18n/case_conversion.cc @@ -0,0 +1,34 @@ +// 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 "base/i18n/case_conversion.h" + +#include "base/utf_string_conversions.h" +#include "unicode/unistr.h" + +namespace base { +namespace i18n { + +string16 ToLower(const string16& string) { + icu::UnicodeString unicode_string(string.c_str(), string.size()); + unicode_string.toLower(); + return string16(unicode_string.getBuffer(), unicode_string.length()); +} + +std::wstring WideToLower(const std::wstring& string) { + return UTF16ToWide(ToLower(WideToUTF16(string))); +} + +string16 ToUpper(const string16& string) { + icu::UnicodeString unicode_string(string.c_str(), string.size()); + unicode_string.toUpper(); + return string16(unicode_string.getBuffer(), unicode_string.length()); +} + +std::wstring WideToUpper(const std::wstring& string) { + return UTF16ToWide(ToUpper(WideToUTF16(string))); +} + +} // namespace i18n +} // namespace base diff --git a/base/i18n/case_conversion.h b/base/i18n/case_conversion.h new file mode 100644 index 0000000..d834ede --- /dev/null +++ b/base/i18n/case_conversion.h @@ -0,0 +1,27 @@ +// 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. + +#ifndef BASE_I18N_CASE_CONVERSION_ +#define BASE_I18N_CASE_CONVERSION_ +#pragma once + +#include <string> + +#include "base/string16.h" + +namespace base { +namespace i18n { + +// Returns the lower case equivalent of string. Uses ICU's default locale. +string16 ToLower(const string16& string); +std::wstring WideToLower(const std::wstring& string); + +// Returns the upper case equivalent of string. Uses ICU's default locale. +string16 ToUpper(const string16& string); +std::wstring WideToUpper(const std::wstring& string); + +} // namespace i18n +} // namespace base + +#endif // BASE_I18N_CASE_CONVERSION_ diff --git a/base/i18n/case_conversion_unittest.cc b/base/i18n/case_conversion_unittest.cc new file mode 100644 index 0000000..87a349e --- /dev/null +++ b/base/i18n/case_conversion_unittest.cc @@ -0,0 +1,39 @@ +// 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 "base/i18n/case_conversion.h" +#include "base/utf_string_conversions.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +// Test upper and lower case string conversion. +TEST(CaseConversionTest, UpperLower) { + string16 mixed(ASCIIToUTF16("Text with UPPer & lowER casE.")); + const string16 expected_lower(ASCIIToUTF16("text with upper & lower case.")); + const string16 expected_upper(ASCIIToUTF16("TEXT WITH UPPER & LOWER CASE.")); + + string16 result = base::i18n::ToLower(mixed); + EXPECT_EQ(expected_lower, result); + + result = base::i18n::ToUpper(mixed); + EXPECT_EQ(expected_upper, result); +} + +// Test upper and lower case string conversion. +TEST(CaseConversionTest, WideUpperLower) { + std::wstring mixed(L"Text with UPPer & lowER casE."); + const std::wstring expected_lower(L"text with upper & lower case."); + const std::wstring expected_upper(L"TEXT WITH UPPER & LOWER CASE."); + + std::wstring result = base::i18n::WideToLower(mixed); + EXPECT_EQ(expected_lower, result); + + result = base::i18n::WideToUpper(mixed); + EXPECT_EQ(expected_upper, result); +} + +// TODO(jshin): More tests are needed, especially with non-ASCII characters. + +} // namespace |