Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / search / tokenized_string_char_iterator.h
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 #ifndef CHROME_BROWSER_UI_APP_LIST_SEARCH_TOKENIZED_STRING_CHAR_ITERATOR_H_
6 #define CHROME_BROWSER_UI_APP_LIST_SEARCH_TOKENIZED_STRING_CHAR_ITERATOR_H_
7
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/browser/ui/app_list/search/tokenized_string.h"
11
12 namespace base {
13 namespace i18n {
14 class UTF16CharIterator;
15 }
16 }
17
18 namespace app_list {
19
20 // An UTF16 char iterator for a TokenizedString.
21 class TokenizedStringCharIterator {
22  public:
23   struct State {
24     State();
25     State(size_t token_index, int char_index);
26
27     size_t token_index;
28     int32 char_index;
29   };
30
31   // Requires |tokenized| out-lives this iterator.
32   explicit TokenizedStringCharIterator(const TokenizedString& tokenized);
33   ~TokenizedStringCharIterator();
34
35   // Advances to the next char. Returns false if there is no next char.
36   bool NextChar();
37
38   // Advances to the first char of the next token. Returns false if there is
39   // no next token.
40   bool NextToken();
41
42   // Returns the current char if there is one. Otherwise, returns 0.
43   int32 Get() const;
44
45   // Returns the array index in original text of the tokenized string that is
46   // passed in constructor.
47   int32 GetArrayPos() const;
48
49   // Returns the number of UTF16 code units for the current char.
50   size_t GetCharSize() const;
51
52   // Returns true if the current char is the first char of the current token.
53   bool IsFirstCharOfToken() const;
54
55   // Helpers to get and restore the iterator's state.
56   State GetState() const;
57   void SetState(const State& state);
58
59   // Returns true if the iterator is at the end.
60   bool end() const { return !current_token_iter_; }
61
62  private:
63   void CreateTokenCharIterator();
64
65   const TokenizedString::Tokens& tokens_;
66   const TokenizedString::Mappings& mappings_;
67
68   size_t current_token_;
69   scoped_ptr<base::i18n::UTF16CharIterator> current_token_iter_;
70
71   DISALLOW_COPY_AND_ASSIGN(TokenizedStringCharIterator);
72 };
73
74 }  // namespace app_list
75
76 #endif  // CHROME_BROWSER_UI_APP_LIST_SEARCH_TOKENIZED_STRING_CHAR_ITERATOR_H_