Skip to content

Commit 43a8258

Browse files
committed
Pass a Rope to getBackRef() and deduplicate the helper methods
1 parent aa85163 commit 43a8258

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

src/main/java/org/truffleruby/core/regexp/MatchDataNodes.java

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.truffleruby.core.regexp.MatchDataNodesFactory.ValuesNodeFactory;
3737
import org.truffleruby.core.rope.Rope;
3838
import org.truffleruby.core.rope.RopeNodes;
39+
import org.truffleruby.core.rope.RopeOperations;
3940
import org.truffleruby.core.string.RubyString;
4041
import org.truffleruby.core.string.StringSupport;
4142
import org.truffleruby.core.string.StringUtils;
@@ -314,18 +315,18 @@ protected Object getIndexSymbolKnownRegexp(RubyMatchData matchData, RubySymbol s
314315
if (backRefs == 1) {
315316
return executeGetIndex(matchData, backRefIndex, NotProvided.INSTANCE);
316317
} else {
317-
final int i = getBackRef(matchData, cachedRegexp, nameEntry, lazyProfile, libInterop);
318+
final int i = getBackRef(matchData, cachedRegexp, cachedSymbol.getRope(), lazyProfile, libInterop);
318319
return executeGetIndex(matchData, i, NotProvided.INSTANCE);
319320
}
320321
}
321322

322323
@Specialization
323-
protected Object getIndexSymbol(RubyMatchData matchData, RubySymbol index, NotProvided length,
324+
protected Object getIndexSymbol(RubyMatchData matchData, RubySymbol symbol, NotProvided length,
324325
@Cached ConditionProfile lazyProfile,
325326
@CachedLibrary(limit = "getInteropCacheLimit()") InteropLibrary libInterop) {
326327
return executeGetIndex(
327328
matchData,
328-
getBackRefFromSymbol(matchData, index, lazyProfile, libInterop),
329+
getBackRef(matchData, getRegexp(matchData), symbol.getRope(), lazyProfile, libInterop),
329330
NotProvided.INSTANCE);
330331
}
331332

@@ -336,7 +337,7 @@ protected Object getIndexString(RubyMatchData matchData, Object index, NotProvid
336337
@CachedLibrary(limit = "getInteropCacheLimit()") InteropLibrary libInterop) {
337338
return executeGetIndex(
338339
matchData,
339-
getBackRefFromRope(matchData, libIndex.getRope(index), lazyProfile, libInterop),
340+
getBackRef(matchData, getRegexp(matchData), libIndex.getRope(index), lazyProfile, libInterop),
340341
NotProvided.INSTANCE);
341342
}
342343

@@ -396,46 +397,29 @@ protected RubyRegexp getRegexp(RubyMatchData matchData) {
396397
return regexpNode.executeGetRegexp(matchData);
397398
}
398399

399-
private int getBackRefFromSymbol(RubyMatchData matchData, RubySymbol index, ConditionProfile lazyProfile,
400-
InteropLibrary libInterop) {
401-
return getBackRefFromRope(matchData, index.getRope(), lazyProfile, libInterop);
402-
}
403-
404-
private int getBackRefFromRope(RubyMatchData matchData, Rope value, ConditionProfile lazyProfile,
405-
InteropLibrary libInterop) {
406-
if (lazyProfile.profile(matchData.tRegexResult != null)) {
407-
// force the calculation of lazy capture group results before invoking
408-
// nameToBackrefNumber
409-
forceLazyMatchData(matchData, libInterop);
410-
}
411-
return nameToBackrefNumber(matchData, getRegexp(matchData), value.getBytes(), 0, value.byteLength());
412-
}
413-
414-
private int getBackRef(RubyMatchData matchData, RubyRegexp regexp, NameEntry name, ConditionProfile lazyProfile,
415-
InteropLibrary libInterop) {
400+
private int getBackRef(RubyMatchData matchData, RubyRegexp regexp, Rope name,
401+
ConditionProfile lazyProfile, InteropLibrary libInterop) {
416402
if (lazyProfile.profile(matchData.tRegexResult != null)) {
417-
// force the calculation of lazy capture group results before invoking
418-
// nameToBackrefNumber
403+
// force the calculation of lazy capture group results before invoking nameToBackrefNumber()
419404
forceLazyMatchData(matchData, libInterop);
420405
}
421-
return nameToBackrefNumber(matchData, regexp, name.name, name.nameP, name.nameEnd);
406+
return nameToBackrefNumber(matchData, regexp, name);
422407
}
423408

424409
@TruffleBoundary
425-
private int nameToBackrefNumber(RubyMatchData matchData, RubyRegexp regexp, byte[] name, int nameP,
426-
int nameEnd) {
410+
private int nameToBackrefNumber(RubyMatchData matchData, RubyRegexp regexp, Rope name) {
427411
try {
428412
return regexp.regex.nameToBackrefNumber(
429-
name,
430-
nameP,
431-
nameEnd,
413+
name.getBytes(),
414+
0,
415+
name.byteLength(),
432416
matchData.region);
433417
} catch (ValueException e) {
434418
throw new RaiseException(
435419
getContext(),
436420
coreExceptions().indexError(
437421
StringUtils
438-
.format("undefined group name reference: %s", new String(name, nameP, nameEnd)),
422+
.format("undefined group name reference: %s", RopeOperations.decodeRope(name)),
439423
this));
440424
}
441425
}

0 commit comments

Comments
 (0)