Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions java/com/google/re2j/Machine.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,8 @@ private void initNewCap(int ncap) {
this.matchcap = new int[ncap];
}

int[] submatches() {
if (ncap == 0) {
return Utils.EMPTY_INTS;
}
return Arrays.copyOf(matchcap, ncap);
void submatches(int[] cap) {
System.arraycopy(matchcap, 0, cap, 0, Math.min(cap.length, ncap));
}

// alloc() allocates a new thread with the given instruction.
Expand Down
23 changes: 11 additions & 12 deletions java/com/google/re2j/RE2.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ public String toString() {
// the position of its subexpressions.
// Derived from exec.go.
private int[] doExecute(MachineInput in, int pos, int anchor, int ncap) {
int[] cap = ncap == 0 ? Utils.EMPTY_INTS : new int[ncap];
return doExecute(in, pos, anchor, ncap, cap) ? cap : null;
}

private boolean doExecute(MachineInput in, int pos, int anchor, int ncap, int[] cap) {
Machine m = get();
// The Treiber stack cannot reuse nodes, unless the node to be reused has only ever been at
// the bottom of the stack (i.e., next == null).
Expand All @@ -305,9 +310,12 @@ private int[] doExecute(MachineInput in, int pos, int anchor, int ncap) {
}

m.init(ncap);
int[] cap = m.match(in, pos, anchor) ? m.submatches() : null;
boolean ok = m.match(in, pos, anchor);
if (ok && cap != null) {
m.submatches(cap);
}
put(m, isNew);
return cap;
return ok;
}

/**
Expand Down Expand Up @@ -350,16 +358,7 @@ boolean match(MatcherInput input, int start, int end, int anchor, int[] group, i
input.getEncoding() == Encoding.UTF_16
? MachineInput.fromUTF16(input.asCharSequence(), 0, end)
: MachineInput.fromUTF8(input.asBytes(), 0, end);
int[] groupMatch = doExecute(machineInput, start, anchor, 2 * ngroup);

if (groupMatch == null) {
return false;
}

if (group != null) {
System.arraycopy(groupMatch, 0, group, 0, groupMatch.length);
}
return true;
return doExecute(machineInput, start, anchor, 2 * ngroup, group);
}

/**
Expand Down