github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/unicode/example_test.gno (about)

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