Skip to content

Commit

Permalink
fix: optimize isASCII, remove rune slice casts
Browse files Browse the repository at this point in the history
name               old time/op    new time/op    delta
IsASCII/Len=1-12     5.60ns ± 1%    3.28ns ± 0%  -41.37%  (p=0.000 n=9+8)
IsASCII/Len=8-12     18.8ns ± 2%     8.8ns ± 0%  -53.29%  (p=0.000 n=9+10)
IsASCII/Len=16-12    27.2ns ± 2%    16.9ns ± 3%  -37.76%  (p=0.000 n=10+9)
IsASCII/Len=32-12    57.3ns ±14%    30.8ns ± 2%  -46.26%  (p=0.000 n=10+10)
  • Loading branch information
tdakkota committed Oct 15, 2021
1 parent 1fb7f7a commit 2bc1f87
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions ascii.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package asciicheck

import "unicode"
import (
"unicode"
"unicode/utf8"
)

func isASCII(s string) (rune, bool) {
if len(s) == 1 {
return []rune(s)[0], s[0] <= unicode.MaxASCII
r, size := utf8.DecodeRuneInString(s)
return r, size < 2
}

r := []rune(s)
for i := 0; i < len(s); i++ {
if r[i] > unicode.MaxASCII {
return r[i], false
for _, r := range s {
if r > unicode.MaxASCII {
return r, false
}
}

Expand Down

0 comments on commit 2bc1f87

Please sign in to comment.