Commit 4cf0d9d1 authored by tfarina@chromium.org's avatar tfarina@chromium.org

base/i18n: Add namespace i18n to CharIterator.

BUG=None
TEST=None

Review URL: http://codereview.chromium.org/6355005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71910 0039d316-1c4b-4281-b951-d872f2087c98
parent d462c1ab
// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "net/base/net_util.h" #include "net/base/net_util.h"
#include "net/base/registry_controlled_domain.h" #include "net/base/registry_controlled_domain.h"
namespace { namespace {
const char* kEllipsis = "\xE2\x80\xA6"; const char* kEllipsis = "\xE2\x80\xA6";
...@@ -616,7 +615,7 @@ void RectangleString::AddWord(const string16& word) { ...@@ -616,7 +615,7 @@ void RectangleString::AddWord(const string16& word) {
// Word is so big that it must be fragmented. // Word is so big that it must be fragmented.
int array_start = 0; int array_start = 0;
int char_start = 0; int char_start = 0;
base::UTF16CharIterator chars(&word); base::i18n::UTF16CharIterator chars(&word);
while (!chars.end()) { while (!chars.end()) {
// When boundary is hit, add as much as will fit on this line. // When boundary is hit, add as much as will fit on this line.
if (current_col_ + (chars.char_pos() - char_start) >= max_cols_) { if (current_col_ + (chars.char_pos() - char_start) >= max_cols_) {
......
// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "unicode/utf16.h" #include "unicode/utf16.h"
namespace base { namespace base {
namespace i18n {
UTF8CharIterator::UTF8CharIterator(const std::string* str) UTF8CharIterator::UTF8CharIterator(const std::string* str)
: str_(reinterpret_cast<const uint8_t*>(str->data())), : str_(reinterpret_cast<const uint8_t*>(str->data())),
...@@ -20,6 +21,9 @@ UTF8CharIterator::UTF8CharIterator(const std::string* str) ...@@ -20,6 +21,9 @@ UTF8CharIterator::UTF8CharIterator(const std::string* str)
U8_NEXT(str_, next_pos_, len_, char_); U8_NEXT(str_, next_pos_, len_, char_);
} }
UTF8CharIterator::~UTF8CharIterator() {
}
bool UTF8CharIterator::Advance() { bool UTF8CharIterator::Advance() {
if (array_pos_ >= len_) if (array_pos_ >= len_)
return false; return false;
...@@ -54,6 +58,9 @@ UTF16CharIterator::UTF16CharIterator(const char16* str, size_t str_len) ...@@ -54,6 +58,9 @@ UTF16CharIterator::UTF16CharIterator(const char16* str, size_t str_len)
ReadChar(); ReadChar();
} }
UTF16CharIterator::~UTF16CharIterator() {
}
bool UTF16CharIterator::Advance() { bool UTF16CharIterator::Advance() {
if (array_pos_ >= len_) if (array_pos_ >= len_)
return false; return false;
...@@ -71,4 +78,5 @@ void UTF16CharIterator::ReadChar() { ...@@ -71,4 +78,5 @@ void UTF16CharIterator::ReadChar() {
U16_NEXT(str_, next_pos_, len_, char_); U16_NEXT(str_, next_pos_, len_, char_);
} }
} // namespace i18n
} // namespace base } // namespace base
// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -25,12 +25,13 @@ typedef unsigned char uint8_t; ...@@ -25,12 +25,13 @@ typedef unsigned char uint8_t;
#endif #endif
namespace base { namespace base {
namespace i18n {
class UTF8CharIterator { class UTF8CharIterator {
public: public:
// Requires |str| to live as long as the UTF8CharIterator does. // Requires |str| to live as long as the UTF8CharIterator does.
UTF8CharIterator(const std::string* str); UTF8CharIterator(const std::string* str);
~UTF8CharIterator() {} ~UTF8CharIterator();
// Return the starting array index of the current character within the // Return the starting array index of the current character within the
// string. // string.
...@@ -77,7 +78,7 @@ class UTF16CharIterator { ...@@ -77,7 +78,7 @@ class UTF16CharIterator {
// Requires |str| to live as long as the UTF16CharIterator does. // Requires |str| to live as long as the UTF16CharIterator does.
UTF16CharIterator(const string16* str); UTF16CharIterator(const string16* str);
UTF16CharIterator(const char16* str, size_t str_len); UTF16CharIterator(const char16* str, size_t str_len);
~UTF16CharIterator() {} ~UTF16CharIterator();
// Return the starting array index of the current character within the // Return the starting array index of the current character within the
// string. // string.
...@@ -123,6 +124,7 @@ class UTF16CharIterator { ...@@ -123,6 +124,7 @@ class UTF16CharIterator {
DISALLOW_COPY_AND_ASSIGN(UTF16CharIterator); DISALLOW_COPY_AND_ASSIGN(UTF16CharIterator);
}; };
} // namespace i18n
} // namespace base } // namespace base
#endif // BASE_I18N_CHAR_ITERATOR_H_ #endif // BASE_I18N_CHAR_ITERATOR_H_
// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -7,16 +7,19 @@ ...@@ -7,16 +7,19 @@
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace i18n {
TEST(CharIteratorsTest, TestUTF8) { TEST(CharIteratorsTest, TestUTF8) {
std::string empty(""); std::string empty("");
base::UTF8CharIterator empty_iter(&empty); UTF8CharIterator empty_iter(&empty);
ASSERT_TRUE(empty_iter.end()); ASSERT_TRUE(empty_iter.end());
ASSERT_EQ(0, empty_iter.array_pos()); ASSERT_EQ(0, empty_iter.array_pos());
ASSERT_EQ(0, empty_iter.char_pos()); ASSERT_EQ(0, empty_iter.char_pos());
ASSERT_FALSE(empty_iter.Advance()); ASSERT_FALSE(empty_iter.Advance());
std::string str("s\303\273r"); // [u with circumflex] std::string str("s\303\273r"); // [u with circumflex]
base::UTF8CharIterator iter(&str); UTF8CharIterator iter(&str);
ASSERT_FALSE(iter.end()); ASSERT_FALSE(iter.end());
ASSERT_EQ(0, iter.array_pos()); ASSERT_EQ(0, iter.array_pos());
ASSERT_EQ(0, iter.char_pos()); ASSERT_EQ(0, iter.char_pos());
...@@ -47,7 +50,7 @@ TEST(CharIteratorsTest, TestUTF8) { ...@@ -47,7 +50,7 @@ TEST(CharIteratorsTest, TestUTF8) {
TEST(CharIteratorsTest, TestUTF16) { TEST(CharIteratorsTest, TestUTF16) {
string16 empty = UTF8ToUTF16(""); string16 empty = UTF8ToUTF16("");
base::UTF16CharIterator empty_iter(&empty); UTF16CharIterator empty_iter(&empty);
ASSERT_TRUE(empty_iter.end()); ASSERT_TRUE(empty_iter.end());
ASSERT_EQ(0, empty_iter.array_pos()); ASSERT_EQ(0, empty_iter.array_pos());
ASSERT_EQ(0, empty_iter.char_pos()); ASSERT_EQ(0, empty_iter.char_pos());
...@@ -59,7 +62,7 @@ TEST(CharIteratorsTest, TestUTF16) { ...@@ -59,7 +62,7 @@ TEST(CharIteratorsTest, TestUTF16) {
// math double-struck A - 4 bytes in UTF8, 2 codewords in UTF16 // math double-struck A - 4 bytes in UTF8, 2 codewords in UTF16
// z // z
string16 str = UTF8ToUTF16("x\303\273\360\235\224\270z"); string16 str = UTF8ToUTF16("x\303\273\360\235\224\270z");
base::UTF16CharIterator iter(&str); UTF16CharIterator iter(&str);
ASSERT_FALSE(iter.end()); ASSERT_FALSE(iter.end());
ASSERT_EQ(0, iter.array_pos()); ASSERT_EQ(0, iter.array_pos());
ASSERT_EQ(0, iter.char_pos()); ASSERT_EQ(0, iter.char_pos());
...@@ -93,3 +96,6 @@ TEST(CharIteratorsTest, TestUTF16) { ...@@ -93,3 +96,6 @@ TEST(CharIteratorsTest, TestUTF16) {
ASSERT_FALSE(iter.Advance()); ASSERT_FALSE(iter.Advance());
} }
} // namespace i18n
} // namespace base
// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -27,7 +27,7 @@ namespace keys = extension_accessibility_api_constants; ...@@ -27,7 +27,7 @@ namespace keys = extension_accessibility_api_constants;
namespace { namespace {
static std::string SubstringUTF8(std::string str, int start, int len) { static std::string SubstringUTF8(std::string str, int start, int len) {
base::UTF8CharIterator iter(&str); base::i18n::UTF8CharIterator iter(&str);
for (int i = 0; i < start; i++) { for (int i = 0; i < start; i++) {
if (!iter.Advance()) if (!iter.Advance())
return std::string(); return std::string();
...@@ -300,7 +300,7 @@ std::string WizardAccessibilityHandler::GetTextBoxValue( ...@@ -300,7 +300,7 @@ std::string WizardAccessibilityHandler::GetTextBoxValue(
const AccessibilityTextBoxInfo* textbox_info) { const AccessibilityTextBoxInfo* textbox_info) {
std::string value = textbox_info->value(); std::string value = textbox_info->value();
if (textbox_info->password()) { if (textbox_info->password()) {
base::UTF8CharIterator iter(&value); base::i18n::UTF8CharIterator iter(&value);
std::string obscured; std::string obscured;
while (!iter.end()) { while (!iter.end()) {
obscured += "*"; obscured += "*";
...@@ -377,7 +377,7 @@ void WizardAccessibilityHandler::DescribeTextContentsChanged( ...@@ -377,7 +377,7 @@ void WizardAccessibilityHandler::DescribeTextContentsChanged(
// character's UTF8-encoded representation. // character's UTF8-encoded representation.
scoped_array<int32> old_chars(new int32[old_array_len]); scoped_array<int32> old_chars(new int32[old_array_len]);
scoped_array<int> old_indices(new int[old_array_len + 1]); scoped_array<int> old_indices(new int[old_array_len + 1]);
base::UTF8CharIterator old_iter(&old_value); base::i18n::UTF8CharIterator old_iter(&old_value);
while (!old_iter.end()) { while (!old_iter.end()) {
old_chars[old_iter.char_pos()] = old_iter.get(); old_chars[old_iter.char_pos()] = old_iter.get();
old_indices[old_iter.char_pos()] = old_iter.array_pos(); old_indices[old_iter.char_pos()] = old_iter.array_pos();
...@@ -388,7 +388,7 @@ void WizardAccessibilityHandler::DescribeTextContentsChanged( ...@@ -388,7 +388,7 @@ void WizardAccessibilityHandler::DescribeTextContentsChanged(
scoped_array<int32> new_chars(new int32[new_array_len]); scoped_array<int32> new_chars(new int32[new_array_len]);
scoped_array<int> new_indices(new int[new_array_len + 1]); scoped_array<int> new_indices(new int[new_array_len + 1]);
base::UTF8CharIterator new_iter(&new_value); base::i18n::UTF8CharIterator new_iter(&new_value);
while (!new_iter.end()) { while (!new_iter.end()) {
new_chars[new_iter.char_pos()] = new_iter.get(); new_chars[new_iter.char_pos()] = new_iter.get();
new_indices[new_iter.char_pos()] = new_iter.array_pos(); new_indices[new_iter.char_pos()] = new_iter.array_pos();
......
// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -94,7 +94,7 @@ void AppendCharEvent(const WebInputEvent& event, ...@@ -94,7 +94,7 @@ void AppendCharEvent(const WebInputEvent& event,
utf16_char_count++; utf16_char_count++;
// Make a separate PP_InputEvent for each Unicode character in the input. // Make a separate PP_InputEvent for each Unicode character in the input.
base::UTF16CharIterator iter(key_event.text, utf16_char_count); base::i18n::UTF16CharIterator iter(key_event.text, utf16_char_count);
while (!iter.end()) { while (!iter.end()) {
PP_InputEvent result = GetPPEventWithCommonFieldsAndType(event); PP_InputEvent result = GetPPEventWithCommonFieldsAndType(event);
result.u.character.modifier = key_event.modifiers; result.u.character.modifier = key_event.modifiers;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment