This repository has been archived by the owner on Nov 26, 2023. It is now read-only.
forked from simeji/jid
-
Notifications
You must be signed in to change notification settings - Fork 32
/
query.go
178 lines (154 loc) · 3.45 KB
/
query.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package jiq
import (
"regexp"
"strings"
)
type Query struct {
query *[]rune
}
func NewQuery(query []rune) *Query {
q := &Query{
query: &[]rune{},
}
_ = q.Set(query)
return q
}
func (q *Query) Get() []rune {
return *q.query
}
func (q *Query) Set(query []rune) []rune {
q.query = &query
return q.Get()
}
func (q *Query) Insert(query []rune, idx int) []rune {
qq := q.Get()
if idx == 0 {
qq = append(query, qq...)
} else if idx > 0 && len(qq) >= idx {
_q := make([]rune, idx+len(query)-1)
copy(_q, qq[:idx])
qq = append(append(_q, query...), qq[idx:]...)
}
return q.Set(qq)
}
func (q *Query) StringInsert(query string, idx int) string {
return string(q.Insert([]rune(query), idx))
}
func (q *Query) Add(query []rune) []rune {
return q.Set(append(q.Get(), query...))
}
func (q *Query) Delete(i int) []rune {
qq := q.Get()
lastIdx := len(qq)
if i < 0 {
if lastIdx+i >= 0 {
qq = qq[0 : lastIdx+i]
} else {
qq = qq[0:0]
}
} else if i == 0 {
if len(qq) == 0 {
return q.Set(qq)
}
qq = qq[1:]
} else if i > 0 && i < lastIdx {
qq = append(qq[:i], qq[i+1:]...)
}
return q.Set(qq)
}
func (q *Query) Clear() []rune {
return q.Set([]rune(""))
}
func (q *Query) GetKeywords() [][]rune {
query := string(*q.query)
if query == "" {
return [][]rune{}
}
splitQuery := strings.Split(query, ".")
lastIdx := len(splitQuery) - 1
keywords := [][]rune{}
for i, keyword := range splitQuery {
if keyword != "" || i == lastIdx {
re := regexp.MustCompile(`\[[0-9]*\]?`)
matchIndexes := re.FindAllStringIndex(keyword, -1)
if len(matchIndexes) < 1 {
keywords = append(keywords, []rune(keyword))
} else {
if matchIndexes[0][0] > 0 {
keywords = append(keywords, []rune(keyword[0:matchIndexes[0][0]]))
}
for _, matchIndex := range matchIndexes {
k := keyword[matchIndex[0]:matchIndex[1]]
keywords = append(keywords, []rune(k))
}
}
}
}
return keywords
}
func (q *Query) GetLastKeyword() []rune {
keywords := q.GetKeywords()
if l := len(keywords); l > 0 {
return keywords[l-1]
}
return []rune("")
}
func (q *Query) StringGetLastKeyword() string {
return string(q.GetLastKeyword())
}
func (q *Query) PopKeyword() ([]rune, []rune) {
var keyword []rune
var lastSepIdx int
var lastBracketIdx int
qq := q.Get()
for i, e := range qq {
if e == '.' {
lastSepIdx = i
} else if e == '[' {
lastBracketIdx = i
}
}
if lastBracketIdx > lastSepIdx {
lastSepIdx = lastBracketIdx
}
keywords := q.GetKeywords()
if l := len(keywords); l > 0 {
keyword = keywords[l-1]
}
query := q.Set(qq[0:lastSepIdx])
return keyword, query
}
func (q *Query) StringGet() string {
return string(q.Get())
}
func (q *Query) StringSet(query string) string {
return string(q.Set([]rune(query)))
}
func (q *Query) StringAdd(query string) string {
return string(q.Add([]rune(query)))
}
func (q *Query) StringGetKeywords() []string {
var keywords []string
for _, keyword := range q.GetKeywords() {
keywords = append(keywords, string(keyword))
}
return keywords
}
func (q *Query) StringPopKeyword() (string, []rune) {
keyword, query := q.PopKeyword()
return string(keyword), query
}
func (q *Query) StringSplitLastKeyword() (string, string) {
filter := q.StringGet()
last := strings.LastIndexAny(filter, "].")
if last == -1 {
return strings.TrimSpace(filter), ""
}
switch filter[last] {
case '.':
return filter[:last], filter[last+1:]
case ']':
return filter[:last+1], filter[last+1:]
}
return filter, ""
}