Created
January 31, 2016 08:21
-
-
Save tkrajina/d423e9b9fc2e72d63072 to your computer and use it in GitHub Desktop.
Golang remove accents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"unicode" | |
"golang.org/x/text/transform" | |
"golang.org/x/text/unicode/norm" | |
) | |
func isMn(r rune) bool { | |
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks | |
} | |
func main() { | |
s := "Yoùr Śtring šđč枊ĐČĆŽ Ötzi's Nationalität èàì" | |
b := make([]byte, len(s)) | |
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC) | |
_, _, e := t.Transform(b, []byte(s), true) | |
if e != nil { | |
panic(e) | |
} | |
fmt.Println(string(b)) | |
} |
Had to increase destination buffer size (length of the string is sometimes larger after this transform, which leads to "transform: short destination buffer" error)
I'm using this to rename files but I'm having trouble as it removes the . of the file.
Any suggestion?
A little late to the party but it looks like that function is now deprecated. You can use runes.Remove
instead:
transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
sorry the Ñ is a not character accent in spanish
this works but takes the Ñ as an accent char so does not work in Spanish
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
otherwise there were some trailing x\00