wide-int: Fix up mul_internal overflow checking [PR116224]
[official-gcc.git] / libgo / go / bytes / example_test.go
blob54a7aa6ae6c04960ce88f6b0d9cabbc01b46acbe
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.
5 package bytes_test
7 import (
8 "bytes"
9 "encoding/base64"
10 "fmt"
11 "io"
12 "os"
13 "sort"
14 "unicode"
17 func ExampleBuffer() {
18 var b bytes.Buffer // A Buffer needs no initialization.
19 b.Write([]byte("Hello "))
20 fmt.Fprintf(&b, "world!")
21 b.WriteTo(os.Stdout)
22 // Output: Hello world!
25 func ExampleBuffer_reader() {
26 // A Buffer can turn a string or a []byte into an io.Reader.
27 buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==")
28 dec := base64.NewDecoder(base64.StdEncoding, buf)
29 io.Copy(os.Stdout, dec)
30 // Output: Gophers rule!
33 func ExampleBuffer_Bytes() {
34 buf := bytes.Buffer{}
35 buf.Write([]byte{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'})
36 os.Stdout.Write(buf.Bytes())
37 // Output: hello world
40 func ExampleBuffer_Cap() {
41 buf1 := bytes.NewBuffer(make([]byte, 10))
42 buf2 := bytes.NewBuffer(make([]byte, 0, 10))
43 fmt.Println(buf1.Cap())
44 fmt.Println(buf2.Cap())
45 // Output:
46 // 10
47 // 10
50 func ExampleBuffer_Grow() {
51 var b bytes.Buffer
52 b.Grow(64)
53 bb := b.Bytes()
54 b.Write([]byte("64 bytes or fewer"))
55 fmt.Printf("%q", bb[:b.Len()])
56 // Output: "64 bytes or fewer"
59 func ExampleBuffer_Len() {
60 var b bytes.Buffer
61 b.Grow(64)
62 b.Write([]byte("abcde"))
63 fmt.Printf("%d", b.Len())
64 // Output: 5
67 func ExampleBuffer_Next() {
68 var b bytes.Buffer
69 b.Grow(64)
70 b.Write([]byte("abcde"))
71 fmt.Printf("%s\n", string(b.Next(2)))
72 fmt.Printf("%s\n", string(b.Next(2)))
73 fmt.Printf("%s", string(b.Next(2)))
74 // Output:
75 // ab
76 // cd
77 // e
80 func ExampleBuffer_Read() {
81 var b bytes.Buffer
82 b.Grow(64)
83 b.Write([]byte("abcde"))
84 rdbuf := make([]byte, 1)
85 n, err := b.Read(rdbuf)
86 if err != nil {
87 panic(err)
89 fmt.Println(n)
90 fmt.Println(b.String())
91 fmt.Println(string(rdbuf))
92 // Output
93 // 1
94 // bcde
95 // a
98 func ExampleBuffer_ReadByte() {
99 var b bytes.Buffer
100 b.Grow(64)
101 b.Write([]byte("abcde"))
102 c, err := b.ReadByte()
103 if err != nil {
104 panic(err)
106 fmt.Println(c)
107 fmt.Println(b.String())
108 // Output
109 // 97
110 // bcde
113 func ExampleCompare() {
114 // Interpret Compare's result by comparing it to zero.
115 var a, b []byte
116 if bytes.Compare(a, b) < 0 {
117 // a less b
119 if bytes.Compare(a, b) <= 0 {
120 // a less or equal b
122 if bytes.Compare(a, b) > 0 {
123 // a greater b
125 if bytes.Compare(a, b) >= 0 {
126 // a greater or equal b
129 // Prefer Equal to Compare for equality comparisons.
130 if bytes.Equal(a, b) {
131 // a equal b
133 if !bytes.Equal(a, b) {
134 // a not equal b
138 func ExampleCompare_search() {
139 // Binary search to find a matching byte slice.
140 var needle []byte
141 var haystack [][]byte // Assume sorted
142 i := sort.Search(len(haystack), func(i int) bool {
143 // Return haystack[i] >= needle.
144 return bytes.Compare(haystack[i], needle) >= 0
146 if i < len(haystack) && bytes.Equal(haystack[i], needle) {
147 // Found it!
151 func ExampleContains() {
152 fmt.Println(bytes.Contains([]byte("seafood"), []byte("foo")))
153 fmt.Println(bytes.Contains([]byte("seafood"), []byte("bar")))
154 fmt.Println(bytes.Contains([]byte("seafood"), []byte("")))
155 fmt.Println(bytes.Contains([]byte(""), []byte("")))
156 // Output:
157 // true
158 // false
159 // true
160 // true
163 func ExampleContainsAny() {
164 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "fÄo!"))
165 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "去是伟大的."))
166 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), ""))
167 fmt.Println(bytes.ContainsAny([]byte(""), ""))
168 // Output:
169 // true
170 // true
171 // false
172 // false
175 func ExampleContainsRune() {
176 fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'f'))
177 fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'ö'))
178 fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '大'))
179 fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '!'))
180 fmt.Println(bytes.ContainsRune([]byte(""), '@'))
181 // Output:
182 // true
183 // false
184 // true
185 // true
186 // false
189 func ExampleCount() {
190 fmt.Println(bytes.Count([]byte("cheese"), []byte("e")))
191 fmt.Println(bytes.Count([]byte("five"), []byte(""))) // before & after each rune
192 // Output:
193 // 3
194 // 5
197 func ExampleCut() {
198 show := func(s, sep string) {
199 before, after, found := bytes.Cut([]byte(s), []byte(sep))
200 fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found)
202 show("Gopher", "Go")
203 show("Gopher", "ph")
204 show("Gopher", "er")
205 show("Gopher", "Badger")
206 // Output:
207 // Cut("Gopher", "Go") = "", "pher", true
208 // Cut("Gopher", "ph") = "Go", "er", true
209 // Cut("Gopher", "er") = "Goph", "", true
210 // Cut("Gopher", "Badger") = "Gopher", "", false
213 func ExampleEqual() {
214 fmt.Println(bytes.Equal([]byte("Go"), []byte("Go")))
215 fmt.Println(bytes.Equal([]byte("Go"), []byte("C++")))
216 // Output:
217 // true
218 // false
221 func ExampleEqualFold() {
222 fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))
223 // Output: true
226 func ExampleFields() {
227 fmt.Printf("Fields are: %q", bytes.Fields([]byte(" foo bar baz ")))
228 // Output: Fields are: ["foo" "bar" "baz"]
231 func ExampleFieldsFunc() {
232 f := func(c rune) bool {
233 return !unicode.IsLetter(c) && !unicode.IsNumber(c)
235 fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte(" foo1;bar2,baz3..."), f))
236 // Output: Fields are: ["foo1" "bar2" "baz3"]
239 func ExampleHasPrefix() {
240 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go")))
241 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("C")))
242 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("")))
243 // Output:
244 // true
245 // false
246 // true
249 func ExampleHasSuffix() {
250 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go")))
251 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O")))
252 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami")))
253 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("")))
254 // Output:
255 // true
256 // false
257 // false
258 // true
261 func ExampleIndex() {
262 fmt.Println(bytes.Index([]byte("chicken"), []byte("ken")))
263 fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr")))
264 // Output:
265 // 4
266 // -1
269 func ExampleIndexByte() {
270 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('k')))
271 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('g')))
272 // Output:
273 // 4
274 // -1
277 func ExampleIndexFunc() {
278 f := func(c rune) bool {
279 return unicode.Is(unicode.Han, c)
281 fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f))
282 fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f))
283 // Output:
284 // 7
285 // -1
288 func ExampleIndexAny() {
289 fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy"))
290 fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy"))
291 // Output:
292 // 2
293 // -1
296 func ExampleIndexRune() {
297 fmt.Println(bytes.IndexRune([]byte("chicken"), 'k'))
298 fmt.Println(bytes.IndexRune([]byte("chicken"), 'd'))
299 // Output:
300 // 4
301 // -1
304 func ExampleJoin() {
305 s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}
306 fmt.Printf("%s", bytes.Join(s, []byte(", ")))
307 // Output: foo, bar, baz
310 func ExampleLastIndex() {
311 fmt.Println(bytes.Index([]byte("go gopher"), []byte("go")))
312 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go")))
313 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent")))
314 // Output:
315 // 0
316 // 3
317 // -1
320 func ExampleLastIndexAny() {
321 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "MüQp"))
322 fmt.Println(bytes.LastIndexAny([]byte("go 地鼠"), "地大"))
323 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "z,!."))
324 // Output:
325 // 5
326 // 3
327 // -1
330 func ExampleLastIndexByte() {
331 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('g')))
332 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('r')))
333 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('z')))
334 // Output:
335 // 3
336 // 8
337 // -1
340 func ExampleLastIndexFunc() {
341 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsLetter))
342 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsPunct))
343 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsNumber))
344 // Output:
345 // 8
346 // 9
347 // -1
350 func ExampleReader_Len() {
351 fmt.Println(bytes.NewReader([]byte("Hi!")).Len())
352 fmt.Println(bytes.NewReader([]byte("こんにちは!")).Len())
353 // Output:
354 // 3
355 // 16
358 func ExampleRepeat() {
359 fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2))
360 // Output: banana
363 func ExampleReplace() {
364 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2))
365 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1))
366 // Output:
367 // oinky oinky oink
368 // moo moo moo
371 func ExampleReplaceAll() {
372 fmt.Printf("%s\n", bytes.ReplaceAll([]byte("oink oink oink"), []byte("oink"), []byte("moo")))
373 // Output:
374 // moo moo moo
377 func ExampleRunes() {
378 rs := bytes.Runes([]byte("go gopher"))
379 for _, r := range rs {
380 fmt.Printf("%#U\n", r)
382 // Output:
383 // U+0067 'g'
384 // U+006F 'o'
385 // U+0020 ' '
386 // U+0067 'g'
387 // U+006F 'o'
388 // U+0070 'p'
389 // U+0068 'h'
390 // U+0065 'e'
391 // U+0072 'r'
394 func ExampleSplit() {
395 fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(",")))
396 fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a ")))
397 fmt.Printf("%q\n", bytes.Split([]byte(" xyz "), []byte("")))
398 fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins")))
399 // Output:
400 // ["a" "b" "c"]
401 // ["" "man " "plan " "canal panama"]
402 // [" " "x" "y" "z" " "]
403 // [""]
406 func ExampleSplitN() {
407 fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2))
408 z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0)
409 fmt.Printf("%q (nil = %v)\n", z, z == nil)
410 // Output:
411 // ["a" "b,c"]
412 // [] (nil = true)
415 func ExampleSplitAfter() {
416 fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(",")))
417 // Output: ["a," "b," "c"]
420 func ExampleSplitAfterN() {
421 fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2))
422 // Output: ["a," "b,c"]
425 func ExampleTitle() {
426 fmt.Printf("%s", bytes.Title([]byte("her royal highness")))
427 // Output: Her Royal Highness
430 func ExampleToTitle() {
431 fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises")))
432 fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб")))
433 // Output:
434 // LOUD NOISES
435 // ХЛЕБ
438 func ExampleToTitleSpecial() {
439 str := []byte("ahoj vývojári golang")
440 totitle := bytes.ToTitleSpecial(unicode.AzeriCase, str)
441 fmt.Println("Original : " + string(str))
442 fmt.Println("ToTitle : " + string(totitle))
443 // Output:
444 // Original : ahoj vývojári golang
445 // ToTitle : AHOJ VÝVOJÁRİ GOLANG
448 func ExampleTrim() {
449 fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! "))
450 // Output: ["Achtung! Achtung"]
453 func ExampleTrimFunc() {
454 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsLetter)))
455 fmt.Println(string(bytes.TrimFunc([]byte("\"go-gopher!\""), unicode.IsLetter)))
456 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsPunct)))
457 fmt.Println(string(bytes.TrimFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
458 // Output:
459 // -gopher!
460 // "go-gopher!"
461 // go-gopher
462 // go-gopher!
465 func ExampleTrimLeft() {
466 fmt.Print(string(bytes.TrimLeft([]byte("453gopher8257"), "0123456789")))
467 // Output:
468 // gopher8257
471 func ExampleTrimLeftFunc() {
472 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher"), unicode.IsLetter)))
473 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher!"), unicode.IsPunct)))
474 fmt.Println(string(bytes.TrimLeftFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
475 // Output:
476 // -gopher
477 // go-gopher!
478 // go-gopher!567
481 func ExampleTrimPrefix() {
482 var b = []byte("Goodbye,, world!")
483 b = bytes.TrimPrefix(b, []byte("Goodbye,"))
484 b = bytes.TrimPrefix(b, []byte("See ya,"))
485 fmt.Printf("Hello%s", b)
486 // Output: Hello, world!
489 func ExampleTrimSpace() {
490 fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n")))
491 // Output: a lone gopher
494 func ExampleTrimSuffix() {
495 var b = []byte("Hello, goodbye, etc!")
496 b = bytes.TrimSuffix(b, []byte("goodbye, etc!"))
497 b = bytes.TrimSuffix(b, []byte("gopher"))
498 b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...)
499 os.Stdout.Write(b)
500 // Output: Hello, world!
503 func ExampleTrimRight() {
504 fmt.Print(string(bytes.TrimRight([]byte("453gopher8257"), "0123456789")))
505 // Output:
506 // 453gopher
509 func ExampleTrimRightFunc() {
510 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher"), unicode.IsLetter)))
511 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher!"), unicode.IsPunct)))
512 fmt.Println(string(bytes.TrimRightFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
513 // Output:
514 // go-
515 // go-gopher
516 // 1234go-gopher!
519 func ExampleToLower() {
520 fmt.Printf("%s", bytes.ToLower([]byte("Gopher")))
521 // Output: gopher
524 func ExampleToLowerSpecial() {
525 str := []byte("AHOJ VÝVOJÁRİ GOLANG")
526 totitle := bytes.ToLowerSpecial(unicode.AzeriCase, str)
527 fmt.Println("Original : " + string(str))
528 fmt.Println("ToLower : " + string(totitle))
529 // Output:
530 // Original : AHOJ VÝVOJÁRİ GOLANG
531 // ToLower : ahoj vývojári golang
534 func ExampleToUpper() {
535 fmt.Printf("%s", bytes.ToUpper([]byte("Gopher")))
536 // Output: GOPHER
539 func ExampleToUpperSpecial() {
540 str := []byte("ahoj vývojári golang")
541 totitle := bytes.ToUpperSpecial(unicode.AzeriCase, str)
542 fmt.Println("Original : " + string(str))
543 fmt.Println("ToUpper : " + string(totitle))
544 // Output:
545 // Original : ahoj vývojári golang
546 // ToUpper : AHOJ VÝVOJÁRİ GOLANG