Skip to content

Commit

Permalink
Allow explicitly set recursionLimit with strict accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Jan 7, 2025
1 parent 94083a2 commit e816a89
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 26 deletions.
21 changes: 6 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ Using default `accuracy` adds support for the following features, depending on `

- All targets (`ES2025` and earlier):
- Enables use of `\X` using a close approximation of a Unicode extended grapheme cluster.
- Enables using recursion (ex: `\g<0>`) with a depth limit lower than `20` (Oniguruma's limit).
- `ES2024` and earlier:
- Enables use of case-insensitive backreferences to case-sensitive groups.
- `ES2018`:
Expand Down Expand Up @@ -211,14 +210,6 @@ Include JavaScript flag `d` (`hasIndices`) in the result.

Recursion depth limit. The default value of `20` is Oniguruma's limit, but you can use an integer from `2` to `20`.

Use of recursion with a limit lower than `20` results in an error if using strict `accuracy`.

<details>
<summary>More details</summary>

Using a high limit can impact performance, but this is generally only a problem if the regex has an existing issue with runaway backtracking that recursion exacerbates. Higher limits have no effect on regexes that don't use recursion, so you should feel free to increase this if helpful.
</details>

### `rules`

Advanced pattern options that override standard error checking and flags when enabled.
Expand Down Expand Up @@ -864,10 +855,10 @@ Notice that nearly every feature below has at least subtle differences from Java
<code>\g&lt;0></code>,<br>
<code>\g'0'</code>
</td>
<td align="middle">☑️</td>
<td align="middle">☑️</td>
<td align="middle">☑️<sup>[6]</sup></td>
<td align="middle">☑️<sup>[6]</sup></td>
<td>
● Has depth limit<sup>[6]</sup><br>
✔ Limits depth to 20 levels<br>
</td>
</tr>
<tr valign="top">
Expand All @@ -877,10 +868,10 @@ Notice that nearly every feature below has at least subtle differences from Java
<code>(…\g&lt;-1>?…)</code>,<br>
<code>(?&lt;a>…\g&lt;a>?…)</code>, etc.
</td>
<td align="middle">☑️</td>
<td align="middle">☑️</td>
<td align="middle">☑️<sup>[6]</sup></td>
<td align="middle">☑️<sup>[6]</sup></td>
<td>
● Has depth limit<sup>[6]</sup><br>
✔ Limits depth to 20 levels<br>
</td>
</tr>

Expand Down
7 changes: 0 additions & 7 deletions spec/match-recursion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ describe('Recursion', () => {
}
});

it('should throw if recursionLimit below 20 used with strict accuracy', () => {
// Default is 20
expect(() => toDetails(r`a\g<0>?`, {accuracy: 'strict'})).not.toThrow();
expect(() => toDetails(r`a\g<0>?`, {accuracy: 'strict', recursionLimit: 20})).not.toThrow();
expect(() => toDetails(r`a\g<0>?`, {accuracy: 'strict', recursionLimit: 5})).toThrow();
});

// Documenting current behavior
it('should throw if backref used with recursion when the recursed subpattern contains captures', () => {
expect(() => toDetails(r`(a)\1\g<0>?`)).toThrow();
Expand Down
4 changes: 0 additions & 4 deletions src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,6 @@ function genGroup({atomic, flags, parent, alternatives}, state, gen) {

function genRecursion({ref}, state) {
const limit = state.recursionLimit;
const onigRecursionLimit = 20;
if (limit !== onigRecursionLimit && state.accuracy === 'strict') {
throw new Error('Use of recursion with limit less than 20 requires non-strict accuracy');
}
// Using the syntax supported by `regex-recursion`
return ref === 0 ? `(?R=${limit})` : r`\g<${ref}&R=${limit}>`;
}
Expand Down

0 comments on commit e816a89

Please sign in to comment.