7 "github.com/mjl-/mox/store"
11 searchResult bool // "$"
15// containsSeq returns whether seq is in the numSet, given uids and (saved) searchResult.
16// uids and searchResult must be sorted. searchResult can have uids that are no longer in uids.
17func (ss numSet) containsSeq(seq msgseq, uids []store.UID, searchResult []store.UID) bool {
22 uid := uids[int(seq)-1]
23 return uidSearch(searchResult, uid) > 0 && uidSearch(uids, uid) > 0
25 for _, r := range ss.ranges {
26 first := r.first.number
34 last = uint32(len(uids))
37 if last > uint32(len(uids)) {
38 last = uint32(len(uids))
40 if uint32(seq) >= first && uint32(seq) <= last {
47func (ss numSet) containsUID(uid store.UID, uids []store.UID, searchResult []store.UID) bool {
52 return uidSearch(searchResult, uid) > 0 && uidSearch(uids, uid) > 0
54 for _, r := range ss.ranges {
55 first := store.UID(r.first.number)
60 // Num in <num>:* can be larger than last, but it still matches the last...
63 last = store.UID(r.last.number)
65 last = uids[len(uids)-1]
69 } else if r.first.star && last < first {
73 if uid < first || uid > last {
76 if uidSearch(uids, uid) > 0 {
83// contains returns whether the numset contains the number.
84// only allowed on basic, strictly increasing numsets.
85func (ss numSet) contains(v uint32) bool {
86 for _, r := range ss.ranges {
87 if r.first.number == v || r.last != nil && v > r.first.number && v <= r.last.number {
94func (ss numSet) empty() bool {
95 return !ss.searchResult && len(ss.ranges) == 0
98// Strings returns the numset in zero or more strings of maxSize bytes. If
99// maxSize is <= 0, a single string is returned.
100func (ss numSet) Strings(maxSize int) []string {
106 for _, r := range ss.ranges {
111 s += fmt.Sprintf("%d", r.first.number)
115 panic("invalid numSet range first star without last")
122 s += fmt.Sprintf("%d", r.last.number)
126 nsize := len(line) + len(s)
130 if maxSize > 0 && nsize > maxSize {
146func (ss numSet) String() string {
154type setNumber struct {
159type numRange struct {
161 last *setNumber // if nil, this numRange is just a setNumber in "first" and first.star will be false
164// interpretStar returns a numset that interprets stars in a numset, returning a new
165// numset without stars with increasing first/last.
166func (s numSet) interpretStar(uids []store.UID) numSet {
168 for _, r := range s.ranges {
169 first := r.first.number
174 first = uint32(uids[0])
183 last = uint32(uids[len(uids)-1])
187 } else if r.first.star && last < first {
192 first, last = last, first
194 nr := numRange{first: setNumber{number: first}}
196 nr.last = &setNumber{number: last}
198 ns.ranges = append(ns.ranges, nr)
203// whether numSet only has numbers (no star/search), and is strictly increasing.
204func (s *numSet) isBasicIncreasing() bool {
209 for _, r := range s.ranges {
210 if r.first.star || r.first.number <= last || r.last != nil && (r.last.star || r.last.number < r.first.number) {
213 last = r.first.number
227// newIter must only be called on a numSet that is basic (no star/search) and ascending.
228func (s numSet) newIter() *numIter {
229 return &numIter{s: s, i: 0, r: s.ranges[0].newIter()}
232func (i *numIter) Next() (uint32, bool) {
233 if v, ok := i.r.Next(); ok {
237 if i.i >= len(i.s.ranges) {
240 i.r = i.s.ranges[i.i].newIter()
244type rangeIter struct {
249// newIter must only be called on a range in a numSet that is basic (no star/search) and ascending.
250func (r numRange) newIter() *rangeIter {
251 return &rangeIter{r: r, o: 0}
254func (r *rangeIter) Next() (uint32, bool) {
257 return r.r.first.number, true
259 if r.r.last == nil || r.r.first.number+uint32(r.o) > r.r.last.number {
262 v := r.r.first.number + uint32(r.o)
267// append adds a new number to the set, extending a range, or starting a new one (possibly the first).
268// can only be used on basic numsets, without star/searchResult.
269func (s *numSet) append(v uint32) {
270 if len(s.ranges) == 0 {
271 s.ranges = []numRange{{first: setNumber{number: v}}}
274 ri := len(s.ranges) - 1
276 if v == r.first.number+1 && r.last == nil {
277 s.ranges[ri].last = &setNumber{number: v}
278 } else if r.last != nil && v == r.last.number+1 {
281 s.ranges = append(s.ranges, numRange{first: setNumber{number: v}})
290type sectionPart struct {
295type sectionText struct {
296 mime bool // if "MIME"
297 msgtext *sectionMsgtext
300// a non-nil *sectionSpec with nil msgtext & nil part means there were []'s, but nothing inside. e.g. "BODY[]".
301type sectionSpec struct {
302 msgtext *sectionMsgtext
306type sectionMsgtext struct {
307 s string // "HEADER", "HEADER.FIELDS", "HEADER.FIELDS.NOT", "TEXT"
308 headers []string // for "HEADER.FIELDS"*
311type fetchAtt struct {
312 field string // uppercase, eg "ENVELOPE", "BODY". ".PEEK" is removed.
315 sectionBinary []uint32
319type searchKey struct {
320 // Only one of searchKeys, seqSet and op can be non-nil/non-empty.
321 searchKeys []searchKey // In case of nested/multiple keys. Also for the top-level command.
322 seqSet *numSet // In case of bare sequence set. For op UID, field uidSet contains the parameter.
323 op string // Determines which of the fields below are set.
330 searchKey2 *searchKey
335func compactUIDSet(l []store.UID) (r numSet) {
338 for ; e < len(l) && l[e] == l[e-1]+1; e++ {
340 first := setNumber{number: uint32(l[0])}
343 last = &setNumber{number: uint32(l[e-1])}
345 r.ranges = append(r.ranges, numRange{first, last})