1. 程式人生 > >GO中常用包筆記 bytes(四)

GO中常用包筆記 bytes(四)

g 學習筆記 bytes包

Package bytes

對字節數組進行操作的包。功能和strings包相似.

bytes包提供的功能有:

  1. 和另一個字節數組切片的關系(逐字節比較大小,是否相等/相似,是否包含/包含次數,位置搜索,是否是前綴後綴)

2.字節數組切片和字符串的關系(字符串中是否含有字節數組所包含的rune,以及在字符串中的位置)

3.字節數組切片和rune的關系(字節數組中是否含有特定的或滿足特定條件的rune,以及在字節數組中的位置)

4.字節數組切片和字節的關系(包含/位置)

5.分割分組,分組連結

6.大小寫轉換/標題化

7.修剪兩端

8.按規則修改包含的每個rune

9.重復

10.替換

11.提供處理字節流的Buffer,可讀可寫(strings包沒有提供)

12提供Reader


具體如下:

字節數組切片和字節數組切片的關系(逐字節比較,相等,相似,包含,出現的位置,包含次數,是否是前綴後綴)

func Compare(a, b []byte) int //逐字節比較

func Equal(a, b []byte) bool //是否相等

func EqualFold(s, t []byte) bool //是否相似(只有大小寫可能不同)

func Contains(b, subslice []byte) bool //後者是否為前者子串

func Index(s, sep []byte) int //後者在前者中出現的位置

func LastIndex(s, sep []byte) int

func Count(s, sep []byte) int //sep在s中的重復次數。如果sep為空,返回len([]rune(s))+1

func HasPrefix(s, prefix []byte) bool //後者是否是前者的前綴

func HasSuffix(s, suffix []byte) bool //後者是否是前者的後綴

字符數組切片和字符串的關系(是否包含串裏任何一個rune以及在字符串中的位置)

func ContainsAny(b []byte, chars string) bool //字符串中是否有指定的runes中的任何一個

func IndexAny(s []byte, chars string) int //ContainsAny調用此方法;判斷字符串中是否含有指定的runes中的任何一個;並返回rune出現在字符串中的第一個位置

func LastIndexAny(s []byte, chars string) int

字符數組切片和rune的關系(包含,轉換)

func ContainsRune(b []byte, r rune) bool // rune是否存在於字節數組切片中

func IndexRune(s []byte, r rune) int //從字節切片中尋找一個rune

func IndexFunc(s []byte, f func(r rune) bool) int //從字節切片中尋找滿足條件的rune(的位置)

func LastIndexFunc(s []byte, f func(r rune) bool) int

func Runes(s []byte) []rune //類型轉換

字符數組切片和字節的關系(位置,)

func IndexByte(s []byte, c byte) int //位置

func LastIndexByte(s []byte, c byte) int

操作字節數組切片(使用空格或者符合條件的rune分割分組,將分組連接成字符串,分割,大小寫轉換,修剪兩端,按規則修改包含的每個rune,重復,替換)

func Fields(s []byte) [][]byte //使用空格將字節分組

func FieldsFunc(s []byte, f func(rune) bool) [][]byte //使用特定的rune(滿足條件的)將字節分隔分組

func Join(s [][]byte, sep []byte) []byte //連接成字符串 連接符為string(sep)

func Split(s, sep []byte) [][]byte //分割

func SplitAfter(s, sep []byte) [][]byte //分割後分隔符跟在每組結尾

func SplitAfterN(s, sep []byte, n int) [][]byte

func SplitN(s, sep []byte, n int) [][]byte //指定分割次數

func Title(s []byte) []byte //標題化(首字母大寫)

func ToLower(s []byte) []byte

func ToLowerSpecial(c unicode.SpecialCase, s []byte) []byte

func ToTitle(s []byte) []byte //標題化(所有大寫)

func ToTitleSpecial(c unicode.SpecialCase, s []byte) []byte

func ToUpper(s []byte) []byte

func ToUpperSpecial(c unicode.SpecialCase, s []byte) []byte

func Trim(s []byte, cutset string) []byte

func TrimFunc(s []byte, f func(r rune) bool) []byte

func TrimLeft(s []byte, cutset string) []byte

func TrimLeftFunc(s []byte, f func(r rune) bool) []byte

func TrimPrefix(s, prefix []byte) []byte

func TrimRight(s []byte, cutset string) []byte

func TrimRightFunc(s []byte, f func(r rune) bool) []byte

func TrimSpace(s []byte) []byte

func TrimSuffix(s, suffix []byte) []byte

func Map(mapping func(r rune) rune, s []byte) []byte //按照指定的方法修改字節數組切片中的每個rune(返回的是副本)

func Repeat(b []byte, count int) []byte //重復

func Replace(s, old, new []byte, n int) []byte //替換



type Reader

func NewReader(b []byte) *Reader

func (r *Reader) Len() int //未讀的長度

func (r *Reader) Read(b []byte) (n int, err error)

func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) //拷貝從指定位置到結束的全部字節到b中,不會改變狀態

func (r *Reader) ReadByte() (byte, error)

func (r *Reader) ReadRune() (ch rune, size int, err error)

func (r *Reader) Reset(b []byte) //重置位置,並用b作為數據源

func (r *Reader) Seek(offset int64, whence int) (int64, error) //設置讀取指針的位置到指定的offset,whence的取值為io.SeekStart ,io.SeekStart ,io.SeekEnd,分別表示絕對偏移,相對文件起始位置偏移,相對文件終止位置偏移

func (r *Reader) Size() int64 //可讀的總大小

func (r *Reader) UnreadByte() error //撤銷

func (r *Reader) UnreadRune() error //撤銷

func (r *Reader) WriteTo(w io.Writer) (n int64, err error)


type Buffer //strings包沒有類似功能

func NewBuffer(buf []byte) *Buffer

func NewBufferString(s string) *Buffer

func (b *Buffer) Bytes() []byte //返回未讀部分的切片

func (b *Buffer) Cap() int //Buffer總容量

func (b *Buffer) Grow(n int) //為Buffer擴充容量

func (b *Buffer) Len() int //未讀部分的大小

func (b *Buffer) Next(n int) []byte //連續讀出一段數據,返回切片

func (b *Buffer) Read(p []byte) (n int, err error) //讀出len(p)長度的數據 而非cap(p)

func (b *Buffer) ReadByte() (byte, error) //讀出字節

func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)//連續讀出 直到遇到delim(返回的切片包含delim)

func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) //將io.reader作為數據源

func (b *Buffer) ReadRune() (r rune, size int, err error) //讀一個rune,r, n := utf8.DecodeRune(b.buf[b.off:])

func (b *Buffer) ReadString(delim byte) (line string, err error) //讀出string,直到遇到delim

func (b *Buffer) Reset() //重置,清空buffer

func (b *Buffer) String() string //將未讀出部分作為string返回(不改變狀態)

func (b *Buffer) Truncate(n int) //除了保留n長度的未讀內容 其余未讀內容清空,壓縮BUFFER

func (b *Buffer) UnreadByte() error //撤銷

func (b *Buffer) UnreadRune() error

func (b *Buffer) Write(p []byte) (n int, err error) //將p寫入Buffer尾部

func (b *Buffer) WriteByte(c byte) error //將字節寫入

func (b *Buffer) WriteRune(r rune) (n int, err error) //將Rune寫入

func (b *Buffer) WriteString(s string) (n int, err error) //將字符串寫入

func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) //將未讀部分寫入io.writer 並清空Buffer



GO中常用包筆記 bytes(四)