From 5cc78f0f88433bca520a15e77c1ff97e27219ce9 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Fri, 7 Feb 2025 01:32:44 +0700 Subject: lib/bytes: add method Peek to Parser The Peek method take a look on n bytes inside the buffer without using delimiters. The returned bytes may empty or have length less than n. --- lib/bytes/parser.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lib/bytes/parser.go') diff --git a/lib/bytes/parser.go b/lib/bytes/parser.go index e5c7addb..b6adc0d8 100644 --- a/lib/bytes/parser.go +++ b/lib/bytes/parser.go @@ -40,6 +40,15 @@ func (bp *Parser) Delimiters() []byte { return bytes.Clone(bp.delims) } +// Peek take a look on n bytes inside the buffer without using delimiters. +// The returned bytes may empty or have length less than n. +func (bp *Parser) Peek(n int) []byte { + if bp.x+n > bp.size { + return bp.content[bp.x:] + } + return bp.content[bp.x : bp.x+n] +} + // Read read a token until one of the delimiters found. // If one of delimiter match, it will return it as d. // When end of content encountered, the returned token may be not empty but -- cgit v1.3-6-g1900