@@ -8,26 +8,26 @@ module vom
8
8
// The third argument matches the escaped characters
9
9
10
10
pub fn escaped (normal Fn, control_char rune , escapable Fn) Fn {
11
- return fn [normal, control_char, escapable] (input string ) ! (string , string ) {
11
+ return fn [normal, control_char, escapable] (input string ) ! (string , string , int ) {
12
12
check_control_char := tag (control_char.str ())
13
- a , b := normal (input) or { input, '' }
14
- c , d := check_control_char (a) or { a, '' }
15
- e , f := escapable (c) or { c, '' }
16
- g , h := normal (e) or { e, '' }
13
+ a , b , len1 := normal (input) or { input, '' , 0 }
14
+ c , d , len2 := check_control_char (a) or { a, '' , 0 }
15
+ e , f , len3 := escapable (c) or { c, '' , 0 }
16
+ g , h , len4 := normal (e) or { e, '' , 0 }
17
17
18
18
if input == g {
19
19
return error ('`escaped` fail' )
20
20
}
21
- return g, b + d + f + h
21
+ return g, b + d + f + h, len 1 + len 2 + len 3 + len 4
22
22
}
23
23
}
24
24
25
25
// Returns the longest slice that matches any character in the `pattern`.
26
26
pub fn is_a (pattern string ) Fn {
27
- return fn [pattern] (input string ) ! (string , string ) {
27
+ return fn [pattern] (input string ) ! (string , string , int ) {
28
28
for i, c in input {
29
29
if ! pattern.bytes ().any (it == c) {
30
- return input[i..], input[..i]
30
+ return input[i..], input[..i], i
31
31
}
32
32
}
33
33
return error ('`is_a` failed with pattern `${pattern} ` on input `${input} `' )
@@ -36,10 +36,10 @@ pub fn is_a(pattern string) Fn {
36
36
37
37
// Parse till certain characters are met.
38
38
pub fn is_not (pattern string ) Fn {
39
- return fn [pattern] (input string ) ! (string , string ) {
39
+ return fn [pattern] (input string ) ! (string , string , int ) {
40
40
for i, c in input {
41
41
if pattern.bytes ().any (it == c) {
42
- return input[i..], input[..i]
42
+ return input[i..], input[..i], i
43
43
}
44
44
}
45
45
return error ('`is_not` failed with pattern `${pattern} ` on input `${input} `' )
@@ -48,12 +48,12 @@ pub fn is_not(pattern string) Fn {
48
48
49
49
// Recognizes a `pattern`.
50
50
pub fn tag (pattern string ) Fn {
51
- return fn [pattern] (input string ) ! (string , string ) {
51
+ return fn [pattern] (input string ) ! (string , string , int ) {
52
52
if input.len < pattern.len {
53
53
return error ('`tag` failed because input `${input} ` is shorter than pattern `${pattern} `' )
54
54
}
55
55
if input[..pattern.len] == pattern {
56
- return input[pattern.len..], input[..pattern.len]
56
+ return input[pattern.len..], input[..pattern.len], pattern.len
57
57
} else {
58
58
return error ('`tag` failed because `${input} [..${pattern.len} ]` is not equal to pattern `${pattern} `' )
59
59
}
@@ -62,12 +62,12 @@ pub fn tag(pattern string) Fn {
62
62
63
63
// Recognizes a case insensitive `pattern`.
64
64
pub fn tag_no_case (pattern string ) Fn {
65
- return fn [pattern] (input string ) ! (string , string ) {
65
+ return fn [pattern] (input string ) ! (string , string , int ) {
66
66
if input.len < pattern.len {
67
67
return error ('`tag_no_case` failed because input `${input} ` is shorter than pattern `${pattern} `' )
68
68
}
69
69
if input[..pattern.len].to_lower () == pattern.to_lower () {
70
- return input[pattern.len..], input[..pattern.len]
70
+ return input[pattern.len..], input[..pattern.len], pattern.len
71
71
} else {
72
72
return error ('`tag_no_case` failed because `${input} [..${pattern.len} ].to_lower()` is not equal to pattern `${pattern} `' )
73
73
}
@@ -76,21 +76,21 @@ pub fn tag_no_case(pattern string) Fn {
76
76
77
77
// Returns an input slice containing the first `n` input elements (Input[..`n`]).
78
78
pub fn take (n int ) Fn {
79
- return fn [n] (input string ) ! (string , string ) {
79
+ return fn [n] (input string ) ! (string , string , int ) {
80
80
if input.len < n {
81
81
return error ('`take` failed with count `${n} ` on input `${input} `' )
82
82
} else {
83
- return input[n..], input[..n]
83
+ return input[n..], input[..n], n
84
84
}
85
85
}
86
86
}
87
87
88
88
// Returns the longest input slice (if any) till a predicate `condition` is met.
89
89
pub fn take_till (condition fn (byte ) bool ) Fn {
90
- return fn [condition] (input string ) ! (string , string ) {
90
+ return fn [condition] (input string ) ! (string , string , int ) {
91
91
for i, c in input.bytes () {
92
92
if condition (c) {
93
- return input[i..], input[..i]
93
+ return input[i..], input[..i], i
94
94
}
95
95
}
96
96
return error ('`take_till` failed on input `${input} `' )
@@ -99,10 +99,10 @@ pub fn take_till(condition fn (byte) bool) Fn {
99
99
100
100
// Returns the longest (at least 1) input slice till a predicate `condition` is met.
101
101
pub fn take_till1 (condition fn (byte ) bool ) Fn {
102
- return fn [condition] (input string ) ! (string , string ) {
102
+ return fn [condition] (input string ) ! (string , string , int ) {
103
103
for i, c in input.bytes () {
104
104
if condition (c) && i > 0 {
105
- return input[i..], input[..i]
105
+ return input[i..], input[..i], i
106
106
}
107
107
}
108
108
return error ('`take_till1` failed on input `${input} `' )
@@ -111,10 +111,10 @@ pub fn take_till1(condition fn (byte) bool) Fn {
111
111
112
112
// Returns the input slice up to the first occurrence of the `pattern`.
113
113
pub fn take_until (pattern string ) Fn {
114
- return fn [pattern] (input string ) ! (string , string ) {
114
+ return fn [pattern] (input string ) ! (string , string , int ) {
115
115
for i := 0 ; i + pattern.len < = input.len; i++ {
116
116
if input[i..i + pattern.len] == pattern {
117
- return input[i..], input[..i]
117
+ return input[i..], input[..i], i
118
118
}
119
119
}
120
120
return error ('`take_until` failed on input `${input} ` with pattern `${pattern} `' )
@@ -123,10 +123,10 @@ pub fn take_until(pattern string) Fn {
123
123
124
124
// Returns the non empty input slice up to the first occurrence of the `pattern`.
125
125
pub fn take_until1 (pattern string ) Fn {
126
- return fn [pattern] (input string ) ! (string , string ) {
126
+ return fn [pattern] (input string ) ! (string , string , int ) {
127
127
for i := 1 ; i + pattern.len < = input.len; i++ {
128
128
if input[i..i + pattern.len] == pattern {
129
- return input[i..], input[..i]
129
+ return input[i..], input[..i], i
130
130
}
131
131
}
132
132
return error ('`take_until1` failed on input `${input} ` with pattern `${pattern} `' )
@@ -135,34 +135,34 @@ pub fn take_until1(pattern string) Fn {
135
135
136
136
// Returns the longest input slice (if any) that matches the predicate `condition`.
137
137
pub fn take_while (condition fn (byte ) bool ) Fn {
138
- return fn [condition] (input string ) ! (string , string ) {
138
+ return fn [condition] (input string ) ! (string , string , int ) {
139
139
for i, c in input.bytes () {
140
140
if ! condition (c) {
141
- return input[i..], input[..i]
141
+ return input[i..], input[..i], i
142
142
}
143
143
}
144
- return '' , input
144
+ return '' , input, input.len
145
145
}
146
146
}
147
147
148
148
// Returns the longest (at least 1) input slice that matches the predicate `condition`.
149
149
pub fn take_while1 (condition fn (byte ) bool ) Fn {
150
- return fn [condition] (input string ) ! (string , string ) {
150
+ return fn [condition] (input string ) ! (string , string , int ) {
151
151
for i, c in input.bytes () {
152
152
if ! condition (c) {
153
153
if i == 0 {
154
154
return error ('`take_while` failed on input `${input} ` because it returned empty' )
155
155
}
156
- return input[i..], input[..i]
156
+ return input[i..], input[..i], i
157
157
}
158
158
}
159
- return '' , input
159
+ return '' , input, input.len
160
160
}
161
161
}
162
162
163
163
// Returns the longest (m <= len <= n) input slice that matches the predicate `condition`.
164
164
pub fn take_while_m_n (m int , n int , condition fn (byte ) bool ) Fn {
165
- return fn [m, n, condition] (input string ) ! (string , string ) {
165
+ return fn [m, n, condition] (input string ) ! (string , string , int ) {
166
166
mut longest := - 1
167
167
for i, c in input.bytes () {
168
168
len := i + 1
@@ -174,7 +174,7 @@ pub fn take_while_m_n(m int, n int, condition fn (byte) bool) Fn {
174
174
}
175
175
}
176
176
if longest != - 1 {
177
- return input[longest..], input[..longest]
177
+ return input[longest..], input[..longest], longest
178
178
} else {
179
179
return error ('`take_while_m_n` failed on input `${input} ` with m `${m} ` and n `${n} `' )
180
180
}
0 commit comments