• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ICING_UTIL_CHARACTER_ITERATOR_H_
16 #define ICING_UTIL_CHARACTER_ITERATOR_H_
17 
18 #include "icing/legacy/core/icing-string-util.h"
19 #include "icing/util/i18n-utils.h"
20 
21 namespace icing {
22 namespace lib {
23 
24 class CharacterIterator {
25  public:
CharacterIterator(std::string_view text)26   explicit CharacterIterator(std::string_view text)
27       : CharacterIterator(text, 0, 0, 0) {}
28 
CharacterIterator(std::string_view text,int utf8_index,int utf16_index,int utf32_index)29   CharacterIterator(std::string_view text, int utf8_index, int utf16_index,
30                     int utf32_index)
31       : text_(text),
32         utf8_index_(utf8_index),
33         utf16_index_(utf16_index),
34         utf32_index_(utf32_index) {}
35 
36   // Moves current position to desired_utf8_index.
37   // REQUIRES: 0 <= desired_utf8_index <= text_.length()
38   bool MoveToUtf8(int desired_utf8_index);
39 
40   // Advances from current position to the character that includes the specified
41   // UTF-8 index.
42   // REQUIRES: desired_utf8_index <= text_.length()
43   // desired_utf8_index is allowed to point one index past the end, but no
44   // further.
45   bool AdvanceToUtf8(int desired_utf8_index);
46 
47   // Rewinds from current position to the character that includes the specified
48   // UTF-8 index.
49   // REQUIRES: 0 <= desired_utf8_index
50   bool RewindToUtf8(int desired_utf8_index);
51 
52   // Moves current position to desired_utf16_index.
53   // REQUIRES: 0 <= desired_utf16_index <= text_.utf16_length()
54   bool MoveToUtf16(int desired_utf16_index);
55 
56   // Advances current position to desired_utf16_index.
57   // REQUIRES: desired_utf16_index <= text_.utf16_length()
58   // desired_utf16_index is allowed to point one index past the end, but no
59   // further.
60   bool AdvanceToUtf16(int desired_utf16_index);
61 
62   // Rewinds current position to desired_utf16_index.
63   // REQUIRES: 0 <= desired_utf16_index
64   bool RewindToUtf16(int desired_utf16_index);
65 
66   // Moves current position to desired_utf32_index.
67   // REQUIRES: 0 <= desired_utf32_index <= text_.utf32_length()
68   bool MoveToUtf32(int desired_utf32_index);
69 
70   // Advances current position to desired_utf32_index.
71   // REQUIRES: desired_utf32_index <= text_.utf32_length()
72   // desired_utf32_index is allowed to point one index past the end, but no
73   // further.
74   bool AdvanceToUtf32(int desired_utf32_index);
75 
76   // Rewinds current position to desired_utf32_index.
77   // REQUIRES: 0 <= desired_utf32_index
78   bool RewindToUtf32(int desired_utf32_index);
79 
utf8_index()80   int utf8_index() const { return utf8_index_; }
utf16_index()81   int utf16_index() const { return utf16_index_; }
utf32_index()82   int utf32_index() const { return utf32_index_; }
83 
84   bool operator==(const CharacterIterator& rhs) const {
85     return text_ == rhs.text_ && utf8_index_ == rhs.utf8_index_ &&
86            utf16_index_ == rhs.utf16_index_ && utf32_index_ == rhs.utf32_index_;
87   }
88 
DebugString()89   std::string DebugString() const {
90     return IcingStringUtil::StringPrintf("(u8:%d,u16:%d,u32:%d)", utf8_index_,
91                                          utf16_index_, utf32_index_);
92   }
93 
94  private:
95   std::string_view text_;
96   int utf8_index_;
97   int utf16_index_;
98   int utf32_index_;
99 };
100 
101 }  // namespace lib
102 }  // namespace icing
103 
104 #endif  // ICING_UTIL_CHARACTER_ITERATOR_H_
105