github.com/primecitizens/pcz/std@v0.2.1/text/unicode/example_test.go (about)

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