深入理解golang中bufio.SplitFunc

栏目: Go · 发布时间: 5年前

内容简介:golang的在这个程序很简单,

前言

golang的 bufio 包里面定以的 SplitFunc 是一个比较重要也比较难以理解的东西,本文希望通过结合简单的实例介绍 SplitFunc 的工作原理以及如何实现一个自己的 SplitFunc

一个例子

bufio 包里面定义了一些常用的 工具 比如 Scanner ,你可能需要读取用户在标准输入里面输入的一些东西,比如我们做一个 复读机 ,读取用户的每一行输入,然后打印出来:

package main
import (
  "bufio"
  "fmt"
  "os"
)
func main() {
  scanner := bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanLines)
  for scanner.Scan()  {
    fmt.Println(scanner.Text())
  }
}

这个程序很简单, os.Stdin 实现了 io.Reader 接口,我们从这个reader创建了一个 scanner ,设置分割函数为 bufio.ScanLines ,然后 for 循环,每次读到一行数据就将文本内容打印出来。麻雀虽小五脏俱全,这个小程序虽然简单,却引出了我们今天要介绍的对象: bufio.SplitFunc ,它的定义是这个样子的:

package "buffio"
type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)

golang官方文档的描述是这个样子的:

SplitFunc is the signature of the split function used to tokenize the input. The arguments are an initial substring of the remaining unprocessed data and a flag, atEOF, that reports whether the Reader has no more data to give. The return values are the number of bytes to advance the input and the next token to return to the user, if any, plus an error, if any.
Scanning stops if the function returns an error, in which case some of the input may be discarded.
Otherwise, the Scanner advances the input. If the token is not nil, the Scanner returns it to the user. If the token is nil, the Scanner reads more data and continues scanning; if there is no more data--if atEOF was true--the Scanner returns. If the data does not yet hold a complete token, for instance if it has no newline while scanning lines, a SplitFunc can return (0, nil, nil) to signal the Scanner to read more data into the slice and try again with a longer slice starting at the same point in the input.
The function is never called with an empty data slice unless atEOF is true. If atEOF is true, however, data may be non-empty and, as always, holds unprocessed text.

英文!参数这么多!返回值这么多!好烦!不知道各位读者遇到这种文档会不会有这种感觉...正式由于这种情况,我才决定写一篇文章介绍一下 SplitFunc 的具体工作原理,用一种通俗的方式结合具体实例加以说明,希望对读者有所帮助。

好了,废话少说,开始正题吧!

Scanner和SplitFunc的工作机制

package "buffio"
type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)

Scanner 是有缓存的,意思是 Scanner 底层维护了一个 Slice 用来保存已经从 Reader 中读取的数据, Scanner 会调用我们设置 SplitFunc ,将缓冲区内容(data)和是否已经输入完了(atEOF)以参数的形式传递给 SplitFunc ,而 SplitFunc 的职责就是根据上述的两个参数返回下一次 Scan 需要前进几个字节(advance),分割出来的数据(token),以及错误(err)。

这是一个通信双向的过程, Scanner 告诉我们的 SplitFunc 已经扫描到的数据和是否到结尾了,我们的 SplitFunc 则根据这些信息将分割的结果返回和下次扫描需要前进的位置返回给 Scanner 。用一个例子来说明:

package main
import (
    "bufio"
    "fmt"
    "strings"
)
func main() {
    input := "abcdefghijkl"
    scanner := bufio.NewScanner(strings.NewReader(input))
    split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
        fmt.Printf("%t\t%d\t%s\n", atEOF, len(data), data)
        return 0, nil, nil
    }
    scanner.Split(split)
    buf := make([]byte, 2)
    scanner.Buffer(buf, bufio.MaxScanTokenSize)
    for scanner.Scan() {
        fmt.Printf("%s\n", scanner.Text())
    }
}

输出

false 2 ab  false 4 abcd  false 8 abcdefgh  false 12 abcdefghijkl  true 12 abcdefghijkl

这里我们把缓冲区的初始大小设置为了2,不够的时候会扩展为原来的2倍,最大为 bufio.MaxScanTokenSize ,这样一开始扫描2个字节,我们的缓冲区就满了,reader的内容还没有读取到EOF,然后split函数执行,输出:

false 2 ab

紧接着函数返回 0, nil, nil 这个返回值告诉Scanner数据不够,下次读取的位置前进0位,需要继续从reader里面读取,此时因为缓冲区满了,所以容量扩展为 2 * 2 = 4 ,reader的内容还没有读取到EOF,输出

false 4 abcd

重复上述步骤,一直到最后全部内容读取完了,EOF此时变成了true

true 12 abcdefghijkl

看了上面的过程是不是对SplitFunc的工作原来有了一点理解了呢?再回头看一下golang的官方文档有没有觉得稍微理解了一点?下面是 bufio.ScanLines 的实现,读者可以自己研究一下该函数是如何工作的

标准库里的ScanLines

func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
    // 表示我们已经扫描到结尾了
    if atEOF && len(data) == 0 {
        return 0, nil, nil
    }
    // 找到\n的位置
    if i := bytes.IndexByte(data, '\n'); i >= 0 {
        // 把下次开始读取的位置向前移动i + 1位
        return i + 1, dropCR(data[0:i]), nil
    }
    // 这里处理的reader内容全部读取完了,但是内容不为空,所以需要把剩余的数据返回
    if atEOF {
        return len(data), dropCR(data), nil
    }
    // 表示现在不能分割,向Reader请求更多的数据
    return 0, nil, nil
}

参考


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java多线程编程实战指南(设计模式篇)

Java多线程编程实战指南(设计模式篇)

黄文海 / 电子工业出版社 / 2015-10 / 59.00

随着CPU 多核时代的到来,多线程编程在充分利用计算资源、提高软件服务质量方面扮演了越来越重要的角色。而 解决多线程编程中频繁出现的普遍问题可以借鉴设计模式所提供的现成解决方案。然而,多线程编程相关的设计模式书籍多采用C++作为描述语言,且书中所举的例子多与应用开发人员的实际工作相去甚远。《Java多线程编程实战指南(设计模式篇)》采用Java(JDK1.6)语言和UML 为描述语言,并结合作者多......一起来看看 《Java多线程编程实战指南(设计模式篇)》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具