Last active
July 30, 2022 01:39
-
-
Save jschaf/78933954cba1ae6fc75b3d29a4c6fc8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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