-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RegEx supports using RE2 to sjsonnet (#244)
With this PR, I'm adding a handful of methods to expose regular expressions in jsonnet, through std.native() I'm modeling them after [this open PR from jsonnet](google/jsonnet#1039), which was ported to jrsonnet. For now, they are in std.native() as they are not part of the default std package and use RE2 instead of the native regexp package (for performance and compatibility reasons with a future go-jsonnet implementation). - regexFullMatch(pattern, str) -- Full match regex - regexPartialMatch(pattern, str) -- Partial match regex - regexReplace(str, pattern, to) -- Replace single occurance using regex - regexGlobalReplace(str, pattern, to) -- Replace globally using regex and the utility function: - regexQuoteMeta(str) -- Escape regex metachararacters Those functions return a object: ``` std.native("regexFullMatch")("h(?P<mid>.*)o", "hello") { "captures": [ "ell" ], "string": "hello" } ``` This PR does not add support for the "namedCaptures" return field due to some complications with scalajs and scalanative. Those language both use the JDK Pattern class (js being powered by ECMA regex and Native being powered by RE2(!)), but JDK<20 Pattern class does not have a straightforward way to list the names of groups without some additional hacks. This will be dealt with in a follow up PR. This PR also adds the ability to cache patterns, and refactors all users of regexes to use it.
- Loading branch information
1 parent
1497955
commit 729e0d8
Showing
11 changed files
with
187 additions
and
36 deletions.
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
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 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")) | ||
} | ||
} | ||
} |