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