nip13: crazier and more fun and hopefully slightly more performant nonce encoding.

This commit is contained in:
fiatjaf
2024-08-20 13:54:51 -03:00
parent 437cdecfb1
commit edbbd6df44
3 changed files with 28 additions and 3 deletions

25
nip13/helpers.go Normal file
View File

@@ -0,0 +1,25 @@
package nip13
const (
maxSafeAscii = 126
minSafeAscii = 35
availableSafeAscii = maxSafeAscii - minSafeAscii
)
func uintToStringCrazy(num uint64) string {
nchars := 1 + num/availableSafeAscii
chars := make([]byte, nchars)
i := 0
for {
if num < availableSafeAscii {
chars[i] = byte(num + minSafeAscii)
break
} else {
chars[i] = byte(num/availableSafeAscii + minSafeAscii)
num -= availableSafeAscii
i++
}
}
return string(chars)
}