hints: Fix Unicode quotes not being removed from paths and URLs

The post-processors index the text by byte, so when looking at the
character before a position they decoded only the last byte of
multi-byte quotes such as ‘’ and “” and never matched them. URLs kept
the closing quote and paths kept both quotes. When the closing quote was
just outside the match, the start was also advanced by a single byte,
splitting the opening quote and producing invalid UTF-8, for example,
for the file name in wget's "Saving to: ‘index.html’".

This worked before the hints kitten was ported to Go.
This commit is contained in:
xucian 2026-10-23 23:40:30 +03:00
parent 07c8731470
commit f9b2fbb3b1
3 changed files with 21 additions and 6 deletions

View file

@ -450,6 +450,9 @@ Detailed list of changes
or too thick when zoomed out. At the configured :opt:`font_size` nothing
changes (:iss:`10362`)
- hints kitten: Fix Unicode quotes not being removed from around paths and
URLs, such as file names in the output of wget
0.49.2 [2026-10-01]

View file

@ -138,17 +138,25 @@ func char_at(s string, i int) string {
return string(ans)
}
func char_before(s string, i int) string {
ans, _ := utf8.DecodeLastRuneInString(s[:i])
if ans == utf8.RuneError {
return ""
}
return string(ans)
}
func matching_remover(openers ...string) PostProcessorFunc {
return func(text string, s, e int) (int, int) {
if s < e && e <= len(text) {
before := char_at(text, s)
if slices.Index(openers, before) > -1 {
q := closing_bracket_for(before)
if e > 0 && char_at(text, e-1) == q {
s++
e--
if e > 0 && char_before(text, e) == q {
s += len(before)
e -= len(q)
} else if char_at(text, e) == q {
s++
s += len(before)
}
}
}
@ -179,8 +187,8 @@ var PostProcessorMap = sync.OnceValue(func() map[string]PostProcessorFunc {
e--
}
// truncate url at closing bracket/quote
if s > 0 && e <= len(text) && closing_bracket_for(char_at(text, s-1)) != "" {
q := closing_bracket_for(char_at(text, s-1))
if s > 0 && e <= len(text) && closing_bracket_for(char_before(text, s)) != "" {
q := closing_bracket_for(char_before(text, s))
idx := strings.Index(text[s:], q)
if idx > 0 {
e = s + idx

View file

@ -71,6 +71,8 @@ func TestHintMarking(t *testing.T) {
r("http://example.com.", "http://example.com")
r("http://example.com!", "http://example.com")
r("http://example.com?", "http://example.com")
r("“"+u+"”", u)
r("‘"+u+"’", u)
opts.Type = "linenum"
m := func(text, path string, line int) {
@ -98,6 +100,8 @@ func TestHintMarking(t *testing.T) {
r("file.epub.", "file.epub")
r("(file.epub)", "file.epub")
r("some/path", "some/path")
r("“some/path”", "some/path")
r("Saving to: ‘index.html’", "index.html")
// long lines without spaces must not cause quadratic backtracking
long_line := strings.Repeat("1,", 20000)
r(long_line)