Skip to content

Commit e253888

Browse files
authored
Fix isAllowed of escapeStreamer (#22814)
The use of `sort.Search` is wrong: The slice should be sorted, and `return >= 0` doen't mean it exists, see the [manual](https://pkg.go.dev/sort#Search). Could be fixed like this if we really need it: ```diff diff --git a/modules/charset/escape_stream.go b/modules/charset/escape_stream.go index 823b635..fcf1ffb 100644 --- a/modules/charset/escape_stream.go +++ b/modules/charset/escape_stream.go @@ -20,6 +20,9 @@ import ( var defaultWordRegexp = regexp.MustCompile(`(-?\d*\.\d\w*)|([^\` + "`" + `\~\!\@\#\$\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s\x00-\x1f]+)`) func NewEscapeStreamer(locale translation.Locale, next HTMLStreamer, allowed ...rune) HTMLStreamer { + sort.Slice(allowed, func(i, j int) bool { + return allowed[i] < allowed[j] + }) return &escapeStreamer{ escaped: &EscapeStatus{}, PassthroughHTMLStreamer: *NewPassthroughStreamer(next), @@ -284,14 +287,8 @@ func (e *escapeStreamer) runeTypes(runes ...rune) (types []runeType, confusables } func (e *escapeStreamer) isAllowed(r rune) bool { - if len(e.allowed) == 0 { - return false - } - if len(e.allowed) == 1 { - return e.allowed[0] == r - } - - return sort.Search(len(e.allowed), func(i int) bool { + i := sort.Search(len(e.allowed), func(i int) bool { return e.allowed[i] >= r - }) >= 0 + }) + return i < len(e.allowed) && e.allowed[i] == r } ``` But I don't think so, a map is better to do it.
1 parent 29aea36 commit e253888

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

modules/charset/escape_stream.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package charset
66
import (
77
"fmt"
88
"regexp"
9-
"sort"
109
"strings"
1110
"unicode"
1211
"unicode/utf8"
@@ -20,12 +19,16 @@ import (
2019
var defaultWordRegexp = regexp.MustCompile(`(-?\d*\.\d\w*)|([^\` + "`" + `\~\!\@\#\$\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s\x00-\x1f]+)`)
2120

2221
func NewEscapeStreamer(locale translation.Locale, next HTMLStreamer, allowed ...rune) HTMLStreamer {
22+
allowedM := make(map[rune]bool, len(allowed))
23+
for _, v := range allowed {
24+
allowedM[v] = true
25+
}
2326
return &escapeStreamer{
2427
escaped: &EscapeStatus{},
2528
PassthroughHTMLStreamer: *NewPassthroughStreamer(next),
2629
locale: locale,
2730
ambiguousTables: AmbiguousTablesForLocale(locale),
28-
allowed: allowed,
31+
allowed: allowedM,
2932
}
3033
}
3134

@@ -34,7 +37,7 @@ type escapeStreamer struct {
3437
escaped *EscapeStatus
3538
locale translation.Locale
3639
ambiguousTables []*AmbiguousTable
37-
allowed []rune
40+
allowed map[rune]bool
3841
}
3942

4043
func (e *escapeStreamer) EscapeStatus() *EscapeStatus {
@@ -256,7 +259,7 @@ func (e *escapeStreamer) runeTypes(runes ...rune) (types []runeType, confusables
256259
runeCounts.numBrokenRunes++
257260
case r == ' ' || r == '\t' || r == '\n':
258261
runeCounts.numBasicRunes++
259-
case e.isAllowed(r):
262+
case e.allowed[r]:
260263
if r > 0x7e || r < 0x20 {
261264
types[i] = nonBasicASCIIRuneType
262265
runeCounts.numNonConfusingNonBasicRunes++
@@ -282,16 +285,3 @@ func (e *escapeStreamer) runeTypes(runes ...rune) (types []runeType, confusables
282285
}
283286
return types, confusables, runeCounts
284287
}
285-
286-
func (e *escapeStreamer) isAllowed(r rune) bool {
287-
if len(e.allowed) == 0 {
288-
return false
289-
}
290-
if len(e.allowed) == 1 {
291-
return e.allowed[0] == r
292-
}
293-
294-
return sort.Search(len(e.allowed), func(i int) bool {
295-
return e.allowed[i] >= r
296-
}) >= 0
297-
}

0 commit comments

Comments
 (0)