aboutsummaryrefslogtreecommitdiff
path: root/lib/bytes
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-04-09 15:52:30 +0700
committerShulhan <ms@kilabit.info>2023-04-09 19:06:01 +0700
commitaf0eb7e03872e759527e00a004df066a0b03a871 (patch)
tree2d9a5f645a3ff3b5d805498129dda7ed91f244e9 /lib/bytes
parentd90ce9219bf3822fa78e713fdd9f6677e08dbc74 (diff)
downloadpakakeh.go-af0eb7e03872e759527e00a004df066a0b03a871.tar.xz
lib/bytes: return the number of skipped chars on Skip(Horizontal)Spaces
Diffstat (limited to 'lib/bytes')
-rw-r--r--lib/bytes/parser.go21
-rw-r--r--lib/bytes/parser_example_test.go38
2 files changed, 32 insertions, 27 deletions
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() {