Skip to content
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

Fix usage of - in stripChars with the introduction of RE2 #253

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sjsonnet/src-native/sjsonnet/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.io.{ByteArrayOutputStream, File}
import java.util
import java.util.Base64
import java.util.zip.GZIPOutputStream
import java.util.regex.Pattern
import scala.scalanative.regex.Pattern

object Platform {
def gzipBytes(b: Array[Byte]): String = {
Expand Down
21 changes: 17 additions & 4 deletions sjsonnet/src/sjsonnet/Std.scala
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,26 @@ class Std(private val additionalNativeFunctions: Map[String, Val.Builtin] = Map.
}

private object StripUtils {
private val dashPattern = Platform.getPatternFromCache("-")

private def cleanupPattern(chars: String): String = {
val matcher = dashPattern.matcher(chars)
if (matcher.find()) {
matcher.replaceAll("") + "-"
} else {
chars
}
}

private def getLeadingPattern(chars: String): String = "^[" + Platform.regexQuote(chars) + "]+"

private def getTrailingPattern(chars: String): String = "[" + Platform.regexQuote(chars) + "]+$"

def unspecializedStrip(str: String, chars: String, left: Boolean, right: Boolean): String = {
var s = str
if (right) s = Platform.getPatternFromCache(getTrailingPattern(chars)).matcher(s).replaceAll("")
if (left) s = Platform.getPatternFromCache(getLeadingPattern(chars)).matcher(s).replaceAll("")
val cleanedUpPattern = cleanupPattern(chars)
if (right) s = Platform.getPatternFromCache(getTrailingPattern(cleanedUpPattern)).matcher(s).replaceAll("")
if (left) s = Platform.getPatternFromCache(getLeadingPattern(cleanedUpPattern)).matcher(s).replaceAll("")
s
}

Expand All @@ -500,8 +512,9 @@ class Std(private val additionalNativeFunctions: Map[String, Val.Builtin] = Map.
right: Boolean,
functionName: String
) extends Val.Builtin1(functionName, "str") {
private[this] val leftPattern = Platform.getPatternFromCache(getLeadingPattern(chars))
private[this] val rightPattern = Platform.getPatternFromCache(getTrailingPattern(chars))
private[this] val cleanedUpPattern = cleanupPattern(chars)
private[this] val leftPattern = Platform.getPatternFromCache(getLeadingPattern(cleanedUpPattern))
private[this] val rightPattern = Platform.getPatternFromCache(getTrailingPattern(cleanedUpPattern))

def evalRhs(str: Val, ev: EvalScope, pos: Position): Val = {
var s = str.asString
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sjsonnet

import sjsonnet.TestUtils.eval
import utest._
import TestUtils.eval
object StdStripCharsTests extends TestSuite {

def tests = Tests {
Expand All @@ -10,13 +10,15 @@ object StdStripCharsTests extends TestSuite {
eval("std.rstripChars(\"aaabbbbcccc\", \"ac\")").toString() ==> """"aaabbbb""""
eval("std.rstripChars(\"cacabbbbaacc\", \"ac\")").toString() ==> """"cacabbbb""""
eval("std.rstripChars(\"cacabbcacabbaacc\", \"ac\")").toString() ==> """"cacabbcacabb""""
eval("std.rstripChars(\"cacabbcacabb-aacc\", \"a-c\")").toString() ==> """"cacabbcacabb""""

eval("""std.rstripChars("cacabbcacabb[aacc]", "ac[]$%^&*(")""").toString() ==> """"cacabbcacabb""""
}
test("stdLStripChars") {
eval("std.lstripChars(\" test test test \", \" \")").toString() ==> """"test test test """"
eval("std.lstripChars(\"aaabbbbcccc\", \"ac\")").toString() ==> """"bbbbcccc""""
eval("std.lstripChars(\"cacabbcacabbaacc\", \"ac\")").toString() ==> """"bbcacabbaacc""""
eval("std.lstripChars(\"-cacabbcacabbaacc\", \"a-c\")").toString() ==> """"bbcacabbaacc""""

eval("std.lstripChars(\"[]aaabbbbcccc\", \"[ac]\")").toString() ==> """"bbbbcccc""""

Expand All @@ -25,6 +27,7 @@ object StdStripCharsTests extends TestSuite {
eval("std.stripChars(\" test test test \", \" \")").toString() ==> """"test test test""""
eval("std.stripChars(\"aaabbbbcccc\", \"ac\")").toString() ==> """"bbbb""""
eval("std.stripChars(\"cacabbcacabbaacc\", \"ac\")").toString() ==> """"bbcacabb""""
eval("std.stripChars(\"c-acabbca-cabbaacc-\", \"a-c\")").toString() ==> """"bbca-cabb""""

eval("std.stripChars(\"[aaabbbbcccc]\", \"ac[]\")").toString() ==> """"bbbb""""
}
Expand Down
Loading