Skip to content

Instantly share code, notes, and snippets.

@hirochachacha
Created May 30, 2016 17:54
Show Gist options
  • Save hirochachacha/49077a4cd64afde90fec0df59bddfcff to your computer and use it in GitHub Desktop.
Save hirochachacha/49077a4cd64afde90fec0df59bddfcff to your computer and use it in GitHub Desktop.
NFD for OS X
package main
import (
"fmt"
"unicode/utf8"
"golang.org/x/text/unicode/norm"
)
func main() {
fmt.Printf("% x\n", "神")
fmt.Printf("% x\n", NormString("神"))
fmt.Printf("% x\n", "神")
}
func NormString(s string) string {
var b [utf8.UTFMax]byte
out := make([]byte, 0, len(s)*2)
for _, r := range s {
n := utf8.EncodeRune(b[:], r)
// http://tama-san.com/hfsplus_normalize/
switch {
case r < 0x2000:
out = norm.NFD.Append(out, b[:n]...)
case r <= 0x2fff:
out = append(out, b[:n]...)
case r < 0xf900:
out = norm.NFD.Append(out, b[:n]...)
case r <= 0xfaff:
out = append(out, b[:n]...)
case r < 0x2f800:
out = norm.NFD.Append(out, b[:n]...)
case r <= 0x2fa1f:
out = append(out, b[:n]...)
default:
out = norm.NFD.Append(out, b[:n]...)
}
}
return string(out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment