Go language learning (ten) bytes packet processing byte slice

Keywords: Google

The bytes package provides a series of functions for reading and writing byte slices
There are many functions for byte slicing, including basic processing function, comparison function, suffix check function, index function and partition function
Case handling function and sub slice handling function, etc

1. Basic processing function api of byte slicing

1.1 contents() function

//The Contains() function checks whether byte slice b contains subslice. If yes, it returns true. Otherwise, it returns false
func Contains(b,subslice []bytes) bool
  • 1
  • 2

1.2Count() function

//The Count() function calculates the number of non overlapping display of byte slice sep in byte slice s
func Count(s,sep[]byte) int
  • 1
  • 2

1.3 repeat() function

//The Repeat() function copies count of slice b, and then synthesizes a new byte slice to return
func Repeat(b[]byte,count int) []byte
  • 1
  • 2

1.4 replace() function

/*Replace()The function returns a copy of the byte slice s and sets the first n non overlapping
 If n < 0, the number of replacement is not limited. Parameter n is the number of replacement*/
func Replace(s,old,new []byte,n int) []byte
  • 1
  • 2
  • 3

1.5runs() function

//The functions of the runs() function are to convert s into UTF-8 encoded byte sequence and return the corresponding Unicode slice
func Runes(s []byte) []rune
  • 1
  • 2

1.6Join() function

Join The function slices bytes sep hold s Each byte slice in is concatenated into a byte slice and returned.
func Join(s [][]byte,sep[]byte) []byte
  • 1
  • 2

[example]

package main

