Home | History | Annotate | Download | only in i18n
      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 
     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() {
     25 }
     26 
     27 bool UTF8CharIterator::Advance() {
     28   if (array_pos_ >= len_)
     29     return false;
     30 
     31   array_pos_ = next_pos_;
     32   char_pos_++;
     33   if (next_pos_ < len_)
     34     U8_NEXT(str_, next_pos_, len_, char_);
     35 
     36   return true;
     37 }
     38 
     39 UTF16CharIterator::UTF16CharIterator(const string16* str)
     40     : str_(reinterpret_cast<const char16*>(str->data())),
     41       len_(str->size()),
     42       array_pos_(0),
     43       next_pos_(0),
     44       char_pos_(0),
     45       char_(0) {
     46   if (len_)
     47     ReadChar();
     48 }
     49 
     50 UTF16CharIterator::UTF16CharIterator(const char16* str, size_t str_len)
     51     : str_(str),
     52       len_(str_len),
     53       array_pos_(0),
     54       next_pos_(0),
     55       char_pos_(0),
     56       char_(0) {
     57   if (len_)
     58     ReadChar();
     59 }
     60 
     61 UTF16CharIterator::~UTF16CharIterator() {
     62 }
     63 
     64 bool UTF16CharIterator::Advance() {
     65   if (array_pos_ >= len_)
     66     return false;
     67 
     68   array_pos_ = next_pos_;
     69   char_pos_++;
     70   if (next_pos_ < len_)
     71     ReadChar();
     72 
     73   return true;
     74 }
     75 
     76 void UTF16CharIterator::ReadChar() {
     77   // This is actually a huge macro, so is worth having in a separate function.
     78   U16_NEXT(str_, next_pos_, len_, char_);
     79 }
     80 
     81 }  // namespace i18n
     82 }  // namespace base
     83