unicode/utf8: use builtin max function to simplify code

This commit is contained in:
Jes Cok 2025-02-25 21:57:35 +08:00
parent bdcd6d1b65
commit 202d498eb0

View file

@ -263,10 +263,7 @@ func DecodeLastRune(p []byte) (r rune, size int) {
// guard against O(n^2) behavior when traversing // guard against O(n^2) behavior when traversing
// backwards through strings with long sequences of // backwards through strings with long sequences of
// invalid UTF-8. // invalid UTF-8.
lim := end - UTFMax lim := max(end - UTFMax, 0)
if lim < 0 {
lim = 0
}
for start--; start >= lim; start-- { for start--; start >= lim; start-- {
if RuneStart(p[start]) { if RuneStart(p[start]) {
break break
@ -303,10 +300,7 @@ func DecodeLastRuneInString(s string) (r rune, size int) {
// guard against O(n^2) behavior when traversing // guard against O(n^2) behavior when traversing
// backwards through strings with long sequences of // backwards through strings with long sequences of
// invalid UTF-8. // invalid UTF-8.
lim := end - UTFMax lim := max(end - UTFMax, 0)
if lim < 0 {
lim = 0
}
for start--; start >= lim; start-- { for start--; start >= lim; start-- {
if RuneStart(s[start]) { if RuneStart(s[start]) {
break break