import(
    "fmt"
    "bytes"
)
func main(){
    //Contains
    b := []byte("mChenys") //String strong to byte slice
    sublice1 := []byte("m")
    sublice2 := []byte("M")
    fmt.Println(bytes.Contains(b,sublice1))//true
    fmt.Println(bytes.Contains(b,sublice2))//false

    //Count
    s := []byte("hahaahaaa")
    sep1 := []byte("hah")
    sep2 := []byte("aa")
    sep3 := []byte("a")
    fmt.Println(bytes.Count(s,sep1))//1
    fmt.Println(bytes.Count(s,sep2))//2
    fmt.Println(bytes.Count(s,sep3))//6

    //Repeat
    b = []byte("ha")
    fmt.Println(string(bytes.Repeat(b,1)))//ha
    fmt.Println(string(bytes.Repeat(b,2)))//haha

    //Replace
    s = []byte("hello,world")
    old := []byte("o")
    news := []byte("ee")
    fmt.Println(string(bytes.Replace(s,old,news,0)))//hello,world
    fmt.Println(string(bytes.Replace(s,old,news,1)))//hellee,world
    fmt.Println(string(bytes.Replace(s,old,news,2)))//hellee,weerld
    fmt.Println(string(bytes.Replace(s,old,news,-1)))//hellee,weerld

    //Runes
    s = []byte("Hello world")
    r := bytes.Runes(s)
    fmt.Println("Length of string before conversion: ",len(s))//12
    fmt.Println("Length of converted String: ",len(r))//4

    //Join
    s := [][]byte{[]byte("Hello"),[]byte("world")}
    sep1 := []byte(",")
    fmt.Println(string(bytes.Join(s,sep1)))//Hello, the world
    sep2 := []byte("#")
    fmt.Println(string(bytes.Join(s,sep2)))//Hello world
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

2. Byte slice comparison function

2.1Compare() function

/*Compare()The function compares the size of byte slice a and b according to the value of byte. If a=b, it returns 0,
If a > B returns 1, if a < B returns - 1*/
func Compare(a,b[]byte) int
  • 1
  • 2
  • 3

2.2Equal() function

/*Equal()The function is used to compare whether two byte slices are equal. If the parameter is nil, it is equivalent to
 Null byte slice, if a=b, then return true, otherwise return false. Case sensitive*/
func Equal(a,b[]byte) bool
  • 1
  • 2
  • 3

2.3 equalfold() function

/*EqualFold()The function converts s and t to UTF-8 strings for comparison,
Case is ignored. If s=t, true is returned. Otherwise, false is returned*/
func EqualFold(s,t[]byte) bool
  • 1
  • 2
  • 3

[example]

package main

import(
    "fmt"
    "bytes"
)
func main(){
    //Compare
    a := []byte("abc")
    b := []byte("a")
    fmt.Println(bytes.Compare(a,b))//1 
    b =[]byte("abcd")
    fmt.Println(bytes.Compare(a,b))//-1
    b =[]byte("abC")
    fmt.Println(bytes.Compare(a,b))//1 lower case letters are greater than upper case letters
    b =[]byte("b")
    fmt.Println(bytes.Compare(a,b))//-1 compare from the first byte, if the same, then compare the length

    //Equal
    a = []byte("abc")
    b = []byte("ABC")
    fmt.Println(bytes.Equal(a,b))//false
    fmt.Println(bytes.Equal(a,nil))//false
    b = []byte("abc")
    fmt.Println(bytes.Equal(a,b))//true

    //EqualFold
    a = []byte("abc")
    b = []byte("ABC")
    fmt.Println(bytes.EqualFold(a,b))//true

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

3. Check before and after byte slicing

3.1 hasprefix() function

//The function of HasPrefix() is to check whether the prefix of byte slice s is prefix. If it is true, if it is not false
func HasPrefix(s,prefix[]byte) bool
  • 1
  • 2

3.2 hashsuffix() function

//The function of HashSuffix() checks whether the suffix of byte slice s is suffix. If it is true, it returns false
func HashSuffix(s,suffix[]byte) bool
  • 1
  • 2

[example]

package main

import(
    "fmt"
    "bytes"
)
func main(){
    //HasPrefix
    s := []byte("mChenys")
    prefix := []byte("m")
    fmt.Println(bytes.HasPrefix(s,prefix))//true
    prefix = []byte("men")
    fmt.Println(bytes.HasPrefix(s,prefix))//false

    //HashSuffix
    suffix := []byte("ys")
    fmt.Println(bytes.HasSuffix(s,suffix))//true

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

4. Byte slice position index function

There are 8 byte slice position index functions, index(), indexany(), indexbyte(), indexfunc(), indexrun()
LastIndex(),LastIndexAny(), and LastIndexFunc()

4.1index() function

//Returns the location index of the first occurrence of sep in s (starting from 0), and - 1 if sep is not in s
func Index(s,sep []byte) int
  • 1
  • 2

4.2 indexany() function

/*Parses s s into UTF-8 encoded byte sequence, returns the first time in s any character in chars
 The index position that appears; returns - 1 if s does not contain any of the chars characters*/
func IndexAny(s []byte,chars string) int
  • 1
  • 2
  • 3

4.3 indexbyte() function

//The function checks the location index of the first occurrence of byte c in s, and returns - 1 if s does not contain c
func IndexByte(s[]byte,c byte) int
  • 1
  • 2

4.4IndexFunc() function

/*Its function is to parse s into UTF-8 byte sequence and return a position of character c satisfying f(c)=true
 Index, if not, return - 1*/
func IndexFunc(s[]byte,f func(r rune)bool) int
  • 1
  • 2
  • 3

4.5indexrun() function

/*The function is to parse s into UTF-8 byte sequence, and return the position index of the rune type character r in S,
Returns - 1 if s does not contain r*/
func IndexRune(s[]byte,r rune) int
  • 1
  • 2
  • 3

4.6 lastindex() function

//The function of is to return the location index of the last occurrence of sep in s, and - 1 if s does not contain sep
func LastIndex(s,sep[]byte) int
  • 1
  • 2

4.7LastIndexAny() function

/*Its function is to parse s into UTF-8 byte sequence and return any character in chars at the end of S
 The location index of the occurrence, if chars is empty or s does not contain any characters in chars, returns - 1*/
func LastIndexAny(s[]byte,chars string) int
  • 1
  • 2
  • 3

4.8 lastindexfunc() function

/*The function is to parse s into UTF-8 byte sequence and return the character c satisfying f(s)=true at the end of S
 One time location index, if not found, - 1*/
func LastIndexFunc(s[]byte,f func(r rune)bool) int
  • 1
  • 2
  • 3

[example]

package main

import(
    "fmt"
    "bytes"
)
func main(){
    //Index
    a := []byte("aaaaa")
    fmt.Println(bytes.Index(a,[]byte("a")))//0
    fmt.Println(bytes.Index(a,[]byte("aa")))//0
    fmt.Println(bytes.Index(a,[]byte("b")))//-1

    //IndexAny
    fmt.Println(bytes.IndexAny(a,"a"))//0
    fmt.Println(bytes.IndexAny(a,"aa"))//0

    //IndexByte
    s := []byte("google")
    var ch byte = 'g'
    fmt.Println(bytes.IndexByte(s,ch))//0

    //IndexFunc, which can receive anonymous functions
    fmt.Println(bytes.IndexFunc(s,func (a rune)bool{
        if a == 'o'{
            return true
        }else{
            return false
        }
    }))//1

    //IndexRune
    fmt.Println(bytes.IndexRune(s,'e'))//5
    fmt.Println(bytes.IndexRune(s,'a'))//-1

    //LastIndex
    fmt.Println(bytes.LastIndex(s,[]byte("g")))//3
    fmt.Println(bytes.LastIndex(s,[]byte("e")))//5
    fmt.Println(bytes.LastIndex(s,[]byte("o")))//2

    //LastIndexAny
    fmt.Println(bytes.LastIndexAny(s,"gle"))//5
    fmt.Println(bytes.LastIndexAny(s,"l"))//4
    fmt.Println(bytes.LastIndexAny(s,"ge"))//5

    //LastIndexFunc
    fmt.Println(bytes.LastIndexFunc(s,func(r rune)bool{
        if r=='g'{
            return true
        }else {
            return false
        }
    }))//3
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

5. Byte slice partition function

There are 6 byte slice partition functions, Fields(),FieldsFunc(),Split(),SplitN()
SplitAfter() and SplitAfter()

5.1Fields() function

/*The function is to divide byte slice s into several byte slices according to one or more consecutive blank characters,
If s contains only blank characters, a null byte slice is returned, where s is the byte slice to be split*/
func Fields(s[]byte) [][]byte wq
  • 1
  • 2
  • 3

5.2FieldsFunc() function

/*The function is to parse s into UTF-8 byte sequence. For each Unicode character c, if f(c)
Return true to split s as a split character. If all characters meet the f(c) is true, a null slice is returned*/
func FieldsFunc(s []byte,f func(r rune)bool) [][]byte
  • 1
  • 2
  • 3

5.3Split() function

/*The function is to Split s into multiple byte slices with sep and return them. If sep is empty, Split will Split s into
 Each byte slice corresponds to a UTF-8 character, and Split() is equivalent to the splitN() function with parameter n*/
func Split(s,sep[]byte)[][]byte
  • 1
  • 2
  • 3

5.4plitafter() function

/*Function uses sep as suffix to slice s into several bytes and return. If sep is empty,
Then cut s into each byte slice corresponding to a UTF-8 character*/
func SplitAfter(s,sep[]byte)[][]byte
  • 1
  • 2
  • 3

5.5splitafter() function

/*The function is to slice s into several bytes with sep as the suffix and return. If sep is empty, slice s into each byte
 Corresponds to a UTF-8 character. Parameter n determines the length of the returned slice: if n > 0, at most N sub byte slices are returned,
Subsets may contain a sequence of uncut bytes; if n=0, an empty slice is returned if n < 0, all subsets are returned.*/
func SplitAfterN(s,sep[]byte,n int)[][]byte
  • 1
  • 2
  • 3
  • 4

5.6 split() function

/*The function is to Split s into multiple byte slices with sep and return them. If sep is empty, Split will Split s
 Each byte slice corresponds to a UTF-8 character. The return length is determined by the parameter n, and the sub slice can be returned when n > 0;
n==0 Return nothing, n < 0 return empty slice*/
func SplitN(s,sep []byte,n int)[][]byte
  • 1
  • 2
  • 3
  • 4

[example]

package main

import(
    "fmt"
    "bytes"
)
func main(){
    //Fields, 2-D slice returned
    s := []byte("a b   c")
    for _,v := range bytes.Fields(s){ 
        //Traverse to get 1-D slice, and then convert it to string
        fmt.Print(string(v)+",") //a,b,c,
    }

    //FieldsFunc, return is 2D slice, receive anonymous function
    for _,v := range bytes.FieldsFunc(s,func(r rune)bool{
        if r == ' '{
            return true //Split by white space
        }else{
            return false
        }
    }){
        fmt.Print(string(v)+",")//a,b,c,
    }

    //Split
    s = []byte("Eating and sleeping")
    for _,v := range bytes.Split(s,[]byte("and")){
        fmt.Print(string(v)+",")//Eat, sleep,
    }
    for _,v := range bytes.Split(s,nil){
        fmt.Print(string(v)+",")//Eat, eat, and sleep,
    }

    //SplitAfter
    s = []byte("abbcbbd")
    for _,v := range bytes.SplitAfter(s,[]byte("bb")){
        fmt.Print(string(v)+",")//abb,cbb,d,
    }
    for _,v := range bytes.SplitAfter(s,nil){
        fmt.Print(string(v)+",")//a,b,b,c,b,b,d,
    }

    //SplitAfterN
    s = []byte("hehehe")
    for _,v := range bytes.SplitAfterN(s,[]byte("he"),0){
        fmt.Print(string(v)+",") //Output nothing
    }
    for _,v := range bytes.SplitAfterN(s,[]byte("he"),1){
        fmt.Print(string(v)+",")//hehehe,
    }
    for _,v := range bytes.SplitAfterN(s,[]byte("he"),-1){
        fmt.Print(string(v)+",")//he,he,he,,
    }

    //SplitN
    s = []byte("hahaha")
    for _,v := range bytes.SplitN(s,[]byte("ha"),0){
        fmt.Print(string(v)+",") //Output nothing
    }
    for _,v := range bytes.SplitN(s,[]byte("ha"),1){
        fmt.Print(string(v)+",")//hahaha,
    }
    for _,v := range bytes.SplitN(s,[]byte("ha"),-1){
        fmt.Print(string(v)+",")//,,,,
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

6. Byte slice case processing

There are 7 functions, Title(),ToTitle(),ToTitleSpecial(),ToLower(),ToLowerSpecial(),ToUpper()
And ToUpperSpecial()

6.1Title() function

//The function of S is to return a copy of S and change the initial of each word in s to Unicode character uppercase
func Title(s[]byte) []byte
  • 1
  • 2

6.2ToTitle() function

//The function of s is to return a copy of s and capitalize all Unicode characters in s
func ToTitle(s []byte) []byte
  • 1
  • 2

6.3totilespecial() function

/*The function is to return a copy of s and specify all Unicode characters in it according to "U case"
The rule of is capitalized*/
func ToTitleSpecial(_case unicode.SpecialCase,s []byte) []byte
  • 1
  • 2
  • 3

6.4ToLower() function

//The function of s is to return a copy of s and turn all Unicode characters into lowercase
func ToLower(s []byte)[]byte
  • 1
  • 2

6.5ToLowerSpecial() function

/*The function is to return a copy of s, and put all Unicode characters in it according to the "U case"
The specified rule is converted to lowercase*/
func ToLowerSpecial(_case unicode.SpecialCase, s []byte) []byte 
  • 1
  • 2
  • 3

6.6ToUpper() function

//Returns a copy of s and capitalizes all Unicode characters in it
func ToUpper(s []byte) []byte 
  • 1
  • 2

6.7ToUpperSpecial() function

/*The function of is to return a copy of s, and all Unicode characters in it are based on the "U case"
The specified rule is capitalized*/
func ToUpperSpecial(_case unicode.SpecialCase, s []byte) []byte
  • 1
  • 2
  • 3
package main

import(
    "fmt"
    "bytes"
    "unicode"
)
func main(){
    s := []byte("abc")
    fmt.Println(string(bytes.Title(s)))//Abc
    fmt.Println(string(bytes.ToTitle(s)))//ABC
    fmt.Println(string(bytes.ToTitleSpecial(unicode.AzeriCase,s)))//ABC
    s = []byte("ABC")
    fmt.Println(string(bytes.ToLower(s)))//abc
    fmt.Println(string(bytes.ToLowerSpecial(unicode.AzeriCase,s)))//abc
    s = []byte("abc")
    fmt.Println(string(bytes.ToUpper(s)))//ABC
    fmt.Println(string(bytes.ToUpperSpecial(unicode.AzeriCase,s)))//ABC

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

7. Sub byte slice processing function

There are 9 in total, trim (), trimfunc (), trimleft (), trimleftfunc (), trimlight (), trimlightfunc ()
TrimSpace(),TrimPrefix(), and TrimSuffix()

7.1Trim() function

/*The function of cutset is to return the sub byte slice of s, and any cutset that appears at the head and tail of s
 Consecutive characters will be removed.*/
func Trim(s []byte, cutset string) []byte 
  • 1
  • 2
  • 3

7.2 trimfunc() function

//The function is to return the sub byte slice of s, and delete the character c satisfying f(c)=true connected by the head and tail of s.
func TrimFunc(s []byte, f func(r rune) bool) []byte 
  • 1
  • 2

7.3TrimLeft() function

//The function of cutset is to return the sub byte slice of S, and any continuous characters appearing at the beginning of s in cutset are deleted.
func TrimLeft(s []byte, cutset string) []byte 
  • 1
  • 2

7.4 trimleftfunc() function

//The function is to return a sub byte slice of S and delete the character c whose first part of s continuously satisfies f(c)=true.
func TrimLeftFunc(s []byte, f func(r rune) bool) []byte  
  • 1
  • 2

7.5 trimlight() function

//The function of S is to return the SubByte slice of S, and any consecutive characters appearing at the end of s in cutset are deleted.
func TrimRight(s []byte, cutset string) []byte 
  • 1
  • 2

7.6 trimrightfunc() function

//The function is to return a sub byte slice of s and delete the character c whose tail continuously satisfies f(c)=true
func TrimRightFunc(s []byte, f func(r rune) bool) []byte 
  • 1
  • 2

7.7TrimSpace() function

/*The function is to return a sub byte slice of s, and delete the continuous at the beginning and end of s
Unicode Blank characters.*/
func TrimSpace(s []byte) []byte 
  • 1
  • 2
  • 3

7.8TrimPrefix() function

//The function is to return a sub byte slice of s and delete the part prefixed with prefix
func TrimPrefix(s, prefix []byte) []byte
  • 1
  • 2

7.9TrimSuffix() function

//The function is to return a sub byte slice of s and delete the suffix suffix suffix
func TrimSuffix(s, suffix []byte) []byte
  • 1
  • 2

8[Example]

package main

import(
    "fmt"
    "bytes"
)
func main(){
    //Trim
    s := []byte(" abc   ")
    fmt.Println(string(bytes.Trim(s," "))+"d")//abcd

    //TrimFunc
    s = []byte("hello world")
    fmt.Println(string(bytes.TrimFunc(s,func(r rune)bool{
        if r=='h' || r=='d'{
            return true
        }else{
            return false
        }
    }))) //ello worl


    s = []byte("helloh")
    fmt.Println(string(bytes.TrimFunc(s,func(r rune)bool{
        if r=='h' || r=='o'{
            return true
        }else{
            return false
        }
    }))) //ell

    s = []byte("helloh")
    fmt.Println(string(bytes.TrimFunc(s,func(r rune)bool{
        if r=='h' && r=='o'{
            return true
        }else{
            return false
        }
    }))) //helloh

    //TrimLeft
    fmt.Println(string(bytes.TrimLeft(s,"h")))//elloh
    fmt.Println(string(bytes.TrimLeft(s,"l")))//helloh

    //TrimLeftFunc
    fmt.Println(string(bytes.TrimLeftFunc(s,func(r rune)bool{
        if r == 'h' || r=='l'{
            return true
        }else{
            return false
        }

    }))) //elloh

    //TrimRight
    fmt.Println(string(bytes.TrimRight(s,"oh")))//hell

    //TrimRightFunc
    fmt.Println(string(bytes.TrimRightFunc(s,func(r rune)bool{
        if r == 'h'{
            return true
        }else{
            return false
        }

    })))//hello

    //TrimSpace
    s = []byte("  abc   ")
    fmt.Println("d"+string(bytes.TrimSpace(s))+"d")//dabcd

    //TrimPrefix
    s = []byte("mChenys")
    fmt.Println(string(bytes.TrimPrefix(s,[]byte("mC"))))//henys
    fmt.Println(string(bytes.TrimPrefix(s,[]byte("en"))))//mChenys

    //TrimSuffix
    s = []byte("mChenys")
    fmt.Println(string(bytes.TrimSuffix(s,[]byte("ys"))))//mChen
    fmt.Println(string(bytes.TrimSuffix(s,[]byte("en"))))//mChenys
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81


    

Posted by Fari on Sat, 02 May 2020 10:52:58 -0700