Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / search / tokenized_string_char_iterator.cc
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.
4
5 #include "chrome/browser/ui/app_list/search/tokenized_string_char_iterator.h"
6
7 #include "base/i18n/char_iterator.h"
8 #include "base/logging.h"
9 #include "third_party/icu/source/common/unicode/utf16.h"
10
11 namespace app_list {
12
13 TokenizedStringCharIterator::State::State() : token_index(0u), char_index(0) {}
14
15 TokenizedStringCharIterator::State::State(size_t token_index, int char_index)
16     : token_index(token_index),
17       char_index(char_index) {
18 }
19
20 TokenizedStringCharIterator::TokenizedStringCharIterator(
21     const TokenizedString& tokenized)
22     : tokens_(tokenized.tokens()),
23       mappings_(tokenized.mappings()),
24       current_token_(0) {
25   CreateTokenCharIterator();
26 }
27
28 TokenizedStringCharIterator::~TokenizedStringCharIterator() {}
29
30 bool TokenizedStringCharIterator::NextChar() {
31   if (current_token_iter_) {
32     current_token_iter_->Advance();
33     if (!current_token_iter_->end())
34       return true;
35   }
36
37   return NextToken();
38 }
39
40 bool TokenizedStringCharIterator::NextToken() {
41   if (current_token_ < tokens_.size()) {
42     ++current_token_;
43     CreateTokenCharIterator();
44   }
45
46   return !!current_token_iter_;
47 }
48
49 int32 TokenizedStringCharIterator::Get() const {
50   return current_token_iter_ ? current_token_iter_->get() : 0;
51 }
52
53 int32 TokenizedStringCharIterator::GetArrayPos() const {
54   DCHECK(current_token_iter_);
55   return mappings_[current_token_].start() +
56       current_token_iter_->array_pos();
57 }
58
59 size_t TokenizedStringCharIterator::GetCharSize() const {
60   return current_token_iter_ ? U16_LENGTH(Get()) : 0;
61 }
62
63 bool TokenizedStringCharIterator::IsFirstCharOfToken() const {
64   return current_token_iter_ && current_token_iter_->char_pos() == 0;
65 }
66
67 TokenizedStringCharIterator::State
68 TokenizedStringCharIterator::GetState() const {
69   return State(current_token_,
70                current_token_iter_ ? current_token_iter_->char_pos() : 0);
71 }
72
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();
79   }
80 }
81
82 void TokenizedStringCharIterator::CreateTokenCharIterator() {
83   if (current_token_ == tokens_.size()) {
84     current_token_iter_.reset();
85     return;
86   }
87
88   current_token_iter_.reset(
89       new base::i18n::UTF16CharIterator(&tokens_[current_token_]));
90 }
91
92 }  // namespace app_list