diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2024-10-25 00:47:47 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2024-10-25 00:47:47 +0200 |
| commit | c6cc0108ca7738023b45e0eeac0fa2390532dd93 (patch) | |
| tree | 36890e6cd3091bbab8efbe686cc56f467f645bfd /vendor/github.com/gosimple/unidecode/unidecode.go | |
| parent | 0130404a1dc663d4aa68d780c9bcb23a4243e68d (diff) | |
| download | jbmafp-master.tar.gz | |
Diffstat (limited to 'vendor/github.com/gosimple/unidecode/unidecode.go')
| -rw-r--r-- | vendor/github.com/gosimple/unidecode/unidecode.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/vendor/github.com/gosimple/unidecode/unidecode.go b/vendor/github.com/gosimple/unidecode/unidecode.go new file mode 100644 index 0000000..bc5f8a1 --- /dev/null +++ b/vendor/github.com/gosimple/unidecode/unidecode.go @@ -0,0 +1,58 @@ +// Package unidecode implements a unicode transliterator +// which replaces non-ASCII characters with their ASCII +// approximations. +package unidecode + +//go:generate go run make_table.go + +import ( + "sync" + "unicode" +) + +const pooledCapacity = 64 + +var ( + slicePool sync.Pool + decodingOnce sync.Once +) + +// Unidecode implements a unicode transliterator, which +// replaces non-ASCII characters with their ASCII +// counterparts. +// Given an unicode encoded string, returns +// another string with non-ASCII characters replaced +// with their closest ASCII counterparts. +// e.g. Unicode("áéíóú") => "aeiou" +func Unidecode(s string) string { + decodingOnce.Do(decodeTransliterations) + l := len(s) + var r []rune + if l > pooledCapacity { + r = make([]rune, 0, len(s)) + } else { + if x := slicePool.Get(); x != nil { + r = x.([]rune)[:0] + } else { + r = make([]rune, 0, pooledCapacity) + } + } + for _, c := range s { + if c <= unicode.MaxASCII { + r = append(r, c) + continue + } + if c > unicode.MaxRune || c >= transCount { + /* Ignore reserved chars */ + continue + } + if d := transliterations[c]; d != nil { + r = append(r, d...) + } + } + res := string(r) + if l <= pooledCapacity { + slicePool.Put(r) + } + return res +} |
