| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2010-2026 Carbo Kuo and contributors |
| 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 | #include <list> |
| 20 | #include <string_view> |
| 21 | |
| 22 | #include "ConversionChain.hpp" |
| 23 | #include "Segments.hpp" |
| 24 | |
| 25 | using namespace opencc; |
| 26 | |
| 27 | ConversionChain::ConversionChain(const std::list<ConversionPtr> _conversions) |
| 28 | : conversions(_conversions) {} |
| 29 | |
| 30 | SegmentsPtr ConversionChain::Convert(const SegmentsPtr& input) const { |
| 31 | SegmentsPtr output = input; |
| 32 | for (auto conversion : conversions) { |
| 33 | output = conversion->Convert(output); |
| 34 | } |
| 35 | return output; |
| 36 | } |
| 37 | |
| 38 | void ConversionChain::AppendConvertedSegment(const char* segment, |
| 39 | std::string* output) const { |
| 40 | if (conversions.empty()) { |
| 41 | output->append(segment); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | auto conversion = conversions.begin(); |
| 46 | auto lastConversion = conversions.end(); |
| 47 | --lastConversion; |
| 48 | if (conversion == lastConversion) { |
| 49 | (*conversion)->AppendConverted(segment, output); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | std::string converted; |
| 54 | (*conversion)->AppendConverted(segment, &converted); |
| 55 | ++conversion; |
| 56 | for (; conversion != lastConversion; ++conversion) { |
| 57 | std::string next; |
| 58 | (*conversion)->AppendConverted(converted, &next); |
| 59 | converted.swap(next); |
| 60 | } |
| 61 | (*lastConversion)->AppendConverted(converted, output); |
| 62 | } |
| 63 | |
| 64 | void ConversionChain::AppendConvertedSegment(std::string_view segment, |
| 65 | std::string* output) const { |
| 66 | if (segment.empty()) { |
| 67 | return; |
| 68 | } |
| 69 | if (conversions.empty()) { |
| 70 | output->append(segment.data(), segment.size()); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | auto conversion = conversions.begin(); |
| 75 | auto lastConversion = conversions.end(); |
| 76 | --lastConversion; |
| 77 | if (conversion == lastConversion) { |
| 78 | (*conversion)->AppendConverted(segment, output); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | std::string converted; |
| 83 | (*conversion)->AppendConverted(segment, &converted); |
| 84 | ++conversion; |
| 85 | for (; conversion != lastConversion; ++conversion) { |
| 86 | std::string next; |
| 87 | (*conversion)->AppendConverted(std::string_view(converted), &next); |
| 88 | converted.swap(next); |
| 89 | } |
| 90 | (*lastConversion)->AppendConverted(std::string_view(converted), output); |
| 91 | } |
| 92 | |
| 93 | std::vector<SegmentsPtr> |
| 94 | ConversionChain::ConvertWithTrace(const SegmentsPtr& input) const { |
| 95 | std::vector<SegmentsPtr> trace; |
| 96 | trace.reserve(conversions.size()); |
| 97 | SegmentsPtr output = input; |
| 98 | for (auto conversion : conversions) { |
| 99 | output = conversion->Convert(output); |
| 100 | trace.push_back(output); |
| 101 | } |
| 102 | return trace; |
| 103 | } |