-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequence.go
117 lines (98 loc) · 2.41 KB
/
sequence.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package contraption
import "unicode/utf8"
// Sequence is the thing that can generate elements for a compound.
type Sequence interface {
Get(wo *World, j int, buf []Sorm) (n int)
Length(wo *World) int
}
type adhocSequence struct {
get func(j int) Sorm
length func() int
}
func (s *adhocSequence) Get(wo *World, j int, buf []Sorm) (n int) {
sl := s.length()
for i := 0; i < min(len(buf), sl-j); i++ {
buf[i] = s.get(j + i)
}
return sl - j
}
func (s *adhocSequence) Length(wo *World) int {
return s.length()
}
func AdhocSequence(get func(i int) Sorm, length func() int) Sequence {
return &adhocSequence{get: get, length: length}
}
func SliceSequence[T any](sl []T, produce func(T) Sorm) Sequence {
return AdhocSequence(func(i int) Sorm { return produce(sl[i]) }, func() int { return len(sl) })
}
func SliceSequence2[T any](sl []T, produce func(int) Sorm) Sequence {
return AdhocSequence(func(i int) Sorm { return produce(i) }, func() int { return len(sl) })
}
type Scrollptr struct {
Index int
Offset float64
y float64
Dirty bool
}
// type appendSequence struct {
// pre bool
// affix []Sorm
// orig Sequence
// }
// func (s *appendSequence) Get(wo *World, i int) Sorm {
// if s.pre {
// al := len(s.affix)
// if i < al {
// return s.affix[i]
// } else {
// return s.orig.Get(wo, al-i)
// }
// } else {
// al := s.orig.Length(wo)
// if i < al {
// return s.orig.Get(wo, i)
// } else {
// return s.affix[al-i]
// }
// }
// }
// func (s *appendSequence) Length(wo *World) int {
// return s.orig.Length(wo) + len(s.affix)
// }
// func PrependSeq(seq Sequence, wo *World, s ...Sorm) Sequence {
// return &appendSequence{
// pre: true,
// affix: wo.stash(s),
// orig: seq,
// }
// }
// func AppendSeq(seq Sequence, wo *World, s ...Sorm) Sequence {
// return &appendSequence{
// pre: false,
// affix: wo.stash(s),
// orig: seq,
// }
// }
type stringSeq struct {
string
len, consumed int
produce func(rune) Sorm
}
func (s *stringSeq) Get(wo *World, j int, buf []Sorm) (n int) {
for i := 0; i < min(len(buf), s.len); i++ {
r, sz := utf8.DecodeRuneInString(s.string[s.consumed:])
buf[i] = s.produce(r)
s.consumed += sz
}
return s.len - j
}
func (s *stringSeq) Length(wo *World) int {
return s.len
}
func StringSeq(s string, produce func(rune) Sorm) Sequence {
return &stringSeq{
string: s,
produce: produce,
len: utf8.RuneCountInString(s),
}
}