spellsuggest fails to offer insertions when the inserted character and the following character share the same UTF‑8 lead byte

Offen Anfängerfreundlich
#22,107 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 13 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
61/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
c, vim

Rechercherichtung

Beginne in src/spellsuggest.c bei suggest_trie_walk() und untersuche den STATE_INS-Fall, insbesondere den im Issue beschriebenen Bytevergleich. Reproduziere den Fehler mit dem bulgarischen Wörterbuch und den angegebenen spellsuggest()-Schritten. Überprüfe dann, ob das erwartete Wörterbuchwort bei der fehlschlagenden Löschung erscheint und der andere Einfügefall weiterhin funktioniert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

bug
Steps to reproduce
  1. In an empty directory create two files. Xtest.aff – one line:
printf 'SET UTF-8\n' > Xtest.aff

Xtest.dic – two lines:

printf '1\n\u0434\u044a\u043b\u0436\u0438\u043d\u0438\u0442\u0435\n' > Xtest.dic

(Xtest.dic now holds the Bulgarian word "дължините", U+0434 U+044A U+043B U+0436 U+0438 U+043D U+0438 U+0442 U+0435)
2. Start Vim vim --clean
3. :set encoding=utf-8
4. :mkspell! Xtest Xtest
5. :set spl=Xtest.utf-8.spl spell
6. :let bad = join(map([0x434,0x44A,0x436,0x438,0x43D,0x438,0x442,0x435], 'nr2char(v:val)'), '')
7. :echo spellsuggest(bad, 5)
bad is the dictionary word with the single character л (U+043B) removed.

Expected behaviour

Observed: []
Expected: a one-element list containing the dictionary word

Additional context
The same dictionary produces correct suggestions for other single‑character deletions:
input=длжините, suggestion=дължините, ъ inserted, lead bytes 0xD1 vs 0xD0
:let b1 = join(map([0x44A,0x43B,0x436,0x438,0x43D,0x438,0x442,0x435], 'nr2char(v:val)'), '')
:echo spellsuggest(b1, 5)

The only failing case is when the character to be inserted and the following character share the same UTF‑8 lead byte. This affects the ranges U+0430–U+043F (а–п, lead byte 0xD0) and U+0440–U+044F (р–я, lead byte 0xD1) – roughly half of all insertion attempts in Cyrillic text.

Cause
In src/spellsuggest.c, the STATE_INS case inside suggest_trie_walk() skips a trie byte when it is equal to the current byte of the misspelled word:

// - Skip the byte if it's equal to the byte in the word,
//   accepting that byte is always better.
if (c != fword[sp->ts_fidx] && TRY_DEEPER(su, stack, depth, newscore))

This optimisation is correct for single‑byte encodings, where c == fword[...] means the same character. In UTF‑8, however, two different multibyte characters can share the same lead byte. When the code tries to insert a character, it first offers the lead byte (0xD0). That lead byte equals the next byte of the misspelled word (which is also 0xD0 because the following character also starts with that lead byte). The entire insertion branch is then discarded before any of the trailing bytes are examined, so the insertion is never attempted.

Suggested fix
The following one-line patch.

vim-spellsuggest.patch

Version of Vim

Vim 9.1.0697 (v:versionlong 9010697), vim --clean. Also reproduces on master, 9.2 patch 1448, built from source.

Environment

Linux Mint

Logs and stack traces

Vorherrschende Sprache
Vim Script
Sterne
41.4k
Forks
6.2k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus vim/vim

Alle Issues in vim/vim

Ähnliche Issues

Weitere Issues zu Backend & API Design

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.