Skip to content

Commit 4363a2e

Browse files
committedJul 31, 2018
fix MatchData#named_captures
1 parent 219f62d commit 4363a2e

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed
 

‎core/src/main/java/org/jruby/RubyMatchData.java

+15-17
Original file line numberDiff line numberDiff line change
@@ -810,27 +810,25 @@ public RubyFixnum hash() {
810810

811811
@JRubyMethod
812812
public RubyHash named_captures(ThreadContext context) {
813+
check();
813814
Ruby runtime = context.runtime;
814-
815815
RubyHash hash = RubyHash.newHash(runtime);
816-
817-
if (regexp.getPattern().numberOfNames() > 0) {
818-
Iterator<NameEntry> nameEntryIterator = regexp.getPattern().namedBackrefIterator();
819-
while (nameEntryIterator.hasNext()) {
820-
NameEntry entry = nameEntryIterator.next();
821-
RubyString key = RubyString.newStringShared(runtime, new ByteList(entry.name, entry.nameP, entry.nameEnd - entry.nameP, regexp.getEncoding(), false));
822-
boolean found = false;
823-
824-
for (int i : entry.getBackRefs()) {
825-
IRubyObject value = RubyRegexp.nth_match(i, this);
826-
if (value.isTrue()) {
827-
hash.op_asetForString(runtime, key, value);
828-
found = true;
829-
}
816+
if (regexp == context.nil) return hash;
817+
818+
for (Iterator<NameEntry> i = regexp.pattern.namedBackrefIterator(); i.hasNext();) {
819+
NameEntry entry = i.next();
820+
RubyString key = RubyString.newStringShared(runtime, new ByteList(entry.name, entry.nameP, entry.nameEnd - entry.nameP, regexp.getEncoding(), false));
821+
boolean found = false;
822+
823+
for (int b : entry.getBackRefs()) {
824+
IRubyObject value = RubyRegexp.nth_match(b, this);
825+
if (value.isTrue()) {
826+
hash.op_asetForString(runtime, key, value);
827+
found = true;
830828
}
831-
832-
if (!found) hash.op_asetForString(runtime, key, context.nil);
833829
}
830+
831+
if (!found) hash.op_asetForString(runtime, key, context.nil);
834832
}
835833

836834
return hash;

0 commit comments

Comments
 (0)
Please sign in to comment.