blob: 20b9073b9a262ae3d31fd24a0f49a97dd76b53f6 [file] [log] [blame]
Adam Lesinski549e4372017-06-27 18:39:07 -07001/*
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 Lesinski66ea8402017-06-28 11:44:11 -070017#include "text/Utf8Iterator.h"
Adam Lesinski549e4372017-06-27 18:39:07 -070018
19#include "android-base/logging.h"
20#include "utils/Unicode.h"
21
22using ::android::StringPiece;
23
24namespace aapt {
Adam Lesinski66ea8402017-06-28 11:44:11 -070025namespace text {
Adam Lesinski549e4372017-06-27 18:39:07 -070026
27Utf8Iterator::Utf8Iterator(const StringPiece& str)
Adam Lesinskie967d3f2017-07-24 18:19:36 -070028 : str_(str), current_pos_(0), next_pos_(0), current_codepoint_(0) {
Adam Lesinski549e4372017-06-27 18:39:07 -070029 DoNext();
30}
31
32void Utf8Iterator::DoNext() {
Adam Lesinskie967d3f2017-07-24 18:19:36 -070033 current_pos_ = next_pos_;
34 int32_t result = utf32_from_utf8_at(str_.data(), str_.size(), current_pos_, &next_pos_);
Adam Lesinski549e4372017-06-27 18:39:07 -070035 if (result == -1) {
36 current_codepoint_ = 0u;
37 } else {
38 current_codepoint_ = static_cast<char32_t>(result);
Adam Lesinski549e4372017-06-27 18:39:07 -070039 }
40}
41
42bool Utf8Iterator::HasNext() const {
43 return current_codepoint_ != 0;
44}
45
Adam Lesinskie967d3f2017-07-24 18:19:36 -070046size_t Utf8Iterator::Position() const {
47 return current_pos_;
48}
49
Adam Lesinski549e4372017-06-27 18:39:07 -070050void Utf8Iterator::Skip(int amount) {
51 while (amount > 0 && HasNext()) {
52 Next();
53 --amount;
54 }
55}
56
57char32_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 Lesinski66ea8402017-06-28 11:44:11 -070064} // namespace text
Adam Lesinski549e4372017-06-27 18:39:07 -070065} // namespace aapt