| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2010-2014 Carbo Kuo <[email protected]> |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include <cstring> |
| 22 | #include <iterator> |
| 23 | #include <string_view> |
| 24 | |
| 25 | #include "Common.hpp" |
| 26 | |
| 27 | namespace opencc { |
| 28 | /** |
| 29 | * Segmented text |
| 30 | * @ingroup opencc_cpp_api |
| 31 | */ |
| 32 | class OPENCC_EXPORT Segments { |
| 33 | public: |
| 34 | Segments() {} |
| 35 | |
| 36 | Segments(std::initializer_list<const char*> initList) { |
| 37 | for (const char* item : initList) { |
| 38 | AddSegment(item); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | Segments(std::initializer_list<std::string> initList) { |
| 43 | for (const std::string& item : initList) { |
| 44 | AddSegment(item); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void AddSegment(const char* unmanagedString) { |
| 49 | indexes.push_back(std::make_pair(unmanaged.size(), false)); |
| 50 | unmanaged.push_back(unmanagedString); |
| 51 | } |
| 52 | |
| 53 | void AddSegment(const std::string& str) { |
| 54 | indexes.push_back(std::make_pair(managed.size(), true)); |
| 55 | managed.push_back(str); |
| 56 | } |
| 57 | |
| 58 | void AddSegment(std::string_view sv) { |
| 59 | indexes.push_back(std::make_pair(managed.size(), true)); |
| 60 | managed.emplace_back(sv); |
| 61 | } |
| 62 | |
| 63 | class iterator { |
| 64 | public: |
| 65 | using iterator_category = std::input_iterator_tag; |
| 66 | using value_type = const char*; |
| 67 | |
| 68 | iterator(const Segments* const _segments, size_t _cursor) |
| 69 | : segments(_segments), cursor(_cursor) {} |
| 70 | |
| 71 | iterator& operator++() { |
| 72 | cursor++; |
| 73 | return *this; |
| 74 | } |
| 75 | |
| 76 | bool operator==(const iterator& that) const { |
| 77 | return cursor == that.cursor && segments == that.segments; |
| 78 | } |
| 79 | |
| 80 | bool operator!=(const iterator& that) const { |
| 81 | return !this->operator==(that); |
| 82 | } |
| 83 | |
| 84 | const char* operator*() const { return segments->At(cursor); } |
| 85 | |
| 86 | private: |
| 87 | const Segments* const segments; |
| 88 | size_t cursor; |
| 89 | }; |
| 90 | |
| 91 | const char* At(size_t cursor) const { |
| 92 | const auto& index = indexes[cursor]; |
| 93 | if (index.second) { |
| 94 | return managed[index.first].c_str(); |
| 95 | } else { |
| 96 | return unmanaged[index.first]; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | size_t Length() const { return indexes.size(); } |
| 101 | |
| 102 | iterator begin() const { return iterator(this, 0); } |
| 103 | |
| 104 | iterator end() const { return iterator(this, indexes.size()); } |
| 105 | |
| 106 | std::string ToString() const { |
| 107 | size_t totalLength = 0; |
| 108 | for (const char* segment : *this) { |
| 109 | totalLength += std::strlen(segment); |
| 110 | } |
| 111 | |
| 112 | std::string buffer; |
| 113 | buffer.reserve(totalLength); |
| 114 | for (const char* segment : *this) { |
| 115 | buffer.append(segment); |
| 116 | } |
| 117 | return buffer; |
| 118 | } |
| 119 | |
| 120 | std::vector<std::string> ToVector() const { |
| 121 | std::vector<std::string> result; |
| 122 | result.reserve(indexes.size()); |
| 123 | for (const char* segment : *this) { |
| 124 | result.emplace_back(segment); |
| 125 | } |
| 126 | return result; |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | Segments(const Segments&) {} |
| 131 | |
| 132 | std::vector<const char*> unmanaged; |
| 133 | std::vector<std::string> managed; |
| 134 | // index, managed |
| 135 | std::vector<std::pair<size_t, bool>> indexes; |
| 136 | }; |
| 137 | } // namespace opencc |