@@ -4,12 +4,10 @@ import java.io.StringWriter
44import java .nio .charset .StandardCharsets .UTF_8
55import java .util .Base64
66import java .util
7- import java .util .regex .Pattern
87import sjsonnet .Expr .Member .Visibility
98
109import scala .collection .Searching ._
1110import scala .collection .mutable
12- import scala .util .matching .Regex
1311
1412/**
1513 * The Jsonnet standard library, `std`, with each builtin function implemented
@@ -19,8 +17,8 @@ import scala.util.matching.Regex
1917class Std (private val additionalNativeFunctions : Map [String , Val .Builtin ] = Map .empty) {
2018 private val dummyPos : Position = new Position (null , 0 )
2119 private val emptyLazyArray = new Array [Lazy ](0 )
22- private val leadingWhiteSpacePattern = Pattern .compile (" ^[ \t\n\f\r\u0085\u00A0 ']+" )
23- private val trailingWhiteSpacePattern = Pattern .compile (" [ \t\n\f\r\u0085\u00A0 ']+$" )
20+ private val leadingWhiteSpacePattern = Platform .getPatternFromCache (" ^[ \t\n\f\r\u0085\u00A0 ']+" )
21+ private val trailingWhiteSpacePattern = Platform .getPatternFromCache (" [ \t\n\f\r\u0085\u00A0 ']+$" )
2422 private val oldNativeFunctions = Map (
2523 builtin(" gzip" , " v" ){ (_, _, v : Val ) =>
2624 v match {
@@ -48,7 +46,7 @@ class Std(private val additionalNativeFunctions: Map[String, Val.Builtin] = Map.
4846 },
4947 )
5048 require(oldNativeFunctions.forall(k => ! additionalNativeFunctions.contains(k._1)), " Conflicting native functions" )
51- private val nativeFunctions = oldNativeFunctions ++ additionalNativeFunctions
49+ private val nativeFunctions = oldNativeFunctions ++ additionalNativeFunctions ++ StdRegex .functions
5250
5351 private object AssertEqual extends Val .Builtin2 (" assertEqual" , " a" , " b" ) {
5452 def evalRhs (v1 : Val , v2 : Val , ev : EvalScope , pos : Position ): Val = {
@@ -474,26 +472,24 @@ class Std(private val additionalNativeFunctions: Map[String, Val.Builtin] = Map.
474472 Val .Str (pos, str.asString.replaceAll(from.asString, to.asString))
475473 override def specialize (args : Array [Expr ]) = args match {
476474 case Array (str, from : Val .Str , to) =>
477- try { (new SpecFrom (Pattern .compile( from.value) ), Array (str, to)) } catch { case _ : Exception => null }
475+ try { (new SpecFrom (from.value), Array (str, to)) } catch { case _ : Exception => null }
478476 case _ => null
479477 }
480- private class SpecFrom (from : Pattern ) extends Val .Builtin2 (" strReplaceAll" , " str" , " to" ) {
478+ private class SpecFrom (from : String ) extends Val .Builtin2 (" strReplaceAll" , " str" , " to" ) {
481479 def evalRhs (str : Val , to : Val , ev : EvalScope , pos : Position ): Val =
482- Val .Str (pos, from.matcher(str.asString).replaceAll(to.asString))
480+ Val .Str (pos, Platform .getPatternFromCache( from) .matcher(str.asString).replaceAll(to.asString))
483481 }
484482 }
485483
486484 private object StripUtils {
487- private def getLeadingPattern (chars : String ): Pattern =
488- Pattern .compile(" ^[" + Regex .quote(chars) + " ]+" )
485+ private def getLeadingPattern (chars : String ): String = " ^[" + Platform .regexQuote(chars) + " ]+"
489486
490- private def getTrailingPattern (chars : String ): Pattern =
491- Pattern .compile(" [" + Regex .quote(chars) + " ]+$" )
487+ private def getTrailingPattern (chars : String ): String = " [" + Platform .regexQuote(chars) + " ]+$"
492488
493489 def unspecializedStrip (str : String , chars : String , left : Boolean , right : Boolean ): String = {
494490 var s = str
495- if (right) s = getTrailingPattern(chars).matcher(s).replaceAll(" " )
496- if (left) s = getLeadingPattern(chars).matcher(s).replaceAll(" " )
491+ if (right) s = Platform .getPatternFromCache( getTrailingPattern(chars) ).matcher(s).replaceAll(" " )
492+ if (left) s = Platform .getPatternFromCache( getLeadingPattern(chars) ).matcher(s).replaceAll(" " )
497493 s
498494 }
499495
@@ -508,8 +504,8 @@ class Std(private val additionalNativeFunctions: Map[String, Val.Builtin] = Map.
508504
509505 def evalRhs (str : Val , ev : EvalScope , pos : Position ): Val = {
510506 var s = str.asString
511- if (right) s = rightPattern.matcher(s).replaceAll(" " )
512- if (left) s = leftPattern.matcher(s).replaceAll(" " )
507+ if (right) s = Platform .getPatternFromCache( rightPattern) .matcher(s).replaceAll(" " )
508+ if (left) s = Platform .getPatternFromCache( leftPattern) .matcher(s).replaceAll(" " )
513509 Val .Str (pos, s)
514510 }
515511 }
0 commit comments