github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/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  	"unicode"
    15  )
    16  
    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!
    23  }
    24  
    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!
    31  }
    32  
    33  func ExampleCompare() {
    34  	// Interpret Compare's result by comparing it to zero.
    35  	var a, b []byte
    36  	if bytes.Compare(a, b) < 0 {
    37  		// a less b
    38  	}
    39  	if bytes.Compare(a, b) <= 0 {
    40  		// a less or equal b
    41  	}
    42  	if bytes.Compare(a, b) > 0 {
    43  		// a greater b
    44  	}
    45  	if bytes.Compare(a, b) >= 0 {
    46  		// a greater or equal b
    47  	}
    48  
    49  	// Prefer Equal to Compare for equality comparisons.
    50  	if bytes.Equal(a, b) {
    51  		// a equal b
    52  	}
    53  	if !bytes.Equal(a, b) {
    54  		// a not equal b
    55  	}
    56  }
    57  
    58  func ExampleCompare_search() {
    59  	// Binary search to find a matching byte slice.
    60  	var needle []byte
    61  	var haystack [][]byte // Assume sorted
    62  	i := sort.Search(len(haystack), func(i int) bool {
    63  		// Return haystack[i] >= needle.
    64  		return bytes.Compare(haystack[i], needle) >= 0
    65  	})
    66  	if i < len(haystack) && bytes.Equal(haystack[i], needle) {
    67  		// Found it!
    68  	}
    69  }
    70  
    71  func ExampleTrimSuffix() {
    72  	var b = []byte("Hello, goodbye, etc!")
    73  	b = bytes.TrimSuffix(b, []byte("goodbye, etc!"))
    74  	b = bytes.TrimSuffix(b, []byte("gopher"))
    75  	b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...)
    76  	os.Stdout.Write(b)
    77  	// Output: Hello, world!
    78  }
    79  
    80  func ExampleTrimPrefix() {
    81  	var b = []byte("Goodbye,, world!")
    82  	b = bytes.TrimPrefix(b, []byte("Goodbye,"))
    83  	b = bytes.TrimPrefix(b, []byte("See ya,"))
    84  	fmt.Printf("Hello%s", b)
    85  	// Output: Hello, world!
    86  }
    87  
    88  func ExampleFields() {
    89  	fmt.Printf("Fields are: %q", bytes.Fields([]byte("  foo bar  baz   ")))
    90  	// Output: Fields are: ["foo" "bar" "baz"]
    91  }
    92  
    93  func ExampleFieldsFunc() {
    94  	f := func(c rune) bool {
    95  		return !unicode.IsLetter(c) && !unicode.IsNumber(c)
    96  	}
    97  	fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte("  foo1;bar2,baz3..."), f))
    98  	// Output: Fields are: ["foo1" "bar2" "baz3"]
    99  }
   100  
   101  func ExampleContains() {
   102  	fmt.Println(bytes.Contains([]byte("seafood"), []byte("foo")))
   103  	fmt.Println(bytes.Contains([]byte("seafood"), []byte("bar")))
   104  	fmt.Println(bytes.Contains([]byte("seafood"), []byte("")))
   105  	fmt.Println(bytes.Contains([]byte(""), []byte("")))
   106  	// Output:
   107  	// true
   108  	// false
   109  	// true
   110  	// true
   111  }
   112  
   113  func ExampleCount() {
   114  	fmt.Println(bytes.Count([]byte("cheese"), []byte("e")))
   115  	fmt.Println(bytes.Count([]byte("five"), []byte(""))) // before & after each rune
   116  	// Output:
   117  	// 3
   118  	// 5
   119  }
   120  
   121  func ExampleEqualFold() {
   122  	fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))
   123  	// Output: true
   124  }
   125  
   126  func ExampleHasPrefix() {
   127  	fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go")))
   128  	fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("C")))
   129  	fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("")))
   130  	// Output:
   131  	// true
   132  	// false
   133  	// true
   134  }
   135  
   136  func ExampleHasSuffix() {
   137  	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go")))
   138  	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O")))
   139  	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami")))
   140  	fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("")))
   141  	// Output:
   142  	// true
   143  	// false
   144  	// false
   145  	// true
   146  }
   147  
   148  func ExampleIndex() {
   149  	fmt.Println(bytes.Index([]byte("chicken"), []byte("ken")))
   150  	fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr")))
   151  	// Output:
   152  	// 4
   153  	// -1
   154  }
   155  
   156  func ExampleIndexFunc() {
   157  	f := func(c rune) bool {
   158  		return unicode.Is(unicode.Han, c)
   159  	}
   160  	fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f))
   161  	fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f))
   162  	// Output:
   163  	// 7
   164  	// -1
   165  }
   166  
   167  func ExampleIndexAny() {
   168  	fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy"))
   169  	fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy"))
   170  	// Output:
   171  	// 2
   172  	// -1
   173  }
   174  
   175  func ExampleIndexRune() {
   176  	fmt.Println(bytes.IndexRune([]byte("chicken"), 'k'))
   177  	fmt.Println(bytes.IndexRune([]byte("chicken"), 'd'))
   178  	// Output:
   179  	// 4
   180  	// -1
   181  }
   182  
   183  func ExampleLastIndex() {
   184  	fmt.Println(bytes.Index([]byte("go gopher"), []byte("go")))
   185  	fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go")))
   186  	fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent")))
   187  	// Output:
   188  	// 0
   189  	// 3
   190  	// -1
   191  }
   192  
   193  func ExampleJoin() {
   194  	s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}
   195  	fmt.Printf("%s", bytes.Join(s, []byte(", ")))
   196  	// Output: foo, bar, baz
   197  }
   198  
   199  func ExampleRepeat() {
   200  	fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2))
   201  	// Output: banana
   202  }
   203  
   204  func ExampleReplace() {
   205  	fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2))
   206  	fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1))
   207  	// Output:
   208  	// oinky oinky oink
   209  	// moo moo moo
   210  }
   211  
   212  func ExampleSplit() {
   213  	fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(",")))
   214  	fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a ")))
   215  	fmt.Printf("%q\n", bytes.Split([]byte(" xyz "), []byte("")))
   216  	fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins")))
   217  	// Output:
   218  	// ["a" "b" "c"]
   219  	// ["" "man " "plan " "canal panama"]
   220  	// [" " "x" "y" "z" " "]
   221  	// [""]
   222  }
   223  
   224  func ExampleSplitN() {
   225  	fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2))
   226  	z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0)
   227  	fmt.Printf("%q (nil = %v)\n", z, z == nil)
   228  	// Output:
   229  	// ["a" "b,c"]
   230  	// [] (nil = true)
   231  }
   232  
   233  func ExampleSplitAfter() {
   234  	fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(",")))
   235  	// Output: ["a," "b," "c"]
   236  }
   237  
   238  func ExampleSplitAfterN() {
   239  	fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2))
   240  	// Output: ["a," "b,c"]
   241  }
   242  
   243  func ExampleTitle() {
   244  	fmt.Printf("%s", bytes.Title([]byte("her royal highness")))
   245  	// Output: Her Royal Highness
   246  }
   247  
   248  func ExampleToTitle() {
   249  	fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises")))
   250  	fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб")))
   251  	// Output:
   252  	// LOUD NOISES
   253  	// ХЛЕБ
   254  }
   255  
   256  func ExampleTrim() {
   257  	fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! "))
   258  	// Output: ["Achtung! Achtung"]
   259  }
   260  
   261  func ExampleMap() {
   262  	rot13 := func(r rune) rune {
   263  		switch {
   264  		case r >= 'A' && r <= 'Z':
   265  			return 'A' + (r-'A'+13)%26
   266  		case r >= 'a' && r <= 'z':
   267  			return 'a' + (r-'a'+13)%26
   268  		}
   269  		return r
   270  	}
   271  	fmt.Printf("%s", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher...")))
   272  	// Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
   273  }
   274  
   275  func ExampleTrimSpace() {
   276  	fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n")))
   277  	// Output: a lone gopher
   278  }
   279  
   280  func ExampleToUpper() {
   281  	fmt.Printf("%s", bytes.ToUpper([]byte("Gopher")))
   282  	// Output: GOPHER
   283  }
   284  
   285  func ExampleToLower() {
   286  	fmt.Printf("%s", bytes.ToLower([]byte("Gopher")))
   287  	// Output: gopher
   288  }