-
Notifications
You must be signed in to change notification settings - Fork 55
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
STDREGEX #244
Draft
stephenamar-db
wants to merge
6
commits into
master
Choose a base branch
from
regexp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+177
−35
Draft
STDREGEX #244
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f78b8fa
STDREGEX
stephenamar-db e446610
Handle null matches properly
stephenamar-db 3212a52
Handle null matches properly
stephenamar-db f9714a0
Handle null matches properly for partialMatch
stephenamar-db ce8a931
fix types
stephenamar-db aee46d2
Move to concurrenthashmap for the pattern cache
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 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 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 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 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 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 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 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 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,73 @@ | ||
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 = Val.Null(pos.noOffset) | ||
val captures = Array.newBuilder[Val.Str] | ||
val groupCount = matcher.groupCount() | ||
while (matcher.find()) { | ||
if (returnStr.isInstanceOf[Val.Null]) { | ||
returnStr = Val.Str(pos.noOffset, matcher.group(0)) | ||
} | ||
for (i <- 1 to groupCount) { | ||
captures += Val.Str(pos.noOffset, matcher.group(i)) | ||
} | ||
} | ||
val result = captures.result() | ||
Val.Obj.mk(pos.noOffset, | ||
"string" -> new Obj.ConstMember(true, Visibility.Normal, 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.Str] | ||
val groupCount = matcher.groupCount() | ||
for (i <- 0 to groupCount) { | ||
captures += Val.Str(pos.noOffset, matcher.group(i)) | ||
} | ||
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("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.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 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 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 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be a concurrenthashmap i think