example_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2015 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 width_test
  5. import (
  6. "fmt"
  7. "golang.org/x/text/width"
  8. )
  9. func ExampleTransformer_fold() {
  10. s := "abヲ₩○¥A"
  11. f := width.Fold.String(s)
  12. fmt.Printf("%U: %s\n", []rune(s), s)
  13. fmt.Printf("%U: %s\n", []rune(f), f)
  14. // Output:
  15. // [U+0061 U+0062 U+FF66 U+FFE6 U+FFEE U+FFE5 U+FF21]: abヲ₩○¥A
  16. // [U+0061 U+0062 U+30F2 U+20A9 U+25CB U+00A5 U+0041]: abヲ₩○¥A
  17. }
  18. func ExampleTransformer_widen() {
  19. s := "ab¥ヲ₩○"
  20. w := width.Widen.String(s)
  21. fmt.Printf("%U: %s\n", []rune(s), s)
  22. fmt.Printf("%U: %s\n", []rune(w), w)
  23. // Output:
  24. // [U+0061 U+0062 U+00A5 U+FF66 U+20A9 U+FFEE]: ab¥ヲ₩○
  25. // [U+FF41 U+FF42 U+FFE5 U+30F2 U+FFE6 U+25CB]: ab¥ヲ₩○
  26. }
  27. func ExampleTransformer_narrow() {
  28. s := "abヲ₩○¥A"
  29. n := width.Narrow.String(s)
  30. fmt.Printf("%U: %s\n", []rune(s), s)
  31. fmt.Printf("%U: %s\n", []rune(n), n)
  32. // Ambiguous characters with a halfwidth equivalent get mapped as well.
  33. s = "←"
  34. n = width.Narrow.String(s)
  35. fmt.Printf("%U: %s\n", []rune(s), s)
  36. fmt.Printf("%U: %s\n", []rune(n), n)
  37. // Output:
  38. // [U+0061 U+0062 U+30F2 U+FFE6 U+25CB U+FFE5 U+FF21]: abヲ₩○¥A
  39. // [U+0061 U+0062 U+FF66 U+20A9 U+FFEE U+00A5 U+0041]: abヲ₩○¥A
  40. // [U+2190]: ←
  41. // [U+FFE9]: ←
  42. }