github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/bytes/example_test.gno (about) 1 // Copyright 2011 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 bytes_test 6 7 import ( 8 "bytes" 9 "encoding/base64" 10 "fmt" 11 "io" 12 "os" 13 "sort" 14 "unicode" 15 ) 16 17 /* XXX native fmt.Fprintf cannot take gno io.Writer. 18 func ExampleBuffer() { 19 var b bytes.Buffer // A Buffer needs no initialization. 20 b.Write([]byte("Hello ")) 21 fmt.Fprintf(&b, "world!") 22 b.WriteTo(os.Stdout) 23 // Output: Hello world! 24 } 25 */ 26 27 func ExampleBuffer_reader() { 28 // A Buffer can turn a string or a []byte into an io.Reader. 29 buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==") 30 dec := base64.NewDecoder(base64.StdEncoding, buf) 31 io.Copy(os.Stdout, dec) 32 // Output: Gophers rule! 33 } 34 35 func ExampleBuffer_Bytes() { 36 buf := bytes.Buffer{} 37 buf.Write([]byte{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}) 38 os.Stdout.Write(buf.Bytes()) 39 // Output: hello world 40 } 41 42 func ExampleBuffer_Grow() { 43 var b bytes.Buffer 44 b.Grow(64) 45 bb := b.Bytes() 46 b.Write([]byte("64 bytes or fewer")) 47 fmt.Printf("%q", bb[:b.Len()]) 48 // Output: "64 bytes or fewer" 49 } 50 51 func ExampleBuffer_Len() { 52 var b bytes.Buffer 53 b.Grow(64) 54 b.Write([]byte("abcde")) 55 fmt.Printf("%d", b.Len()) 56 // Output: 5 57 } 58 59 func ExampleCompare() { 60 // Interpret Compare's result by comparing it to zero. 61 var a, b []byte 62 if bytes.Compare(a, b) < 0 { 63 // a less b 64 } 65 if bytes.Compare(a, b) <= 0 { 66 // a less or equal b 67 } 68 if bytes.Compare(a, b) > 0 { 69 // a greater b 70 } 71 if bytes.Compare(a, b) >= 0 { 72 // a greater or equal b 73 } 74 75 // Prefer Equal to Compare for equality comparisons. 76 if bytes.Equal(a, b) { 77 // a equal b 78 } 79 if !bytes.Equal(a, b) { 80 // a not equal b 81 } 82 } 83 84 func ExampleCompare_search() { 85 // Binary search to find a matching byte slice. 86 var needle []byte 87 var haystack [][]byte // Assume sorted 88 i := sort.Search(len(haystack), func(i int) bool { 89 // Return haystack[i] >= needle. 90 return bytes.Compare(haystack[i], needle) >= 0 91 }) 92 if i < len(haystack) && bytes.Equal(haystack[i], needle) { 93 // Found it! 94 } 95 } 96 97 func ExampleTrimSuffix() { 98 b := []byte("Hello, goodbye, etc!") 99 b = bytes.TrimSuffix(b, []byte("goodbye, etc!")) 100 b = bytes.TrimSuffix(b, []byte("gopher")) 101 b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...) 102 os.Stdout.Write(b) 103 // Output: Hello, world! 104 } 105 106 func ExampleTrimPrefix() { 107 b := []byte("Goodbye,, world!") 108 b = bytes.TrimPrefix(b, []byte("Goodbye,")) 109 b = bytes.TrimPrefix(b, []byte("See ya,")) 110 fmt.Printf("Hello%s", b) 111 // Output: Hello, world! 112 } 113 114 func ExampleFields() { 115 fmt.Printf("Fields are: %q", bytes.Fields([]byte(" foo bar baz "))) 116 // Output: Fields are: ["foo" "bar" "baz"] 117 } 118 119 func ExampleFieldsFunc() { 120 f := func(c rune) bool { 121 return !unicode.IsLetter(c) && !unicode.IsNumber(c) 122 } 123 fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte(" foo1;bar2,baz3..."), f)) 124 // Output: Fields are: ["foo1" "bar2" "baz3"] 125 } 126 127 func ExampleContains() { 128 fmt.Println(bytes.Contains([]byte("seafood"), []byte("foo"))) 129 fmt.Println(bytes.Contains([]byte("seafood"), []byte("bar"))) 130 fmt.Println(bytes.Contains([]byte("seafood"), []byte(""))) 131 fmt.Println(bytes.Contains([]byte(""), []byte(""))) 132 // Output: 133 // true 134 // false 135 // true 136 // true 137 } 138 139 func ExampleContainsAny() { 140 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "fÄo!")) 141 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "去是伟大的.")) 142 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "")) 143 fmt.Println(bytes.ContainsAny([]byte(""), "")) 144 // Output: 145 // true 146 // true 147 // false 148 // false 149 } 150 151 func ExampleContainsRune() { 152 fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'f')) 153 fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'ö')) 154 fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '大')) 155 fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '!')) 156 fmt.Println(bytes.ContainsRune([]byte(""), '@')) 157 // Output: 158 // true 159 // false 160 // true 161 // true 162 // false 163 } 164 165 func ExampleCount() { 166 fmt.Println(bytes.Count([]byte("cheese"), []byte("e"))) 167 fmt.Println(bytes.Count([]byte("five"), []byte(""))) // before & after each rune 168 // Output: 169 // 3 170 // 5 171 } 172 173 func ExampleEqual() { 174 fmt.Println(bytes.Equal([]byte("Go"), []byte("Go"))) 175 fmt.Println(bytes.Equal([]byte("Go"), []byte("C++"))) 176 // Output: 177 // true 178 // false 179 } 180 181 func ExampleEqualFold() { 182 fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go"))) 183 // Output: true 184 } 185 186 func ExampleHasPrefix() { 187 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go"))) 188 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("C"))) 189 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte(""))) 190 // Output: 191 // true 192 // false 193 // true 194 } 195 196 func ExampleHasSuffix() { 197 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go"))) 198 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O"))) 199 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami"))) 200 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte(""))) 201 // Output: 202 // true 203 // false 204 // false 205 // true 206 } 207 208 func ExampleIndex() { 209 fmt.Println(bytes.Index([]byte("chicken"), []byte("ken"))) 210 fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr"))) 211 // Output: 212 // 4 213 // -1 214 } 215 216 func ExampleIndexByte() { 217 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('k'))) 218 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('g'))) 219 // Output: 220 // 4 221 // -1 222 } 223 224 func ExampleIndexFunc() { 225 f := func(c rune) bool { 226 return unicode.Is(unicode.Han, c) 227 } 228 fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f)) 229 fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f)) 230 // Output: 231 // 7 232 // -1 233 } 234 235 func ExampleIndexAny() { 236 fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy")) 237 fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy")) 238 // Output: 239 // 2 240 // -1 241 } 242 243 func ExampleIndexRune() { 244 fmt.Println(bytes.IndexRune([]byte("chicken"), 'k')) 245 fmt.Println(bytes.IndexRune([]byte("chicken"), 'd')) 246 // Output: 247 // 4 248 // -1 249 } 250 251 func ExampleLastIndex() { 252 fmt.Println(bytes.Index([]byte("go gopher"), []byte("go"))) 253 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go"))) 254 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent"))) 255 // Output: 256 // 0 257 // 3 258 // -1 259 } 260 261 func ExampleLastIndexAny() { 262 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "MüQp")) 263 fmt.Println(bytes.LastIndexAny([]byte("go 地鼠"), "地大")) 264 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "z,!.")) 265 // Output: 266 // 5 267 // 3 268 // -1 269 } 270 271 func ExampleLastIndexByte() { 272 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('g'))) 273 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('r'))) 274 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('z'))) 275 // Output: 276 // 3 277 // 8 278 // -1 279 } 280 281 func ExampleLastIndexFunc() { 282 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsLetter)) 283 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsPunct)) 284 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsNumber)) 285 // Output: 286 // 8 287 // 9 288 // -1 289 } 290 291 func ExampleJoin() { 292 s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")} 293 fmt.Printf("%s", bytes.Join(s, []byte(", "))) 294 // Output: foo, bar, baz 295 } 296 297 func ExampleRepeat() { 298 fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2)) 299 // Output: banana 300 } 301 302 func ExampleReplace() { 303 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2)) 304 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1)) 305 // Output: 306 // oinky oinky oink 307 // moo moo moo 308 } 309 310 func ExampleReplaceAll() { 311 fmt.Printf("%s\n", bytes.ReplaceAll([]byte("oink oink oink"), []byte("oink"), []byte("moo"))) 312 // Output: 313 // moo moo moo 314 } 315 316 func ExampleRunes() { 317 rs := bytes.Runes([]byte("go gopher")) 318 for _, r := range rs { 319 fmt.Printf("%#U\n", r) 320 } 321 // Output: 322 // U+0067 'g' 323 // U+006F 'o' 324 // U+0020 ' ' 325 // U+0067 'g' 326 // U+006F 'o' 327 // U+0070 'p' 328 // U+0068 'h' 329 // U+0065 'e' 330 // U+0072 'r' 331 } 332 333 func ExampleSplit() { 334 fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(","))) 335 fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a "))) 336 fmt.Printf("%q\n", bytes.Split([]byte(" xyz "), []byte(""))) 337 fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins"))) 338 // Output: 339 // ["a" "b" "c"] 340 // ["" "man " "plan " "canal panama"] 341 // [" " "x" "y" "z" " "] 342 // [""] 343 } 344 345 func ExampleSplitN() { 346 fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2)) 347 z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0) 348 fmt.Printf("%q (nil = %v)\n", z, z == nil) 349 // Output: 350 // ["a" "b,c"] 351 // [] (nil = true) 352 } 353 354 func ExampleSplitAfter() { 355 fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(","))) 356 // Output: ["a," "b," "c"] 357 } 358 359 func ExampleSplitAfterN() { 360 fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2)) 361 // Output: ["a," "b,c"] 362 } 363 364 func ExampleTitle() { 365 fmt.Printf("%s", bytes.Title([]byte("her royal highness"))) 366 // Output: Her Royal Highness 367 } 368 369 func ExampleToTitle() { 370 fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises"))) 371 fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб"))) 372 // Output: 373 // LOUD NOISES 374 // ХЛЕБ 375 } 376 377 func ExampleToTitleSpecial() { 378 str := []byte("ahoj vývojári golang") 379 totitle := bytes.ToTitleSpecial(unicode.AzeriCase, str) 380 fmt.Println("Original : " + string(str)) 381 fmt.Println("ToTitle : " + string(totitle)) 382 // Output: 383 // Original : ahoj vývojári golang 384 // ToTitle : AHOJ VÝVOJÁRİ GOLANG 385 } 386 387 func ExampleTrim() { 388 fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! ")) 389 // Output: ["Achtung! Achtung"] 390 } 391 392 func ExampleTrimFunc() { 393 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsLetter))) 394 fmt.Println(string(bytes.TrimFunc([]byte("\"go-gopher!\""), unicode.IsLetter))) 395 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsPunct))) 396 fmt.Println(string(bytes.TrimFunc([]byte("1234go-gopher!567"), unicode.IsNumber))) 397 // Output: 398 // -gopher! 399 // "go-gopher!" 400 // go-gopher 401 // go-gopher! 402 } 403 404 func ExampleMap() { 405 rot13 := func(r rune) rune { 406 switch { 407 case r >= 'A' && r <= 'Z': 408 return 'A' + (r-'A'+13)%26 409 case r >= 'a' && r <= 'z': 410 return 'a' + (r-'a'+13)%26 411 } 412 return r 413 } 414 fmt.Printf("%s", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher..."))) 415 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure... 416 } 417 418 func ExampleTrimLeft() { 419 fmt.Print(string(bytes.TrimLeft([]byte("453gopher8257"), "0123456789"))) 420 // Output: 421 // gopher8257 422 } 423 424 func ExampleTrimLeftFunc() { 425 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher"), unicode.IsLetter))) 426 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher!"), unicode.IsPunct))) 427 fmt.Println(string(bytes.TrimLeftFunc([]byte("1234go-gopher!567"), unicode.IsNumber))) 428 // Output: 429 // -gopher 430 // go-gopher! 431 // go-gopher!567 432 } 433 434 func ExampleTrimSpace() { 435 fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n"))) 436 // Output: a lone gopher 437 } 438 439 func ExampleTrimRight() { 440 fmt.Print(string(bytes.TrimRight([]byte("453gopher8257"), "0123456789"))) 441 // Output: 442 // 453gopher 443 } 444 445 func ExampleTrimRightFunc() { 446 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher"), unicode.IsLetter))) 447 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher!"), unicode.IsPunct))) 448 fmt.Println(string(bytes.TrimRightFunc([]byte("1234go-gopher!567"), unicode.IsNumber))) 449 // Output: 450 // go- 451 // go-gopher 452 // 1234go-gopher! 453 } 454 455 func ExampleToUpper() { 456 fmt.Printf("%s", bytes.ToUpper([]byte("Gopher"))) 457 // Output: GOPHER 458 } 459 460 func ExampleToUpperSpecial() { 461 str := []byte("ahoj vývojári golang") 462 totitle := bytes.ToUpperSpecial(unicode.AzeriCase, str) 463 fmt.Println("Original : " + string(str)) 464 fmt.Println("ToUpper : " + string(totitle)) 465 // Output: 466 // Original : ahoj vývojári golang 467 // ToUpper : AHOJ VÝVOJÁRİ GOLANG 468 } 469 470 func ExampleToLower() { 471 fmt.Printf("%s", bytes.ToLower([]byte("Gopher"))) 472 // Output: gopher 473 } 474 475 func ExampleToLowerSpecial() { 476 str := []byte("AHOJ VÝVOJÁRİ GOLANG") 477 totitle := bytes.ToLowerSpecial(unicode.AzeriCase, str) 478 fmt.Println("Original : " + string(str)) 479 fmt.Println("ToLower : " + string(totitle)) 480 // Output: 481 // Original : AHOJ VÝVOJÁRİ GOLANG 482 // ToLower : ahoj vývojári golang 483 } 484 485 func ExampleReader_Len() { 486 fmt.Println(bytes.NewReader([]byte("Hi!")).Len()) 487 fmt.Println(bytes.NewReader([]byte("こんにちは!")).Len()) 488 // Output: 489 // 3 490 // 16 491 }