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