summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/bytes/parser.go9
-rw-r--r--lib/bytes/parser_example_test.go16
2 files changed, 25 insertions, 0 deletions
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
diff --git a/lib/bytes/parser_example_test.go b/lib/bytes/parser_example_test.go
index b6582542..2f91c090 100644
--- a/lib/bytes/parser_example_test.go
+++ b/lib/bytes/parser_example_test.go
@@ -41,6 +41,22 @@ func ExampleParser_Delimiters() {
// =;
}
+func ExampleParser_Peek() {
+ var (
+ content = []byte("a = b; ")
+ delims = []byte{'=', ';'}
+ parser = libbytes.NewParser(content, delims)
+ )
+ var stream = parser.Peek(1)
+ fmt.Printf("peek=%q\n", stream)
+
+ stream = parser.Peek(len(content) + 1)
+ fmt.Printf("peek=%q\n", stream)
+ // Output:
+ // peek="a"
+ // peek="a = b; "
+}
+
func ExampleParser_Read() {
var (
content = []byte("a = b; ")