Skip to content

Commit 6bc0439

Browse files
authored
std/strings: Add a few Functions (#132)
* wdte: Update dependencies. * std/strings: Add `Repeat()`. * std/strings: Add `Join()`.
1 parent 746d0bd commit 6bc0439

File tree

4 files changed

+84
-8
lines changed

4 files changed

+84
-8
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module github.com/DeedleFake/wdte
22

33
require (
44
github.com/peterh/liner v1.1.0
5-
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b
6-
golang.org/x/sys v0.0.0-20180920110915-d641721ec2de // indirect
5+
golang.org/x/crypto v0.0.0-20181024171144-74cb1d3d52f4
6+
golang.org/x/sys v0.0.0-20181024145615-5cd93ef61a7c // indirect
77
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8Bz
22
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
33
github.com/peterh/liner v1.1.0 h1:f+aAedNJA6uk7+6rXsYBnhdo4Xux7ESLe+kcuVUF5os=
44
github.com/peterh/liner v1.1.0/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0=
5-
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b h1:2b9XGzhjiYsYPnKXoEfL7klWZQIt8IfyRCz62gCqqlQ=
6-
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
7-
golang.org/x/sys v0.0.0-20180920110915-d641721ec2de h1:soC2mvPVpAV+Ld2qtpNn1eq25WTn76uIGNV23bofu6Q=
8-
golang.org/x/sys v0.0.0-20180920110915-d641721ec2de/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5+
golang.org/x/crypto v0.0.0-20181024171144-74cb1d3d52f4 h1:4v3KN0hcTEAkyusBypx0RrpPAhKsTP3YXj10LonM8J8=
6+
golang.org/x/crypto v0.0.0-20181024171144-74cb1d3d52f4/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
7+
golang.org/x/sys v0.0.0-20181024145615-5cd93ef61a7c h1:8QwKN2PcBeeHEiYIX6348SzigNWH9uHHP1EOEs5ExSc=
8+
golang.org/x/sys v0.0.0-20181024145615-5cd93ef61a7c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

std/strings/strings.go

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,81 @@ func Lower(frame wdte.Frame, args ...wdte.Func) wdte.Func {
138138
return wdte.String(strings.ToLower(string(args[0].Call(frame).(wdte.String))))
139139
}
140140

141+
// Repeat is a WDTE function with the following signatures:
142+
//
143+
// repeat string times
144+
// (repeat string) times
145+
// repeat times string
146+
// (repeat times) string
147+
//
148+
// It returns a new string containing the given string repeated the
149+
// number of times specified.
150+
func Repeat(frame wdte.Frame, args ...wdte.Func) wdte.Func {
151+
frame = frame.Sub("repeat")
152+
153+
switch len(args) {
154+
case 0:
155+
return wdte.GoFunc(Repeat)
156+
case 1:
157+
return wdte.GoFunc(func(frame wdte.Frame, next ...wdte.Func) wdte.Func {
158+
return Repeat(frame, append(args, next...)...)
159+
})
160+
}
161+
162+
var str wdte.String
163+
var times wdte.Number
164+
switch a0 := args[0].Call(frame).(type) {
165+
case wdte.String:
166+
str = a0
167+
times = args[1].Call(frame).(wdte.Number)
168+
169+
case wdte.Number:
170+
times = a0
171+
str = args[1].Call(frame).(wdte.String)
172+
}
173+
174+
return wdte.String(strings.Repeat(string(str), int(times)))
175+
}
176+
177+
// Join is a WDTE function with the following signatures:
178+
//
179+
// join strings sep
180+
// (join sep) strings
181+
//
182+
// It returns a new string containing the strings in the provided
183+
// array with sep inserted between each.
184+
func Join(frame wdte.Frame, args ...wdte.Func) wdte.Func {
185+
frame = frame.Sub("join")
186+
187+
switch len(args) {
188+
case 0:
189+
return wdte.GoFunc(Join)
190+
case 1:
191+
return wdte.GoFunc(func(frame wdte.Frame, next ...wdte.Func) wdte.Func {
192+
return Join(frame, append(next, args...)...)
193+
})
194+
}
195+
196+
a := args[0].Call(frame).(wdte.Array)
197+
s := make([]string, 0, len(a))
198+
for _, str := range a {
199+
s = append(s, string(str.(wdte.String)))
200+
}
201+
202+
return wdte.String(strings.Join(s, string(args[1].Call(frame).(wdte.String))))
203+
}
204+
141205
// Scope is a scope containing the functions in this package.
142206
var Scope = wdte.S().Map(map[wdte.ID]wdte.Func{
143207
"contains": wdte.GoFunc(Contains),
144208
"prefix": wdte.GoFunc(Prefix),
145209
"suffix": wdte.GoFunc(Suffix),
146210
"index": wdte.GoFunc(Index),
147211

148-
"upper": wdte.GoFunc(Upper),
149-
"lower": wdte.GoFunc(Lower),
212+
"upper": wdte.GoFunc(Upper),
213+
"lower": wdte.GoFunc(Lower),
214+
"repeat": wdte.GoFunc(Repeat),
215+
"join": wdte.GoFunc(Join),
150216

151217
"format": wdte.GoFunc(Format),
152218
})

wdte_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,16 @@ func TestStrings(t *testing.T) {
570570
script: `let str => import 'strings'; let main => str.lower 'QwErTy';`,
571571
ret: wdte.String("qwerty"),
572572
},
573+
{
574+
name: "Repeat",
575+
script: `let str => import 'strings'; str.repeat 'test' 3;`,
576+
ret: wdte.String("testtesttest"),
577+
},
578+
{
579+
name: "Join",
580+
script: `let str => import 'strings'; str.join ['this'; 'is'; 'a'; 'test'] ' ';`,
581+
ret: wdte.String("this is a test"),
582+
},
573583
{
574584
name: "Format",
575585
script: `let str => import 'strings'; let main => str.format '{#2}{#0}{}' 3 6 9;`,

0 commit comments

Comments
 (0)