Skip to content

Instantly share code, notes, and snippets.

@captncraig
Created August 26, 2015 23:21
Show Gist options
  • Select an option

  • Save captncraig/e295fbfd343fd4cd4e4f to your computer and use it in GitHub Desktop.

Select an option

Save captncraig/e295fbfd343fd4cd4e4f to your computer and use it in GitHub Desktop.

Revisions

  1. Craig Peterson created this gist Aug 26, 2015.
    21 changes: 21 additions & 0 deletions read.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    func ReadUntilRegex(r io.Reader, regex string) (string, error) {
    accumulated := []byte{}
    reg, err := regexp.Compile(regex)
    if err != nil {
    return "", nil
    }
    for {
    buf := make([]byte, 16*1024)
    n, err := r.Read(buf)
    if err != nil && err != io.EOF {
    return "", err
    }
    accumulated = append(accumulated, buf[:n]...)
    if err == io.EOF {
    return string(accumulated), err
    }
    if reg.Match(accumulated) {
    return string(accumulated), nil
    }
    }
    }