| 27 | : dict(_dict), prefixMatch(new PrefixMatch(_dict)) {} |
| 28 | |
| 29 | void Conversion::AppendConverted(std::string_view phrase, |
| 30 | std::string* output) const { |
| 31 | if (phrase.empty()) { |
| 32 | return; |
| 33 | } |
| 34 | const size_t phraseLength = phrase.size(); |
| 35 | output->reserve(output->size() + phraseLength + phraseLength / 5); |
| 36 | |
| 37 | const char* phraseData = phrase.data(); |
| 38 | const char* phraseEnd = phraseData + phraseLength; |
| 39 | for (const char* pstr = phraseData; pstr < phraseEnd;) { |
| 40 | size_t remainingLength = phraseEnd - pstr; |
| 41 | const PrefixMatchView matched = |
| 42 | prefixMatch->MatchPrefixView(pstr, remainingLength); |
| 43 | size_t matchedLength; |
| 44 | if (!matched.matched) { |
| 45 | matchedLength = |
| 46 | UTF8Util::NextIdeographicDescriptionSequenceLength(pstr, |
| 47 | remainingLength); |
| 48 | if (matchedLength == 0) { |
| 49 | matchedLength = UTF8Util::NextCharLength(pstr); |
| 50 | } |
| 51 | if (matchedLength > remainingLength) { |
| 52 | matchedLength = remainingLength; |
| 53 | } |
| 54 | output->append(pstr, matchedLength); |
| 55 | } else { |
| 56 | matchedLength = matched.keyLength; |
| 57 | if (matchedLength > remainingLength) { |
| 58 | matchedLength = remainingLength; |
| 59 | } |
| 60 | output->append(matched.value.data(), matched.value.size()); |
| 61 | } |
| 62 | pstr += matchedLength; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | std::string Conversion::Convert(std::string_view phrase) const { |
| 67 | if (phrase.empty()) { |
no test coverage detected