example_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package runes_test
  5. import (
  6. "fmt"
  7. "unicode"
  8. "golang.org/x/text/runes"
  9. "golang.org/x/text/transform"
  10. "golang.org/x/text/unicode/norm"
  11. "golang.org/x/text/width"
  12. )
  13. func ExampleRemove() {
  14. t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
  15. s, _, _ := transform.String(t, "résumé")
  16. fmt.Println(s)
  17. // Output:
  18. // resume
  19. }
  20. func ExampleMap() {
  21. replaceHyphens := runes.Map(func(r rune) rune {
  22. if unicode.Is(unicode.Hyphen, r) {
  23. return '|'
  24. }
  25. return r
  26. })
  27. s, _, _ := transform.String(replaceHyphens, "a-b‐c⸗d﹣e")
  28. fmt.Println(s)
  29. // Output:
  30. // a|b|c|d|e
  31. }
  32. func ExampleIn() {
  33. // Convert Latin characters to their canonical form, while keeping other
  34. // width distinctions.
  35. t := runes.If(runes.In(unicode.Latin), width.Fold, nil)
  36. s, _, _ := transform.String(t, "アルアノリウ tech / アルアノリウ tech")
  37. fmt.Println(s)
  38. // Output:
  39. // アルアノリウ tech / アルアノリウ tech
  40. }
  41. func ExampleIf() {
  42. // Widen everything but ASCII.
  43. isASCII := func(r rune) bool { return r <= unicode.MaxASCII }
  44. t := runes.If(runes.Predicate(isASCII), nil, width.Widen)
  45. s, _, _ := transform.String(t, "アルアノリウ tech / 中國 / 5₩")
  46. fmt.Println(s)
  47. // Output:
  48. // アルアノリウ tech / 中國 / 5₩
  49. }