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

Implement std.trim for string #684

Merged
merged 5 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,15 @@ func builtinIsEmpty(i *interpreter, strv value) (value, error) {
return makeValueBoolean(len(sStr) == 0), nil
}

func builtinTrim(i *interpreter, strv value) (value, error) {
str, err := i.getString(strv)
if err != nil {
return nil, err
}
sStr := str.getGoString()
return makeValueString(strings.TrimSpace(sStr)), nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be compatible with C++, this should be strings.Trim with explicit " \t" I think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strings.TrimSpace removes all kinds of spaces '\t', '\n', '\v', '\f', '\r', ' ', U+0085 (NEL), U+00A0 (NBSP).

}

func base64DecodeGoBytes(i *interpreter, str string) ([]byte, error) {
strLen := len(str)
if strLen%4 != 0 {
Expand Down Expand Up @@ -2230,6 +2239,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
&ternaryBuiltin{name: "splitLimit", function: builtinSplitLimit, params: ast.Identifiers{"str", "c", "maxsplits"}},
&ternaryBuiltin{name: "strReplace", function: builtinStrReplace, params: ast.Identifiers{"str", "from", "to"}},
&unaryBuiltin{name: "isEmpty", function: builtinIsEmpty, params: ast.Identifiers{"str"}},
&unaryBuiltin{name: "trim", function: builtinTrim, params: ast.Identifiers{"str"}},
&unaryBuiltin{name: "base64Decode", function: builtinBase64Decode, params: ast.Identifiers{"str"}},
&unaryBuiltin{name: "base64DecodeBytes", function: builtinBase64DecodeBytes, params: ast.Identifiers{"str"}},
&unaryBuiltin{name: "parseInt", function: builtinParseInt, params: ast.Identifiers{"str"}},
Expand Down
5 changes: 3 additions & 2 deletions linter/internal/types/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func prepareStdlib(g *typeGraph) {
"stringChars": g.newSimpleFuncType(stringType, "str"),
"format": g.newSimpleFuncType(stringType, "str", "vals"),
"isEmpty": g.newSimpleFuncType(boolType, "str"),
"trim": g.newSimpleFuncType(stringType, "str"),
// TODO(sbarzowski) Fix when they match the documentation
"escapeStringBash": g.newSimpleFuncType(stringType, "str_"),
"escapeStringDollars": g.newSimpleFuncType(stringType, "str_"),
Expand Down Expand Up @@ -176,8 +177,8 @@ func prepareStdlib(g *typeGraph) {

// Boolean

"xor": g.newSimpleFuncType(boolType, "x", "y"),
"xnor": g.newSimpleFuncType(boolType, "x", "y"),
"xor": g.newSimpleFuncType(boolType, "x", "y"),
"xnor": g.newSimpleFuncType(boolType, "x", "y"),
}

fieldContains := map[string][]placeholderID{}
Expand Down
1 change: 1 addition & 0 deletions testdata/builtinTrim.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"already trimmed string"
1 change: 1 addition & 0 deletions testdata/builtinTrim.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.trim("already trimmed string")
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinTrim1.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"string with spaces on both ends"
1 change: 1 addition & 0 deletions testdata/builtinTrim1.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.trim(" string with spaces on both ends ")
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinTrim2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"string with newline character at end"
1 change: 1 addition & 0 deletions testdata/builtinTrim2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.trim("string with newline character at end\n")
Empty file.
Empty file added testdata/builtinTrim3.golden
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinTrim3.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.trim("string with tabs at end\t\t")
Empty file.
9 changes: 9 additions & 0 deletions testdata/builtinTrim4.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
RUNTIME ERROR: Unexpected type number, expected string
-------------------------------------------------
testdata/builtinTrim4:1:1-13 $

std.trim(10)

-------------------------------------------------
During evaluation

1 change: 1 addition & 0 deletions testdata/builtinTrim4.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.trim(10)
Empty file.