1// Package unidecode implements a unicode transliterator
2// which replaces non-ASCII characters with their ASCII
3// approximations.
4package unidecode
5
6//go:generate go run make_table.go
7
8import (
9 "sync"
10 "unicode"
11)
12
13const pooledCapacity = 64
14
15var (
16 slicePool sync.Pool
17 decodingOnce sync.Once
18)
19
20// Unidecode implements a unicode transliterator, which
21// replaces non-ASCII characters with their ASCII
22// counterparts.
23// Given an unicode encoded string, returns
24// another string with non-ASCII characters replaced
25// with their closest ASCII counterparts.
26// e.g. Unicode("áéíóú") => "aeiou"
27func Unidecode(s string) string {
28 decodingOnce.Do(decodeTransliterations)
29 l := len(s)
30 var r []rune
31 if l > pooledCapacity {
32 r = make([]rune, 0, len(s))
33 } else {
34 if x := slicePool.Get(); x != nil {
35 r = x.([]rune)[:0]
36 } else {
37 r = make([]rune, 0, pooledCapacity)
38 }
39 }
40 for _, c := range s {
41 if c <= unicode.MaxASCII {
42 r = append(r, c)
43 continue
44 }
45 if c > unicode.MaxRune || c >= transCount {
46 /* Ignore reserved chars */
47 continue
48 }
49 if d := transliterations[c]; d != nil {
50 r = append(r, d...)
51 }
52 }
53 res := string(r)
54 if l <= pooledCapacity {
55 slicePool.Put(r)
56 }
57 return res
58}