github.com/lovishpuri/go-40569/src@v0.0.0-20230519171745-f8623e7c56cf/bytes/example_test.go (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 "strconv" 15 "unicode" 16 ) 17 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 func ExampleBuffer_reader() { 27 // A Buffer can turn a string or a []byte into an io.Reader. 28 buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==") 29 dec := base64.NewDecoder(base64.StdEncoding, buf) 30 io.Copy(os.Stdout, dec) 31 // Output: Gophers rule! 32 } 33 34 func ExampleBuffer_Bytes() { 35 buf := bytes.Buffer{} 36 buf.Write([]byte{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}) 37 os.Stdout.Write(buf.Bytes()) 38 // Output: hello world 39 } 40 41 func ExampleBuffer_AvailableBuffer() { 42 var buf bytes.Buffer 43 for i := 0; i < 4; i++ { 44 b := buf.AvailableBuffer() 45 b = strconv.AppendInt(b, int64(i), 10) 46 b = append(b, ' ') 47 buf.Write(b) 48 } 49 os.Stdout.Write(buf.Bytes()) 50 // Output: 0 1 2 3 51 } 52 53 func ExampleBuffer_Cap() { 54 buf1 := bytes.NewBuffer(make([]byte, 10)) 55 buf2 := bytes.NewBuffer(make([]byte, 0, 10)) 56 fmt.Println(buf1.Cap()) 57 fmt.Println(buf2.Cap()) 58 // Output: 59 // 10 60 // 10 61 } 62 63 func ExampleBuffer_Grow() { 64 var b bytes.Buffer 65 b.Grow(64) 66 bb := b.Bytes() 67 b.Write([]byte("64 bytes or fewer")) 68 fmt.Printf("%q", bb[:b.Len()]) 69 // Output: "64 bytes or fewer" 70 } 71 72 func ExampleBuffer_Len() { 73 var b bytes.Buffer 74 b.Grow(64) 75 b.Write([]byte("abcde")) 76 fmt.Printf("%d", b.Len()) 77 // Output: 5 78 } 79 80 func ExampleBuffer_Next() { 81 var b bytes.Buffer 82 b.Grow(64) 83 b.Write([]byte("abcde")) 84 fmt.Printf("%s\n", string(b.Next(2))) 85 fmt.Printf("%s\n", string(b.Next(2))) 86 fmt.Printf("%s", string(b.Next(2))) 87 // Output: 88 // ab 89 // cd 90 // e 91 } 92 93 func ExampleBuffer_Read() { 94 var b bytes.Buffer 95 b.Grow(64) 96 b.Write([]byte("abcde")) 97 rdbuf := make([]byte, 1) 98 n, err := b.Read(rdbuf) 99 if err != nil { 100 panic(err) 101 } 102 fmt.Println(n) 103 fmt.Println(b.String()) 104 fmt.Println(string(rdbuf)) 105 // Output 106 // 1 107 // bcde 108 // a 109 } 110 111 func ExampleBuffer_ReadByte() { 112 var b bytes.Buffer 113 b.Grow(64) 114 b.Write([]byte("abcde")) 115 c, err := b.ReadByte() 116 if err != nil { 117 panic(err) 118 } 119 fmt.Println(c) 120 fmt.Println(b.String()) 121 // Output 122 // 97 123 // bcde 124 } 125 126 func ExampleClone() { 127 b := []byte("abc") 128 clone := bytes.Clone(b) 129 fmt.Printf("%s\n", clone) 130 clone[0] = 'd' 131 fmt.Printf("%s\n", b) 132 fmt.Printf("%s\n", clone) 133 // Output: 134 // abc 135 // abc 136 // dbc 137 } 138 139 func ExampleCompare() { 140 // Interpret Compare's result by comparing it to zero. 141 var a, b []byte 142 if bytes.Compare(a, b) < 0 { 143 // a less b 144 } 145 if bytes.Compare(a, b) <= 0 { 146 // a less or equal b 147 } 148 if bytes.Compare(a, b) > 0 { 149 // a greater b 150 } 151 if bytes.Compare(a, b) >= 0 { 152 // a greater or equal b 153 } 154 155 // Prefer Equal to Compare for equality comparisons. 156 if bytes.Equal(a, b) { 157 // a equal b 158 } 159 if !bytes.Equal(a, b) { 160 // a not equal b 161 } 162 } 163 164 func ExampleCompare_search() { 165 // Binary search to find a matching byte slice. 166 var needle []byte 167 var haystack [][]byte // Assume sorted 168 i := sort.Search(len(haystack), func(i int) bool { 169 // Return haystack[i] >= needle. 170 return bytes.Compare(haystack[i], needle) >= 0 171 }) 172 if i < len(haystack) && bytes.Equal(haystack[i], needle) { 173 // Found it! 174 } 175 } 176 177 func ExampleContains() { 178 fmt.Println(bytes.Contains([]byte("seafood"), []byte("foo"))) 179 fmt.Println(bytes.Contains([]byte("seafood"), []byte("bar"))) 180 fmt.Println(bytes.Contains([]byte("seafood"), []byte(""))) 181 fmt.Println(bytes.Contains([]byte(""), []byte(""))) 182 // Output: 183 // true 184 // false 185 // true 186 // true 187 } 188 189 func ExampleContainsAny() { 190 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "fÄo!")) 191 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "去是伟大的.")) 192 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "")) 193 fmt.Println(bytes.ContainsAny([]byte(""), "")) 194 // Output: 195 // true 196 // true 197 // false 198 // false 199 } 200 201 func ExampleContainsRune() { 202 fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'f')) 203 fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'ö')) 204 fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '大')) 205 fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '!')) 206 fmt.Println(bytes.ContainsRune([]byte(""), '@')) 207 // Output: 208 // true 209 // false 210 // true 211 // true 212 // false 213 } 214 215 func ExampleCount() { 216 fmt.Println(bytes.Count([]byte("cheese"), []byte("e"))) 217 fmt.Println(bytes.Count([]byte("five"), []byte(""))) // before & after each rune 218 // Output: 219 // 3 220 // 5 221 } 222 223 func ExampleCut() { 224 show := func(s, sep string) { 225 before, after, found := bytes.Cut([]byte(s), []byte(sep)) 226 fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found) 227 } 228 show("Gopher", "Go") 229 show("Gopher", "ph") 230 show("Gopher", "er") 231 show("Gopher", "Badger") 232 // Output: 233 // Cut("Gopher", "Go") = "", "pher", true 234 // Cut("Gopher", "ph") = "Go", "er", true 235 // Cut("Gopher", "er") = "Goph", "", true 236 // Cut("Gopher", "Badger") = "Gopher", "", false 237 } 238 239 func ExampleCutPrefix() { 240 show := func(s, sep string) { 241 after, found := bytes.CutPrefix([]byte(s), []byte(sep)) 242 fmt.Printf("CutPrefix(%q, %q) = %q, %v\n", s, sep, after, found) 243 } 244 show("Gopher", "Go") 245 show("Gopher", "ph") 246 // Output: 247 // CutPrefix("Gopher", "Go") = "pher", true 248 // CutPrefix("Gopher", "ph") = "Gopher", false 249 } 250 251 func ExampleCutSuffix() { 252 show := func(s, sep string) { 253 before, found := bytes.CutSuffix([]byte(s), []byte(sep)) 254 fmt.Printf("CutSuffix(%q, %q) = %q, %v\n", s, sep, before, found) 255 } 256 show("Gopher", "Go") 257 show("Gopher", "er") 258 // Output: 259 // CutSuffix("Gopher", "Go") = "Gopher", false 260 // CutSuffix("Gopher", "er") = "Goph", true 261 } 262 263 func ExampleEqual() { 264 fmt.Println(bytes.Equal([]byte("Go"), []byte("Go"))) 265 fmt.Println(bytes.Equal([]byte("Go"), []byte("C++"))) 266 // Output: 267 // true 268 // false 269 } 270 271 func ExampleEqualFold() { 272 fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go"))) 273 // Output: true 274 } 275 276 func ExampleFields() { 277 fmt.Printf("Fields are: %q", bytes.Fields([]byte(" foo bar baz "))) 278 // Output: Fields are: ["foo" "bar" "baz"] 279 } 280 281 func ExampleFieldsFunc() { 282 f := func(c rune) bool { 283 return !unicode.IsLetter(c) && !unicode.IsNumber(c) 284 } 285 fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte(" foo1;bar2,baz3..."), f)) 286 // Output: Fields are: ["foo1" "bar2" "baz3"] 287 } 288 289 func ExampleHasPrefix() { 290 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go"))) 291 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("C"))) 292 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte(""))) 293 // Output: 294 // true 295 // false 296 // true 297 } 298 299 func ExampleHasSuffix() { 300 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go"))) 301 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O"))) 302 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami"))) 303 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte(""))) 304 // Output: 305 // true 306 // false 307 // false 308 // true 309 } 310 311 func ExampleIndex() { 312 fmt.Println(bytes.Index([]byte("chicken"), []byte("ken"))) 313 fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr"))) 314 // Output: 315 // 4 316 // -1 317 } 318 319 func ExampleIndexByte() { 320 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('k'))) 321 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('g'))) 322 // Output: 323 // 4 324 // -1 325 } 326 327 func ExampleIndexFunc() { 328 f := func(c rune) bool { 329 return unicode.Is(unicode.Han, c) 330 } 331 fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f)) 332 fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f)) 333 // Output: 334 // 7 335 // -1 336 } 337 338 func ExampleIndexAny() { 339 fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy")) 340 fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy")) 341 // Output: 342 // 2 343 // -1 344 } 345 346 func ExampleIndexRune() { 347 fmt.Println(bytes.IndexRune([]byte("chicken"), 'k')) 348 fmt.Println(bytes.IndexRune([]byte("chicken"), 'd')) 349 // Output: 350 // 4 351 // -1 352 } 353 354 func ExampleJoin() { 355 s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")} 356 fmt.Printf("%s", bytes.Join(s, []byte(", "))) 357 // Output: foo, bar, baz 358 } 359 360 func ExampleLastIndex() { 361 fmt.Println(bytes.Index([]byte("go gopher"), []byte("go"))) 362 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go"))) 363 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent"))) 364 // Output: 365 // 0 366 // 3 367 // -1 368 } 369 370 func ExampleLastIndexAny() { 371 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "MüQp")) 372 fmt.Println(bytes.LastIndexAny([]byte("go 地鼠"), "地大")) 373 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "z,!.")) 374 // Output: 375 // 5 376 // 3 377 // -1 378 } 379 380 func ExampleLastIndexByte() { 381 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('g'))) 382 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('r'))) 383 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('z'))) 384 // Output: 385 // 3 386 // 8 387 // -1 388 } 389 390 func ExampleLastIndexFunc() { 391 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsLetter)) 392 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsPunct)) 393 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsNumber)) 394 // Output: 395 // 8 396 // 9 397 // -1 398 } 399 400 func ExampleMap() { 401 rot13 := func(r rune) rune { 402 switch { 403 case r >= 'A' && r <= 'Z': 404 return 'A' + (r-'A'+13)%26 405 case r >= 'a' && r <= 'z': 406 return 'a' + (r-'a'+13)%26 407 } 408 return r 409 } 410 fmt.Printf("%s\n", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher..."))) 411 // Output: 412 // 'Gjnf oevyyvt naq gur fyvgul tbcure... 413 } 414 415 func ExampleReader_Len() { 416 fmt.Println(bytes.NewReader([]byte("Hi!")).Len()) 417 fmt.Println(bytes.NewReader([]byte("こんにちは!")).Len()) 418 // Output: 419 // 3 420 // 16 421 } 422 423 func ExampleRepeat() { 424 fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2)) 425 // Output: banana 426 } 427 428 func ExampleReplace() { 429 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2)) 430 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1)) 431 // Output: 432 // oinky oinky oink 433 // moo moo moo 434 } 435 436 func ExampleReplaceAll() { 437 fmt.Printf("%s\n", bytes.ReplaceAll([]byte("oink oink oink"), []byte("oink"), []byte("moo"))) 438 // Output: 439 // moo moo moo 440 } 441 442 func ExampleRunes() { 443 rs := bytes.Runes([]byte("go gopher")) 444 for _, r := range rs { 445 fmt.Printf("%#U\n", r) 446 } 447 // Output: 448 // U+0067 'g' 449 // U+006F 'o' 450 // U+0020 ' ' 451 // U+0067 'g' 452 // U+006F 'o' 453 // U+0070 'p' 454 // U+0068 'h' 455 // U+0065 'e' 456 // U+0072 'r' 457 } 458 459 func ExampleSplit() { 460 fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(","))) 461 fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a "))) 462 fmt.Printf("%q\n", bytes.Split([]byte(" xyz "), []byte(""))) 463 fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins"))) 464 // Output: 465 // ["a" "b" "c"] 466 // ["" "man " "plan " "canal panama"] 467 // [" " "x" "y" "z" " "] 468 // [""] 469 } 470 471 func ExampleSplitN() { 472 fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2)) 473 z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0) 474 fmt.Printf("%q (nil = %v)\n", z, z == nil) 475 // Output: 476 // ["a" "b,c"] 477 // [] (nil = true) 478 } 479 480 func ExampleSplitAfter() { 481 fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(","))) 482 // Output: ["a," "b," "c"] 483 } 484 485 func ExampleSplitAfterN() { 486 fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2)) 487 // Output: ["a," "b,c"] 488 } 489 490 func ExampleTitle() { 491 fmt.Printf("%s", bytes.Title([]byte("her royal highness"))) 492 // Output: Her Royal Highness 493 } 494 495 func ExampleToTitle() { 496 fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises"))) 497 fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб"))) 498 // Output: 499 // LOUD NOISES 500 // ХЛЕБ 501 } 502 503 func ExampleToTitleSpecial() { 504 str := []byte("ahoj vývojári golang") 505 totitle := bytes.ToTitleSpecial(unicode.AzeriCase, str) 506 fmt.Println("Original : " + string(str)) 507 fmt.Println("ToTitle : " + string(totitle)) 508 // Output: 509 // Original : ahoj vývojári golang 510 // ToTitle : AHOJ VÝVOJÁRİ GOLANG 511 } 512 513 func ExampleToValidUTF8() { 514 fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("abc"), []byte("\uFFFD"))) 515 fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("a\xffb\xC0\xAFc\xff"), []byte(""))) 516 fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("\xed\xa0\x80"), []byte("abc"))) 517 // Output: 518 // abc 519 // abc 520 // abc 521 } 522 523 func ExampleTrim() { 524 fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! ")) 525 // Output: ["Achtung! Achtung"] 526 } 527 528 func ExampleTrimFunc() { 529 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsLetter))) 530 fmt.Println(string(bytes.TrimFunc([]byte("\"go-gopher!\""), unicode.IsLetter))) 531 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsPunct))) 532 fmt.Println(string(bytes.TrimFunc([]byte("1234go-gopher!567"), unicode.IsNumber))) 533 // Output: 534 // -gopher! 535 // "go-gopher!" 536 // go-gopher 537 // go-gopher! 538 } 539 540 func ExampleTrimLeft() { 541 fmt.Print(string(bytes.TrimLeft([]byte("453gopher8257"), "0123456789"))) 542 // Output: 543 // gopher8257 544 } 545 546 func ExampleTrimLeftFunc() { 547 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher"), unicode.IsLetter))) 548 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher!"), unicode.IsPunct))) 549 fmt.Println(string(bytes.TrimLeftFunc([]byte("1234go-gopher!567"), unicode.IsNumber))) 550 // Output: 551 // -gopher 552 // go-gopher! 553 // go-gopher!567 554 } 555 556 func ExampleTrimPrefix() { 557 var b = []byte("Goodbye,, world!") 558 b = bytes.TrimPrefix(b, []byte("Goodbye,")) 559 b = bytes.TrimPrefix(b, []byte("See ya,")) 560 fmt.Printf("Hello%s", b) 561 // Output: Hello, world! 562 } 563 564 func ExampleTrimSpace() { 565 fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n"))) 566 // Output: a lone gopher 567 } 568 569 func ExampleTrimSuffix() { 570 var b = []byte("Hello, goodbye, etc!") 571 b = bytes.TrimSuffix(b, []byte("goodbye, etc!")) 572 b = bytes.TrimSuffix(b, []byte("gopher")) 573 b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...) 574 os.Stdout.Write(b) 575 // Output: Hello, world! 576 } 577 578 func ExampleTrimRight() { 579 fmt.Print(string(bytes.TrimRight([]byte("453gopher8257"), "0123456789"))) 580 // Output: 581 // 453gopher 582 } 583 584 func ExampleTrimRightFunc() { 585 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher"), unicode.IsLetter))) 586 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher!"), unicode.IsPunct))) 587 fmt.Println(string(bytes.TrimRightFunc([]byte("1234go-gopher!567"), unicode.IsNumber))) 588 // Output: 589 // go- 590 // go-gopher 591 // 1234go-gopher! 592 } 593 594 func ExampleToLower() { 595 fmt.Printf("%s", bytes.ToLower([]byte("Gopher"))) 596 // Output: gopher 597 } 598 599 func ExampleToLowerSpecial() { 600 str := []byte("AHOJ VÝVOJÁRİ GOLANG") 601 totitle := bytes.ToLowerSpecial(unicode.AzeriCase, str) 602 fmt.Println("Original : " + string(str)) 603 fmt.Println("ToLower : " + string(totitle)) 604 // Output: 605 // Original : AHOJ VÝVOJÁRİ GOLANG 606 // ToLower : ahoj vývojári golang 607 } 608 609 func ExampleToUpper() { 610 fmt.Printf("%s", bytes.ToUpper([]byte("Gopher"))) 611 // Output: GOPHER 612 } 613 614 func ExampleToUpperSpecial() { 615 str := []byte("ahoj vývojári golang") 616 totitle := bytes.ToUpperSpecial(unicode.AzeriCase, str) 617 fmt.Println("Original : " + string(str)) 618 fmt.Println("ToUpper : " + string(totitle)) 619 // Output: 620 // Original : ahoj vývojári golang 621 // ToUpper : AHOJ VÝVOJÁRİ GOLANG 622 }