Combinations with partial longer match not composed issue resolved

(resolves #6)
This commit is contained in:
Simon Rozman 2016-04-08 14:45:28 +02:00
parent f918c49bfd
commit 279537b1f3

View File

@ -37,10 +37,9 @@ void ZRCola::translation_db::Compose(_In_z_count_(inputMax) const wchar_t* input
indexComp::size_type compositionsCount = idxComp.size(); indexComp::size_type compositionsCount = idxComp.size();
for (size_t i = 0; i < inputMax;) { for (size_t i = 0; i < inputMax;) {
// Start with the full search area at i-th character. // Find the longest matching composition at i-th character.
for (size_t l = 0, r = compositionsCount, ii = i, j = 0;; ii++, j++) { size_t l_match = (size_t)-1;
if (ii < inputMax) { for (size_t l = 0, r = compositionsCount, ii = i, j = 0; ii < inputMax && l < r; ii++, j++) {
size_t l_prev = l;
wchar_t c = input[ii]; wchar_t c = input[ii];
while (l < r) { while (l < r) {
// Test the composition in the middle of the search area. // Test the composition in the middle of the search area.
@ -74,46 +73,31 @@ void ZRCola::translation_db::Compose(_In_z_count_(inputMax) const wchar_t* input
if (s <= c) ll = m + 1; else r = m; if (s <= c) ll = m + 1; else r = m;
} }
const translation &trans = idxComp[l];
if (j + 1 == trans.str_len) {
// The first composition of the run was a match (thus far). Save it.
l_match = l;
}
break; break;
} }
} }
}
if (l >= r) { if (l_match < compositionsCount) {
// The search area is empty. // The saved composition was an exact match.
if (j && l_prev < compositionsCount && j == idxComp[l_prev].str_len) { const translation &trans = idxComp[l_match];
// The first composition of the previous run was a match. output += trans.chr;
output += idxComp[l_prev].chr; i += trans.str_len;
i = ii; if (trans.str_len > 1 && map) {
if (j > 1 && map) {
// Mapping changed. // Mapping changed.
map->push_back(ZRCola::mapping(output.length(), i)); map->push_back(ZRCola::mapping(output.length(), i));
} }
} else { } else {
// The exact match was not found. // The match was not found.
output += input[i]; output += input[i];
i++; i++;
} }
break;
}
} else {
// End of input reached.
if (l < compositionsCount && j == idxComp[l].str_len) {
// The first composition of the previous run was a match.
output += idxComp[l].chr;
i = ii;
if (j > 1 && map) {
// Mapping changed.
map->push_back(ZRCola::mapping(output.length(), i));
}
} else {
output += input[i];
i++;
}
break;
}
}
} }
} }