-
Notifications
You must be signed in to change notification settings - Fork 14
Add pluggable regex engine support #41
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
677ad33
Add pluggable regex engine support
rayokota 61395af
copilot feedback
rayokota 59e6eef
copilot feedback
rayokota 086926c
copilot feedback
rayokota 1c1ef8e
copilot feedback
rayokota 51748bb
simplify regex engine
rayokota 5bd224b
copilot feedback
rayokota c8dd71d
Fix nested envs
rayokota e722d28
Fix nested envs
rayokota 66154da
Add timeout and stack guardrails
rayokota File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import re | ||
|
rayokota marked this conversation as resolved.
Outdated
Copilot marked this conversation as resolved.
Outdated
rayokota marked this conversation as resolved.
Outdated
|
||
|
|
||
| import jsonata | ||
| import pytest | ||
|
|
||
| re2 = pytest.importorskip("re2") | ||
|
|
||
|
|
||
| class RE2Engine: | ||
| """ | ||
| Adapts google-re2's Options-based compile() to the re.compile(pattern, flags) | ||
| interface expected by Jsonata's regex_engine hook. | ||
| """ | ||
|
|
||
| @staticmethod | ||
| def compile(pattern, flags=0): | ||
| options = re2.Options() | ||
| options.case_sensitive = not bool(flags & re.IGNORECASE) | ||
| options.one_line = not bool(flags & re.MULTILINE) | ||
| options.log_errors = False | ||
| return re2.compile(pattern, options) | ||
|
|
||
|
|
||
| class TestRE2Engine: | ||
|
|
||
| def test_match(self): | ||
| expr = jsonata.Jsonata('$match("hello world", /o w/)', RE2Engine) | ||
| assert expr.evaluate(None) == {"match": "o w", "index": 4, "groups": []} | ||
|
|
||
| def test_match_case_insensitive_flag(self): | ||
| expr = jsonata.Jsonata('$match("HELLO", /hello/i)', RE2Engine) | ||
| result = expr.evaluate(None) | ||
| assert result["match"] == "HELLO" | ||
|
|
||
| def test_contains(self): | ||
| expr = jsonata.Jsonata('$contains("hello", /ell/)', RE2Engine) | ||
| assert expr.evaluate(None) is True | ||
|
|
||
| def test_replace_with_string(self): | ||
| expr = jsonata.Jsonata('$replace("abc123def", /[0-9]+/, "#")', RE2Engine) | ||
| assert expr.evaluate(None) == "abc#def" | ||
|
|
||
| def test_replace_with_function(self): | ||
| expr = jsonata.Jsonata( | ||
| '$replace("abc123", /[0-9]+/, function($m) { $m.match & "!" })', RE2Engine | ||
| ) | ||
| assert expr.evaluate(None) == "abc123!" | ||
|
|
||
| def test_split(self): | ||
| expr = jsonata.Jsonata('$split("a1b2c3", /[0-9]/)', RE2Engine) | ||
| assert expr.evaluate(None) == ["a", "b", "c", ""] | ||
|
|
||
| def test_rejects_backreferences(self): | ||
| # Proves RE2 is actually compiling the pattern rather than silently | ||
| # falling back to stdlib re: backreferences can't run in RE2's | ||
| # guaranteed-linear-time engine, so this must fail at parse time | ||
| # (regex literals are compiled while the expression is constructed). | ||
| with pytest.raises(re2.error): | ||
| jsonata.Jsonata(r'$match("abab", /(a)\1/)', RE2Engine) | ||
|
|
||
| def test_default_engine_still_stdlib_re(self): | ||
| # No regex_engine argument -> falls back to stdlib re, which *does* | ||
| # support backreferences. Confirms the default path is unaffected. | ||
| expr = jsonata.Jsonata(r'$match("xaay", /(a)\1/)') | ||
| assert expr.evaluate(None)["match"] == "aa" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.