Olivier Duperray | dcc5fe1 | 2012-01-18 09:40:50 -0800 | [diff] [blame] | 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 | |
Andrew Gerrand | 9834a25 | 2011-12-16 09:43:58 +1100 | [diff] [blame] | 5 | package bytes_test |
| 6 | |
| 7 | import ( |
Andrew Gerrand | e7c222c | 2012-10-10 11:15:41 +1100 | [diff] [blame] | 8 | "bytes" |
Andrew Gerrand | 9834a25 | 2011-12-16 09:43:58 +1100 | [diff] [blame] | 9 | "encoding/base64" |
Andrew Gerrand | e7c222c | 2012-10-10 11:15:41 +1100 | [diff] [blame] | 10 | "fmt" |
Andrew Gerrand | 9834a25 | 2011-12-16 09:43:58 +1100 | [diff] [blame] | 11 | "io" |
| 12 | "os" |
Matthew Dempsky | 5696127 | 2013-01-07 09:59:37 +1100 | [diff] [blame] | 13 | "sort" |
Andrew Gerrand | 9834a25 | 2011-12-16 09:43:58 +1100 | [diff] [blame] | 14 | ) |
| 15 | |
Andrew Gerrand | 9834a25 | 2011-12-16 09:43:58 +1100 | [diff] [blame] | 16 | func ExampleBuffer() { |
Andrew Gerrand | e7c222c | 2012-10-10 11:15:41 +1100 | [diff] [blame] | 17 | var b bytes.Buffer // A Buffer needs no initialization. |
Andrew Gerrand | 9834a25 | 2011-12-16 09:43:58 +1100 | [diff] [blame] | 18 | b.Write([]byte("Hello ")) |
Andrew Gerrand | e7c222c | 2012-10-10 11:15:41 +1100 | [diff] [blame] | 19 | fmt.Fprintf(&b, "world!") |
Andrew Gerrand | 9834a25 | 2011-12-16 09:43:58 +1100 | [diff] [blame] | 20 | b.WriteTo(os.Stdout) |
Andrew Gerrand | 11e113d | 2012-02-16 11:50:28 +1100 | [diff] [blame] | 21 | // Output: Hello world! |
Andrew Gerrand | 9834a25 | 2011-12-16 09:43:58 +1100 | [diff] [blame] | 22 | } |
| 23 | |
Andrew Gerrand | 9834a25 | 2011-12-16 09:43:58 +1100 | [diff] [blame] | 24 | func ExampleBuffer_reader() { |
| 25 | // A Buffer can turn a string or a []byte into an io.Reader. |
Andrew Gerrand | e7c222c | 2012-10-10 11:15:41 +1100 | [diff] [blame] | 26 | buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==") |
Andrew Gerrand | 9834a25 | 2011-12-16 09:43:58 +1100 | [diff] [blame] | 27 | dec := base64.NewDecoder(base64.StdEncoding, buf) |
| 28 | io.Copy(os.Stdout, dec) |
Andrew Gerrand | 11e113d | 2012-02-16 11:50:28 +1100 | [diff] [blame] | 29 | // Output: Gophers rule! |
Andrew Gerrand | 9834a25 | 2011-12-16 09:43:58 +1100 | [diff] [blame] | 30 | } |
Matthew Dempsky | 5696127 | 2013-01-07 09:59:37 +1100 | [diff] [blame] | 31 | |
| 32 | func ExampleCompare() { |
| 33 | // Interpret Compare's result by comparing it to zero. |
| 34 | var a, b []byte |
| 35 | if bytes.Compare(a, b) < 0 { |
| 36 | // a less b |
| 37 | } |
| 38 | if bytes.Compare(a, b) <= 0 { |
| 39 | // a less or equal b |
| 40 | } |
| 41 | if bytes.Compare(a, b) > 0 { |
| 42 | // a greater b |
| 43 | } |
| 44 | if bytes.Compare(a, b) >= 0 { |
| 45 | // a greater or equal b |
| 46 | } |
| 47 | |
| 48 | // Prefer Equal to Compare for equality comparisons. |
| 49 | if bytes.Equal(a, b) { |
| 50 | // a equal b |
| 51 | } |
| 52 | if !bytes.Equal(a, b) { |
| 53 | // a not equal b |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func ExampleCompare_search() { |
| 58 | // Binary search to find a matching byte slice. |
| 59 | var needle []byte |
| 60 | var haystack [][]byte // Assume sorted |
| 61 | i := sort.Search(len(haystack), func(i int) bool { |
Matthew Dempsky | 8cf4590 | 2013-01-06 22:43:32 -0500 | [diff] [blame] | 62 | // Return haystack[i] >= needle. |
| 63 | return bytes.Compare(haystack[i], needle) >= 0 |
Matthew Dempsky | 5696127 | 2013-01-07 09:59:37 +1100 | [diff] [blame] | 64 | }) |
Matthew Dempsky | 8cf4590 | 2013-01-06 22:43:32 -0500 | [diff] [blame] | 65 | if i < len(haystack) && bytes.Equal(haystack[i], needle) { |
Matthew Dempsky | 5696127 | 2013-01-07 09:59:37 +1100 | [diff] [blame] | 66 | // Found it! |
| 67 | } |
| 68 | } |
Brad Fitzpatrick | e515d80 | 2013-02-01 08:41:25 -0800 | [diff] [blame] | 69 | |
| 70 | func ExampleTrimSuffix() { |
| 71 | var b = []byte("Hello, goodbye, etc!") |
| 72 | b = bytes.TrimSuffix(b, []byte("goodbye, etc!")) |
| 73 | b = bytes.TrimSuffix(b, []byte("gopher")) |
| 74 | b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...) |
| 75 | os.Stdout.Write(b) |
| 76 | // Output: Hello, world! |
| 77 | } |
| 78 | |
| 79 | func ExampleTrimPrefix() { |
| 80 | var b = []byte("Goodbye,, world!") |
| 81 | b = bytes.TrimPrefix(b, []byte("Goodbye,")) |
| 82 | b = bytes.TrimPrefix(b, []byte("See ya,")) |
| 83 | fmt.Printf("Hello%s", b) |
| 84 | // Output: Hello, world! |
| 85 | } |