/
string_split.go
62 lines (57 loc) · 1.29 KB
/
string_split.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package elem
import (
"strings"
"github.com/Bitspark/slang/pkg/core"
"github.com/google/uuid"
)
var stringSplitCfg = &builtinConfig{
safe: true,
blueprint: core.Blueprint{
Id: uuid.MustParse("c02bc7ad-65e5-4a43-a2a3-7d86b109915d"),
Meta: core.BlueprintMetaDef{
Name: "split string",
ShortDescription: "splits a string at a given separator and emits its pieces as stream",
Icon: "cut",
Tags: []string{"string"},
DocURL: "https://bitspark.de/slang/docs/operator/split-string",
},
ServiceDefs: map[string]*core.ServiceDef{
core.MAIN_SERVICE: {
In: core.TypeDef{
Type: "string",
},
Out: core.TypeDef{
Type: "stream",
Stream: &core.TypeDef{
Type: "string",
},
},
},
},
DelegateDefs: map[string]*core.DelegateDef{},
PropertyDefs: map[string]*core.TypeDef{
"separator": {
Type: "string",
},
},
},
opFunc: func(op *core.Operator) {
in := op.Main().In()
out := op.Main().Out()
sep := op.Property("separator").(string)
for !op.CheckStop() {
i := in.Pull()
if core.IsMarker(i) {
out.Push(i)
continue
}
str := i.(string)
strs := strings.Split(str, sep)
out.PushBOS()
for _, s := range strs {
out.Stream().Push(s)
}
out.PushEOS()
}
},
}