Skip to content

Instantly share code, notes, and snippets.

@jschaf
Last active July 30, 2022 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jschaf/78933954cba1ae6fc75b3d29a4c6fc8d to your computer and use it in GitHub Desktop.
Save jschaf/78933954cba1ae6fc75b3d29a4c6fc8d to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"unicode"
)
var asciiTransformer = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
// normalize converts all unicode chars into ASCII.
func normalize(cs []byte) []byte {
chars := make([]byte, len(cs))
nDst, _, err := asciiTransformer.Transform(chars, cs, true)
if err != nil {
// PANIC here
// transform unicode "wind-Pa\x00\x00\x00" to ascii: transform: short destination buffer
panic(fmt.Sprintf("transform unicode %q to ascii: %s", string(chars), err.Error()))
}
chars = chars[:nDst]
return chars
}
func main() {
got := normalize([]byte("wind-Pa\x00\x00\x00"))
fmt.Printf("got: %q\n", got)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment