@@ -373,6 +373,10 @@ my knowhow RegexCaptures {
373
373
has @ ! named-capture-names ;
374
374
has @ ! named-capture-counts ;
375
375
376
+ # If there's only one name and captured multiple times, then we fast-path it
377
+ # in MATCH. If such a case, this holds the only name.
378
+ has str $ ! onlyname ;
379
+
376
380
# Form this data structure from a capnames hash.
377
381
method from-capnames (% capnames ) {
378
382
nqp ::create(self ). ' !from-capnames' (% capnames )
@@ -385,6 +389,8 @@ my knowhow RegexCaptures {
385
389
@ ! named-capture-counts := nqp ::list_i();
386
390
387
391
# Go over the captures and build up the data structure.
392
+ my int $ num-names := 0 ;
393
+ my str $ onlyname := ' ' ;
388
394
for % capnames {
389
395
my $ name := nqp ::iterkey_s($ _ );
390
396
if $ name ne ' ' {
@@ -396,9 +402,15 @@ my knowhow RegexCaptures {
396
402
nqp ::push_s(@ ! named-capture-names , $ name );
397
403
nqp ::push_i(@ ! named-capture-counts , $ count );
398
404
}
405
+ $ num-names ++ ;
406
+ if $ count >= 2 && nqp :: ord ($ name ) != 36 {
407
+ $ onlyname := $ name ;
408
+ }
399
409
}
400
410
}
401
411
412
+ $ ! onlyname := $ num-names == 1 && $ onlyname ne ' ' ?? $ onlyname !! ' ' ;
413
+
402
414
self
403
415
}
404
416
@@ -451,7 +463,7 @@ my knowhow RegexCaptures {
451
463
}
452
464
453
465
# Get the name of the only capture, if there is only one.
454
- method onlyname () { ' ' }
466
+ method onlyname () { $ ! onlyname }
455
467
}
456
468
457
469
0 commit comments