From af0eb7e03872e759527e00a004df066a0b03a871 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 9 Apr 2023 15:52:30 +0700 Subject: lib/bytes: return the number of skipped chars on Skip(Horizontal)Spaces --- lib/bytes/parser.go | 21 ++++++++++++--------- lib/bytes/parser_example_test.go | 38 ++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 27 deletions(-) (limited to 'lib/bytes') diff --git a/lib/bytes/parser.go b/lib/bytes/parser.go index d6c9475d..70230622 100644 --- a/lib/bytes/parser.go +++ b/lib/bytes/parser.go @@ -194,17 +194,18 @@ func (bp *Parser) SkipN(n int) (c byte) { } // SkipHorizontalSpaces skip space (" "), tab ("\t"), carriage return -// ("\r"), and form feed ("\f") characters; and return the first non-space -// character or 0 if it reach end-of-content. -func (bp *Parser) SkipHorizontalSpaces() (c byte) { +// ("\r"), and form feed ("\f") characters; and return the number of space +// skipped and first non-space character or 0 if it reach end-of-content. +func (bp *Parser) SkipHorizontalSpaces() (n int, c byte) { for ; bp.x < bp.size; bp.x++ { c = bp.content[bp.x] if c == ' ' || c == '\t' || c == '\r' || c == '\f' { + n++ continue } - return c + return n, c } - return 0 + return n, 0 } // SkipLine skip all characters until new line. @@ -222,16 +223,18 @@ func (bp *Parser) SkipLine() (c byte) { } // SkipSpaces skip all spaces character (' ', '\f', '\n', '\r', '\t') and -// return the first non-space character or 0 if it reach end-of-content. -func (bp *Parser) SkipSpaces() (c byte) { +// return the number of spaces skipped and first non-space character or 0 if +// it reach end-of-content. +func (bp *Parser) SkipSpaces() (n int, c byte) { for ; bp.x < bp.size; bp.x++ { c = bp.content[bp.x] if ascii.IsSpace(c) { + n++ continue } - return c + return n, c } - return 0 + return n, 0 } // Stop the parser, return the remaining unparsed content and its last diff --git a/lib/bytes/parser_example_test.go b/lib/bytes/parser_example_test.go index 25ee6ba1..c62258fe 100644 --- a/lib/bytes/parser_example_test.go +++ b/lib/bytes/parser_example_test.go @@ -209,25 +209,26 @@ func ExampleParser_SkipHorizontalSpaces() { content = []byte(" \t\r\fA. \nB.") delims = []byte{'.'} parser = libbytes.NewParser(content, delims) + n int ) - parser.SkipHorizontalSpaces() + n, _ = parser.SkipHorizontalSpaces() token, d := parser.Read() - fmt.Printf("token:%s delim:%c\n", token, d) + fmt.Printf("n:%d token:%s delim:%c\n", n, token, d) - parser.SkipHorizontalSpaces() + n, _ = parser.SkipHorizontalSpaces() token, d = parser.Read() // The token include \n. - fmt.Printf("token:%s delim:%c\n", token, d) + fmt.Printf("n:%d token:%s delim:%c\n", n, token, d) - parser.SkipHorizontalSpaces() + n, _ = parser.SkipHorizontalSpaces() token, d = parser.Read() // The token include \n. - fmt.Printf("token:%s delim:%d\n", token, d) + fmt.Printf("n:%d token:%s delim:%d\n", n, token, d) // Output: - // token:A delim:. - // token: + // n:4 token:A delim:. + // n:1 token: // B delim:. - // token: delim:0 + // n:0 token: delim:0 } func ExampleParser_SkipSpaces() { @@ -235,24 +236,25 @@ func ExampleParser_SkipSpaces() { content = []byte(" \t\r\fA. \nB.") delims = []byte{'.'} parser = libbytes.NewParser(content, delims) + n int ) - parser.SkipSpaces() + n, _ = parser.SkipSpaces() token, d := parser.Read() - fmt.Printf("token:%s delim:%c\n", token, d) + fmt.Printf("n:%d token:%s delim:%c\n", n, token, d) - parser.SkipSpaces() + n, _ = parser.SkipSpaces() token, d = parser.Read() // The token include \n. - fmt.Printf("token:%s delim:%c\n", token, d) + fmt.Printf("n:%d token:%s delim:%c\n", n, token, d) - parser.SkipSpaces() + n, _ = parser.SkipSpaces() token, d = parser.Read() // The token include \n. - fmt.Printf("token:%s delim:%d\n", token, d) + fmt.Printf("n:%d token:%s delim:%d\n", n, token, d) // Output: - // token:A delim:. - // token:B delim:. - // token: delim:0 + // n:4 token:A delim:. + // n:2 token:B delim:. + // n:0 token: delim:0 } func ExampleParser_Stop() { -- cgit v1.3-6-g1900