github.com/filosottile/go@v0.0.0-20170906193555-dbed9972d994/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 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 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 67 func 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 80 func 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 89 func 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 97 func ExampleFields() { 98 fmt.Printf("Fields are: %q", bytes.Fields([]byte(" foo bar baz "))) 99 // Output: Fields are: ["foo" "bar" "baz"] 100 } 101 102 func 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 110 func 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 122 func 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 134 func 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 148 func 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 156 func ExampleEqualFold() { 157 fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go"))) 158 // Output: true 159 } 160 161 func ExampleHasPrefix() { 162 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go"))) 163 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("C"))) 164 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte(""))) 165 // Output: 166 // true 167 // false 168 // true 169 } 170 171 func ExampleHasSuffix() { 172 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go"))) 173 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O"))) 174 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami"))) 175 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte(""))) 176 // Output: 177 // true 178 // false 179 // false 180 // true 181 } 182 183 func ExampleIndex() { 184 fmt.Println(bytes.Index([]byte("chicken"), []byte("ken"))) 185 fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr"))) 186 // Output: 187 // 4 188 // -1 189 } 190 191 func ExampleIndexFunc() { 192 f := func(c rune) bool { 193 return unicode.Is(unicode.Han, c) 194 } 195 fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f)) 196 fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f)) 197 // Output: 198 // 7 199 // -1 200 } 201 202 func ExampleIndexAny() { 203 fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy")) 204 fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy")) 205 // Output: 206 // 2 207 // -1 208 } 209 210 func ExampleIndexRune() { 211 fmt.Println(bytes.IndexRune([]byte("chicken"), 'k')) 212 fmt.Println(bytes.IndexRune([]byte("chicken"), 'd')) 213 // Output: 214 // 4 215 // -1 216 } 217 218 func ExampleLastIndex() { 219 fmt.Println(bytes.Index([]byte("go gopher"), []byte("go"))) 220 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go"))) 221 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent"))) 222 // Output: 223 // 0 224 // 3 225 // -1 226 } 227 228 func ExampleLastIndexAny() { 229 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "MüQp")) 230 fmt.Println(bytes.LastIndexAny([]byte("go 地鼠"), "地大")) 231 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "z,!.")) 232 // Output: 233 // 5 234 // 3 235 // -1 236 } 237 238 func ExampleLastIndexByte() { 239 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('g'))) 240 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('r'))) 241 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('z'))) 242 // Output: 243 // 3 244 // 8 245 // -1 246 } 247 248 func ExampleLastIndexFunc() { 249 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsLetter)) 250 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsPunct)) 251 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsNumber)) 252 // Output: 253 // 8 254 // 9 255 // -1 256 } 257 258 func ExampleJoin() { 259 s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")} 260 fmt.Printf("%s", bytes.Join(s, []byte(", "))) 261 // Output: foo, bar, baz 262 } 263 264 func ExampleRepeat() { 265 fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2)) 266 // Output: banana 267 } 268 269 func ExampleReplace() { 270 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2)) 271 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1)) 272 // Output: 273 // oinky oinky oink 274 // moo moo moo 275 } 276 277 func ExampleRunes() { 278 rs := bytes.Runes([]byte("go gopher")) 279 for _, r := range rs { 280 fmt.Printf("%#U\n", r) 281 } 282 // Output: 283 // U+0067 'g' 284 // U+006F 'o' 285 // U+0020 ' ' 286 // U+0067 'g' 287 // U+006F 'o' 288 // U+0070 'p' 289 // U+0068 'h' 290 // U+0065 'e' 291 // U+0072 'r' 292 } 293 294 func ExampleSplit() { 295 fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(","))) 296 fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a "))) 297 fmt.Printf("%q\n", bytes.Split([]byte(" xyz "), []byte(""))) 298 fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins"))) 299 // Output: 300 // ["a" "b" "c"] 301 // ["" "man " "plan " "canal panama"] 302 // [" " "x" "y" "z" " "] 303 // [""] 304 } 305 306 func ExampleSplitN() { 307 fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2)) 308 z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0) 309 fmt.Printf("%q (nil = %v)\n", z, z == nil) 310 // Output: 311 // ["a" "b,c"] 312 // [] (nil = true) 313 } 314 315 func ExampleSplitAfter() { 316 fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(","))) 317 // Output: ["a," "b," "c"] 318 } 319 320 func ExampleSplitAfterN() { 321 fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2)) 322 // Output: ["a," "b,c"] 323 } 324 325 func ExampleTitle() { 326 fmt.Printf("%s", bytes.Title([]byte("her royal highness"))) 327 // Output: Her Royal Highness 328 } 329 330 func ExampleToTitle() { 331 fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises"))) 332 fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб"))) 333 // Output: 334 // LOUD NOISES 335 // ХЛЕБ 336 } 337 338 func ExampleTrim() { 339 fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! ")) 340 // Output: ["Achtung! Achtung"] 341 } 342 343 func ExampleTrimFunc() { 344 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsLetter))) 345 fmt.Println(string(bytes.TrimFunc([]byte("\"go-gopher!\""), unicode.IsLetter))) 346 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsPunct))) 347 fmt.Println(string(bytes.TrimFunc([]byte("1234go-gopher!567"), unicode.IsNumber))) 348 // Output: 349 // -gopher! 350 // "go-gopher!" 351 // go-gopher 352 // go-gopher! 353 } 354 355 func ExampleMap() { 356 rot13 := func(r rune) rune { 357 switch { 358 case r >= 'A' && r <= 'Z': 359 return 'A' + (r-'A'+13)%26 360 case r >= 'a' && r <= 'z': 361 return 'a' + (r-'a'+13)%26 362 } 363 return r 364 } 365 fmt.Printf("%s", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher..."))) 366 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure... 367 } 368 369 func ExampleTrimLeft() { 370 fmt.Print(string(bytes.TrimLeft([]byte("+ 005400"), "+0 "))) 371 // Output: 372 // 5400 373 } 374 375 func ExampleTrimLeftFunc() { 376 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher"), unicode.IsLetter))) 377 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher!"), unicode.IsPunct))) 378 fmt.Println(string(bytes.TrimLeftFunc([]byte("1234go-gopher!567"), unicode.IsNumber))) 379 // Output: 380 // -gopher 381 // go-gopher! 382 // go-gopher!567 383 } 384 385 func ExampleTrimSpace() { 386 fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n"))) 387 // Output: a lone gopher 388 } 389 390 func ExampleTrimRight() { 391 fmt.Print(string(bytes.TrimRight([]byte("453gopher8257"), "0123456789"))) 392 // Output: 393 // 453gopher 394 } 395 396 func ExampleTrimRightFunc() { 397 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher"), unicode.IsLetter))) 398 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher!"), unicode.IsPunct))) 399 fmt.Println(string(bytes.TrimRightFunc([]byte("1234go-gopher!567"), unicode.IsNumber))) 400 // Output: 401 // go- 402 // go-gopher 403 // 1234go-gopher! 404 } 405 406 func ExampleToUpper() { 407 fmt.Printf("%s", bytes.ToUpper([]byte("Gopher"))) 408 // Output: GOPHER 409 } 410 411 func ExampleToLower() { 412 fmt.Printf("%s", bytes.ToLower([]byte("Gopher"))) 413 // Output: gopher 414 }