Created
May 27, 2021 16:23
-
-
Save icholy/989a669d1ee92a348afad365719fedc0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bufutil | |
import ( | |
"bufio" | |
"bytes" | |
) | |
// SkipTo finds the first instance of delim and discards everything before it. | |
func SkipTo(br *bufio.Reader, delim []byte) (err error) { | |
min := len(delim) | |
if min == 0 { | |
return nil | |
} | |
for { | |
n := br.Buffered() | |
if n < min { | |
n = min | |
} | |
buf, err := br.Peek(n) | |
if err != nil { | |
return err | |
} | |
if i := bytes.Index(buf, delim); i >= 0 { | |
_, err := br.Discard(i) | |
return err | |
} | |
if _, err = br.Discard(n - min + 1); err != nil { | |
return err | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment