@@ -31,9 +31,30 @@ proc test-slurp-by {
31
31
seq 8 | slurp-by (3)
32
32
}
33
33
34
- # Note:
35
- # - these are all the same algorithm
36
- # - also word, block, etc. are all optional
34
+ ### Awk
35
+
36
+ # Naming
37
+ #
38
+ # TEXT INPUT
39
+ # each-word # this doesn't go by lines, it does a global regex split or something?
40
+ #
41
+ # LINE INPUT
42
+ # each-line --j8 { echo "-- $_line" } # similar to @()
43
+ # each-line --j8 (^"-- $_line") # is this superfluous?
44
+ #
45
+ # each-split name1 name2
46
+ # (delim=' ')
47
+ # (ifs=' ')
48
+ # (pat=/d+/)
49
+ # # also assign names for each part?
50
+ #
51
+ # each-match # regex match
52
+ # must-match # assert that every line matches
53
+ #
54
+ # TABLE INPUT
55
+ # each-row # TSV and TSV8 input?
56
+ #
57
+ # They all take templates or blocks?
37
58
38
59
proc each-line (...words; template=null; ; block=null) {
39
60
# TODO:
@@ -112,7 +133,9 @@ proc must-split-by (; ; ifs=null; block) {
112
133
echo TODO
113
134
}
114
135
115
- proc if-match (; pattern; ; block) {
136
+ # Naming: each-match, each-split?
137
+
138
+ proc if-match (; pattern, template=null; ; block=null) {
116
139
### like 'grep' but with submatches
117
140
118
141
for line in (io.stdin) {
@@ -122,7 +145,14 @@ proc if-match (; pattern; ; block) {
122
145
#var groups = m.groups()
123
146
124
147
# Should we also pass _line?
125
- call io->eval(block, dollar0=m.group(0))
148
+
149
+ if (block) {
150
+ call io->eval(block, dollar0=m.group(0))
151
+ } elif (template) {
152
+ echo TEMPLATE
153
+ } else {
154
+ echo TSV
155
+ }
126
156
}
127
157
}
128
158
@@ -144,6 +174,8 @@ proc line-data {
144
174
/// 42 bar'''
145
175
}
146
176
177
+ const pat = /<capture d+> s+ <capture w+>/
178
+
147
179
proc test-if-match {
148
180
var z = 'z' # test out scoping
149
181
var count = 0 # test out mutation
@@ -153,7 +185,6 @@ proc test-if-match {
153
185
# sed: print a new line based on submatches
154
186
# awk: re-arrange the cols, and also accumulate counters
155
187
156
- var pat = /<capture d+> s+ <capture w+>/
157
188
line-data | if-match (pat) {
158
189
echo "$z $0 $z"
159
190
# TODO: need pos_args
@@ -165,6 +196,32 @@ proc test-if-match {
165
196
echo "count = $count"
166
197
}
167
198
199
+ proc test-if-match-2 {
200
+ # If there's no block or template, it should print out a TSV with:
201
+ #
202
+ # $0 ...
203
+ # $1 $2
204
+ # $_line maybe?
205
+
206
+ #line-data | if-match (pat)
207
+
208
+ var z = 'z' # scoping
209
+ line-data | if-match (pat, ^"$z $0 $z")
210
+ line-data | if-match (pat, ^"-- $0 --")
211
+ }
212
+
213
+ # might be a nice way to write it, not sure if byo.sh can discover it
214
+ if false {
215
+ tests 'if-match' {
216
+ proc case-block {
217
+ echo TODO
218
+ }
219
+ proc case-template {
220
+ echo TODO
221
+ }
222
+ }
223
+ }
224
+
168
225
# Protocol:
169
226
#
170
227
# - The file lists its tests the "actions"
0 commit comments