-1

Is there a Go library that can take Sjöström as input and return Sjostrom as output?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
codefx
  • 9,872
  • 16
  • 53
  • 81

1 Answers1

2

You can use golang.org/x/text/unicode/norm to handle this.

package main

import (
    "fmt"
    "io/ioutil"
    "strings"
    "unicode"

    "golang.org/x/text/transform"
    "golang.org/x/text/unicode/norm"
)

func main() {

    isMn := func(r rune) bool {
        return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
    }
    t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)

    r := strings.NewReader("Sjöström")
    x := transform.NewReader(r, t)
    b, err := ioutil.ReadAll(x)
    if err != nil {
        panic(err)
    }

    fmt.Println(string(b))

}

See also: https://blog.golang.org/normalization

leaf bebop
  • 7,643
  • 2
  • 17
  • 27