diff --git a/irregex-core.scm b/irregex-core.scm
index db8a1e8..dd9792d 100644
a
|
b
|
|
252 | 252 | (if (pair? opt) |
253 | 253 | (if (number? (car opt)) |
254 | 254 | (car opt) |
255 | | (let lp ((ls (irregex-match-names m))) |
256 | | (cond ((null? ls) (error "unknown match name" (car opt))) |
257 | | ((and (eq? (car opt) (caar ls)) |
258 | | (%irregex-match-start-chunk m (cdar ls))) |
259 | | (cdar ls)) |
260 | | (else (lp (cdr ls)))))) |
| 255 | (let lp ((ls (irregex-match-names m)) |
| 256 | (exists #f)) |
| 257 | (cond ((null? ls) |
| 258 | (if exists #f (error "unknown match name" (car opt)))) |
| 259 | ((eq? (car opt) (caar ls)) |
| 260 | (if (%irregex-match-start-chunk m (cdar ls)) |
| 261 | (cdar ls) |
| 262 | (lp (cdr ls) #t))) |
| 263 | (else (lp (cdr ls) exists))))) |
261 | 264 | 0)) |
262 | 265 | |
263 | 266 | (cond-expand |
… |
… |
|
298 | 301 | (get-subchunk (chunker-get-subchunk cnk))) |
299 | 302 | (if (not get-subchunk) |
300 | 303 | (error "this chunk type does not support match subchunks") |
301 | | (and (%irregex-match-valid-index? m n) |
| 304 | (and n |
| 305 | (%irregex-match-valid-index? m n) |
302 | 306 | (get-subchunk |
303 | 307 | (%irregex-match-start-chunk m n) |
304 | 308 | (%irregex-match-start-index m n) |
diff --git a/tests/test-irregex.scm b/tests/test-irregex.scm
index fbeb628..7007ec7 100644
a
|
b
|
|
295 | 295 | (irregex-match-substring (irregex-match irx str) name)) |
296 | 296 | |
297 | 297 | (test-group "named submatches" |
298 | | (test-equal "matching alternative is used" |
| 298 | (test "matching submatch is seen and extracted" |
| 299 | "first" (extract 'first `(or (submatch-named first "first") |
| 300 | (submatch-named second "second")) |
| 301 | "first")) |
| 302 | (test "nonmatching submatch is known but returns false" |
| 303 | #f (extract 'second `(or (submatch-named first "first") |
| 304 | (submatch-named second "second")) |
| 305 | "first")) |
| 306 | (test-error "nonexisting submatch is unknown and raises an error" |
| 307 | (extract 'third `(or (submatch-named first "first") |
| 308 | (submatch-named second "second")) |
| 309 | "first")) |
| 310 | (test "matching alternative is used" |
299 | 311 | "first" (extract 'sub `(or (submatch-named sub "first") |
300 | 312 | (submatch-named sub "second")) |
301 | 313 | "first")) |
302 | | (test-equal "matching alternative is used (second match)" |
| 314 | (test "matching alternative is used (second match)" |
303 | 315 | "second" (extract 'sub `(or (submatch-named sub "first") |
304 | 316 | (submatch-named sub "second")) |
305 | 317 | "second")) |
306 | | (test-equal "last match is used with multiple matches for a name" |
| 318 | (test "last match is used with multiple matches for a name" |
307 | 319 | "second" (extract 'sub `(seq (submatch-named sub "first") |
308 | 320 | space |
309 | 321 | (submatch-named sub "second")) |