funcExampleErrTooLarge(){// NOTE(jay): See [bytes.Buffer.Grow] for another example.
varbufbytes.Bufferdeferfunc(){ifr:=recover();r!=nil{switcherr:=r.(type){caseerror:iferrors.Is(err,bytes.ErrTooLarge){fmt.Printf("it's too big: %v\n",err)}default:fmt.Println(r)}}}()buf.Grow(1<<62)// Output:
// it's too big: bytes.Buffer: too large
}
// https://github.com/pingcap/tidb/blob/00791e7968ffad2de33d7af7f6f8a21580e2ab7e/ddl/cluster.go#L280
// https://github.com/ethereum/go-ethereum/blob/fb75f11e87420ec25ff72f7eeeb741fa8974e87e/trie/proof.go#L251-L258
// https://github.com/kubernetes/kubernetes/blob/61ca612cbb85efa13444a6d8ae517cd5e9c151a4/pkg/api/v1/endpoints/util.go#L177
// https://github.com/cockroachdb/cockroach/blob/1c377714b802ae9b354344a81f2611a96eb3255f/pkg/storage/pebble.go#L113
// https://github.com/hashicorp/vault/blob/6bede501766490007fb8d9323b9a37ef2883c07b/helper/dhutil/dhutil.go#L70
funcExampleCompare(){a4b:=[]byte("aaaab")a3b:=[]byte("aaab")a2:=[]byte("aa")tellMe:=func(a,b[]byte){switchbytes.Compare(a,b){case-1:fmt.Printf("🤏 %q\thas a lexical score less than %q\n",a,b)case0:fmt.Printf("🟰 %q\thas a lexical score that equals %q\n",a,b)case1:fmt.Printf("💪 %q\thas a lexical score greater than %q\n",a,b)}}tellMe(a4b,a3b)tellMe(a2,a3b)tellMe(a4b,a2)tellMe(a2,a2)// Output:
// 🤏 "aaaab" has a lexical score less than "aaab"
// 🤏 "aa" has a lexical score less than "aaab"
// 💪 "aaaab" has a lexical score greater than "aa"
// 🟰 "aa" has a lexical score that equals "aa"
}
funcExampleContains(){data:=[]byte("My dentist tells me that chewing bricks is very bad for your teeth.")findOut:=func(a,b[]byte){switchbytes.Contains(a,b){casetrue:fmt.Printf("✅ %q contains %q\n",a,b)casefalse:fmt.Printf("❌ %q does NOT contain %q\n",a,b)}}findOut(data,[]byte("my"))findOut(data,[]byte("brushing"))findOut(data,[]byte("wing"))findOut(data,[]byte("hat"))findOut(data,[]byte("My"))// Output:
// ❌ "My dentist tells me that chewing bricks is very bad for your teeth." does NOT contain "my"
// ❌ "My dentist tells me that chewing bricks is very bad for your teeth." does NOT contain "brushing"
// ✅ "My dentist tells me that chewing bricks is very bad for your teeth." contains "wing"
// ✅ "My dentist tells me that chewing bricks is very bad for your teeth." contains "hat"
// ✅ "My dentist tells me that chewing bricks is very bad for your teeth." contains "My"
}
funcExampleContainsAny(){data:=[]byte("At that moment I was the most fearsome weasel in the entire swamp.")findOut:=func(a[]byte,charsstring){switchbytes.ContainsAny(a,chars){casetrue:fmt.Printf("✅ %q contains one of these characters %q\n",a,chars)casefalse:fmt.Printf("❌ %q does NOT contain any of these characters %q\n",a,chars)}}findOut(data,"stuff")findOut(data,"x")findOut(data,"XXX")findOut(data," ")// Output:
// ✅ "At that moment I was the most fearsome weasel in the entire swamp." contains one of these characters "stuff"
// ❌ "At that moment I was the most fearsome weasel in the entire swamp." does NOT contain any of these characters "x"
// ❌ "At that moment I was the most fearsome weasel in the entire swamp." does NOT contain any of these characters "XXX"
// ✅ "At that moment I was the most fearsome weasel in the entire swamp." contains one of these characters " "
}
funcExampleCount(){data:=[]byte("It isn't true that my mattress is made of cotton candy.")emojis:=[]byte("🫶🙌👌👍🔥🙃😎😁🤗")howMany:=func(a,b[]byte){fmt.Printf("there are %d of %q in %q\n",bytes.Count(a,b),b,a)}howMany(data,[]byte("t"))howMany(data,[]byte("is"))howMany(data,[]byte(""))howMany(emojis,[]byte{0xf0})howMany(emojis,[]byte(""))// Output:
// there are 9 of "t" in "It isn't true that my mattress is made of cotton candy."
// there are 2 of "is" in "It isn't true that my mattress is made of cotton candy."
// there are 56 of "" in "It isn't true that my mattress is made of cotton candy."
// there are 9 of "\xf0" in "\U0001faf6🙌👌👍🔥🙃😎😁🤗"
// there are 10 of "" in "\U0001faf6🙌👌👍🔥🙃😎😁🤗"
}
funcExampleCut(){data:=[]byte("Today we gathered moss for my uncle's wedding.")split:=func(a,b[]byte){be,af,found:=bytes.Cut(a,b)switchfound{casetrue:fmt.Printf("✅ before: %q, after: %q, found: %t\n",be,af,found)casefalse:fmt.Printf("❌ could NOT find %q in %q:\n\tbefore: %q, after: %q\n",b,a,be,af)}}split(data,[]byte("my"))split(data,[]byte(" "))split(data,[]byte("XXX"))// Output:
// ✅ before: "Today we gathered moss for ", after: " uncle's wedding.", found: true
// ✅ before: "Today", after: "we gathered moss for my uncle's wedding.", found: true
// ❌ could NOT find "XXX" in "Today we gathered moss for my uncle's wedding.":
// before: "Today we gathered moss for my uncle's wedding.", after: ""
}
funcExampleEqual(){data:=[]byte("Peter found road kill an excellent way to save money on dinner.")same:=func(a,b[]byte){switchbytes.Equal(a,b){casetrue:fmt.Printf("✅ %q is equal to %q\n",a,b)casefalse:fmt.Printf("❌ %q is NOT equal to %q\n",a,b)}}same([]byte(""),nil)same([]byte(""),[]byte(""))same([]byte{},[]byte(""))same(data,[]byte("Peter found road kill an excellent way to save money on dinner."))same(data,[]byte("peter found road kill an excellent way to save money on dinner."))// Output:
// ✅ "" is equal to ""
// ✅ "" is equal to ""
// ✅ "" is equal to ""
// ❌ "Peter found road kill an excellent way to save money on dinner." is NOT equal to "Peter found road kill an excellent way to save\u00a0money on dinner."
// ❌ "Peter found road kill an excellent way to save money on dinner." is NOT equal to "peter found road kill an excellent way to save money on dinner."
}
funcExampleEqualFold(){data:=[]byte("There's probably enough glass in my cupboard to build an undersea aquarium.")same:=func(a,b[]byte){switchbytes.EqualFold(a,b){casetrue:fmt.Printf("✅ %q is case-insensitively equal to %q\n",a,b)casefalse:fmt.Printf("❌ %q is NOT case-insensitively equal to %q\n",a,b)}}same([]byte(""),nil)same([]byte(""),[]byte(""))same([]byte{},nil)same([]byte("aaa"),[]byte("AAA"))same(data,[]byte("THERE'S pRoBabLY eNOuGh GlAsS iN my CuPBOaRd To BuILd AN uNdErSeA AqUaRiUM."))// Output:
// ✅ "" is case-insensitively equal to ""
// ✅ "" is case-insensitively equal to ""
// ✅ "" is case-insensitively equal to ""
// ✅ "aaa" is case-insensitively equal to "AAA"
// ✅ "There's probably enough glass in my cupboard to build an undersea aquarium." is case-insensitively equal to "THERE'S pRoBabLY eNOuGh GlAsS iN my CuPBOaRd To BuILd AN uNdErSeA AqUaRiUM."
}
funcExampleHasPrefix(){data:=[]byte("I want a giraffe, but I'm a turtle eating waffles.")prefixed:=func(a,b[]byte){switchbytes.HasPrefix(a,b){casetrue:fmt.Printf("✅ %q has %q as a prefix\n",a,b)casefalse:fmt.Printf("❌ %q does NOT have %q as a prefix\n",a,b)}}prefixed(data,[]byte("I want"))prefixed(data,[]byte("I want waffle fries"))prefixed(data,[]byte("I want a giraffe, but I'm a turtle eating waffles."))// Output:
// ✅ "I want a giraffe, but I'm a turtle eating waffles." has "I want" as a prefix
// ❌ "I want a giraffe, but I'm a turtle eating waffles." does NOT have "I want waffle fries" as a prefix
// ✅ "I want a giraffe, but I'm a turtle eating waffles." has "I want a giraffe, but I'm a turtle eating waffles." as a prefix
}
funcExampleHasSuffix(){data:=[]byte("If my calculator had a history, it would be more embarrassing than my browser history.")suffixed:=func(a,b[]byte){switchbytes.HasSuffix(a,b){casetrue:fmt.Printf("✅ %q has %q as a suffix\n",a,b)casefalse:fmt.Printf("❌ %q does NOT have %q as a suffix\n",a,b)}}suffixed(data,[]byte("history."))suffixed(data,[]byte("embarrassing my browser history."))suffixed(data,[]byte("If my calculator had a history, it would be more embarrassing than my browser history."))// Output:
// ✅ "If my calculator had a history, it would be more embarrassing than my browser history." has "history." as a suffix
// ❌ "If my calculator had a history, it would be more embarrassing than my browser history." does NOT have "embarrassing my browser history." as a suffix
// ✅ "If my calculator had a history, it would be more embarrassing than my browser history." has "If my calculator had a history, it would be more embarrassing than my browser history." as a suffix
}
funcExampleIndex(){data:=[]byte("He felt that dining on the bridge brought romance to his relationship with his cat.")idxOf:=func(a,b[]byte){i:=bytes.Index(a,b)ifi==-1{fmt.Printf("❌ %q could not be found in %q, index is %d\n",b,a,i)return}fmt.Printf("✅ %q starts at index: %d of %q\n\tyield: %q\n",b,i,a,a[i:])}idxOf(data,[]byte("H"))idxOf(data,[]byte("He"))idxOf(data,[]byte("he"))idxOf(data,[]byte("cat"))idxOf(data,[]byte("dog"))// Output:
// ✅ "H" starts at index: 0 of "He felt that dining on the bridge brought romance to his relationship with his cat."
// yield: "He felt that dining on the bridge brought romance to his relationship with his cat."
// ✅ "He" starts at index: 0 of "He felt that dining on the bridge brought romance to his relationship with his cat."
// yield: "He felt that dining on the bridge brought romance to his relationship with his cat."
// ✅ "he" starts at index: 24 of "He felt that dining on the bridge brought romance to his relationship with his cat."
// yield: "he bridge brought romance to his relationship with his cat."
// ✅ "cat" starts at index: 79 of "He felt that dining on the bridge brought romance to his relationship with his cat."
// yield: "cat."
// ❌ "dog" could not be found in "He felt that dining on the bridge brought romance to his relationship with his cat.", index is -1
}
funcExampleIndexAny(){data:=[]byte("Shakespeare was a famous 17th-century diesel mechanic.")findAny:=func(a[]byte,charsstring){i:=bytes.IndexAny(a,chars)ifi==-1{fmt.Printf("❌ %q does NOT contain any of these characters %q\n",a,chars)return}fmt.Printf("✅ %q contains one of these characters %q at index: %d and is %q\n",a,chars,i,a[i])}findAny(data,"Smechanic")findAny(data,"")findAny(data,"zy18*/q")findAny(data,"zx8*/q")// Output:
// ✅ "Shakespeare was a famous 17th-century diesel mechanic." contains one of these characters "Smechanic" at index: 0 and is 'S'
// ❌ "Shakespeare was a famous 17th-century diesel mechanic." does NOT contain any of these characters ""
// ✅ "Shakespeare was a famous 17th-century diesel mechanic." contains one of these characters "zy18*/q" at index: 25 and is '1'
// ❌ "Shakespeare was a famous 17th-century diesel mechanic." does NOT contain any of these characters "zx8*/q"
}
funcExampleIndexByte(){data:=[]byte("It's not often you find a soggy banana on the street.")byteMe:=func(a[]byte,bbyte){i:=bytes.IndexByte(a,b)ifi==-1{fmt.Printf("%q does not have byte: %q\n",a,b)return}fmt.Printf("%q is found at %d of %q and yields: %q\n",b,i,a,a[i:])}byteMe(data,'b')byteMe(data,'x')// Output:
// 'b' is found at 32 of "It's not often you find a soggy banana on the street." and yields: "banana on the street."
// "It's not often you find a soggy banana on the street." does not have byte: 'x'
}
funcExampleIndexFunc(){data:=[]byte("I caught my squirrel rustling through my gym bag.")nonPrintable:=func(a[]byte){i:=bytes.IndexFunc(a,func(rrune)bool{return!unicode.IsPrint(r)})ifi==-1{fmt.Printf("❌ %q does NOT contain any non-printable characters\n",a)return}fmt.Printf("✅ %q contains a non-printable character at %d and is %q\n",a,i,a[i])}data[20]='\t'nonPrintable(data)data[20]=' 'nonPrintable(data)// Output:
// ✅ "I caught my squirrel\trustling through my gym bag." contains a non-printable character at 20 and is '\t'
// ❌ "I caught my squirrel rustling through my gym bag." does NOT contain any non-printable characters
}
funcExampleIndexRune(){data:=[]byte("Nobody questions who built the pyramids in Mexico🥑.")runed:=func(a[]byte,rrune){i:=bytes.IndexRune(a,r)ifi==-1{fmt.Printf("❌ %q does NOT contain the rune %q\n",a,r)return}fmt.Printf("✅ %q contains the rune at index: %d\n",a,i)}runed(data,'🥑')runed(data,'🌮')// Output:
// ✅ "Nobody questions who built the pyramids in Mexico🥑." contains the rune at index: 49
// ❌ "Nobody questions who built the pyramids in Mexico🥑." does NOT contain the rune '🌮'
}
funcExampleJoin(){data:=[][]byte{{73},{97,108,119,97,121,115},{100,114,101,97,109,101,100},{97,98,111,117,116},{98,101,105,110,103},{115,116,114,97,110,100,101,100},{111,110},{97},{100,101,115,101,114,116},{105,115,108,97,110,100},{117,110,116,105,108},{105,116},{97,99,116,117,97,108,108,121},{104,97,112,112,101,110,101,100,46},}fmt.Printf("joined bytes: %q\n",bytes.Join(data,[]byte(" ")))oddWS:=[]byte("Nobody has\nencountered an explosive\r\ndaisy\t\tand lived to tell the tale.")fmt.Printf("sanitized bytes: %q\n",bytes.Join(bytes.Fields(oddWS),[]byte(" ")))// Output:
// joined bytes: "I always dreamed about being stranded on a desert island until it actually happened."
// sanitized bytes: "Nobody has encountered an explosive daisy and lived to tell the tale."
}
funcExampleLastIndex(){data:=[]byte("The fog was so dense even a laser decided it wasn't worth the effort.")lastIdx:=func(a,b[]byte){i:=bytes.LastIndex(a,b)ifi==-1{fmt.Printf("❌ %q could not be found in %q, index is %d\n",b,a,i)return}fmt.Printf("✅ last occurence of %q starts at index: %d of %q\n\tyield: %q\n",b,i,a,a[i:])}lastIdx(data,[]byte("f"))lastIdx(data,[]byte("XXX"))// Output:
// ✅ last occurence of "f" starts at index: 64 of "The fog was so dense even a laser decided it wasn't worth the effort."
// yield: "fort."
// ❌ "XXX" could not be found in "The fog was so dense even a laser decided it wasn't worth the effort.", index is -1
}
funcExampleLastIndexAny(){data:=[]byte("Patricia found the meaning of life in a bowl of Cheerios.")findAny:=func(a[]byte,charsstring){i:=bytes.LastIndexAny(a,chars)ifi==-1{fmt.Printf("❌ %q does NOT contain any of these characters %q\n",a,chars)return}fmt.Printf("✅ %q contains one of these characters %q last found at index: %d and is %q\n",a,chars,i,a[i])}findAny(data,"PCheerios")findAny(data,"")findAny(data,"zxy*/q")// Output:
// ✅ "Patricia found the meaning of life in a bowl of Cheerios." contains one of these characters "PCheerios" last found at index: 55 and is 's'
// ❌ "Patricia found the meaning of life in a bowl of Cheerios." does NOT contain any of these characters ""
// ❌ "Patricia found the meaning of life in a bowl of Cheerios." does NOT contain any of these characters "zxy*/q"
}
funcExampleLastIndexByte(){data:=[]byte("Be careful with that butter knife.")byteMeLast:=func(a[]byte,bbyte){i:=bytes.LastIndexByte(a,b)ifi==-1{fmt.Printf("%q does not have byte: %q\n",a,b)return}fmt.Printf("%q is found at last index: %d of %q and yields: %q\n",b,i,a,a[i:])}byteMeLast(data,'f')byteMeLast(data,'x')// Output:
// 'f' is found at last index: 31 of "Be careful with that butter knife." and yields: "fe."
// "Be careful with that butter knife." does not have byte: 'x'
}
funcExampleLastIndexFunc(){data:=[]byte("I ate a sock... because people on the Internet told me to.")lastPeriod:=func(a[]byte){i:=bytes.LastIndexFunc(a,func(rrune)bool{returnr=='.'})ifi==-1{fmt.Printf("❌ %q does NOT contain a '.'\n",a)return}fmt.Printf("✅ %q contains a '.' at last index: %d and yields %q\n",a,i,a[i:])}data[len(data)-1]=','lastPeriod(data)data[len(data)-1]='.'lastPeriod(data)lastPeriod(bytes.ReplaceAll(data,[]byte("."),[]byte("")))// Output:
// ✅ "I ate a sock... because people on the Internet told me to," contains a '.' at last index: 14 and yields ". because people on the Internet told me to,"
// ✅ "I ate a sock... because people on the Internet told me to." contains a '.' at last index: 57 and yields "."
// ❌ "I ate a sock because people on the Internet told me to" does NOT contain a '.'
}
funcExampleMap(){data:=[]byte("Th)e l!yric^s., of the213 so*ng() sound230ed li$$ke fi===n93gerna;ils on a cha_+}\\lkboa<]rd.")fmt.Printf("from:\t%q\nto:\t%q",data,bytes.Map(func(rrune)rune{switch{case!unicode.IsLetter(r)&&!unicode.IsSpace(r):return-1caser>'m':returnunicode.ToUpper(r)default:returnr}},data),)// Output:
// from: "Th)e l!yric^s., of the213 so*ng() sound230ed li$$ke fi===n93gerna;ils on a cha_+}\\lkboa<]rd."
// to: "The lYRicS Of The SONg SOUNded like fiNgeRNailS ON a chalkbOaRd"
}
funcExampleRepeat(){data:=[]byte("The glacier came alive as the climbers hiked closer.")deferfunc(){ifr:=recover();r!=nil{fmt.Printf("panic: %q\n",r)fmt.Printf("%q\n",bytes.Repeat(data,2))}}()fmt.Println(bytes.Repeat(data,1<<62))// XXX(jay): This panics
// Output:
// panic: "bytes: Repeat count causes overflow"
// "The glacier came alive as the climbers hiked closer.The glacier came alive as the climbers hiked closer."
}
funcExampleReplace(){data:=[]byte("It's much more difficult to play tennis with a bowling ball than it is to bowl with a tennis ball, but tennis is difficult in general.")etp:=func(a,old,_new[]byte,nint){fmt.Printf("orig:\t%q\nnew:\t%q\n",a,bytes.Replace(a,old,_new,n))fmt.Println("-------------","replaced",n,"-------------")}etp(data,[]byte("difficult"),[]byte("easy😎"),1)etp(data,[]byte("difficult"),[]byte("easy😎"),2)etp(data,[]byte("tennis"),[]byte("football🏈"),-1)// == [bytes.ReplaceAll]
// Output:
// orig: "It's much more difficult to play tennis with a bowling ball than it is to bowl with a tennis ball, but tennis is difficult in general."
// new: "It's much more easy😎 to play tennis with a bowling ball than it is to bowl with a tennis ball, but tennis is difficult in general."
// ------------- replaced 1 -------------
// orig: "It's much more difficult to play tennis with a bowling ball than it is to bowl with a tennis ball, but tennis is difficult in general."
// new: "It's much more easy😎 to play tennis with a bowling ball than it is to bowl with a tennis ball, but tennis is easy😎 in general."
// ------------- replaced 2 -------------
// orig: "It's much more difficult to play tennis with a bowling ball than it is to bowl with a tennis ball, but tennis is difficult in general."
// new: "It's much more difficult to play football🏈 with a bowling ball than it is to bowl with a football🏈 ball, but football🏈 is difficult in general."
// ------------- replaced -1 -------------
}
funcExampleReplaceAll(){data:=[]byte("It's much more difficult to play tennis with a bowling ball than it is to bowl with a tennis ball, but tennis is difficult in general.")fmt.Printf("orig:\t%q\nnew:\t%q\n",data,bytes.ReplaceAll(data,[]byte("tennis"),[]byte("football🏈")),)// Output:
// orig: "It's much more difficult to play tennis with a bowling ball than it is to bowl with a tennis ball, but tennis is difficult in general."
// new: "It's much more difficult to play football🏈 with a bowling ball than it is to bowl with a football🏈 ball, but football🏈 is difficult in general."
}
funcExampleTitle(){// XXX(jay): Don't use this! DEPRECATED
// [bytes.Title]
src:=[]byte("The rain pelted the windshield as the darkness engulfed us.")dst:=make([]byte,len(src))// Use this in it's place.
nDst,nSrc,err:=cases.Title(language.English,cases.NoLower).Transform(dst,src,true)fmt.Printf("orig:\t%q\nresult:\t%q\nnDst: %d\tnSrc: %d\terr: %v\n",src,dst,nDst,nSrc,err)// Output:
// orig: "The rain pelted the windshield as the darkness engulfed us."
// result: "The Rain Pelted The Windshield As The Darkness Engulfed Us."
// nDst: 59 nSrc: 59 err: <nil>
}
funcExampleToLower(){data:=[]byte("PEopLE wHo InsIst on pICKInG tHEIr tEEtH wItH tHEIr ELBows ArE so AnnoyInG!")fmt.Printf("before:\t%q\nafter:\t%q\n",data,bytes.ToLower(data))// Output:
// before: "PEopLE wHo InsIst on pICKInG tHEIr tEEtH wItH tHEIr ELBows ArE so AnnoyInG!"
// after: "people who insist on picking their teeth with their elbows are so annoying!"
}
funcExampleToTitle(){// NOTE(jay): Compare with [bytes.ToUpper] and [bytes.Title]
data:=[]byte("He found a leprechaun in his walnut shell.")data2:=[]byte("он нашел лепрекона в скорлупе грецкого ореха.")comp:=func(a[]byte){fmt.Printf("before:\t%q\nafter:\t%q\n",a,bytes.ToTitle(a))}comp(data)comp(data2)// Output:
// before: "He found a leprechaun in his walnut shell."
// after: "HE FOUND A LEPRECHAUN IN HIS WALNUT SHELL."
// before: "он нашел лепрекона в скорлупе грецкого ореха."
// after: "ОН НАШЕЛ ЛЕПРЕКОНА В СКОРЛУПЕ ГРЕЦКОГО ОРЕХА."
}
funcExampleToUpper(){// NOTE(jay): Compare with [bytes.ToTitle] and [bytes.Title]
data:=[]byte("He found a leprechaun in his walnut shell.")data2:=[]byte("он нашел лепрекона в скорлупе грецкого ореха.")comp:=func(a[]byte){fmt.Printf("before:\t%q\nafter:\t%q\n",a,bytes.ToUpper(a))}comp(data)comp(data2)// Output:
// before: "He found a leprechaun in his walnut shell."
// after: "HE FOUND A LEPRECHAUN IN HIS WALNUT SHELL."
// before: "он нашел лепрекона в скорлупе грецкого ореха."
// after: "ОН НАШЕЛ ЛЕПРЕКОНА В СКОРЛУПЕ ГРЕЦКОГО ОРЕХА."
}
funcExampleToValidUTF8(){// NOTE(jay): Bad bytes taken from
// https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
data:=[]byte{0x54,0xc0,0xaf,// Bad bytes
0x6f,0x64,0x61,0xff,// Bad byte
0x79,0x20,0x69,0xfe,// Bad byte
0x73,0x20,0xc1,0xbf,// Bad bytes
0x74,0x68,0x65,0x20,0xf8,0x87,0xbf,0xbf,// Bad bytes
0x64,0x61,0x79,0x20,0x49,0x27,0x6c,0x6c,0x20,0x66,0xc0,0x80,0xfc,0x80,0x80,0x80,0x80,// Bad bytes
0x69,0x6e,0x61,0x6c,0x6c,0x79,0x20,0x6b,0x6e,0x6f,0x77,0x20,0xed,0xa0,0x80,// Bad bytes
0x77,0x68,0x61,0x74,0x20,0x62,0x72,0x69,0x63,0x6b,0x20,0x74,0xed,0xa0,0x80,0xed,0xb0,0x80,// Bad bytes
0x61,0x73,0x74,0x65,0x73,0x20,0x6c,0x69,0x6b,0x65,0x2e,}fmt.Printf("%q\n",bytes.ToValidUTF8(data,[]byte("")))fmt.Printf("%q\n",bytes.ToValidUTF8(data,[]byte("🥴")))// Output:
// "Today is the day I'll finally know what brick tastes like."
// "T🥴oda🥴y i🥴s 🥴the 🥴day I'll f🥴inally know 🥴what brick t🥴astes like."
}
funcExampleTrim(){data:=[]byte("🍞🥜🥜🍇🍞👵Peanut butter and jelly caused the elderly lady to think about her past.🍞🥜🥜🍓🍓🍞👵")fmt.Printf("trimmed: %q",bytes.Trim(data,"👵🍇🍞🥜🍓"))// Output:
// trimmed: "Peanut butter and jelly caused the elderly lady to think about her past."
}
funcExampleTrimFunc(){data:=[]byte(`
/####%%%#######%%%#######'#######'#######'#######'####'#######'#######'#######%%%#######%%%######\
*####%%%#######%%%###################################################################%%%#######%%%######*
*####%%%#### `Thesecretingredienttohiswonderfullifewascrime.###%%%######*`
*######%##########%######################################################################%##########%#######*
\####'#'#######'#'###################################################################'#'#######'#'######/
`[1:])fmt.Printf("cleaned: %q\n",bytes.TrimFunc(data,func(rrune)bool{returnr=='\n'||r==' '||r=='#'||r=='\\'||r=='*'||r=='\''||r=='/'||r=='%'}))// Output:
// cleaned: "The secret ingredient to his wonderful life was crime."
}
funcExampleTrimLeft(){data:=[]byte("🕵️♂️📔🎤🗣The mysterious diary records the voice.")fmt.Printf("trimmed: %q\n",bytes.TrimLeft(data,"📔🕵️🗣♂️🎤"))// Output:
// trimmed: "The mysterious diary records the voice."
}
funcExampleTrimPrefix(){data:=[]byte("He had reached the point where he was paranoid about being paranoid.")fmt.Printf("trimmed: %q\n",bytes.TrimPrefix(data,[]byte("He had reached the point where")))fmt.Printf("trimmed: %q\n",bytes.TrimPrefix(data,[]byte("He had reached")))fmt.Printf("trimmed: %q\n",bytes.TrimPrefix(data,[]byte("XXX")))// Output:
// trimmed: " he was paranoid about being paranoid."
// trimmed: " the point where he was paranoid about being paranoid."
// trimmed: "He had reached the point where he was paranoid about being paranoid."
}
funcExampleTrimRight(){fmt.Printf("trimmed: %q",bytes.TrimRight([]byte("Imagine his surprise when he discovered that the safe was full of pudding.🫃🍮🍮🍮🍮🍮🍮🍮🍮🍮"),"🍮🫃🧷"))// Output:
// trimmed: "Imagine his surprise when he discovered that the safe was full of pudding."
}
funcExampleTrimRightFunc(){fmt.Printf("trimmed: %q\n",bytes.TrimRightFunc([]byte(`Harrold felt confident that nobody would ever suspect his spy pigeon.
-- Narrator 04/20/69`),func(rrune)bool{returnunicode.In(r,unicode.Letter,unicode.Digit,unicode.White_Space)||r=='-'||r=='/'}))// Output:
// trimmed: "Harrold felt confident that nobody would ever suspect his spy pigeon."
}
funcExampleTrimSpace(){data:=[]byte("\t \n \v \f \r \u0085 \u00a0 You bite up because of your lower jaw.😬 \u00a0 \u0085 \r \f \v \n \t")fmt.Printf("with space:\t%q\nwithout:\t%q\n",data,bytes.TrimSpace(data))// Output:
// with space: "\t \n \v \f \r \u0085 \u00a0 You bite up because of your lower jaw.😬 \u00a0 \u0085 \r \f \v \n \t"
// without: "You bite up because of your lower jaw.😬"
}
funcExampleTrimSuffix(){data:=[]byte("He put heat on the wound to see what would grow.")fmt.Printf("trimmed: %q\n",bytes.TrimSuffix(data,[]byte("what would grow.")))fmt.Printf("trimmed: %q\n",bytes.TrimSuffix(data,[]byte("grow.")))fmt.Printf("trimmed: %q\n",bytes.TrimSuffix(data,[]byte("He put")))// Output:
// trimmed: "He put heat on the wound to see "
// trimmed: "He put heat on the wound to see what would "
// trimmed: "He put heat on the wound to see what would grow."
}
funcExampleBuffer_Cap(){// NOTE(jay): compare with Reader.Size()
fmt.Println("the total space allocated for the buffer's data is:",stubBuffer().Cap())// Output:
// the total space allocated for the buffer's data is: 43
}
funcExampleBuffer_Grow(){buf:=stubBuffer()fmt.Println("the total space allocated for the buffer's data is:",buf.Cap())buf.Grow(1<<9)fmt.Println("the total space allocated for the buffer's data is:",buf.Cap())deferfunc(){ifr:=recover();r!=nil{fmt.Println(r)// bytes.Buffer: too large
}}()deferfunc(){ifr:=recover();r!=nil{fmt.Println(r)// bytes.Buffer.Grow: negative count
buf.Grow(1<<62)}}()buf.Grow(-1)// Output:
// the total space allocated for the buffer's data is: 43
// the total space allocated for the buffer's data is: 576
// bytes.Buffer.Grow: negative count
// bytes.Buffer: too large
}
funcExampleBuffer_Len(){buf:=stubBuffer()fmt.Println("the number of bytes in the UNREAD portion:",buf.Len())fmt.Println("the total space allocated for the buffer's data is:",buf.Cap())p:=make([]byte,1<<4)buf.Read(p)fmt.Println("the number of bytes in the UNREAD portion:",buf.Len())fmt.Println("the total space allocated for the buffer's data is:",buf.Cap())fmt.Println("\ndon't forget Len updates dynamically based on UNREAD bytes")fori:=0;i<buf.Len();i++{b,_:=buf.ReadByte()fmt.Printf(" %q",b)}b,err:=buf.ReadByte()// err should be [io.EOF]
fmt.Printf("\ndidn't make it to the end: byte: %q err: %v",b,err)\(\/\/.*\)\(io\.\w\+\)\(.*\)// Output:
// the number of bytes in the UNREAD portion: 43
// the total space allocated for the buffer's data is: 43
// the number of bytes in the UNREAD portion: 27
// the total space allocated for the buffer's data is: 43
//
// don't forget Len updates dynamically based on UNREAD bytes
// 'e' 'r' ' ' 'w' 'a' 's' ' ' 'a' 'c' 't' 'u' 'a' 'l' 'l'
// didn't make it to the end: byte: 'y' err: <nil>
}
funcExampleBuffer_Next(){buf:=stubBuffer()// NOTE(jay): Memory allocated for returned byte slice. Not as efficient as Buffer.Read
data:=buf.Next(1<<5)fmt.Printf("data Len: %d, Cap: %d, result: %q\n",len(data),cap(data),data)data=buf.Next(1<<62)// Fewer than 1 << 62 returns entire buffer.
fmt.Printf("data Len: %d, Cap: %d, result: %q\n",len(data),cap(data),data)buf.Write([]byte("This is not reflected in data and invalidates data as up to date."))fmt.Printf("same Len: %d, Cap: %d, result: %q\n",len(data),cap(data),data)// Output:
// data Len: 32, Cap: 43, result: "The hand sanitizer was actually "
// data Len: 11, Cap: 11, result: "clear glue."
// same Len: 11, Cap: 11, result: "clear glue."
}
funcExampleBuffer_Read(){buf:=stubBuffer()data:=make([]byte,1<<4)n,err:=buf.Read(data)fmt.Printf("number bytes read into data: %d, any error: %v, result: %q\n",n,err,data[:n])n,err=buf.Read(data)fmt.Printf("number bytes read into data: %d, any error: %v, result: %q\n",n,err,data[:n])n,err=buf.Read(data)fmt.Printf("number bytes read into data: %d, any error: %v, result: %q\n",n,err,data[:n])n,err=buf.Read(data)fmt.Printf("number bytes read into data: %d, any error: %v, result: %q\n",n,err,data)// Output:
// number bytes read into data: 16, any error: <nil>, result: "The hand sanitiz"
// number bytes read into data: 16, any error: <nil>, result: "er was actually "
// number bytes read into data: 11, any error: <nil>, result: "clear glue."
// number bytes read into data: 0, any error: EOF, result: "clear glue.ally "
}
funcExampleBuffer_ReadFrom(){buf:=stubBuffer()buf.ReadFrom(bytes.NewReader([]byte(" There's a message for you if you look to the left.")))fmt.Printf("%q\n",buf.String())// Output:
// "The hand sanitizer was actually clear glue. There's a message for you if you look to the left."
}
funcExampleBuffer_Truncate(){buf:=stubBuffer()buf.Truncate(0)// buf.Reset()
fmt.Printf("reset:\tlen: %d and cap: %d\n\n",buf.Len(),buf.Cap())buf=stubBuffer()buf.Truncate(16)fmt.Printf("trunc:\tlen: %d and cap: %d\n",buf.Len(),buf.Cap())fmt.Printf("data after truncation: %q\n",buf.String())// Output:
// reset: len: 0 and cap: 43
//
// trunc: len: 16 and cap: 43
// data after truncation: "The hand sanitiz"
}
funcExampleBuffer_UnreadByte(){buf:=bytes.NewBuffer([]byte("se al stuf"))varoutbytes.Bufferrepeat:=falseforb,err:=buf.ReadByte();err==nil;b,err=buf.ReadByte(){out.WriteByte(b)switchb{case'e','l','f':repeat=!repeatifrepeat{buf.UnreadByte()}}}fmt.Printf("%q\n\n",out.String())buf.ReadBytes('|')// Drain buf
buf.ReadByte()// err == [io.EOF]
err:=buf.UnreadByte()fmt.Printf("%v\n\n",err)buf.Write([]byte("fill"))buf.ReadByte()buf.Write([]byte("cause error"))err=buf.UnreadByte()fmt.Printf("%v\n",err)// Output:
// "see all stuff"
//
// bytes.Buffer: UnreadByte: previous operation was not a successful read
//
// bytes.Buffer: UnreadByte: previous operation was not a successful read
}
funcExampleBuffer_Write(){buf:=stubBuffer()n,err:=buf.Write([]byte(" I may struggle with geography, but I'm sure I'm somewhere around here."))fmt.Printf("number of bytes written: %d, any error: %v, result: %q\n",n,err,buf.String())// XXX(jay): Panics with [bytes.ErrorTooLarge] if string too large to grow buf, see
// [Buffer.Grow] for example of [bytes.ErrorTooLarge].
// Output:
// number of bytes written: 71, any error: <nil>, result: "The hand sanitizer was actually clear glue. I may struggle with geography, but I'm sure I'm somewhere around here."
}
funcExampleBuffer_WriteByte(){buf:=stubBuffer()fmt.Printf("any error: %v, result: %q\n",buf.WriteByte('}'),buf.String())// XXX(jay): Panics with [bytes.ErrorTooLarge] if string too large to grow buf, see
// [Buffer.Grow] for example of [bytes.ErrorTooLarge].
// Output:
// any error: <nil>, result: "The hand sanitizer was actually clear glue.}"
}
funcExampleBuffer_WriteRune(){buf:=stubBuffer()n,err:=buf.WriteRune('🧴')fmt.Printf("number of bytes written: %d, any error: %v, result: %q\n",n,err,buf.String())// XXX(jay): Panics with [bytes.ErrorTooLarge] if string too large to grow buf, see
// [Buffer.Grow] for example of [bytes.ErrorTooLarge].
// Output:
// number of bytes written: 4, any error: <nil>, result: "The hand sanitizer was actually clear glue.🧴"
}
funcExampleBuffer_WriteString(){buf:=stubBuffer()buf.WriteString(" There was no telling what thoughts would come from the machine.")fmt.Printf("%q\n\n",buf.String())// XXX(jay): Panics with [bytes.ErrorTooLarge] if string too large to grow buf, see
// [Buffer.Grow] for example of [bytes.ErrorTooLarge].
// Output:
// "The hand sanitizer was actually clear glue. There was no telling what thoughts would come from the machine."
}
funcExampleBuffer_WriteTo(){buf:=stubBuffer()dblbuf:=stubBuffer()n,err:=buf.WriteTo(dblbuf)fmt.Printf("number of bytes written: %d, error: %v\n",n,err)fmt.Printf("all the written data plus contents of other writer:\n\t%q\n",dblbuf.String())// Output:
// number of bytes written: 43, error: <nil>
// all the written data plus contents of other writer:
// "The hand sanitizer was actually clear glue.The hand sanitizer was actually clear glue."
}
funcstubReader()*bytes.Reader{returnbytes.NewReader([]byte("The changing of down comforters to cotton bedspreads always meant the gophers had returned."))}
funcExampleReader_Len(){fmt.Println("the number of bytes in the UNREAD portion:",stubReader().Len())// Output:
// the number of bytes in the UNREAD portion: 91
}
funcExampleReader_Reset(){r:=stubReader()fmt.Printf("reset:\tlen: %d and cap: %d\n",r.Len(),r.Size())// resets or switches to new Reader
r.Reset([]byte("All you need to do is pick up the pen and begin."))fmt.Printf("reset:\tlen: %d and cap: %d\n",r.Len(),r.Size())data:=make([]byte,bytes.MinRead)n,err:=r.Read(data)fmt.Printf("number bytes read into data: %d, any error: %v\nresult: %q\n",n,err,data[:n])// Output:
// reset: len: 91 and cap: 91
// reset: len: 48 and cap: 48
// number bytes read into data: 48, any error: <nil>
// result: "All you need to do is pick up the pen and begin."
}
funcExampleReader_Seek(){r:=stubReader()data:=make([]byte,bytes.MinRead)newOffset,err:=r.Seek(66,io.SeekStart)fmt.Printf("offset in reader: %d, any error: %v\n",newOffset,err)n,_:=r.Read(data)fmt.Printf("data read in: %q\n\n",data[:n])newData:=make([]byte,bytes.MinRead)newOffset,err=r.Seek(0,io.SeekStart)fmt.Printf("offset in reader: %d, any error: %v\n",newOffset,err)n,_=r.Read(newData)fmt.Printf("newData read in: %q\n\n",newData[:n])data=make([]byte,bytes.MinRead)newOffset,err=r.Seek(35,io.SeekStart)fmt.Printf("offset in reader: %d, any error: %v\n",newOffset,err)n,_=r.Read(data)fmt.Printf("repurpose data slice: %q\n",data[:n])// Output:
// offset in reader: 66, any error: <nil>
// data read in: "the gophers had returned."
//
// offset in reader: 0, any error: <nil>
// newData read in: "The changing of down comforters to cotton bedspreads always meant the gophers had returned."
//
// offset in reader: 35, any error: <nil>
// repurpose data slice: "cotton bedspreads always meant the gophers had returned."
}
funcExampleReader_Size(){// NOTE(jay): compare with Buffer.Cap()
r:=stubReader()data:=make([]byte,1<<4)r.Read(data)fmt.Printf("the total space allocated for the reader: %d and length of unread data: %d\n",r.Size(),r.Len())// Output:
// the total space allocated for the reader: 91 and length of unread data: 75
}
funcExampleReader_UnreadByte(){// NOTE(jay): For more info checkout [io.ByteReader]
r:=bytes.NewReader([]byte("se al stuf"))varoutbytes.Bufferrepeat:=falseforb,err:=r.ReadByte();err==nil;b,err=r.ReadByte(){out.WriteByte(b)switchb{case'e','l','f':repeat=!repeatifrepeat{r.UnreadByte()}}}fmt.Printf("%q\n\n",out.String())drain:=make([]byte,1<<8)r.Read(drain)// Drain r
r.ReadByte()// err == [io.EOF]
err:=r.UnreadByte()fmt.Printf("does not return error at EOF: %v\n\n",err)fmt.Printf("error: %v\n",stubReader().UnreadByte())// Output:
// "see all stuff"
//
// does not return error at EOF: <nil>
//
// error: bytes.Reader.UnreadByte: at beginning of slice
}
funcExampleReader_WriteTo(){r:=stubReader()varbufbytes.Buffer// NOTE(jay): To see what other Writers exist in the std lib use `guru`.
// https://docs.google.com/document/d/1_Y9xCEMj5S-7rv2ooHpZNH15JgRT5iM742gJkw5LtmQ/edit
n,err:=r.WriteTo(&buf)// Can panic or give error in **_very_** bad circumstances.
fmt.Printf("wrote: %d, error: %v\ntransfer: %q\n",n,err,buf.String())// Output:
// wrote: 91, error: <nil>
// transfer: "The changing of down comforters to cotton bedspreads always meant the gophers had returned."
}