mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-05 15:17:37 +03:00
Refactor breakOffsets function: simplify return statements and improve readability
This commit is contained in:
parent
247463a4b9
commit
3806f1883a
@ -88,25 +88,17 @@ size_t byteOffsetForIndex(const std::vector<CodepointInfo>& cps, const size_t in
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
std::vector<size_t> Hyphenator::breakOffsets(const std::string& word, const bool includeFallback) {
|
std::vector<size_t> Hyphenator::breakOffsets(const std::string& word, const bool includeFallback) {
|
||||||
std::vector<size_t> byteOffsets;
|
|
||||||
if (word.empty()) {
|
if (word.empty()) {
|
||||||
return byteOffsets;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto cps = collectCodepoints(word);
|
auto cps = collectCodepoints(word);
|
||||||
trimTrailingPunctuation(cps);
|
trimTrailingPunctuation(cps);
|
||||||
if (cps.size() < MIN_PREFIX_CP + MIN_SUFFIX_CP) {
|
if (cps.size() < MIN_PREFIX_CP + MIN_SUFFIX_CP) {
|
||||||
return byteOffsets;
|
return {};
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<size_t> indexes;
|
|
||||||
indexes.reserve(cps.size());
|
|
||||||
|
|
||||||
if (hasOnlyAlphabetic(cps)) {
|
|
||||||
auto dictBreaks = collectBreakIndexes(cps);
|
|
||||||
indexes.insert(indexes.end(), dictBreaks.begin(), dictBreaks.end());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<size_t> indexes = hasOnlyAlphabetic(cps) ? collectBreakIndexes(cps) : std::vector<size_t>();
|
||||||
if (includeFallback) {
|
if (includeFallback) {
|
||||||
for (size_t idx = MIN_PREFIX_CP; idx + MIN_SUFFIX_CP <= cps.size(); ++idx) {
|
for (size_t idx = MIN_PREFIX_CP; idx + MIN_SUFFIX_CP <= cps.size(); ++idx) {
|
||||||
indexes.push_back(idx);
|
indexes.push_back(idx);
|
||||||
@ -114,16 +106,16 @@ std::vector<size_t> Hyphenator::breakOffsets(const std::string& word, const bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (indexes.empty()) {
|
if (indexes.empty()) {
|
||||||
return byteOffsets;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::sort(indexes.begin(), indexes.end());
|
std::sort(indexes.begin(), indexes.end());
|
||||||
indexes.erase(std::unique(indexes.begin(), indexes.end()), indexes.end());
|
indexes.erase(std::unique(indexes.begin(), indexes.end()), indexes.end());
|
||||||
|
|
||||||
|
std::vector<size_t> byteOffsets;
|
||||||
byteOffsets.reserve(indexes.size());
|
byteOffsets.reserve(indexes.size());
|
||||||
for (const size_t idx : indexes) {
|
for (const size_t idx : indexes) {
|
||||||
byteOffsets.push_back(byteOffsetForIndex(cps, idx));
|
byteOffsets.push_back(byteOffsetForIndex(cps, idx));
|
||||||
}
|
}
|
||||||
|
|
||||||
return byteOffsets;
|
return byteOffsets;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user