Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 17 | #include "text/Utf8Iterator.h" |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 18 | |
| 19 | #include "android-base/logging.h" |
| 20 | #include "utils/Unicode.h" |
| 21 | |
| 22 | using ::android::StringPiece; |
| 23 | |
| 24 | namespace aapt { |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 25 | namespace text { |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 26 | |
| 27 | Utf8Iterator::Utf8Iterator(const StringPiece& str) |
Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 28 | : str_(str), current_pos_(0), next_pos_(0), current_codepoint_(0) { |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 29 | DoNext(); |
| 30 | } |
| 31 | |
| 32 | void Utf8Iterator::DoNext() { |
Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 33 | current_pos_ = next_pos_; |
| 34 | int32_t result = utf32_from_utf8_at(str_.data(), str_.size(), current_pos_, &next_pos_); |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 35 | if (result == -1) { |
| 36 | current_codepoint_ = 0u; |
| 37 | } else { |
| 38 | current_codepoint_ = static_cast<char32_t>(result); |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | bool Utf8Iterator::HasNext() const { |
| 43 | return current_codepoint_ != 0; |
| 44 | } |
| 45 | |
Adam Lesinski | e967d3f | 2017-07-24 18:19:36 -0700 | [diff] [blame] | 46 | size_t Utf8Iterator::Position() const { |
| 47 | return current_pos_; |
| 48 | } |
| 49 | |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 50 | void Utf8Iterator::Skip(int amount) { |
| 51 | while (amount > 0 && HasNext()) { |
| 52 | Next(); |
| 53 | --amount; |
| 54 | } |
| 55 | } |
| 56 | |
| 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 | |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 64 | } // namespace text |
Adam Lesinski | 549e437 | 2017-06-27 18:39:07 -0700 | [diff] [blame] | 65 | } // namespace aapt |