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