example_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 unicode_test
  5. import (
  6. "fmt"
  7. "unicode"
  8. )
  9. // Functions starting with "Is" can be used to inspect which table of range a
  10. // rune belongs to. Note that runes may fit into more than one range.
  11. func Example_is() {
  12. // constant with mixed type runes
  13. const mixed = "\b5Ὂg̀9! ℃ᾭG"
  14. for _, c := range mixed {
  15. fmt.Printf("For %q:\n", c)
  16. if unicode.IsControl(c) {
  17. fmt.Println("\tis control rune")
  18. }
  19. if unicode.IsDigit(c) {
  20. fmt.Println("\tis digit rune")
  21. }
  22. if unicode.IsGraphic(c) {
  23. fmt.Println("\tis graphic rune")
  24. }
  25. if unicode.IsLetter(c) {
  26. fmt.Println("\tis letter rune")
  27. }
  28. if unicode.IsLower(c) {
  29. fmt.Println("\tis lower case rune")
  30. }
  31. if unicode.IsMark(c) {
  32. fmt.Println("\tis mark rune")
  33. }
  34. if unicode.IsNumber(c) {
  35. fmt.Println("\tis number rune")
  36. }
  37. if unicode.IsPrint(c) {
  38. fmt.Println("\tis printable rune")
  39. }
  40. if !unicode.IsPrint(c) {
  41. fmt.Println("\tis not printable rune")
  42. }
  43. if unicode.IsPunct(c) {
  44. fmt.Println("\tis punct rune")
  45. }
  46. if unicode.IsSpace(c) {
  47. fmt.Println("\tis space rune")
  48. }
  49. if unicode.IsSymbol(c) {
  50. fmt.Println("\tis symbol rune")
  51. }
  52. if unicode.IsTitle(c) {
  53. fmt.Println("\tis title case rune")
  54. }
  55. if unicode.IsUpper(c) {
  56. fmt.Println("\tis upper case rune")
  57. }
  58. }
  59. // Output:
  60. // For '\b':
  61. // is control rune
  62. // is not printable rune
  63. // For '5':
  64. // is digit rune
  65. // is graphic rune
  66. // is number rune
  67. // is printable rune
  68. // For 'Ὂ':
  69. // is graphic rune
  70. // is letter rune
  71. // is printable rune
  72. // is upper case rune
  73. // For 'g':
  74. // is graphic rune
  75. // is letter rune
  76. // is lower case rune
  77. // is printable rune
  78. // For '̀':
  79. // is graphic rune
  80. // is mark rune
  81. // is printable rune
  82. // For '9':
  83. // is digit rune
  84. // is graphic rune
  85. // is number rune
  86. // is printable rune
  87. // For '!':
  88. // is graphic rune
  89. // is printable rune
  90. // is punct rune
  91. // For ' ':
  92. // is graphic rune
  93. // is printable rune
  94. // is space rune
  95. // For '℃':
  96. // is graphic rune
  97. // is printable rune
  98. // is symbol rune
  99. // For 'ᾭ':
  100. // is graphic rune
  101. // is letter rune
  102. // is printable rune
  103. // is title case rune
  104. // For 'G':
  105. // is graphic rune
  106. // is letter rune
  107. // is printable rune
  108. // is upper case rune
  109. }
  110. func ExampleSimpleFold() {
  111. fmt.Printf("%#U\n", unicode.SimpleFold('A')) // 'a'
  112. fmt.Printf("%#U\n", unicode.SimpleFold('a')) // 'A'
  113. fmt.Printf("%#U\n", unicode.SimpleFold('K')) // 'k'
  114. fmt.Printf("%#U\n", unicode.SimpleFold('k')) // '\u212A' (Kelvin symbol, K)
  115. fmt.Printf("%#U\n", unicode.SimpleFold('\u212A')) // 'K'
  116. fmt.Printf("%#U\n", unicode.SimpleFold('1')) // '1'
  117. // Output:
  118. // U+0061 'a'
  119. // U+0041 'A'
  120. // U+006B 'k'
  121. // U+212A 'K'
  122. // U+004B 'K'
  123. // U+0031 '1'
  124. }
  125. func ExampleTo() {
  126. const lcG = 'g'
  127. fmt.Printf("%#U\n", unicode.To(unicode.UpperCase, lcG))
  128. fmt.Printf("%#U\n", unicode.To(unicode.LowerCase, lcG))
  129. fmt.Printf("%#U\n", unicode.To(unicode.TitleCase, lcG))
  130. const ucG = 'G'
  131. fmt.Printf("%#U\n", unicode.To(unicode.UpperCase, ucG))
  132. fmt.Printf("%#U\n", unicode.To(unicode.LowerCase, ucG))
  133. fmt.Printf("%#U\n", unicode.To(unicode.TitleCase, ucG))
  134. // Output:
  135. // U+0047 'G'
  136. // U+0067 'g'
  137. // U+0047 'G'
  138. // U+0047 'G'
  139. // U+0067 'g'
  140. // U+0047 'G'
  141. }
  142. func ExampleToLower() {
  143. const ucG = 'G'
  144. fmt.Printf("%#U\n", unicode.ToLower(ucG))
  145. // Output:
  146. // U+0067 'g'
  147. }
  148. func ExampleToTitle() {
  149. const ucG = 'g'
  150. fmt.Printf("%#U\n", unicode.ToTitle(ucG))
  151. // Output:
  152. // U+0047 'G'
  153. }
  154. func ExampleToUpper() {
  155. const ucG = 'g'
  156. fmt.Printf("%#U\n", unicode.ToUpper(ucG))
  157. // Output:
  158. // U+0047 'G'
  159. }
  160. func ExampleSpecialCase() {
  161. t := unicode.TurkishCase
  162. const lci = 'i'
  163. fmt.Printf("%#U\n", t.ToLower(lci))
  164. fmt.Printf("%#U\n", t.ToTitle(lci))
  165. fmt.Printf("%#U\n", t.ToUpper(lci))
  166. const uci = 'İ'
  167. fmt.Printf("%#U\n", t.ToLower(uci))
  168. fmt.Printf("%#U\n", t.ToTitle(uci))
  169. fmt.Printf("%#U\n", t.ToUpper(uci))
  170. // Output:
  171. // U+0069 'i'
  172. // U+0130 'İ'
  173. // U+0130 'İ'
  174. // U+0069 'i'
  175. // U+0130 'İ'
  176. // U+0130 'İ'
  177. }
  178. func ExampleIsDigit() {
  179. fmt.Printf("%t\n", unicode.IsDigit('৩'))
  180. fmt.Printf("%t\n", unicode.IsDigit('A'))
  181. // Output:
  182. // true
  183. // false
  184. }
  185. func ExampleIsNumber() {
  186. fmt.Printf("%t\n", unicode.IsNumber('Ⅷ'))
  187. fmt.Printf("%t\n", unicode.IsNumber('A'))
  188. // Output:
  189. // true
  190. // false
  191. }
  192. func ExampleIsLetter() {
  193. fmt.Printf("%t\n", unicode.IsLetter('A'))
  194. fmt.Printf("%t\n", unicode.IsLetter('7'))
  195. // Output:
  196. // true
  197. // false
  198. }
  199. func ExampleIsLower() {
  200. fmt.Printf("%t\n", unicode.IsLower('a'))
  201. fmt.Printf("%t\n", unicode.IsLower('A'))
  202. // Output:
  203. // true
  204. // false
  205. }
  206. func ExampleIsUpper() {
  207. fmt.Printf("%t\n", unicode.IsUpper('A'))
  208. fmt.Printf("%t\n", unicode.IsUpper('a'))
  209. // Output:
  210. // true
  211. // false
  212. }
  213. func ExampleIsTitle() {
  214. fmt.Printf("%t\n", unicode.IsTitle('Dž'))
  215. fmt.Printf("%t\n", unicode.IsTitle('a'))
  216. // Output:
  217. // true
  218. // false
  219. }
  220. func ExampleIsSpace() {
  221. fmt.Printf("%t\n", unicode.IsSpace(' '))
  222. fmt.Printf("%t\n", unicode.IsSpace('\n'))
  223. fmt.Printf("%t\n", unicode.IsSpace('\t'))
  224. fmt.Printf("%t\n", unicode.IsSpace('a'))
  225. // Output:
  226. // true
  227. // true
  228. // true
  229. // false
  230. }