-
Notifications
You must be signed in to change notification settings - Fork 67
Add RegEx supports using RE2 to sjsonnet #244
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
50fa5e4
STDREGEX
stephenamar-db 699a255
Handle null matches properly
stephenamar-db a2f9f92
Handle null matches properly
stephenamar-db 65a822d
Handle null matches properly for partialMatch
stephenamar-db 39c7bce
fix types
stephenamar-db e101c10
Move to concurrenthashmap for the pattern cache
stephenamar-db df59fee
Fix performance regression in stripChars
stephenamar-db 599bccc
address comments
stephenamar-db 2d317f6
Add regex functions to the "builtinnativefunctions"
stephenamar-db fb9f800
Fix specialization
stephenamar-db 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,89 @@ | ||
| package sjsonnet | ||
|
|
||
| import sjsonnet.Expr.Member.Visibility | ||
| import sjsonnet.Val.Obj | ||
|
|
||
| object StdRegex { | ||
| def functions: Map[String, Val.Builtin] = Map( | ||
| "regexPartialMatch" -> new Val.Builtin2("regexPartialMatch", "pattern", "str") { | ||
| override def evalRhs(pattern: Val, str: Val, ev: EvalScope, pos: Position): Val = { | ||
| val compiledPattern = Platform.getPatternFromCache(pattern.asString) | ||
| val matcher = compiledPattern.matcher(str.asString) | ||
| var returnStr: Val = null | ||
| val captures = Array.newBuilder[Val] | ||
| val groupCount = matcher.groupCount() | ||
| while (matcher.find()) { | ||
| if (returnStr == null) { | ||
| val m = matcher.group(0) | ||
| if (m != null) { | ||
| returnStr = Val.Str(pos.noOffset, matcher.group(0)) | ||
| } else { | ||
| returnStr = Val.Null(pos.noOffset) | ||
| } | ||
| } | ||
| for (i <- 1 to groupCount) { | ||
| val m = matcher.group(i) | ||
| if (m == null) { | ||
| captures += Val.Null(pos.noOffset) | ||
| } else { | ||
| captures += Val.Str(pos.noOffset, m) | ||
| } | ||
| } | ||
| } | ||
| val result = captures.result() | ||
| Val.Obj.mk(pos.noOffset, | ||
| "string" -> new Obj.ConstMember(true, Visibility.Normal, | ||
| if (returnStr == null) Val.Null(pos.noOffset) else returnStr), | ||
| "captures" -> new Obj.ConstMember(true, Visibility.Normal, new Val.Arr(pos.noOffset, result)) | ||
| ) | ||
| } | ||
| }, | ||
| "regexFullMatch" -> new Val.Builtin2("regexFullMatch", "pattern", "str") { | ||
| override def evalRhs(pattern: Val, str: Val, ev: EvalScope, pos: Position): Val = { | ||
| val compiledPattern = Platform.getPatternFromCache(pattern.asString) | ||
| val matcher = compiledPattern.matcher(str.asString) | ||
| if (!matcher.matches()) { | ||
| Val.Obj.mk(pos.noOffset, | ||
| "string" -> new Obj.ConstMember(true, Visibility.Normal, Val.Null(pos.noOffset)), | ||
| "captures" -> new Obj.ConstMember(true, Visibility.Normal, new Val.Arr(pos.noOffset, Array.empty[Lazy])) | ||
| ) | ||
| } else { | ||
| val captures = Array.newBuilder[Val] | ||
| val groupCount = matcher.groupCount() | ||
| for (i <- 0 to groupCount) { | ||
| val m = matcher.group(i) | ||
| if (m == null) { | ||
| captures += Val.Null(pos.noOffset) | ||
| } else { | ||
| captures += Val.Str(pos.noOffset, m) | ||
| } | ||
| } | ||
| val result = captures.result() | ||
| Val.Obj.mk(pos.noOffset, | ||
| "string" -> new Obj.ConstMember(true, Visibility.Normal, result.head), | ||
| "captures" -> new Obj.ConstMember(true, Visibility.Normal, new Val.Arr(pos.noOffset, result.drop(1))) | ||
| ) | ||
| } | ||
| } | ||
| }, | ||
| "regexGlobalReplace" -> new Val.Builtin3("regexGlobalReplace", "str", "pattern", "to") { | ||
| override def evalRhs(str: Val, pattern: Val, to: Val, ev: EvalScope, pos: Position): Val = { | ||
| val compiledPattern = Platform.getPatternFromCache(pattern.asString) | ||
| val matcher = compiledPattern.matcher(str.asString) | ||
| Val.Str(pos.noOffset, matcher.replaceAll(to.asString)) | ||
| } | ||
| }, | ||
| "regexReplace" -> new Val.Builtin3("regexReplace", "str", "pattern", "to") { | ||
| override def evalRhs(str: Val, pattern: Val, to: Val, ev: EvalScope, pos: Position): Val = { | ||
| val compiledPattern = Platform.getPatternFromCache(pattern.asString) | ||
| val matcher = compiledPattern.matcher(str.asString) | ||
| Val.Str(pos.noOffset, matcher.replaceFirst(to.asString)) | ||
| } | ||
| }, | ||
| "regexQuoteMeta" -> new Val.Builtin1("regexQuoteMeta", "str") { | ||
| override def evalRhs(str: Val, ev: EvalScope, pos: Position): Val = { | ||
| Val.Str(pos.noOffset, Platform.regexQuote(str.asString)) | ||
| } | ||
| } | ||
| ) | ||
| } |
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,38 @@ | ||
| package sjsonnet | ||
|
|
||
| import sjsonnet.TestUtils.eval | ||
| import utest._ | ||
|
|
||
| object StdRegexTests extends TestSuite { | ||
| def tests: Tests = Tests { | ||
| test("std.native - regex") { | ||
| eval("""std.native("regexPartialMatch")("a(b)c", "cabc")""") ==> ujson.Obj( | ||
| "string" -> "abc", | ||
| "captures" -> ujson.Arr("b") | ||
| ) | ||
| eval("""std.native("regexPartialMatch")("a(b)c", "def")""") ==> ujson.Obj( | ||
| "string" -> ujson.Null, | ||
| "captures" -> ujson.Arr() | ||
| ) | ||
| eval("""std.native("regexPartialMatch")("a(b)c", "abcabc")""") ==> ujson.Obj( | ||
| "string" -> "abc", | ||
| "captures" -> ujson.Arr("b", "b") | ||
| ) | ||
| eval("""std.native("regexFullMatch")("a(b)c", "abc")""") ==> ujson.Obj( | ||
| "string" -> "abc", | ||
| "captures" -> ujson.Arr("b") | ||
| ) | ||
| eval("""std.native("regexFullMatch")("a(b)c", "cabc")""") ==> ujson.Obj( | ||
| "string" -> ujson.Null, | ||
| "captures" -> ujson.Arr() | ||
| ) | ||
| eval("""std.native("regexFullMatch")("a(b)c", "def")""") ==> ujson.Obj( | ||
| "string" -> ujson.Null, | ||
| "captures" -> ujson.Arr() | ||
| ) | ||
| eval("""std.native("regexGlobalReplace")("abcbbb", "b", "d")""") ==> ujson.Str("adcddd") | ||
| eval("""std.native("regexReplace")("abcbbb", "b", "d")""") ==> ujson.Str("adcbbb") | ||
| eval("""std.native("regexQuoteMeta")("a.b")""") ==> ujson.Str(Platform.regexQuote("a.b")) | ||
| } | ||
| } | ||
| } |
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.