Skip to content

Instantly share code, notes, and snippets.

@jschaf
Last active January 13, 2023 01:36
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/bd600ce71ad3798af6c160d74904ac9c to your computer and use it in GitHub Desktop.
Save jschaf/bd600ce71ad3798af6c160d74904ac9c to your computer and use it in GitHub Desktop.
# We append the integers 1..50 to a base prefix.
# The problematic strings have the apostrophe \xe2
#    Andy’s liqour parent account
#    Carr’s Bar N’ Grill
short dest error transforming unicode to ascii src_bytes="wind-Andy\xe2" src_bytes_read=10, dst_bytes="wind-And\x00\x00" dst_bytes_written=8: transform: short destination buffer
short dest error transforming unicode to ascii src_bytes="wind-Andy\xe21" src_bytes_read=11, dst_bytes="wind-And\x00\x00\x00" dst_bytes_written=8: transform: short destination buffer    ...
...
short dest error transforming unicode to ascii src_bytes="wind-Andy\xe246" src_bytes_read=12, dst_bytes="wind-Andy\x00\x00\x00" dst_bytes_written=9: transform: short destination buffer
short dest error transforming unicode to ascii src_bytes="wind-Andy\xe247" src_bytes_read=12, dst_bytes="wind-Andy\x00\x00\x00" dst_bytes_written=9: transform: short destination buffer
// normalize converts all unicode chars into ASCII.
func normalize(cs []byte) []byte {
if !hasUnicodeChars(cs) {
return cs
}
chars := make([]byte, len(cs))
asciiTransformer := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
nDst, nSrc, err := asciiTransformer.Transform(chars, cs, true)
if err != nil {
if errors.Is(err, transform.ErrShortDst) {
log.Global().Sugar().Errorf("short dest error transforming unicode to ascii src_bytes=%q src_bytes_read=%d, dst_bytes=%q dst_bytes_written=%d: %s", cs, nSrc, chars, nDst, err.Error())
} else {
panic(fmt.Sprintf("transform unicode %q to ascii: %s", string(chars), err.Error()))
}
}
chars = chars[:nDst]
return chars
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment