Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / app_list / search / tokenized_string_char_iterator.cc
blobbdd75a3e30c3dd9abe98e3bc85ef44e8718b70bd
1 // Copyright 2013 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.
5 #include "ui/app_list/search/tokenized_string_char_iterator.h"
7 #include "base/i18n/char_iterator.h"
8 #include "base/logging.h"
9 #include "third_party/icu/source/common/unicode/utf16.h"
11 namespace app_list {
13 TokenizedStringCharIterator::State::State() : token_index(0u), char_index(0) {}
15 TokenizedStringCharIterator::State::State(size_t token_index, int char_index)
16 : token_index(token_index),
17 char_index(char_index) {
20 TokenizedStringCharIterator::TokenizedStringCharIterator(
21 const TokenizedString& tokenized)
22 : tokens_(tokenized.tokens()),
23 mappings_(tokenized.mappings()),
24 current_token_(0) {
25 CreateTokenCharIterator();
28 TokenizedStringCharIterator::~TokenizedStringCharIterator() {}
30 bool TokenizedStringCharIterator::NextChar() {
31 if (current_token_iter_) {
32 current_token_iter_->Advance();
33 if (!current_token_iter_->end())
34 return true;
37 return NextToken();
40 bool TokenizedStringCharIterator::NextToken() {
41 if (current_token_ < tokens_.size()) {
42 ++current_token_;
43 CreateTokenCharIterator();
46 return !!current_token_iter_;
49 int32 TokenizedStringCharIterator::Get() const {
50 return current_token_iter_ ? current_token_iter_->get() : 0;
53 int32 TokenizedStringCharIterator::GetArrayPos() const {
54 DCHECK(current_token_iter_);
55 return mappings_[current_token_].start() +
56 current_token_iter_->array_pos();
59 size_t TokenizedStringCharIterator::GetCharSize() const {
60 return current_token_iter_ ? U16_LENGTH(Get()) : 0;
63 bool TokenizedStringCharIterator::IsFirstCharOfToken() const {
64 return current_token_iter_ && current_token_iter_->char_pos() == 0;
67 TokenizedStringCharIterator::State
68 TokenizedStringCharIterator::GetState() const {
69 return State(current_token_,
70 current_token_iter_ ? current_token_iter_->char_pos() : 0);
73 void TokenizedStringCharIterator::SetState(const State& state) {
74 current_token_ = state.token_index;
75 CreateTokenCharIterator();
76 if (current_token_iter_) {
77 while (current_token_iter_->char_pos() < state.char_index)
78 current_token_iter_->Advance();
82 void TokenizedStringCharIterator::CreateTokenCharIterator() {
83 if (current_token_ == tokens_.size()) {
84 current_token_iter_.reset();
85 return;
88 current_token_iter_.reset(
89 new base::i18n::UTF16CharIterator(&tokens_[current_token_]));
92 } // namespace app_list