Skip to content

Commit a7225df

Browse files
committed
feat: add trim_left and trim_right functions
These functions trim characters from the left or right side of a string. They take a character set (not a literal substring) and remove all matching characters from the specified side. Examples: trim_left("aaabbb", "a") → "bbb" (all 'a's removed from left) trim_right("aaabbb", "b") → "aaa" (all 'b's removed from right) trim_left(" hello ") → "hello " (whitespace from left) When no second argument is provided, they default to trimming whitespace. As of this commit, they behave exactly like trim_prefix and trim_suffix, but we're about to change these to work specifically on literal prefix/suffixes.
1 parent f62848d commit a7225df

File tree

6 files changed

+94
-2
lines changed

6 files changed

+94
-2
lines changed

core/funcs.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ const (
9191
FUNC_TRIM = "trim"
9292
FUNC_TRIM_PREFIX = "trim_prefix"
9393
FUNC_TRIM_SUFFIX = "trim_suffix"
94+
FUNC_TRIM_LEFT = "trim_left"
95+
FUNC_TRIM_RIGHT = "trim_right"
9496
FUNC_READ_FILE = "read_file"
9597
FUNC_WRITE_FILE = "write_file"
9698
FUNC_READ_STDIN = "read_stdin"
@@ -1035,6 +1037,22 @@ func init() {
10351037
})
10361038
},
10371039
},
1040+
{
1041+
Name: FUNC_TRIM_LEFT,
1042+
Execute: func(f FuncInvocation) RadValue {
1043+
return runTrim(f, func(str RadString, chars string) RadString {
1044+
return str.TrimLeft(chars)
1045+
})
1046+
},
1047+
},
1048+
{
1049+
Name: FUNC_TRIM_RIGHT,
1050+
Execute: func(f FuncInvocation) RadValue {
1051+
return runTrim(f, func(str RadString, chars string) RadString {
1052+
return str.TrimRight(chars)
1053+
})
1054+
},
1055+
},
10381056
{
10391057
// todo potential additional named args
10401058
// - encoding="utf-8", # Or null for raw bytes
@@ -1928,9 +1946,9 @@ func httpMethodFromFuncName(httpFunc string) string {
19281946

19291947
func runTrim(f FuncInvocation, trimFunc func(str RadString, chars string) RadString) RadValue {
19301948
subject := f.GetStr("_subject")
1931-
toTrim := f.GetStr("_to_trim").Plain()
1949+
chars := f.GetStr("_chars").Plain()
19321950

1933-
subject = trimFunc(subject, toTrim)
1951+
subject = trimFunc(subject, chars)
19341952
return f.Return(subject)
19351953
}
19361954

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package testing
2+
3+
import "testing"
4+
5+
func Test_Func_Trim_Left_SingleChar(t *testing.T) {
6+
script := `
7+
print(trim_left("aaabbb", "a"))
8+
`
9+
setupAndRunCode(t, script, "--color=never")
10+
assertOnlyOutput(t, stdOutBuffer, "bbb\n") // ALL "a"s removed
11+
assertNoErrors(t)
12+
}
13+
14+
func Test_Func_Trim_Left_MultiChar(t *testing.T) {
15+
script := `
16+
print(trim_left(",,!!,hello,!,", "!,"))
17+
`
18+
setupAndRunCode(t, script, "--color=never")
19+
assertOnlyOutput(t, stdOutBuffer, "hello,!,\n")
20+
assertNoErrors(t)
21+
}
22+
23+
func Test_Func_Trim_Left_NoArg_Whitespace(t *testing.T) {
24+
script := `
25+
print(trim_left(" hello "))
26+
`
27+
setupAndRunCode(t, script, "--color=never")
28+
assertOnlyOutput(t, stdOutBuffer, "hello \n")
29+
assertNoErrors(t)
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package testing
2+
3+
import "testing"
4+
5+
func Test_Func_Trim_Right_SingleChar(t *testing.T) {
6+
script := `
7+
print(trim_right("aaabbb", "b"))
8+
`
9+
setupAndRunCode(t, script, "--color=never")
10+
assertOnlyOutput(t, stdOutBuffer, "aaa\n") // ALL "b"s removed
11+
assertNoErrors(t)
12+
}
13+
14+
func Test_Func_Trim_Right_MultiChar(t *testing.T) {
15+
script := `
16+
print(trim_right(",,!!,hello,!,", "!,"))
17+
`
18+
setupAndRunCode(t, script, "--color=never")
19+
assertOnlyOutput(t, stdOutBuffer, ",,!!,hello\n")
20+
assertNoErrors(t)
21+
}
22+
23+
func Test_Func_Trim_Right_NoArg_Whitespace(t *testing.T) {
24+
script := `
25+
print(trim_right(" hello "))
26+
`
27+
setupAndRunCode(t, script, "--color=never")
28+
assertOnlyOutput(t, stdOutBuffer, " hello\n")
29+
assertNoErrors(t)
30+
}

core/type_string.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ func (s *RadString) TrimSuffix(suffix string) RadString {
213213
return NewRadString(strings.TrimRight(s.Plain(), suffix))
214214
}
215215

216+
func (s *RadString) TrimLeft(chars string) RadString {
217+
// todo should maintain attr info
218+
return NewRadString(strings.TrimLeft(s.Plain(), chars))
219+
}
220+
221+
func (s *RadString) TrimRight(chars string) RadString {
222+
// todo should maintain attr info
223+
return NewRadString(strings.TrimRight(s.Plain(), chars))
224+
}
225+
216226
func (s *RadString) Reverse() RadString {
217227
// todo should maintain attr info
218228
return NewRadString(com.Reverse(s.Plain()))

rts/embedded/functions.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ starts_with
9292
str
9393
sum
9494
trim
95+
trim_left
9596
trim_prefix
97+
trim_right
9698
trim_suffix
9799
truncate
98100
type_of

rts/signatures.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ func init() {
8080
newFnSignature(`trim(_subject: str, _to_trim: str = " \t\n") -> str`),
8181
newFnSignature(`trim_prefix(_subject: str, _to_trim: str = " \t\n") -> str`),
8282
newFnSignature(`trim_suffix(_subject: str, _to_trim: str = " \t\n") -> str`),
83+
newFnSignature(`trim_left(_subject: str, _to_trim: str = " \t\n") -> str`),
84+
newFnSignature(`trim_right(_subject: str, _to_trim: str = " \t\n") -> str`),
8385
newFnSignature(`read_file(_path: str, *, mode: ["text", "bytes"] = "text") -> error|{ "size_bytes": int, "content": str|int[] }`),
8486
newFnSignature(`write_file(_path: str, _content: str, *, append: bool = false) -> error|{ "bytes_written": int, "path": str }`),
8587
newFnSignature(`read_stdin() -> str?|error`),

0 commit comments

Comments
 (0)