github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/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 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 ExampleRunes() { 302 rs := bytes.Runes([]byte("go gopher")) 303 for _, r := range rs { 304 fmt.Printf("%#U\n", r) 305 } 306 // Output: 307 // U+0067 'g' 308 // U+006F 'o' 309 // U+0020 ' ' 310 // U+0067 'g' 311 // U+006F 'o' 312 // U+0070 'p' 313 // U+0068 'h' 314 // U+0065 'e' 315 // U+0072 'r' 316 } 317 318 func ExampleSplit() { 319 fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(","))) 320 fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a "))) 321 fmt.Printf("%q\n", bytes.Split([]byte(" xyz "), []byte(""))) 322 fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins"))) 323 // Output: 324 // ["a" "b" "c"] 325 // ["" "man " "plan " "canal panama"] 326 // [" " "x" "y" "z" " "] 327 // [""] 328 } 329 330 func ExampleSplitN() { 331 fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2)) 332 z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0) 333 fmt.Printf("%q (nil = %v)\n", z, z == nil) 334 // Output: 335 // ["a" "b,c"] 336 // [] (nil = true) 337 } 338 339 func ExampleSplitAfter() { 340 fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(","))) 341 // Output: ["a," "b," "c"] 342 } 343 344 func ExampleSplitAfterN() { 345 fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2)) 346 // Output: ["a," "b,c"] 347 } 348 349 func ExampleTitle() { 350 fmt.Printf("%s", bytes.Title([]byte("her royal highness"))) 351 // Output: Her Royal Highness 352 } 353 354 func ExampleToTitle() { 355 fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises"))) 356 fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб"))) 357 // Output: 358 // LOUD NOISES 359 // ХЛЕБ 360 } 361 362 func ExampleTrim() { 363 fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! ")) 364 // Output: ["Achtung! Achtung"] 365 } 366 367 func ExampleTrimFunc() { 368 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsLetter))) 369 fmt.Println(string(bytes.TrimFunc([]byte("\"go-gopher!\""), unicode.IsLetter))) 370 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsPunct))) 371 fmt.Println(string(bytes.TrimFunc([]byte("1234go-gopher!567"), unicode.IsNumber))) 372 // Output: 373 // -gopher! 374 // "go-gopher!" 375 // go-gopher 376 // go-gopher! 377 } 378 379 func ExampleMap() { 380 rot13 := func(r rune) rune { 381 switch { 382 case r >= 'A' && r <= 'Z': 383 return 'A' + (r-'A'+13)%26 384 case r >= 'a' && r <= 'z': 385 return 'a' + (r-'a'+13)%26 386 } 387 return r 388 } 389 fmt.Printf("%s", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher..."))) 390 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure... 391 } 392 393 func ExampleTrimLeft() { 394 fmt.Print(string(bytes.TrimLeft([]byte("453gopher8257"), "0123456789"))) 395 // Output: 396 // gopher8257 397 } 398 399 func ExampleTrimLeftFunc() { 400 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher"), unicode.IsLetter))) 401 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher!"), unicode.IsPunct))) 402 fmt.Println(string(bytes.TrimLeftFunc([]byte("1234go-gopher!567"), unicode.IsNumber))) 403 // Output: 404 // -gopher 405 // go-gopher! 406 // go-gopher!567 407 } 408 409 func ExampleTrimSpace() { 410 fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n"))) 411 // Output: a lone gopher 412 } 413 414 func ExampleTrimRight() { 415 fmt.Print(string(bytes.TrimRight([]byte("453gopher8257"), "0123456789"))) 416 // Output: 417 // 453gopher 418 } 419 420 func ExampleTrimRightFunc() { 421 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher"), unicode.IsLetter))) 422 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher!"), unicode.IsPunct))) 423 fmt.Println(string(bytes.TrimRightFunc([]byte("1234go-gopher!567"), unicode.IsNumber))) 424 // Output: 425 // go- 426 // go-gopher 427 // 1234go-gopher! 428 } 429 430 func ExampleToUpper() { 431 fmt.Printf("%s", bytes.ToUpper([]byte("Gopher"))) 432 // Output: GOPHER 433 } 434 435 func ExampleToLower() { 436 fmt.Printf("%s", bytes.ToLower([]byte("Gopher"))) 437 // Output: gopher 438 } 439 440 func ExampleReader_Len() { 441 fmt.Println(bytes.NewReader([]byte("Hi!")).Len()) 442 fmt.Println(bytes.NewReader([]byte("こんにちは!")).Len()) 443 // Output: 444 // 3 445 // 16 446 }