-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
repeat.go
40 lines (34 loc) · 825 Bytes
/
repeat.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
package op
import (
"bytes"
"fmt"
"github.com/emicklei/melrose/core"
)
type Repeat struct {
Target []core.Sequenceable
Times core.HasValue
}
func (r Repeat) S() core.Sequence {
times := core.Int(r.Times)
repeated := []core.Sequenceable{}
for i := 0; i < times; i++ {
for _, each := range r.Target {
repeated = append(repeated, each.S())
}
}
return Join{Target: repeated}.S()
}
func (r Repeat) Storex() string {
var b bytes.Buffer
fmt.Fprintf(&b, "repeat(%s", core.Storex(r.Times))
core.AppendStorexList(&b, false, r.Target)
fmt.Fprintf(&b, ")")
return b.String()
}
// Replaced is part of Replaceable
func (r Repeat) Replaced(from, to core.Sequenceable) core.Sequenceable {
if core.IsIdenticalTo(r, from) {
return to
}
return Repeat{Target: replacedAll(r.Target, from, to), Times: r.Times}
}