lib/protocol: faster Luhn algorithm and better testing (#6475)
The previous implementation was very generic; its tests didn't cover the actual alphabet for device IDs. Benchmark results on amd64: name old time/op new time/op delta Luhnify-8 1.00µs ± 1% 0.28µs ± 4% -72.38% (p=0.000 n=9+10) Unluhnify-8 992ns ± 2% 274ns ± 1% -72.39% (p=0.000 n=10+9)
This commit is contained in:
@@ -165,7 +165,7 @@ func luhnify(s string) (string, error) {
|
||||
for i := 0; i < 4; i++ {
|
||||
p := s[i*13 : (i+1)*13]
|
||||
copy(res[i*(13+1):], p)
|
||||
l, err := luhnBase32.generate(p)
|
||||
l, err := luhn32(p)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -183,7 +183,7 @@ func unluhnify(s string) (string, error) {
|
||||
for i := 0; i < 4; i++ {
|
||||
p := s[i*(13+1) : (i+1)*(13+1)-1]
|
||||
copy(res[i*13:], p)
|
||||
l, err := luhnBase32.generate(p)
|
||||
l, err := luhn32(p)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user