从读者读取直到到达字符串

时间:2013-07-29 05:54:56

标签: string go

我正在尝试编写一个函数来继续读取缓冲读取器,直到我点击某个字符串,然后停止读取并返回该字符串之前读取的所有内容。

换句话说,我想做与reader.ReadString()相同的事情,除了取一个字符串而不是一个字节。

例如:

mydata, err := reader.ReadString("\r\n.\r\n") //obviously will not compile

我该怎么做?

提前致谢,

Twichy

修正案1:先前的尝试

这是我以前的尝试;它编写得很糟糕,并没有起作用,但希望它能证明我的目的。

func readDotData(reader *bufio.Reader)(string, error){
delims := []byte{ '\r', '\n', '.', '\r', '\n'}
curpos := 0
var buffer []byte
for {
    curpos = 0
    data, err := reader.ReadSlice(delims[0])
    if err!=nil{ return "", err }
    buffer = append(buffer, data...)
    for {
        curpos++
        b, err := reader.ReadByte()
        if err!=nil{ return "", err }
        if b!=delims[curpos]{
            for curpos >= 0{
                buffer = append(buffer, delims[curpos])
                curpos--
            }
            break
        }
        if curpos == len(delims){
            return string(buffer[len(buffer)-1:]), nil
        }
    }
}
panic("unreachable")
}

4 个答案:

答案 0 :(得分:7)

package main

import (
        "bytes"
        "fmt"
        "log"
)

type reader interface {
        ReadString(delim byte) (line string, err error)
}

func read(r reader, delim []byte) (line []byte, err error) {
        for {
                s := ""
                s, err = r.ReadString(delim[len(delim)-1])
                if err != nil {
                        return
                }

                line = append(line, []byte(s)...)
                if bytes.HasSuffix(line, delim) {
                        return line[:len(line)-len(delim)], nil
                }
        }
}

func main() {
        src := bytes.NewBufferString("123deli456elim789delimABCdelimDEF")
        for {
                b, err := read(src, []byte("delim"))
                if err != nil {
                        log.Fatal(err)
                }

                fmt.Printf("%q\n", b)
        }
}

Playground


输出:

"123deli456elim789"
"ABC"
2009/11/10 23:00:00 EOF

答案 1 :(得分:3)

http://play.golang.org/p/BpA5pOc-Rn

package main

import (
    "bytes"
    "fmt"
)

func main() {
    b := bytes.NewBuffer([]byte("Hello, playground!\r\n.\r\nIrrelevant trailer."))
    c := make([]byte, 0, b.Len())
    for {
        p := b.Bytes()
        if bytes.Equal(p[:5], []byte("\r\n.\r\n")) {
            fmt.Println(string(c))
            return
        }
        c = append(c, b.Next(1)...)

    }
}

答案 2 :(得分:2)

例如,

package main

import (
    "bufio"
    "bytes"
    "fmt"
    "strings"
)

var delim = []byte{'\r', '\n', '.', '\r', '\n'}

func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
    if atEOF && len(data) == 0 {
        return 0, nil, nil
    }
    for i := 0; i+len(delim) <= len(data); {
        j := i + bytes.IndexByte(data[i:], delim[0])
        if j < i {
            break
        }
        if bytes.Equal(data[j+1:j+len(delim)], delim[1:]) {
            // We have a full delim-terminated line.
            return j + len(delim), data[0:j], nil
        }
        i = j + 1
    }
    // If we're at EOF, we have a final, non-terminated line. Return it.
    if atEOF {
        return len(data), data, nil
    }
    // Request more data.
    return 0, nil, nil
}

func main() {
    delims := string(delim)
    input := "1234" + delims + "5678" + delims + "1234567901234567890" + delims
    scanner := bufio.NewScanner(strings.NewReader(input))
    scanner.Split(ScanLines)
    for scanner.Scan() {
        fmt.Printf("%s\n", scanner.Text())
    }
    if err := scanner.Err(); err != nil {
        fmt.Printf("Invalid input: %s", err)
    }
}

输出:

1234
5678
1234567901234567890

答案 3 :(得分:0)

人:    因为你在字符串中有相同的字节,你可以这样做:

func readWithEnd(reader *bufio.Reader) ([]byte, error) {
message, err := reader.ReadBytes('#')
if err != nil {
    return nil, err
}

a1, err := reader.ReadByte()
if err != nil {
    return nil, err
}
message = append(message, a1)
if a1 != '\t' {
    message2, err := readWithEnd(reader)
    if err != nil {
        return nil, err
    }
    ret := append(message, message2...)
    return ret, nil
}

a2, err := reader.ReadByte()
if err != nil {
    return nil, err
}
message = append(message, a2)
if a2 != '#' {
    message2, err := readWithEnd(reader)
    if err != nil {
        return nil, err
    }
    ret := append(message, message2...)
    return ret, nil
}
return message, nil

}

这是可以识别tcp连接中的“#\ t#”的样本