gime-lib/tag_collection.go

81 lines
1.8 KiB
Go
Raw Normal View History

2017-07-31 12:00:16 +00:00
package gime
import "fmt"
// TagCollection is a collection of strings for gomobile bindings
type TagCollection struct {
list []string
}
// Length returns the number of tags in the collection
func (tc *TagCollection) Length() int {
return len(tc.list)
}
// Get returns the string at idx or an empty string
func (tc *TagCollection) Get(idx int) string {
if idx < tc.Length() {
return tc.list[idx]
}
return ""
}
// Clear empties the list of Tags
func (tc *TagCollection) Clear() {
tc.list = tc.list[:0]
}
// Index finds the index of the given string or -1 if not found
func (tc *TagCollection) Index(t string) int {
for i, tst := range tc.list {
if tst == t {
return i
}
}
return -1
}
// Insert inserts a string into the collection at i
func (tc *TagCollection) Insert(i int, t string) {
if i < 0 || i > tc.Length() {
fmt.Println("gime: Attempted to insert string at invalid index")
}
tc.list = append(tc.list, "")
copy(tc.list[i+1:], tc.list[i:])
tc.list[i] = t
}
// Remove removes the string at i from the collection
func (tc *TagCollection) Remove(i int) {
if i < 0 || i >= tc.Length() {
fmt.Println("gime: Attempted to remove tag at invalid index")
}
copy(tc.list[i:], tc.list[i+1:])
tc.list[len(tc.list)-1] = ""
tc.list = tc.list[:len(tc.list)-1]
}
// Push adds an element to the end of the collection
func (tc *TagCollection) Push(t string) {
tc.Insert(tc.Length(), t)
}
// Pop removes the last element from the collection
func (tc *TagCollection) Pop() string {
ret := tc.list[tc.Length()-1]
tc.Remove(tc.Length() - 1)
return ret
}
// Unshift adds an element to the front of the collection
func (tc *TagCollection) Unshift(t string) {
tc.Insert(0, t)
}
// Shift removes an element from the front of the collection
func (tc *TagCollection) Shift() string {
ret := tc.list[0]
tc.Remove(0)
return ret
}