7 "github.com/mjl-/mox/store"
11 searchResult bool // "$"
17 last *setNumber // if nil, this numRange is just a setNumber in "first" and first.star will be false
20type setNumber struct {
25// containsSeq returns whether seq is in the numSet, given uids and (saved) searchResult.
26// uids and searchResult must be sorted. searchResult can have uids that are no longer in uids.
27func (ss numSet) containsSeq(seq msgseq, uids []store.UID, searchResult []store.UID) bool {
32 uid := uids[int(seq)-1]
33 return uidSearch(searchResult, uid) > 0 && uidSearch(uids, uid) > 0
35 for _, r := range ss.ranges {
36 first := r.first.number
37 if r.first.star || first > uint32(len(uids)) {
38 first = uint32(len(uids))
44 if r.last.star || last > uint32(len(uids)) {
45 last = uint32(len(uids))
49 first, last = last, first
52 if uint32(seq) >= first && uint32(seq) <= last {
59func (ss numSet) containsUID(uid store.UID, uids []store.UID, searchResult []store.UID) bool {
64 return uidSearch(searchResult, uid) > 0 && uidSearch(uids, uid) > 0
66 for _, r := range ss.ranges {
67 first := store.UID(r.first.number)
68 if r.first.star || first > uids[len(uids)-1] {
69 first = uids[len(uids)-1]
72 // Num in <num>:* can be larger than last, but it still matches the last...
75 last = store.UID(r.last.number)
76 if r.last.star || last > uids[len(uids)-1] {
77 last = uids[len(uids)-1]
81 first, last = last, first
83 if uid >= first && uid <= last && uidSearch(uids, uid) > 0 {
90// contains returns whether the numset contains the number.
91// only allowed on basic, strictly increasing numsets.
92func (ss numSet) contains(v uint32) bool {
93 for _, r := range ss.ranges {
94 if r.first.number == v || r.last != nil && v > r.first.number && v <= r.last.number {
101func (ss numSet) empty() bool {
102 return !ss.searchResult && len(ss.ranges) == 0
105// Strings returns the numset in zero or more strings of maxSize bytes. If
106// maxSize is <= 0, a single string is returned.
107func (ss numSet) Strings(maxSize int) []string {
113 for _, r := range ss.ranges {
118 s += fmt.Sprintf("%d", r.first.number)
122 panic("invalid numSet range first star without last")
129 s += fmt.Sprintf("%d", r.last.number)
133 nsize := len(line) + len(s)
137 if maxSize > 0 && nsize > maxSize {
153func (ss numSet) String() string {
161// interpretStar returns a numset that interprets stars in a numset, returning a new
162// numset without stars with increasing first/last.
163func (s numSet) interpretStar(uids []store.UID) numSet {
169 for _, r := range s.ranges {
170 first := r.first.number
171 if r.first.star || first > uint32(uids[len(uids)-1]) {
172 first = uint32(uids[len(uids)-1])
177 if r.last.star || last > uint32(uids[len(uids)-1]) {
178 last = uint32(uids[len(uids)-1])
182 first, last = last, first
184 nr := numRange{first: setNumber{number: first}}
186 nr.last = &setNumber{number: last}
188 ns.ranges = append(ns.ranges, nr)
193// whether numSet only has numbers (no star/search), and is strictly increasing.
194func (s *numSet) isBasicIncreasing() bool {
199 for _, r := range s.ranges {
200 if r.first.star || r.first.number <= last || r.last != nil && (r.last.star || r.last.number < r.first.number) {
203 last = r.first.number
217// newIter must only be called on a numSet that is basic (no star/search) and ascending.
218func (s numSet) newIter() *numIter {
219 return &numIter{s: s}
222func (i *numIter) Next() (uint32, bool) {
223 if v, ok := i.r.Next(); ok {
226 if i.i >= len(i.s.ranges) {
229 i.r = i.s.ranges[i.i].newIter()
234type rangeIter struct {
239// newIter must only be called on a range in a numSet that is basic (no star/search) and ascending.
240func (r numRange) newIter() *rangeIter {
241 return &rangeIter{r: r, o: 0}
244func (r *rangeIter) Next() (uint32, bool) {
250 return r.r.first.number, true
252 if r.r.last == nil || r.r.first.number+uint32(r.o) > r.r.last.number {
255 v := r.r.first.number + uint32(r.o)
260// append adds a new number to the set, extending a range, or starting a new one (possibly the first).
261// can only be used on basic numsets, without star/searchResult.
262func (s *numSet) append(v uint32) {
263 if len(s.ranges) == 0 {
264 s.ranges = []numRange{{first: setNumber{number: v}}}
267 ri := len(s.ranges) - 1
269 if v == r.first.number+1 && r.last == nil {
270 s.ranges[ri].last = &setNumber{number: v}
271 } else if r.last != nil && v == r.last.number+1 {
274 s.ranges = append(s.ranges, numRange{first: setNumber{number: v}})
283type sectionPart struct {
288type sectionText struct {
289 mime bool // if "MIME"
290 msgtext *sectionMsgtext
293// a non-nil *sectionSpec with nil msgtext & nil part means there were []'s, but nothing inside. e.g. "BODY[]".
294type sectionSpec struct {
295 msgtext *sectionMsgtext
299type sectionMsgtext struct {
300 s string // "HEADER", "HEADER.FIELDS", "HEADER.FIELDS.NOT", "TEXT"
301 headers []string // for "HEADER.FIELDS"*
304type fetchAtt struct {
305 field string // uppercase, eg "ENVELOPE", "BODY". ".PEEK" is removed.
308 sectionBinary []uint32
312type searchKey struct {
313 // Only one of searchKeys, seqSet and op can be non-nil/non-empty.
314 searchKeys []searchKey // In case of nested/multiple keys. Also for the top-level command.
315 seqSet *numSet // In case of bare sequence set. For op UID, field uidSet contains the parameter.
316 op string // Determines which of the fields below are set.
323 searchKey2 *searchKey
328func compactUIDSet(l []store.UID) (r numSet) {
331 for ; e < len(l) && l[e] == l[e-1]+1; e++ {
333 first := setNumber{number: uint32(l[0])}
336 last = &setNumber{number: uint32(l[e-1])}
338 r.ranges = append(r.ranges, numRange{first, last})