summaryrefslogtreecommitdiffstats
path: root/base/i18n/char_iterator.cc
diff options
context:
space:
mode:
authordmazzoni@chromium.org <dmazzoni@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-23 14:30:27 +0000
committerdmazzoni@chromium.org <dmazzoni@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-23 14:30:27 +0000
commit0ca5c104bc719dc5e7dcdc19cca1576c27391e65 (patch)
treefbb7dbff560b787cf3007f72236b51b3a41b5a5e /base/i18n/char_iterator.cc
parent9ea6435cf0c61350b6676821c12e7c95ee1f20ca (diff)
downloadchromium_src-0ca5c104bc719dc5e7dcdc19cca1576c27391e65.zip
chromium_src-0ca5c104bc719dc5e7dcdc19cca1576c27391e65.tar.gz
chromium_src-0ca5c104bc719dc5e7dcdc19cca1576c27391e65.tar.bz2
Add functions to get the length or compute a substring of UTF8 and UTF16
encoded strings. BUG=none TEST=Added new unit tests for each. Review URL: http://codereview.chromium.org/3133028 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57051 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/i18n/char_iterator.cc')
-rw-r--r--base/i18n/char_iterator.cc58
1 files changed, 58 insertions, 0 deletions
diff --git a/base/i18n/char_iterator.cc b/base/i18n/char_iterator.cc
new file mode 100644
index 0000000..c323c5d
--- /dev/null
+++ b/base/i18n/char_iterator.cc
@@ -0,0 +1,58 @@
+// Copyright (c) 2010 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/char_iterator.h"
+
+#include "unicode/utf8.h"
+#include "unicode/utf16.h"
+
+namespace base {
+
+UTF8CharIterator::UTF8CharIterator(const std::string* str)
+ : str_(reinterpret_cast<const uint8_t*>(str->data())),
+ len_(str->size()),
+ array_pos_(0),
+ next_pos_(0),
+ char_pos_(0),
+ char_(0) {
+ if (len_)
+ U8_NEXT(str_, next_pos_, len_, char_);
+}
+
+bool UTF8CharIterator::Advance() {
+ if (array_pos_ >= len_)
+ return false;
+
+ array_pos_ = next_pos_;
+ char_pos_++;
+ if (next_pos_ < len_)
+ U8_NEXT(str_, next_pos_, len_, char_);
+
+ return true;
+}
+
+UTF16CharIterator::UTF16CharIterator(const string16* str)
+ : str_(reinterpret_cast<const char16*>(str->data())),
+ len_(str->size()),
+ array_pos_(0),
+ next_pos_(0),
+ char_pos_(0),
+ char_(0) {
+ if (len_)
+ U16_NEXT(str_, next_pos_, len_, char_);
+}
+
+bool UTF16CharIterator::Advance() {
+ if (array_pos_ >= len_)
+ return false;
+
+ array_pos_ = next_pos_;
+ char_pos_++;
+ if (next_pos_ < len_)
+ U16_NEXT(str_, next_pos_, len_, char_);
+
+ return true;
+}
+
+} // namespace base