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