1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #include "base/i18n/char_iterator.h"
6*635a8641SAndroid Build Coastguard Worker
7*635a8641SAndroid Build Coastguard Worker #include "third_party/icu/source/common/unicode/utf8.h"
8*635a8641SAndroid Build Coastguard Worker #include "third_party/icu/source/common/unicode/utf16.h"
9*635a8641SAndroid Build Coastguard Worker
10*635a8641SAndroid Build Coastguard Worker namespace base {
11*635a8641SAndroid Build Coastguard Worker namespace i18n {
12*635a8641SAndroid Build Coastguard Worker
UTF8CharIterator(const std::string * str)13*635a8641SAndroid Build Coastguard Worker UTF8CharIterator::UTF8CharIterator(const std::string* str)
14*635a8641SAndroid Build Coastguard Worker : str_(reinterpret_cast<const uint8_t*>(str->data())),
15*635a8641SAndroid Build Coastguard Worker len_(str->size()),
16*635a8641SAndroid Build Coastguard Worker array_pos_(0),
17*635a8641SAndroid Build Coastguard Worker next_pos_(0),
18*635a8641SAndroid Build Coastguard Worker char_pos_(0),
19*635a8641SAndroid Build Coastguard Worker char_(0) {
20*635a8641SAndroid Build Coastguard Worker if (len_)
21*635a8641SAndroid Build Coastguard Worker U8_NEXT(str_, next_pos_, len_, char_);
22*635a8641SAndroid Build Coastguard Worker }
23*635a8641SAndroid Build Coastguard Worker
24*635a8641SAndroid Build Coastguard Worker UTF8CharIterator::~UTF8CharIterator() = default;
25*635a8641SAndroid Build Coastguard Worker
Advance()26*635a8641SAndroid Build Coastguard Worker bool UTF8CharIterator::Advance() {
27*635a8641SAndroid Build Coastguard Worker if (array_pos_ >= len_)
28*635a8641SAndroid Build Coastguard Worker return false;
29*635a8641SAndroid Build Coastguard Worker
30*635a8641SAndroid Build Coastguard Worker array_pos_ = next_pos_;
31*635a8641SAndroid Build Coastguard Worker char_pos_++;
32*635a8641SAndroid Build Coastguard Worker if (next_pos_ < len_)
33*635a8641SAndroid Build Coastguard Worker U8_NEXT(str_, next_pos_, len_, char_);
34*635a8641SAndroid Build Coastguard Worker
35*635a8641SAndroid Build Coastguard Worker return true;
36*635a8641SAndroid Build Coastguard Worker }
37*635a8641SAndroid Build Coastguard Worker
UTF16CharIterator(const string16 * str)38*635a8641SAndroid Build Coastguard Worker UTF16CharIterator::UTF16CharIterator(const string16* str)
39*635a8641SAndroid Build Coastguard Worker : str_(reinterpret_cast<const char16*>(str->data())),
40*635a8641SAndroid Build Coastguard Worker len_(str->size()),
41*635a8641SAndroid Build Coastguard Worker array_pos_(0),
42*635a8641SAndroid Build Coastguard Worker next_pos_(0),
43*635a8641SAndroid Build Coastguard Worker char_pos_(0),
44*635a8641SAndroid Build Coastguard Worker char_(0) {
45*635a8641SAndroid Build Coastguard Worker if (len_)
46*635a8641SAndroid Build Coastguard Worker ReadChar();
47*635a8641SAndroid Build Coastguard Worker }
48*635a8641SAndroid Build Coastguard Worker
UTF16CharIterator(const char16 * str,size_t str_len)49*635a8641SAndroid Build Coastguard Worker UTF16CharIterator::UTF16CharIterator(const char16* str, size_t str_len)
50*635a8641SAndroid Build Coastguard Worker : str_(str),
51*635a8641SAndroid Build Coastguard Worker len_(str_len),
52*635a8641SAndroid Build Coastguard Worker array_pos_(0),
53*635a8641SAndroid Build Coastguard Worker next_pos_(0),
54*635a8641SAndroid Build Coastguard Worker char_pos_(0),
55*635a8641SAndroid Build Coastguard Worker char_(0) {
56*635a8641SAndroid Build Coastguard Worker if (len_)
57*635a8641SAndroid Build Coastguard Worker ReadChar();
58*635a8641SAndroid Build Coastguard Worker }
59*635a8641SAndroid Build Coastguard Worker
60*635a8641SAndroid Build Coastguard Worker UTF16CharIterator::~UTF16CharIterator() = default;
61*635a8641SAndroid Build Coastguard Worker
Advance()62*635a8641SAndroid Build Coastguard Worker bool UTF16CharIterator::Advance() {
63*635a8641SAndroid Build Coastguard Worker if (array_pos_ >= len_)
64*635a8641SAndroid Build Coastguard Worker return false;
65*635a8641SAndroid Build Coastguard Worker
66*635a8641SAndroid Build Coastguard Worker array_pos_ = next_pos_;
67*635a8641SAndroid Build Coastguard Worker char_pos_++;
68*635a8641SAndroid Build Coastguard Worker if (next_pos_ < len_)
69*635a8641SAndroid Build Coastguard Worker ReadChar();
70*635a8641SAndroid Build Coastguard Worker
71*635a8641SAndroid Build Coastguard Worker return true;
72*635a8641SAndroid Build Coastguard Worker }
73*635a8641SAndroid Build Coastguard Worker
ReadChar()74*635a8641SAndroid Build Coastguard Worker void UTF16CharIterator::ReadChar() {
75*635a8641SAndroid Build Coastguard Worker // This is actually a huge macro, so is worth having in a separate function.
76*635a8641SAndroid Build Coastguard Worker U16_NEXT(str_, next_pos_, len_, char_);
77*635a8641SAndroid Build Coastguard Worker }
78*635a8641SAndroid Build Coastguard Worker
79*635a8641SAndroid Build Coastguard Worker } // namespace i18n
80*635a8641SAndroid Build Coastguard Worker } // namespace base
81