-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollections.go
More file actions
54 lines (45 loc) · 1.27 KB
/
collections.go
File metadata and controls
54 lines (45 loc) · 1.27 KB
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
package lege
// Location represents location in an input string
type Location struct {
Line int
Pos int
}
// Collection represents a string that has been "plucked" from a source
type Collection struct {
Boundary
runes []rune
StartLocation Location
EndLocation Location
}
// Collections is a list of *Collection
type Collections []*Collection
// NewCollection allows a caller to construct a collection
func NewCollection(start, end Location, boundary Boundary, s string) *Collection {
return &Collection{
StartLocation: start,
EndLocation: end,
Boundary: boundary,
runes: []rune(s),
}
}
func (collections Collections) getLast() *Collection {
return collections[len(collections)-1]
}
// Strings returns each collection as a string, in a list of strings
func (collections Collections) Strings() (s []string) {
for _, collection := range collections {
s = append(s, collection.String())
}
return s
}
func (collection *Collection) addRune(r rune) {
collection.runes = append(collection.runes, r)
}
func (collection *Collection) trimRightRunes(num int) {
if num <= len(collection.runes) {
collection.runes = collection.runes[:len(collection.runes)-num]
}
}
func (collection *Collection) String() string {
return string(collection.runes)
}