Skip to content

Instantly share code, notes, and snippets.

@fujimaki-k
Created May 3, 2023 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujimaki-k/739ccd482da2a346285997571a0a6d77 to your computer and use it in GitHub Desktop.
Save fujimaki-k/739ccd482da2a346285997571a0a6d77 to your computer and use it in GitHub Desktop.
Unicode string normalization example
package main
import (
"fmt"
"regexp"
"strings"
"golang.org/x/text/unicode/norm"
)
var (
controlCodeRegexp = regexp.MustCompile(`\{p}`)
lineFeedRegexp = regexp.MustCompile(`\r\n|\r`)
)
func Normalize(value string) string {
values := strings.Split(lineFeedRegexp.ReplaceAllString(value, "\n"), "\n")
lines := make([]string, len(values))
for index, line := range values {
lines[index] = controlCodeRegexp.ReplaceAllString(line, "")
}
return strings.TrimSpace(norm.NFKC.String(strings.Join(lines, "")))
}
func main() {
message := "Fight dayo!"
fmt.Println(message)
fmt.Println(Normalize(message))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment