diff --git a/src/core.cpp b/src/core.cpp index 34064cb..6855ad4 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -645,15 +645,25 @@ int Run::match(const std::wstring &text, const std::vector &layouts, con { if (text.size() != keys.size() || overflow) return -1; + const auto normalized = lower(text); + int fallback = -1; for (int i = 0; i < (int)layouts.size(); ++i) - if (layouts[i].convertible) + { + const auto &candidate = layouts[i]; + const bool tracked = layout && candidate.handle == layout; + if (!candidate.convertible || (!tracked && fallback >= 0)) + continue; + + std::wstring expected; + for (wchar_t c : keys) + expected += c == L' ' ? L' ' : candidate.at(physical.code(c)); + if (lower(expected) == normalized) { - std::wstring expected; - for (wchar_t c : keys) - expected += c == L' ' ? L' ' : layouts[i].at(physical.code(c)); - if (lower(expected) == lower(text)) + if (tracked) return i; + fallback = i; } - return -1; + } + return fallback; } } // namespace ks diff --git a/tests/tests.cpp b/tests/tests.cpp index adb8096..337e4a4 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -449,6 +449,18 @@ int main(int argc, char **argv) "cycle does not use disabled English when all candidates are unchanged"); require(ks::nextConversionLayout(L"123 ", layouts, {0, 1, 2}, 0) == 1, "unchanged digits finish after a bounded cycle"); + auto digitLayouts = layouts; + digitLayouts[2].handle = reinterpret_cast(static_cast(0xf0a80422)); + ks::Run digitRun; + digitRun.layout = digitLayouts[2].handle; + digitRun.keys = L"4732"; + const int digitSource = digitRun.match(L"4732", digitLayouts, en); + require(digitSource == 2 && ks::nextConversionLayout(L"4732", digitLayouts, {1, 0, 2}, digitSource) == 1 && + ks::convertTo(L"4732", digitLayouts, 1, digitSource) == L"4732", + "shared digits advance from the tracked Ukrainian layout to Russian without changing text"); + digitRun.keys = L"ghbdtn"; + require(digitRun.match(L"ghbdtn", digitLayouts, en) == 0, + "recent input falls back when the tracked layout does not reproduce the selection"); require(ks::nextConversionLayout(L"рф", layouts, {2, 0}, 1) == 0, "source outside cycle still skips unchanged candidates"); require(ks::cycleCase(L"hello world") == L"Hello World", "title case");