Skip to content

Commit 1c35f08

Browse files
wolfogreSysoev, Vladimir
authored and
Sysoev, Vladimir
committed
Avoid frequent string2bytes conversions (go-gitea#20940)
Fix go-gitea#20939
1 parent 9d4e42b commit 1c35f08

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

modules/charset/escape_stream.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func (e *escapeStreamer) Text(data string) error {
5050
_, _ = sb.WriteString(data[:len(UTF8BOM)])
5151
pos = len(UTF8BOM)
5252
}
53+
dataBytes := []byte(data)
5354
for pos < len(data) {
5455
nextIdxs := defaultWordRegexp.FindStringIndex(data[pos:])
5556
if nextIdxs == nil {
@@ -64,30 +65,30 @@ func (e *escapeStreamer) Text(data string) error {
6465
positions := make([]int, 0, next-until+1)
6566

6667
for pos < until {
67-
r, sz := utf8.DecodeRune([]byte(data)[pos:])
68+
r, sz := utf8.DecodeRune(dataBytes[pos:])
6869
positions = positions[:0]
6970
positions = append(positions, pos, pos+sz)
7071
types, confusables, _ := e.runeTypes(r)
71-
if err := e.handleRunes(data, []rune{r}, positions, types, confusables, sb); err != nil {
72+
if err := e.handleRunes(dataBytes, []rune{r}, positions, types, confusables, sb); err != nil {
7273
return err
7374
}
7475
pos += sz
7576
}
7677

7778
for i := pos; i < next; {
78-
r, sz := utf8.DecodeRune([]byte(data)[i:])
79+
r, sz := utf8.DecodeRune(dataBytes[i:])
7980
runes = append(runes, r)
8081
positions = append(positions, i)
8182
i += sz
8283
}
8384
positions = append(positions, next)
8485
types, confusables, runeCounts := e.runeTypes(runes...)
8586
if runeCounts.needsEscape() {
86-
if err := e.handleRunes(data, runes, positions, types, confusables, sb); err != nil {
87+
if err := e.handleRunes(dataBytes, runes, positions, types, confusables, sb); err != nil {
8788
return err
8889
}
8990
} else {
90-
_, _ = sb.Write([]byte(data)[pos:next])
91+
_, _ = sb.Write(dataBytes[pos:next])
9192
}
9293
pos = next
9394
}
@@ -99,7 +100,7 @@ func (e *escapeStreamer) Text(data string) error {
99100
return nil
100101
}
101102

102-
func (e *escapeStreamer) handleRunes(data string, runes []rune, positions []int, types []runeType, confusables []rune, sb *strings.Builder) error {
103+
func (e *escapeStreamer) handleRunes(data []byte, runes []rune, positions []int, types []runeType, confusables []rune, sb *strings.Builder) error {
103104
for i, r := range runes {
104105
switch types[i] {
105106
case brokenRuneType:
@@ -111,7 +112,7 @@ func (e *escapeStreamer) handleRunes(data string, runes []rune, positions []int,
111112
}
112113
end := positions[i+1]
113114
start := positions[i]
114-
if err := e.brokenRune([]byte(data)[start:end]); err != nil {
115+
if err := e.brokenRune(data[start:end]); err != nil {
115116
return err
116117
}
117118
case ambiguousRuneType:

0 commit comments

Comments
 (0)