├── README.md └── unidecode ├── table.go ├── unidecode.go └── unidecode_test.go /README.md: -------------------------------------------------------------------------------- 1 | Unicode transliterator (also known as unidecode) for Go 2 | ======================================================= 3 | 4 | Use the following command to install gounidecode 5 | 6 | go get -u github.com/fiam/gounidecode/unidecode 7 | 8 | Example usage 9 | ============= 10 | 11 | package main 12 | 13 | import ( 14 | "fmt" 15 | "github.com/fiam/gounidecode/unidecode" 16 | ) 17 | 18 | func main() { 19 | fmt.Println(Unidecode("áéíóú")) // Will print aeiou 20 | fmt.Println(Unidecode("\u5317\u4EB0")) // Will print Bei Jing 21 | fmt.Println(Unidecode("Κνωσός")) // Will print Knosos 22 | } 23 | -------------------------------------------------------------------------------- /unidecode/unidecode.go: -------------------------------------------------------------------------------- 1 | // Unidecode implements a unicode transliterator, which 2 | // replaces non-ASCII characters with their ASCII 3 | // counterparts 4 | 5 | package unidecode 6 | 7 | import ( 8 | "unicode" 9 | ) 10 | 11 | // Given an unicode encoded string, returns 12 | // another string with non-ASCII characters replaced 13 | // with their closest ASCII counterparts. 14 | // e.g. Unicode("áéíóú") => "aeiou" 15 | func Unidecode(s string) string { 16 | str := "" 17 | for _, c := range s { 18 | if c <= unicode.MaxASCII { 19 | str += string(c) 20 | continue 21 | } 22 | if c > unicode.MaxRune { 23 | /* Ignore reserved chars */ 24 | continue 25 | } 26 | if d, ok := transliterations[c]; ok { 27 | str += d 28 | } 29 | } 30 | return str 31 | } 32 | -------------------------------------------------------------------------------- /unidecode/unidecode_test.go: -------------------------------------------------------------------------------- 1 | package unidecode 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func testTransliteration(original string, decoded string, t *testing.T) { 8 | if r := Unidecode(original); r != decoded { 9 | t.Errorf("Expected '%s', got '%s'\n", decoded, r) 10 | } 11 | } 12 | 13 | func TestASCII(t *testing.T) { 14 | s := "ABCDEF" 15 | testTransliteration(s, s, t) 16 | } 17 | 18 | func TestKnosos(t *testing.T) { 19 | o := "Κνωσός" 20 | d := "Knosos" 21 | testTransliteration(o, d, t) 22 | } 23 | 24 | func TestBeiJing(t *testing.T) { 25 | o := "\u5317\u4EB0" 26 | d := "Bei Jing " 27 | testTransliteration(o, d, t) 28 | } 29 | --------------------------------------------------------------------------------