Golang bytes.ToTitleSpecial() function example

package bytes

ToTitleSpecial returns a copy of the input byte slice with all Unicode letters mapped to their title case, giving priority to the special casing rules.

Golang bytes.ToTitleSpecial() function usage example

 package main

 import (
 "fmt"
 "bytes"
 "unicode"
 )

 func main() {
 str := []byte("funky i ŞĞÜÖIİi title")

 toupper := bytes.ToUpperSpecial(unicode.AzeriCase, str)
 tolower := bytes.ToLowerSpecial(unicode.AzeriCase, str)
 totitle := bytes.ToTitleSpecial(unicode.AzeriCase, str)

 fmt.Println("Original : " + string(str))

 fmt.Println("ToUpper : " + string(toupper))
 fmt.Println("ToLower : " + string(tolower))
 fmt.Println("ToTitle : " + string(totitle))

 }

Output :

Original : funky i ŞĞÜÖIİi title

ToUpper : FUNKY İ ŞĞÜÖIİİ TİTLE

ToLower : funky i şğüöıii title

ToTitle : FUNKY İ ŞĞÜÖIİİ TİTLE

To detect special case from the environment, try this function

  var specialCase unicode.SpecialCase

  func init() {
 for _, env := range []string{"LC_ALL", "LC_CTYPE", "LANG"} {
 locale := os.Getenv(env)
 if strings.HasPrefix("tr_", locale) {
 specialCase = unicode.TurkishCase
 break
 }
 if strings.HasPrefix("az_", locale) {
 specialCase = unicode.AzeriCase
 break
 }
 }
  }

Reference :

http://golang.org/pkg/bytes/#ToTitleSpecial

http://golang.org/pkg/unicode/#SpecialCase

https://gist.github.com/roktas/8514975

Advertisement