From dff8d3f4295a343990974b8ea8eac52e4eccf4e4 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 9 Apr 2023 19:47:42 +0700 Subject: lib/bytes: add method ReadLine to Parser The ReadLine method read until it found new line ('\n') or end of content, ignoring all delimiters. The returned line will not contain '\n'. --- lib/bytes/parser.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib/bytes/parser.go') diff --git a/lib/bytes/parser.go b/lib/bytes/parser.go index 70230622..4a6f09d3 100644 --- a/lib/bytes/parser.go +++ b/lib/bytes/parser.go @@ -56,6 +56,22 @@ func (bp *Parser) Read() (token []byte, d byte) { return token, 0 } +// ReadLine read until it found new line ('\n') or end of content, ignoring +// all delimiters. +// The returned line will not contain '\n'. +func (bp *Parser) ReadLine() (line []byte, c byte) { + for bp.x < bp.size { + c = bp.content[bp.x] + if c == '\n' { + bp.x++ + return line, c + } + line = append(line, c) + bp.x++ + } + return line, 0 +} + // ReadN read exactly n characters ignoring the delimiters. // It will return the token and the character after n or 0 if end-of-content. func (bp *Parser) ReadN(n int) (token []byte, d byte) { -- cgit v1.3