• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "text/Utf8Iterator.h"
18 
19 #include "android-base/logging.h"
20 #include "utils/Unicode.h"
21 
22 using ::android::StringPiece;
23 
24 namespace aapt {
25 namespace text {
26 
Utf8Iterator(const StringPiece & str)27 Utf8Iterator::Utf8Iterator(const StringPiece& str)
28     : str_(str), current_pos_(0), next_pos_(0), current_codepoint_(0) {
29   DoNext();
30 }
31 
DoNext()32 void Utf8Iterator::DoNext() {
33   current_pos_ = next_pos_;
34   int32_t result = utf32_from_utf8_at(str_.data(), str_.size(), current_pos_, &next_pos_);
35   if (result == -1) {
36     current_codepoint_ = 0u;
37   } else {
38     current_codepoint_ = static_cast<char32_t>(result);
39   }
40 }
41 
HasNext() const42 bool Utf8Iterator::HasNext() const {
43   return current_codepoint_ != 0;
44 }
45 
Position() const46 size_t Utf8Iterator::Position() const {
47   return current_pos_;
48 }
49 
Skip(int amount)50 void Utf8Iterator::Skip(int amount) {
51   while (amount > 0 && HasNext()) {
52     Next();
53     --amount;
54   }
55 }
56 
Next()57 char32_t Utf8Iterator::Next() {
58   CHECK(HasNext()) << "Next() called after iterator exhausted";
59   char32_t result = current_codepoint_;
60   DoNext();
61   return result;
62 }
63 
64 }  // namespace text
65 }  // namespace aapt
66