]> Cypherpunks repositories - gostls13.git/commitdiff
bytes: Add missing examples to functions
authorBorja Clemente <borja.clemente@gmail.com>
Wed, 23 Aug 2017 13:51:03 +0000 (15:51 +0200)
committerJoe Tsai <thebrokentoaster@gmail.com>
Fri, 25 Aug 2017 20:50:58 +0000 (20:50 +0000)
Fixes #21570

Change-Id: Ia0734929a04fbce8fdd5fbcb1b7baff9a8bbe39e
Reviewed-on: https://go-review.googlesource.com/58030
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/bytes/example_test.go

index 43d19e186b3af69f3d384da350d7fb4b9305bb0b..a112c9a37363e0ad061d828f53d3bda3a8aede35 100644 (file)
@@ -119,6 +119,32 @@ func ExampleContains() {
        // true
 }
 
+func ExampleContainsAny() {
+       fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "fÄo!"))
+       fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "去是伟大的."))
+       fmt.Println(bytes.ContainsAny([]byte("I like seafood."), ""))
+       fmt.Println(bytes.ContainsAny([]byte(""), ""))
+       // Output:
+       // true
+       // true
+       // false
+       // false
+}
+
+func ExampleContainsRune() {
+       fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'f'))
+       fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'ö'))
+       fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '大'))
+       fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '!'))
+       fmt.Println(bytes.ContainsRune([]byte(""), '@'))
+       // Output:
+       // true
+       // false
+       // true
+       // true
+       // false
+}
+
 func ExampleCount() {
        fmt.Println(bytes.Count([]byte("cheese"), []byte("e")))
        fmt.Println(bytes.Count([]byte("five"), []byte(""))) // before & after each rune
@@ -199,6 +225,36 @@ func ExampleLastIndex() {
        // -1
 }
 
+func ExampleLastIndexAny() {
+       fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "MüQp"))
+       fmt.Println(bytes.LastIndexAny([]byte("go 地鼠"), "地大"))
+       fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "z,!."))
+       // Output:
+       // 5
+       // 3
+       // -1
+}
+
+func ExampleLastIndexByte() {
+       fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('g')))
+       fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('r')))
+       fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('z')))
+       // Output:
+       // 3
+       // 8
+       // -1
+}
+
+func ExampleLastIndexFunc() {
+       fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsLetter))
+       fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsPunct))
+       fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsNumber))
+       // Output:
+       // 8
+       // 9
+       // -1
+}
+
 func ExampleJoin() {
        s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}
        fmt.Printf("%s", bytes.Join(s, []byte(", ")))
@@ -218,6 +274,23 @@ func ExampleReplace() {
        // moo moo moo
 }
 
+func ExampleRunes() {
+       rs := bytes.Runes([]byte("go gopher"))
+       for _, r := range rs {
+               fmt.Printf("%#U\n", r)
+       }
+       // Output:
+       // U+0067 'g'
+       // U+006F 'o'
+       // U+0020 ' '
+       // U+0067 'g'
+       // U+006F 'o'
+       // U+0070 'p'
+       // U+0068 'h'
+       // U+0065 'e'
+       // U+0072 'r'
+}
+
 func ExampleSplit() {
        fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(",")))
        fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a ")))
@@ -267,6 +340,18 @@ func ExampleTrim() {
        // Output: ["Achtung! Achtung"]
 }
 
+func ExampleTrimFunc() {
+       fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsLetter)))
+       fmt.Println(string(bytes.TrimFunc([]byte("\"go-gopher!\""), unicode.IsLetter)))
+       fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsPunct)))
+       fmt.Println(string(bytes.TrimFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
+       // Output:
+       // -gopher!
+       // "go-gopher!"
+       // go-gopher
+       // go-gopher!
+}
+
 func ExampleMap() {
        rot13 := func(r rune) rune {
                switch {
@@ -287,6 +372,16 @@ func ExampleTrimLeft() {
        // 5400
 }
 
+func ExampleTrimLeftFunc() {
+       fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher"), unicode.IsLetter)))
+       fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher!"), unicode.IsPunct)))
+       fmt.Println(string(bytes.TrimLeftFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
+       // Output:
+       // -gopher
+       // go-gopher!
+       // go-gopher!567
+}
+
 func ExampleTrimSpace() {
        fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n")))
        // Output: a lone gopher
@@ -298,6 +393,16 @@ func ExampleTrimRight() {
        // 453gopher
 }
 
+func ExampleTrimRightFunc() {
+       fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher"), unicode.IsLetter)))
+       fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher!"), unicode.IsPunct)))
+       fmt.Println(string(bytes.TrimRightFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
+       // Output:
+       // go-
+       // go-gopher
+       // 1234go-gopher!
+}
+
 func ExampleToUpper() {
        fmt.Printf("%s", bytes.ToUpper([]byte("Gopher")))
        // Output: GOPHER