github.com/primecitizens/pcz/std@v0.2.1/text/unicode/utf8/example_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 // 4 // Copyright 2013 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 utf8_test 9 10 import ( 11 "fmt" 12 13 "github.com/primecitizens/pcz/std/text/unicode/common" 14 "github.com/primecitizens/pcz/std/text/unicode/utf8" 15 ) 16 17 func ExampleLast() { 18 str := "Hello, 世界" 19 20 for len(str) > 0 { 21 r, size := utf8.Last(str) 22 fmt.Printf("%c %v\n", r, size) 23 24 str = str[:len(str)-size] 25 } 26 // Output: 27 // 界 3 28 // 世 3 29 // 1 30 // , 1 31 // o 1 32 // l 1 33 // l 1 34 // e 1 35 // H 1 36 37 } 38 39 func ExampleFirst() { 40 str := "Hello, 世界" 41 42 for len(str) > 0 { 43 r, size := utf8.First(str) 44 fmt.Printf("%c %v\n", r, size) 45 46 str = str[size:] 47 } 48 // Output: 49 // H 1 50 // e 1 51 // l 1 52 // l 1 53 // o 1 54 // , 1 55 // 1 56 // 世 3 57 // 界 3 58 } 59 60 func ExampleEncodeRune() { 61 r := '世' 62 buf := make([]byte, 0, 3) 63 64 buf, n := utf8.EncodeRune(buf, r) 65 66 fmt.Println(buf) 67 fmt.Println(n) 68 // Output: 69 // [228 184 150] 70 // 3 71 } 72 73 func ExampleEncodeRune_outOfRange() { 74 runes := []rune{ 75 // Less than 0, out of range. 76 -1, 77 // Greater than 0x10FFFF, out of range. 78 0x110000, 79 // The Unicode replacement character. 80 common.RuneError, 81 } 82 for i, c := range runes { 83 buf := make([]byte, 0, 3) 84 buf, size := utf8.EncodeRune(buf, c) 85 fmt.Printf("%d: %d %[2]s %d\n", i, buf, size) 86 } 87 // Output: 88 // 0: [239 191 189] � 3 89 // 1: [239 191 189] � 3 90 // 2: [239 191 189] � 3 91 } 92 93 func ExampleFullRune() { 94 str := "世" 95 fmt.Println(utf8.FullRune(str)) 96 fmt.Println(utf8.FullRune(str[:2])) 97 // Output: 98 // true 99 // false 100 } 101 102 func ExampleCount() { 103 str := "Hello, 世界" 104 fmt.Println("bytes =", len(str)) 105 fmt.Println("runes =", utf8.Count(str)) 106 // Output: 107 // bytes = 13 108 // runes = 9 109 } 110 111 func ExampleRuneLen() { 112 fmt.Println(utf8.RuneLen('a')) 113 fmt.Println(utf8.RuneLen('界')) 114 // Output: 115 // 1 116 // 3 117 } 118 119 func ExampleRuneStart() { 120 buf := []byte("a界") 121 fmt.Println(utf8.RuneStart(buf[0])) 122 fmt.Println(utf8.RuneStart(buf[1])) 123 fmt.Println(utf8.RuneStart(buf[2])) 124 // Output: 125 // true 126 // true 127 // false 128 } 129 130 func ExampleRuneValid() { 131 valid := 'a' 132 invalid := rune(0xfffffff) 133 134 fmt.Println(utf8.RuneValid(valid)) 135 fmt.Println(utf8.RuneValid(invalid)) 136 // Output: 137 // true 138 // false 139 } 140 141 func ExampleValid() { 142 valid := "Hello, 世界" 143 invalid := string([]byte{0xff, 0xfe, 0xfd}) 144 145 fmt.Println(utf8.Valid(valid)) 146 fmt.Println(utf8.Valid(invalid)) 147 // Output: 148 // true 149 // false 150 } 151 152 func ExampleAppendRunes() { 153 buf1 := utf8.AppendRunes(nil, 0x10000) 154 buf2 := utf8.AppendRunes([]byte("init"), 0x10000) 155 fmt.Println(string(buf1)) 156 fmt.Println(string(buf2)) 157 // Output: 158 // 𐀀 159 // init𐀀 160 }