41 lines
686 B
Go
41 lines
686 B
Go
package ascii_reduce
|
|
|
|
func getMapping(r rune) []rune {
|
|
if replacement, ok := Mappings[r]; ok {
|
|
return []rune(replacement)
|
|
} else {
|
|
return []rune("")
|
|
}
|
|
}
|
|
|
|
func Reduce(s string, replace_non_ascii_char string) string {
|
|
runes := []rune(s)
|
|
|
|
isAllAscii := true
|
|
for _, r := range runes {
|
|
if r > 0x7e {
|
|
isAllAscii = false
|
|
break
|
|
}
|
|
}
|
|
|
|
if isAllAscii {
|
|
return s
|
|
}
|
|
|
|
newRunes := make([]rune, 0)
|
|
|
|
for _, sourceRune := range runes {
|
|
mapped := getMapping(sourceRune)
|
|
for _, replacedRune := range mapped {
|
|
if replacedRune > 0x7e {
|
|
replacedRune = []rune(replace_non_ascii_char)[0]
|
|
}
|
|
|
|
newRunes = append(newRunes, replacedRune)
|
|
}
|
|
}
|
|
|
|
return string(newRunes)
|
|
}
|