golang中bufio.SplitFunc的深入理解

這篇文章主要給大家介紹了關於golang中bufio.SplitFunc的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用golang具有一定的參考學習價值,需要的朋友們下面隨着小編來一起學習學習吧

前言

bufio模塊是golang標準庫中的模塊之一,主要是實現了一個讀寫的緩存,用於對數據的讀取或者寫入操作。該模塊在多個涉及io的標準庫中被使用,比如http模塊中使用buffio來完成網絡數據的讀寫,壓縮文件的zip模塊利用bufio來操作文件數據的讀寫等。

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
}

參考

In-depth introduction to bufio.Scanner in Golang

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對神馬文庫的支持。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章