You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DateKeys-App/scripts/go-isprint-table.go

51 lines
1.3 KiB

//go:build ignore
// Prints GO_PRINTABLE_RANGES of src/lib/dkc/bytes.ts: the runes above U+007F
// that strconv.IsPrint accepts, as pairs of (gap from the end of the previous
// range, length) in base 36 from U+0080, and the SHA-256 of the IsPrint bit
// set of all runes (bit r at byte r>>3, mask 1<<(r&7)) that bytes.test.ts
// pins. Run it with the Go toolchain of the reference implementation:
//
// go run scripts/go-isprint-table.go
package main
import (
"crypto/sha256"
"fmt"
"runtime"
"strconv"
"strings"
"unicode"
)
func main() {
var parts []string
next, lo, ranges, runes := rune(0x80), rune(-1), 0, 0
flush := func(hi rune) {
parts = append(parts, strconv.FormatInt(int64(lo-next), 36), strconv.FormatInt(int64(hi-lo+1), 36))
next, lo = hi+1, -1
ranges++
}
for r := rune(0x80); r <= unicode.MaxRune; r++ {
if strconv.IsPrint(r) {
runes++
if lo < 0 {
lo = r
}
} else if lo >= 0 {
flush(r - 1)
}
}
if lo >= 0 {
flush(unicode.MaxRune)
}
bits := make([]byte, (unicode.MaxRune+1)/8)
for r := rune(0); r <= unicode.MaxRune; r++ {
if strconv.IsPrint(r) {
bits[r>>3] |= 1 << (r & 7)
}
}
fmt.Printf("// %s, Unicode %s: %d ranges, %d runes, bit set SHA-256 %x\n", runtime.Version(), unicode.Version, ranges, runes, sha256.Sum256(bits))
fmt.Println(strings.Join(parts, ","))
}

Powered by TurnKey Linux.