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