第二天:golang学习笔记之Byte

编码

名称 摘要
★bytes bytes包实现了操作[]byte的常用函数.
encoding encoding包定义了供其它包使用的可以将数据在字节水平和文本表示之间转换的接口.
strconv strconv包实现了基本数据类型和其字符串表示的相互转换.
strings strings包实现了用于操作字符的简单函数.
text scanner对utf-8文本的token扫描服务,tabwriter缩进修正
unicode unicode 包提供了一些测试Unicode码点属性的数据和函数.

bytes

bytes包共有两个Struct分别是 Reader
Buffer
以及一个 ErrTooLarge
的er

  • bytes文件定义了操作[]byte的众多函数:

    • 类型之间的关系:rune,UTF-8,byte[]

      • Contains(b, subslice []byte) bool
      • ContainsAny(b []byte, chars string) bool :相当于UTF-8版本的Contains
      • ContainsRune(b []byte, r rune) bool
      • HasPrefix(s, prefix []byte) bool
      • HasSuffix(s, suffix []byte) bool
      • Equal(a, b []byte) bool
      • EqualFold(s, t ) :UTF8有这样一个函数SimpleFold(r rune):循环迭代一个字符,所以这个就是不区分大小写的Equal
      • Compare(a, b []byte):result will be 0 if a==b, -1 if a b.
    • 寻址

      • IndexByte(b []byte, c byte) int
      • LastIndex(s, sep []byte)
      • LastIndexByte(s []byte, c byte) int
      • IndexRune(s []byte, r rune)
      • IndexAny(s []byte, chars string)
    • 串的自身属性

      • Count(s, sep []byte) int
      • IsUpper(r rune) bool
      • IsLower(r rune) bool
      • IsTitle(r rune) bool
    • 串的操作

      • SplitN(s, sep []byte, n int) [][]byte
      • SplitAfterN(s, sep []byte, n int) [][]byte
      • Split(s, sep []byte) [][]byte
      • SplitAfter(s, sep []byte) [][]byte
      • Fields(s []byte) [][]byte :通过空白字符分割如\n \b \t
      • FieldsFunc(s []byte, f func(rune) bool) [][]byte:通过分割函数分割
      • Join(s [][]byte, sep []byte) []byte
      • Map(mapping func(r rune) rune, s []byte) []byte:遍历byte串并对每个rune执行func,若func返回值不存在,则去除本字符,相当于for each rune in []byte遍历
      • Repeat(b []byte, count int) []byte:相当于python的”asd” * 5
      • Replace(s, old, new []byte, n int) []byte:n是最多替换次数 n<0代表无限次
      • ReplaceAll(s, old, new []byte) []byte
    • 格式转换 // 接下来几个函数前几个都是通过Map函数为基础实现的

      • ToUpper(s []byte) []byte
      • ToLower(s []byte) []byte
      • ToTitle(s []byte) []byte:和ToUpper类似,因为Title字符(即一个单词第一个字符如ωωω)一般都是大写字符如Ωωω
      • Title(s []byte) []byte:首字母大写
      • Trim(s []byte, cutset string)
      • Trim***(s []byte, cutset string) []byte:***可以有如下:Left Right Space
      • ToValidUTF8(s, replacement []byte) []byte:把无效的utf-8字符替换成后面的比如 “略\x99哈哈”->”略wtf哈哈”
      • Runes(s []byte) []rune

Reader

  • Reader定义如下
type Reader struct {
    s        []byte
    i        int64 // current reading index
    prevRune int   // index of previous rune; or < 0  (-1)
}

均为私有成员,s指一段字符串,i即当前读到的游标默认为0,prevRune为上次的读到的rune值(type rune = int32)。所以仅能使用 NewReader()
函数构造一个 Reader

  • Reader 的方法有Len, Size, Reset均是对游标的操作
  • Reader实现了大部分的IO接口,如Readerr、ReadAt、ReadSeeke、ByteReader、ByteScanner、RuneReader、RuneScanner、WriteTo

Buffer

  • Buffer定义如下
type Buffer struct {
    buf      []byte // contents are the bytes buf[off : len(buf)]
    off      int    // read at &buf[off], write at &buf[len(buf)]
    lastRead readOp // last read operation, so that Unread* can work correctly.
}

均为私有成员,buf为底层数组,off为读游标默认为0,lastRead为一个标记,其定义如下:

type readOp int8
const (
    opRead      readOp = -1 // Any other read operation.
    opInvalid   readOp = 0  // Non-read operation.
    opReadRune1 readOp = 1  // Read rune of size 1.
    opReadRune2 readOp = 2  // Read rune of size 2.
    opReadRune3 readOp = 3  // Read rune of size 3.
    opReadRune4 readOp = 4  // Read rune of size 4.
)

另外还定义了buffer数组最小大小: const smallBufferSize = 64
(单位为int),常量在grow(n int)函数中被使用,此函数返回写数据时写的位置,当要写入数据的byte数n小于smallBufferSize时就直接分配一个size为n、cap为smallBufferSize的slice可减少系统内存分配次数开销

  • Buffer 的公有方法有:

    • Bytes:返回可用的底层数组buf[b.off:]
    • Len:buf可读大小
    • Cap:buf总大小
    • Reset:将可读部分复制到buf[0:]并重置游标
    • Truncate:清空buf
    • Grow(n int):扩充buf的Cap使可写大小大于等于n。
  • Buffer在Read的基础上实现了更多的写接口,如ByteWriter,ReadFrom,Writer
  • 可以看出Buffer可以同时读写,用途更加广泛