mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-28 17:01:02 +02:00
Fast, native implementation of strReplace
This commit is contained in:
parent
d1d9a7f5f8
commit
61b33d1a2a
24
builtins.go
24
builtins.go
@ -23,6 +23,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/google/go-jsonnet/ast"
|
||||
)
|
||||
@ -635,6 +636,28 @@ func builtinPow(e *evaluator, basep potentialValue, expp potentialValue) (value,
|
||||
return makeDoubleCheck(e, math.Pow(base.value, exp.value))
|
||||
}
|
||||
|
||||
func builtinStrReplace(e *evaluator, strp, fromp, top potentialValue) (value, error) {
|
||||
str, err := e.evaluateString(strp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
from, err := e.evaluateString(fromp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
to, err := e.evaluateString(top)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sStr := str.getString()
|
||||
sFrom := from.getString()
|
||||
sTo := to.getString()
|
||||
if len(sFrom) == 0 {
|
||||
return nil, e.Error("'from' string must not be zero length.")
|
||||
}
|
||||
return makeValueString(strings.Replace(sStr, sFrom, sTo, -1)), nil
|
||||
}
|
||||
|
||||
func builtinUglyObjectFlatMerge(e *evaluator, objarrp potentialValue) (value, error) {
|
||||
objarr, err := e.evaluateArray(objarrp)
|
||||
if err != nil {
|
||||
@ -875,6 +898,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
|
||||
&BinaryBuiltin{name: "pow", function: builtinPow, parameters: ast.Identifiers{"base", "exp"}},
|
||||
&BinaryBuiltin{name: "modulo", function: builtinModulo, parameters: ast.Identifiers{"x", "y"}},
|
||||
&UnaryBuiltin{name: "md5", function: builtinMd5, parameters: ast.Identifiers{"x"}},
|
||||
&TernaryBuiltin{name: "strReplace", function: builtinStrReplace, parameters: ast.Identifiers{"str", "from", "to"}},
|
||||
&UnaryBuiltin{name: "native", function: builtinNative, parameters: ast.Identifiers{"x"}},
|
||||
|
||||
// internal
|
||||
|
1
testdata/strReplace.golden
vendored
Normal file
1
testdata/strReplace.golden
vendored
Normal file
File diff suppressed because one or more lines are too long
3
testdata/strReplace.jsonnet
vendored
Normal file
3
testdata/strReplace.jsonnet
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
local n = 10000;
|
||||
local text = std.join("", std.makeArray(n, function(x) "ab"));
|
||||
std.strReplace(text, "a", "b")
|
1
testdata/strReplace2.golden
vendored
Normal file
1
testdata/strReplace2.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
"__ __ __"
|
1
testdata/strReplace2.jsonnet
vendored
Normal file
1
testdata/strReplace2.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
||||
std.strReplace("test test test", "test", "__")
|
8
testdata/strReplace3.golden
vendored
Normal file
8
testdata/strReplace3.golden
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
RUNTIME ERROR: 'from' string must not be zero length.
|
||||
-------------------------------------------------
|
||||
<builtin> builtin function <strReplace>
|
||||
|
||||
-------------------------------------------------
|
||||
During evaluation
|
||||
|
||||
|
1
testdata/strReplace3.jsonnet
vendored
Normal file
1
testdata/strReplace3.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
||||
std.strReplace("test", "", "blah")
|
Loading…
x
Reference in New Issue
Block a user