Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #40: optimize Ruby's regex pattern #41

Merged
merged 1 commit into from
Oct 3, 2021

Conversation

farhan443
Copy link
Contributor

@farhan443 farhan443 commented Oct 3, 2021

fix #40

Nested quantifier is a common cause of exponential backtracking. In /(do\s*[|](\w+(,\s*\w+)?)+[|])/, there's a nested quantifier 3 levels deep: (\w+(,\s*\w+)?)+.
I replaced it with \w+(,\s*\w+)* so it became 2 levels deep.

The test on https://regex101.com/r/yT5xg0/1/debugger shows that it no longer causes exponential backtracking on string: "do|aaa", but rather linear.

Other change:

  • removed the first capture group because it seems to serve no purpose.
  • replaced \| with [|] for readability.

@lgtm-com
Copy link

lgtm-com bot commented Oct 3, 2021

This pull request fixes 2 alerts when merging 0073fa1 into 313def7 - view on LGTM.com

fixed alerts:

  • 2 for Inefficient regular expression

@farhan443 farhan443 changed the title fix: optimize Ruby's regex pattern fix: optimize Ruby's regex pattern #40 Oct 3, 2021
@farhan443 farhan443 changed the title fix: optimize Ruby's regex pattern #40 fix #40: optimize Ruby's regex pattern Oct 3, 2021
@codecov
Copy link

codecov bot commented Oct 3, 2021

Codecov Report

Merging #41 (0073fa1) into master (313def7) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #41   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           22        22           
  Lines          824       824           
  Branches        46        46           
=========================================
  Hits           824       824           
Impacted Files Coverage Δ
src/languages/ruby.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 313def7...0073fa1. Read the comment docs.

@aldy505 aldy505 merged commit 9734330 into teknologi-umum:master Oct 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regular expression caused exponential backtracking on Ruby
2 participants