net-imap 0.3.7 → 0.5.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/BSDL +22 -0
  3. data/COPYING +56 -0
  4. data/Gemfile +15 -0
  5. data/LICENSE.txt +3 -22
  6. data/README.md +25 -8
  7. data/Rakefile +0 -7
  8. data/docs/styles.css +72 -23
  9. data/lib/net/imap/authenticators.rb +26 -57
  10. data/lib/net/imap/command_data.rb +74 -54
  11. data/lib/net/imap/config/attr_accessors.rb +75 -0
  12. data/lib/net/imap/config/attr_inheritance.rb +90 -0
  13. data/lib/net/imap/config/attr_type_coercion.rb +62 -0
  14. data/lib/net/imap/config.rb +552 -0
  15. data/lib/net/imap/connection_state.rb +48 -0
  16. data/lib/net/imap/data_encoding.rb +18 -6
  17. data/lib/net/imap/data_lite.rb +226 -0
  18. data/lib/net/imap/deprecated_client_options.rb +142 -0
  19. data/lib/net/imap/errors.rb +60 -1
  20. data/lib/net/imap/esearch_result.rb +180 -0
  21. data/lib/net/imap/fetch_data.rb +597 -0
  22. data/lib/net/imap/flags.rb +1 -1
  23. data/lib/net/imap/response_data.rb +249 -440
  24. data/lib/net/imap/response_parser/parser_utils.rb +245 -0
  25. data/lib/net/imap/response_parser.rb +1867 -1184
  26. data/lib/net/imap/response_reader.rb +73 -0
  27. data/lib/net/imap/sasl/anonymous_authenticator.rb +69 -0
  28. data/lib/net/imap/sasl/authentication_exchange.rb +139 -0
  29. data/lib/net/imap/sasl/authenticators.rb +122 -0
  30. data/lib/net/imap/sasl/client_adapter.rb +123 -0
  31. data/lib/net/imap/{authenticators/cram_md5.rb → sasl/cram_md5_authenticator.rb} +24 -14
  32. data/lib/net/imap/sasl/digest_md5_authenticator.rb +342 -0
  33. data/lib/net/imap/sasl/external_authenticator.rb +83 -0
  34. data/lib/net/imap/sasl/gs2_header.rb +80 -0
  35. data/lib/net/imap/{authenticators/login.rb → sasl/login_authenticator.rb} +28 -18
  36. data/lib/net/imap/sasl/oauthbearer_authenticator.rb +199 -0
  37. data/lib/net/imap/sasl/plain_authenticator.rb +101 -0
  38. data/lib/net/imap/sasl/protocol_adapters.rb +101 -0
  39. data/lib/net/imap/sasl/scram_algorithm.rb +58 -0
  40. data/lib/net/imap/sasl/scram_authenticator.rb +287 -0
  41. data/lib/net/imap/sasl/stringprep.rb +6 -66
  42. data/lib/net/imap/sasl/xoauth2_authenticator.rb +106 -0
  43. data/lib/net/imap/sasl.rb +148 -44
  44. data/lib/net/imap/sasl_adapter.rb +20 -0
  45. data/lib/net/imap/search_result.rb +146 -0
  46. data/lib/net/imap/sequence_set.rb +1871 -0
  47. data/lib/net/imap/stringprep/nameprep.rb +70 -0
  48. data/lib/net/imap/stringprep/saslprep.rb +69 -0
  49. data/lib/net/imap/stringprep/saslprep_tables.rb +96 -0
  50. data/lib/net/imap/stringprep/tables.rb +146 -0
  51. data/lib/net/imap/stringprep/trace.rb +85 -0
  52. data/lib/net/imap/stringprep.rb +159 -0
  53. data/lib/net/imap/uidplus_data.rb +244 -0
  54. data/lib/net/imap/vanished_data.rb +56 -0
  55. data/lib/net/imap.rb +2335 -867
  56. data/net-imap.gemspec +7 -8
  57. data/rakelib/benchmarks.rake +91 -0
  58. data/rakelib/rfcs.rake +2 -0
  59. data/rakelib/saslprep.rake +4 -4
  60. data/rakelib/string_prep_tables_generator.rb +86 -60
  61. data/sample/net-imap.rb +167 -0
  62. metadata +47 -49
  63. data/.github/dependabot.yml +0 -6
  64. data/.github/workflows/test.yml +0 -38
  65. data/.gitignore +0 -10
  66. data/benchmarks/stringprep.yml +0 -65
  67. data/benchmarks/table-regexps.yml +0 -39
  68. data/lib/net/imap/authenticators/digest_md5.rb +0 -115
  69. data/lib/net/imap/authenticators/plain.rb +0 -41
  70. data/lib/net/imap/authenticators/xoauth2.rb +0 -20
  71. data/lib/net/imap/sasl/saslprep.rb +0 -55
  72. data/lib/net/imap/sasl/saslprep_tables.rb +0 -98
  73. data/lib/net/imap/sasl/stringprep_tables.rb +0 -153
@@ -0,0 +1,1871 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set" unless defined?(::Set)
4
+
5
+ module Net
6
+ class IMAP
7
+
8
+ ##
9
+ # An \IMAP sequence set is a set of message sequence numbers or unique
10
+ # identifier numbers ("UIDs"). It contains numbers and ranges of numbers.
11
+ # The numbers are all non-zero unsigned 32-bit integers and one special
12
+ # value (<tt>"*"</tt>) that represents the largest value in the mailbox.
13
+ #
14
+ # Certain types of \IMAP responses will contain a SequenceSet, for example
15
+ # the data for a <tt>"MODIFIED"</tt> ResponseCode. Some \IMAP commands may
16
+ # receive a SequenceSet as an argument, for example IMAP#search, IMAP#fetch,
17
+ # and IMAP#store.
18
+ #
19
+ # == Creating sequence sets
20
+ #
21
+ # SequenceSet.new may receive a single optional argument: a non-zero 32 bit
22
+ # unsigned integer, a range, a <tt>sequence-set</tt> formatted string,
23
+ # another SequenceSet, a Set (containing only numbers or <tt>*</tt>), or an
24
+ # Array containing any of these (array inputs may be nested).
25
+ #
26
+ # set = Net::IMAP::SequenceSet.new(1)
27
+ # set.valid_string #=> "1"
28
+ # set = Net::IMAP::SequenceSet.new(1..100)
29
+ # set.valid_string #=> "1:100"
30
+ # set = Net::IMAP::SequenceSet.new(1...100)
31
+ # set.valid_string #=> "1:99"
32
+ # set = Net::IMAP::SequenceSet.new([1, 2, 5..])
33
+ # set.valid_string #=> "1:2,5:*"
34
+ # set = Net::IMAP::SequenceSet.new("1,2,3:7,5,6:10,2048,1024")
35
+ # set.valid_string #=> "1,2,3:7,5,6:10,2048,1024"
36
+ # set = Net::IMAP::SequenceSet.new(1, 2, 3..7, 5, 6..10, 2048, 1024)
37
+ # set.valid_string #=> "1:10,55,1024:2048"
38
+ #
39
+ # SequenceSet.new with no arguments creates an empty sequence set. Note
40
+ # that an empty sequence set is invalid in the \IMAP grammar.
41
+ #
42
+ # set = Net::IMAP::SequenceSet.new
43
+ # set.empty? #=> true
44
+ # set.valid? #=> false
45
+ # set.valid_string #!> raises DataFormatError
46
+ # set << 1..10
47
+ # set.empty? #=> false
48
+ # set.valid? #=> true
49
+ # set.valid_string #=> "1:10"
50
+ #
51
+ # Using SequenceSet.new with another SequenceSet input behaves the same as
52
+ # calling #dup on the other set. The input's #string will be preserved.
53
+ #
54
+ # input = Net::IMAP::SequenceSet.new("1,2,3:7,5,6:10,2048,1024")
55
+ # copy = Net::IMAP::SequenceSet.new(input)
56
+ # input.valid_string #=> "1,2,3:7,5,6:10,2048,1024"
57
+ # copy.valid_string #=> "1,2,3:7,5,6:10,2048,1024"
58
+ # copy2 = input.dup # same as calling new with a SequenceSet input
59
+ # copy == input #=> true, same set membership
60
+ # copy.eql? input #=> true, same string value
61
+ # copy.equal? input #=> false, different objects
62
+ #
63
+ # copy.normalize!
64
+ # copy.valid_string #=> "1:10,1024,2048"
65
+ # copy == input #=> true, same set membership
66
+ # copy.eql? input #=> false, different string value
67
+ #
68
+ # copy << 999
69
+ # copy.valid_string #=> "1:10,999,1024,2048"
70
+ # copy == input #=> false, different set membership
71
+ # copy.eql? input #=> false, different string value
72
+ #
73
+ # Use Net::IMAP::SequenceSet() to coerce a single (optional) input.
74
+ # A SequenceSet input is returned without duplication, even when frozen.
75
+ #
76
+ # set = Net::IMAP::SequenceSet()
77
+ # set.string #=> nil
78
+ # set.frozen? #=> false
79
+ #
80
+ # # String order is preserved
81
+ # set = Net::IMAP::SequenceSet("1,2,3:7,5,6:10,2048,1024")
82
+ # set.valid_string #=> "1,2,3:7,5,6:10,2048,1024"
83
+ # set.frozen? #=> false
84
+ #
85
+ # # Other inputs are normalized
86
+ # set = Net::IMAP::SequenceSet([1, 2, [3..7, 5], 6..10, 2048, 1024])
87
+ # set.valid_string #=> "1:10,55,1024:2048"
88
+ # set.frozen? #=> false
89
+ #
90
+ # unfrozen = set
91
+ # frozen = set.dup.freeze
92
+ # unfrozen.equal? Net::IMAP::SequenceSet(unfrozen) #=> true
93
+ # frozen.equal? Net::IMAP::SequenceSet(frozen) #=> true
94
+ #
95
+ # Use ::[] to coerce one or more arguments into a valid frozen SequenceSet.
96
+ # A valid frozen SequenceSet is returned directly, without allocating a new
97
+ # object. ::[] will not create an invalid (empty) set.
98
+ #
99
+ # Net::IMAP::SequenceSet[] #!> raises ArgumentError
100
+ # Net::IMAP::SequenceSet[nil] #!> raises DataFormatError
101
+ # Net::IMAP::SequenceSet[""] #!> raises DataFormatError
102
+ #
103
+ # # String order is preserved
104
+ # set = Net::IMAP::SequenceSet["1,2,3:7,5,6:10,2048,1024"]
105
+ # set.valid_string #=> "1,2,3:7,5,6:10,2048,1024"
106
+ # set.frozen? #=> true
107
+ #
108
+ # # Other inputs are normalized
109
+ # set = Net::IMAP::SequenceSet[1, 2, [3..7, 5], 6..10, 2048, 1024]
110
+ # set.valid_string #=> "1:10,55,1024:2048"
111
+ # set.frozen? #=> true
112
+ #
113
+ # frozen = set
114
+ # unfrozen = set.dup
115
+ # frozen.equal? Net::IMAP::SequenceSet[frozen] #=> true
116
+ # unfrozen.equal? Net::IMAP::SequenceSet[unfrozen] #=> false
117
+ #
118
+ # Objects which respond to +to_sequence_set+ (such as SearchResult and
119
+ # ThreadMember) can be coerced to a SequenceSet with ::new, ::try_convert,
120
+ # ::[], or Net::IMAP::SequenceSet.
121
+ #
122
+ # search = imap.uid_search(["SUBJECT", "hello", "NOT", "SEEN"])
123
+ # seqset = Net::IMAP::SequenceSet(search) - already_fetched
124
+ # fetch = imap.uid_fetch(seqset, "FAST")
125
+ #
126
+ # == Ordered and Normalized sets
127
+ #
128
+ # Sometimes the order of the set's members is significant, such as with the
129
+ # +ESORT+, <tt>CONTEXT=SORT</tt>, and +UIDPLUS+ extensions. So, when a
130
+ # sequence set is created from a single string (such as by the parser), that
131
+ # #string representation is preserved. Assigning a string with #string= or
132
+ # #replace will also preserve that string. Use #each_entry, #entries, or
133
+ # #each_ordered_number to enumerate the entries in their #string order.
134
+ # Hash equality (using #eql?) is based on the string representation.
135
+ #
136
+ # Internally, SequenceSet uses a normalized uint32 set representation which
137
+ # sorts and de-duplicates all numbers and coalesces adjacent or overlapping
138
+ # entries. Many methods use this sorted set representation for <tt>O(lg
139
+ # n)</tt> searches. Use #each_element, #elements, #each_range, #ranges,
140
+ # #each_number, or #numbers to enumerate the set in sorted order. Basic
141
+ # object equality (using #==) is based on set membership, without regard to
142
+ # #entry order or #string normalization.
143
+ #
144
+ # Most modification methods reset #string to its #normalized form, so that
145
+ # #entries and #elements are identical. Use #append to preserve #entries
146
+ # order while modifying a set.
147
+ #
148
+ # == Using <tt>*</tt>
149
+ #
150
+ # \IMAP sequence sets may contain a special value <tt>"*"</tt>, which
151
+ # represents the largest number in use. From +seq-number+ in
152
+ # {RFC9051 §9}[https://www.rfc-editor.org/rfc/rfc9051.html#section-9-5]:
153
+ # >>>
154
+ # In the case of message sequence numbers, it is the number of messages
155
+ # in a non-empty mailbox. In the case of unique identifiers, it is the
156
+ # unique identifier of the last message in the mailbox or, if the
157
+ # mailbox is empty, the mailbox's current UIDNEXT value.
158
+ #
159
+ # When creating a SequenceSet, <tt>*</tt> may be input as <tt>-1</tt>,
160
+ # <tt>"*"</tt>, <tt>:*</tt>, an endless range, or a range ending in
161
+ # <tt>-1</tt>. When converting to #elements, #ranges, or #numbers, it will
162
+ # output as either <tt>:*</tt> or an endless range. For example:
163
+ #
164
+ # Net::IMAP::SequenceSet["1,3,*"].to_a #=> [1, 3, :*]
165
+ # Net::IMAP::SequenceSet["1,234:*"].to_a #=> [1, 234..]
166
+ # Net::IMAP::SequenceSet[1234..-1].to_a #=> [1234..]
167
+ # Net::IMAP::SequenceSet[1234..].to_a #=> [1234..]
168
+ #
169
+ # Net::IMAP::SequenceSet[1234..].to_s #=> "1234:*"
170
+ # Net::IMAP::SequenceSet[1234..-1].to_s #=> "1234:*"
171
+ #
172
+ # Use #limit to convert <tt>"*"</tt> to a maximum value. When a range
173
+ # includes <tt>"*"</tt>, the maximum value will always be matched:
174
+ #
175
+ # Net::IMAP::SequenceSet["9999:*"].limit(max: 25)
176
+ # #=> Net::IMAP::SequenceSet["25"]
177
+ #
178
+ # === Surprising <tt>*</tt> behavior
179
+ #
180
+ # When a set includes <tt>*</tt>, some methods may have surprising behavior.
181
+ #
182
+ # For example, #complement treats <tt>*</tt> as its own number. This way,
183
+ # the #intersection of a set and its #complement will always be empty. And
184
+ # <tt>*</tt> is sorted as greater than any other number in the set. This is
185
+ # not how an \IMAP server interprets the set: it will convert <tt>*</tt> to
186
+ # the number of messages in the mailbox, the +UID+ of the last message in
187
+ # the mailbox, or +UIDNEXT+, as appropriate. Several methods have an
188
+ # argument for how <tt>*</tt> should be interpreted.
189
+ #
190
+ # But, for example, this means that there may be overlap between a set and
191
+ # its complement after #limit is applied to each:
192
+ #
193
+ # ~Net::IMAP::SequenceSet["*"] == Net::IMAP::SequenceSet[1..(2**32-1)]
194
+ # ~Net::IMAP::SequenceSet[1..5] == Net::IMAP::SequenceSet["6:*"]
195
+ #
196
+ # set = Net::IMAP::SequenceSet[1..5]
197
+ # (set & ~set).empty? => true
198
+ #
199
+ # (set.limit(max: 4) & (~set).limit(max: 4)).to_a => [4]
200
+ #
201
+ # When counting the number of numbers in a set, <tt>*</tt> will be counted
202
+ # _except_ when UINT32_MAX is also in the set:
203
+ # UINT32_MAX = 2**32 - 1
204
+ # Net::IMAP::SequenceSet["*"].count => 1
205
+ # Net::IMAP::SequenceSet[1..UINT32_MAX - 1, :*].count => UINT32_MAX
206
+ #
207
+ # Net::IMAP::SequenceSet["1:*"].count => UINT32_MAX
208
+ # Net::IMAP::SequenceSet[UINT32_MAX, :*].count => 1
209
+ # Net::IMAP::SequenceSet[UINT32_MAX..].count => 1
210
+ #
211
+ # == What's here?
212
+ #
213
+ # SequenceSet provides methods for:
214
+ # * {Creating a SequenceSet}[rdoc-ref:SequenceSet@Methods+for+Creating+a+SequenceSet]
215
+ # * {Comparing}[rdoc-ref:SequenceSet@Methods+for+Comparing]
216
+ # * {Querying}[rdoc-ref:SequenceSet@Methods+for+Querying]
217
+ # * {Iterating}[rdoc-ref:SequenceSet@Methods+for+Iterating]
218
+ # * {Set Operations}[rdoc-ref:SequenceSet@Methods+for+Set+Operations]
219
+ # * {Assigning}[rdoc-ref:SequenceSet@Methods+for+Assigning]
220
+ # * {Deleting}[rdoc-ref:SequenceSet@Methods+for+Deleting]
221
+ # * {IMAP String Formatting}[rdoc-ref:SequenceSet@Methods+for+IMAP+String+Formatting]
222
+ #
223
+ # === Methods for Creating a \SequenceSet
224
+ # * ::[]: Creates a validated frozen sequence set from one or more inputs.
225
+ # * ::new: Creates a new mutable sequence set, which may be empty (invalid).
226
+ # * ::try_convert: Calls +to_sequence_set+ on an object and verifies that
227
+ # the result is a SequenceSet.
228
+ # * Net::IMAP::SequenceSet(): Coerce an input using ::try_convert or ::new.
229
+ # * ::empty: Returns a frozen empty (invalid) SequenceSet.
230
+ # * ::full: Returns a frozen SequenceSet containing every possible number.
231
+ #
232
+ # === Methods for Comparing
233
+ #
234
+ # <i>Comparison to another \SequenceSet:</i>
235
+ # - #==: Returns whether a given set contains the same numbers as +self+.
236
+ # - #eql?: Returns whether a given set uses the same #string as +self+.
237
+ #
238
+ # <i>Comparison to objects which are convertible to \SequenceSet:</i>
239
+ # - #===:
240
+ # Returns whether a given object is fully contained within +self+, or
241
+ # +nil+ if the object cannot be converted to a compatible type.
242
+ # - #cover?:
243
+ # Returns whether a given object is fully contained within +self+.
244
+ # - #intersect? (aliased as #overlap?):
245
+ # Returns whether +self+ and a given object have any common elements.
246
+ # - #disjoint?:
247
+ # Returns whether +self+ and a given object have no common elements.
248
+ #
249
+ # === Methods for Querying
250
+ # These methods do not modify +self+.
251
+ #
252
+ # <i>Set membership:</i>
253
+ # - #include? (aliased as #member?):
254
+ # Returns whether a given element is contained by the set.
255
+ # - #include_star?: Returns whether the set contains <tt>*</tt>.
256
+ #
257
+ # <i>Minimum and maximum value elements:</i>
258
+ # - #min: Returns one or more of the lowest numbers in the set.
259
+ # - #max: Returns one or more of the highest numbers in the set.
260
+ # - #minmax: Returns the lowest and highest numbers in the set.
261
+ #
262
+ # <i>Accessing value by offset in sorted set:</i>
263
+ # - #[] (aliased as #slice): Returns the number or consecutive subset at a
264
+ # given offset or range of offsets in the sorted set.
265
+ # - #at: Returns the number at a given offset in the sorted set.
266
+ # - #find_index: Returns the given number's offset in the sorted set.
267
+ #
268
+ # <i>Accessing value by offset in ordered entries</i>
269
+ # - #ordered_at: Returns the number at a given offset in the ordered entries.
270
+ # - #find_ordered_index: Returns the index of the given number's first
271
+ # occurrence in entries.
272
+ #
273
+ # <i>Set cardinality:</i>
274
+ # - #count (aliased as #size): Returns the count of numbers in the set.
275
+ # Duplicated numbers are not counted.
276
+ # - #empty?: Returns whether the set has no members. \IMAP syntax does not
277
+ # allow empty sequence sets.
278
+ # - #valid?: Returns whether the set has any members.
279
+ # - #full?: Returns whether the set contains every possible value, including
280
+ # <tt>*</tt>.
281
+ #
282
+ # <i>Denormalized properties:</i>
283
+ # - #has_duplicates?: Returns whether the ordered entries repeat any
284
+ # numbers.
285
+ # - #count_duplicates: Returns the count of repeated numbers in the ordered
286
+ # entries.
287
+ # - #count_with_duplicates: Returns the count of numbers in the ordered
288
+ # entries, including any repeated numbers.
289
+ #
290
+ # === Methods for Iterating
291
+ #
292
+ # <i>Normalized (sorted and coalesced):</i>
293
+ # - #each_element: Yields each number and range in the set, sorted and
294
+ # coalesced, and returns +self+.
295
+ # - #elements (aliased as #to_a): Returns an Array of every number and range
296
+ # in the set, sorted and coalesced.
297
+ # - #each_range:
298
+ # Yields each element in the set as a Range and returns +self+.
299
+ # - #ranges: Returns an Array of every element in the set, converting
300
+ # numbers into ranges of a single value.
301
+ # - #each_number: Yields each number in the set and returns +self+.
302
+ # - #numbers: Returns an Array with every number in the set, expanding
303
+ # ranges into all of their contained numbers.
304
+ # - #to_set: Returns a Set containing all of the #numbers in the set.
305
+ #
306
+ # <i>Order preserving:</i>
307
+ # - #each_entry: Yields each number and range in the set, unsorted and
308
+ # without deduplicating numbers or coalescing ranges, and returns +self+.
309
+ # - #entries: Returns an Array of every number and range in the set,
310
+ # unsorted and without deduplicating numbers or coalescing ranges.
311
+ # - #each_ordered_number: Yields each number in the ordered entries and
312
+ # returns +self+.
313
+ #
314
+ # === Methods for \Set Operations
315
+ # These methods do not modify +self+.
316
+ #
317
+ # - #| (aliased as #union and #+): Returns a new set combining all members
318
+ # from +self+ with all members from the other set.
319
+ # - #& (aliased as #intersection): Returns a new set containing all members
320
+ # common to +self+ and the other set.
321
+ # - #- (aliased as #difference): Returns a copy of +self+ with all members
322
+ # in the other set removed.
323
+ # - #^ (aliased as #xor): Returns a new set containing all members from
324
+ # +self+ and the other set except those common to both.
325
+ # - #~ (aliased as #complement): Returns a new set containing all members
326
+ # that are not in +self+
327
+ # - #above: Return a copy of +self+ which only contains numbers above a
328
+ # given number.
329
+ # - #below: Return a copy of +self+ which only contains numbers below a
330
+ # given value.
331
+ # - #limit: Returns a copy of +self+ which has replaced <tt>*</tt> with a
332
+ # given maximum value and removed all members over that maximum.
333
+ #
334
+ # === Methods for Assigning
335
+ # These methods add or replace elements in +self+.
336
+ #
337
+ # <i>Normalized (sorted and coalesced):</i>
338
+ #
339
+ # These methods always update #string to be fully sorted and coalesced.
340
+ #
341
+ # - #add (aliased as #<<): Adds a given element to the set; returns +self+.
342
+ # - #add?: If the given element is not fully included the set, adds it and
343
+ # returns +self+; otherwise, returns +nil+.
344
+ # - #merge: Adds all members of the given sets into this set; returns +self+.
345
+ # - #complement!: Replaces the contents of the set with its own #complement.
346
+ #
347
+ # <i>Order preserving:</i>
348
+ #
349
+ # These methods _may_ cause #string to not be sorted or coalesced.
350
+ #
351
+ # - #append: Adds the given entry to the set, appending it to the existing
352
+ # string, and returns +self+.
353
+ # - #string=: Assigns a new #string value and replaces #elements to match.
354
+ # - #replace: Replaces the contents of the set with the contents
355
+ # of a given object.
356
+ #
357
+ # === Methods for Deleting
358
+ # These methods remove elements from +self+, and update #string to be fully
359
+ # sorted and coalesced.
360
+ #
361
+ # - #clear: Removes all elements in the set; returns +self+.
362
+ # - #delete: Removes a given element from the set; returns +self+.
363
+ # - #delete?: If the given element is included in the set, removes it and
364
+ # returns it; otherwise, returns +nil+.
365
+ # - #delete_at: Removes the number at a given offset.
366
+ # - #slice!: Removes the number or consecutive numbers at a given offset or
367
+ # range of offsets.
368
+ # - #subtract: Removes all members of the given sets from this set; returns
369
+ # +self+.
370
+ # - #limit!: Replaces <tt>*</tt> with a given maximum value and removes all
371
+ # members over that maximum; returns +self+.
372
+ #
373
+ # === Methods for \IMAP String Formatting
374
+ #
375
+ # - #to_s: Returns the +sequence-set+ string, or an empty string when the
376
+ # set is empty.
377
+ # - #string: Returns the +sequence-set+ string, or nil when empty.
378
+ # - #valid_string: Returns the +sequence-set+ string, or raises
379
+ # DataFormatError when the set is empty.
380
+ # - #normalized_string: Returns a <tt>sequence-set</tt> string with its
381
+ # elements sorted and coalesced, or nil when the set is empty.
382
+ # - #normalize: Returns a new set with this set's normalized +sequence-set+
383
+ # representation.
384
+ # - #normalize!: Updates #string to its normalized +sequence-set+
385
+ # representation and returns +self+.
386
+ #
387
+ class SequenceSet
388
+ # The largest possible non-zero unsigned 32-bit integer
389
+ UINT32_MAX = 2**32 - 1
390
+
391
+ # represents "*" internally, to simplify sorting (etc)
392
+ STAR_INT = UINT32_MAX + 1
393
+ private_constant :STAR_INT
394
+
395
+ # valid inputs for "*"
396
+ STARS = [:*, ?*, -1].freeze
397
+ private_constant :STARS
398
+
399
+ class << self
400
+
401
+ # :call-seq:
402
+ # SequenceSet[*inputs] -> valid frozen sequence set
403
+ #
404
+ # Returns a frozen SequenceSet, constructed from +inputs+.
405
+ #
406
+ # When only a single valid frozen SequenceSet is given, that same set is
407
+ # returned.
408
+ #
409
+ # An empty SequenceSet is invalid and will raise a DataFormatError.
410
+ #
411
+ # Use ::new to create a mutable or empty SequenceSet.
412
+ #
413
+ # Related: ::new, Net::IMAP::SequenceSet(), ::try_convert
414
+ def [](first, *rest)
415
+ if rest.empty?
416
+ set = try_convert(first)&.validate
417
+ set&.frozen? ? set : (set&.dup || new(first).validate).freeze
418
+ else
419
+ new(first).merge(*rest).validate.freeze
420
+ end
421
+ end
422
+
423
+ # :call-seq:
424
+ # SequenceSet.try_convert(obj) -> sequence set or nil
425
+ #
426
+ # If +obj+ is a SequenceSet, returns +obj+. If +obj+ responds_to
427
+ # +to_sequence_set+, calls +obj.to_sequence_set+ and returns the result.
428
+ # Otherwise returns +nil+.
429
+ #
430
+ # If +obj.to_sequence_set+ doesn't return a SequenceSet, an exception is
431
+ # raised.
432
+ #
433
+ # Related: Net::IMAP::SequenceSet(), ::new, ::[]
434
+ def try_convert(obj)
435
+ return obj if obj.is_a?(SequenceSet)
436
+ return nil unless obj.respond_to?(:to_sequence_set)
437
+ obj = obj.to_sequence_set
438
+ return obj if obj.is_a?(SequenceSet)
439
+ raise DataFormatError, "invalid object returned from to_sequence_set"
440
+ end
441
+
442
+ # Returns a frozen empty set singleton. Note that valid \IMAP sequence
443
+ # sets cannot be empty, so this set is _invalid_.
444
+ def empty; EMPTY end
445
+
446
+ # Returns a frozen full set singleton: <tt>"1:*"</tt>
447
+ def full; FULL end
448
+
449
+ end
450
+
451
+ # Create a new SequenceSet object from +input+, which may be another
452
+ # SequenceSet, an IMAP formatted +sequence-set+ string, a non-zero 32 bit
453
+ # unsigned integer, a range, <tt>:*</tt>, a Set of numbers or <tt>*</tt>,
454
+ # an object that responds to +to_sequence_set+ (such as SearchResult) or
455
+ # an Array of these (array inputs may be nested).
456
+ #
457
+ # set = Net::IMAP::SequenceSet.new(1)
458
+ # set.valid_string #=> "1"
459
+ # set = Net::IMAP::SequenceSet.new(1..100)
460
+ # set.valid_string #=> "1:100"
461
+ # set = Net::IMAP::SequenceSet.new(1...100)
462
+ # set.valid_string #=> "1:99"
463
+ # set = Net::IMAP::SequenceSet.new([1, 2, 5..])
464
+ # set.valid_string #=> "1:2,5:*"
465
+ # set = Net::IMAP::SequenceSet.new("1,2,3:7,5,6:10,2048,1024")
466
+ # set.valid_string #=> "1,2,3:7,5,6:10,2048,1024"
467
+ # set = Net::IMAP::SequenceSet.new(1, 2, 3..7, 5, 6..10, 2048, 1024)
468
+ # set.valid_string #=> "1:10,55,1024:2048"
469
+ #
470
+ # With no arguments (or +nil+) creates an empty sequence set. Note that
471
+ # an empty sequence set is invalid in the \IMAP grammar.
472
+ #
473
+ # set = Net::IMAP::SequenceSet.new
474
+ # set.empty? #=> true
475
+ # set.valid? #=> false
476
+ # set.valid_string #!> raises DataFormatError
477
+ # set << 1..10
478
+ # set.empty? #=> false
479
+ # set.valid? #=> true
480
+ # set.valid_string #=> "1:10"
481
+ #
482
+ # When +input+ is a SequenceSet, ::new behaves the same as calling #dup on
483
+ # that other set. The input's #string will be preserved.
484
+ #
485
+ # input = Net::IMAP::SequenceSet.new("1,2,3:7,5,6:10,2048,1024")
486
+ # copy = Net::IMAP::SequenceSet.new(input)
487
+ # input.valid_string #=> "1,2,3:7,5,6:10,2048,1024"
488
+ # copy.valid_string #=> "1,2,3:7,5,6:10,2048,1024"
489
+ # copy2 = input.dup # same as calling new with a SequenceSet input
490
+ # copy == input #=> true, same set membership
491
+ # copy.eql? input #=> true, same string value
492
+ # copy.equal? input #=> false, different objects
493
+ #
494
+ # copy.normalize!
495
+ # copy.valid_string #=> "1:10,1024,2048"
496
+ # copy == input #=> true, same set membership
497
+ # copy.eql? input #=> false, different string value
498
+ #
499
+ # copy << 999
500
+ # copy.valid_string #=> "1:10,999,1024,2048"
501
+ # copy == input #=> false, different set membership
502
+ # copy.eql? input #=> false, different string value
503
+ #
504
+ # === Alternative set creation methods
505
+ #
506
+ # * ::[] returns a frozen validated (non-empty) SequenceSet, without
507
+ # allocating a new object when the input is already a valid frozen
508
+ # SequenceSet.
509
+ # * Net::IMAP::SequenceSet() coerces an input to SequenceSet, without
510
+ # allocating a new object when the input is already a SequenceSet.
511
+ # * ::try_convert calls +to_sequence_set+ on inputs that support it and
512
+ # returns +nil+ for inputs that don't.
513
+ # * ::empty and ::full both return frozen singleton sets which can be
514
+ # combined with set operations (#|, #&, #^, #-, etc) to make new sets.
515
+ #
516
+ # See SequenceSet@Creating+sequence+sets.
517
+ def initialize(input = nil) input ? replace(input) : clear end
518
+
519
+ # Removes all elements and returns self.
520
+ def clear
521
+ modifying! # redundant check, to normalize the error message for JRuby
522
+ @tuples, @string = [], nil
523
+ self
524
+ end
525
+
526
+ # Replace the contents of the set with the contents of +other+ and returns
527
+ # +self+.
528
+ #
529
+ # +other+ may be another SequenceSet or any other object that would be
530
+ # accepted by ::new.
531
+ def replace(other)
532
+ case other
533
+ when SequenceSet then initialize_dup(other)
534
+ when String then self.string = other
535
+ else clear; merge other
536
+ end
537
+ self
538
+ end
539
+
540
+ # Returns the \IMAP +sequence-set+ string representation, or raises a
541
+ # DataFormatError when the set is empty.
542
+ #
543
+ # Use #string to return +nil+ or #to_s to return an empty string without
544
+ # error.
545
+ #
546
+ # Related: #string, #normalized_string, #to_s
547
+ def valid_string
548
+ raise DataFormatError, "empty sequence-set" if empty?
549
+ string
550
+ end
551
+
552
+ # Returns the \IMAP +sequence-set+ string representation, or +nil+ when
553
+ # the set is empty. Note that an empty set is invalid in the \IMAP
554
+ # syntax.
555
+ #
556
+ # Use #valid_string to raise an exception when the set is empty, or #to_s
557
+ # to return an empty string.
558
+ #
559
+ # If the set was created from a single string, it is not normalized. If
560
+ # the set is updated the string will be normalized.
561
+ #
562
+ # Related: #valid_string, #normalized_string, #to_s
563
+ def string; @string ||= normalized_string if valid? end
564
+
565
+ # Returns an array with #normalized_string when valid and an empty array
566
+ # otherwise.
567
+ def deconstruct; valid? ? [normalized_string] : [] end
568
+
569
+ # Assigns a new string to #string and resets #elements to match. It
570
+ # cannot be set to an empty string—assign +nil+ or use #clear instead.
571
+ # The string is validated but not normalized.
572
+ #
573
+ # Use #add or #merge to add a string to an existing set.
574
+ #
575
+ # Related: #replace, #clear
576
+ def string=(str)
577
+ if str.nil?
578
+ clear
579
+ else
580
+ modifying! # redundant check, to normalize the error message for JRuby
581
+ str = String.try_convert(str) or raise ArgumentError, "not a string"
582
+ tuples = str_to_tuples str
583
+ @tuples, @string = [], -str
584
+ tuples_add tuples
585
+ end
586
+ str
587
+ end
588
+
589
+ # Returns the \IMAP +sequence-set+ string representation, or an empty
590
+ # string when the set is empty. Note that an empty set is invalid in the
591
+ # \IMAP syntax.
592
+ #
593
+ # Related: #valid_string, #normalized_string, #to_s
594
+ def to_s; string || "" end
595
+
596
+ # Freezes and returns the set. A frozen SequenceSet is Ractor-safe.
597
+ def freeze
598
+ return self if frozen?
599
+ string
600
+ @tuples.each(&:freeze).freeze
601
+ super
602
+ end
603
+
604
+ # :call-seq: self == other -> true or false
605
+ #
606
+ # Returns true when the other SequenceSet represents the same message
607
+ # identifiers. Encoding difference—such as order, overlaps, or
608
+ # duplicates—are ignored.
609
+ #
610
+ # Net::IMAP::SequenceSet["1:3"] == Net::IMAP::SequenceSet["1:3"]
611
+ # #=> true
612
+ # Net::IMAP::SequenceSet["1,2,3"] == Net::IMAP::SequenceSet["1:3"]
613
+ # #=> true
614
+ # Net::IMAP::SequenceSet["1,3"] == Net::IMAP::SequenceSet["3,1"]
615
+ # #=> true
616
+ # Net::IMAP::SequenceSet["9,1:*"] == Net::IMAP::SequenceSet["1:*"]
617
+ # #=> true
618
+ #
619
+ # Related: #eql?, #normalize
620
+ def ==(other)
621
+ self.class == other.class &&
622
+ (to_s == other.to_s || tuples == other.tuples)
623
+ end
624
+
625
+ # :call-seq: eql?(other) -> true or false
626
+ #
627
+ # Hash equality requires the same encoded #string representation.
628
+ #
629
+ # Net::IMAP::SequenceSet["1:3"] .eql? Net::IMAP::SequenceSet["1:3"]
630
+ # #=> true
631
+ # Net::IMAP::SequenceSet["1,2,3"].eql? Net::IMAP::SequenceSet["1:3"]
632
+ # #=> false
633
+ # Net::IMAP::SequenceSet["1,3"] .eql? Net::IMAP::SequenceSet["3,1"]
634
+ # #=> false
635
+ # Net::IMAP::SequenceSet["9,1:*"].eql? Net::IMAP::SequenceSet["1:*"]
636
+ # #=> false
637
+ #
638
+ # Related: #==, #normalize
639
+ def eql?(other) self.class == other.class && string == other.string end
640
+
641
+ # See #eql?
642
+ def hash; [self.class, string].hash end
643
+
644
+ # :call-seq: self === other -> true | false | nil
645
+ #
646
+ # Returns whether +other+ is contained within the set. +other+ may be any
647
+ # object that would be accepted by ::new. Returns +nil+ if StandardError
648
+ # is raised while converting +other+ to a comparable type.
649
+ #
650
+ # Related: #cover?, #include?, #include_star?
651
+ def ===(other)
652
+ cover?(other)
653
+ rescue
654
+ nil
655
+ end
656
+
657
+ # :call-seq: cover?(other) -> true | false | nil
658
+ #
659
+ # Returns whether +other+ is contained within the set. +other+ may be any
660
+ # object that would be accepted by ::new.
661
+ #
662
+ # Related: #===, #include?, #include_star?, #intersect?
663
+ def cover?(other) input_to_tuples(other).none? { !include_tuple?(_1) } end
664
+
665
+ # Returns +true+ when a given number or range is in +self+, and +false+
666
+ # otherwise. Returns +nil+ when +number+ isn't a valid SequenceSet
667
+ # element (Integer, Range, <tt>*</tt>, +sequence-set+ string).
668
+ #
669
+ # set = Net::IMAP::SequenceSet["5:10,100,111:115"]
670
+ # set.include? 1 #=> false
671
+ # set.include? 5..10 #=> true
672
+ # set.include? 11..20 #=> false
673
+ # set.include? 100 #=> true
674
+ # set.include? 6 #=> true, covered by "5:10"
675
+ # set.include? 6..9 #=> true, covered by "5:10"
676
+ # set.include? "6:9" #=> true, strings are parsed
677
+ # set.include? 4..9 #=> false, intersection is not sufficient
678
+ # set.include? "*" #=> false, use #limit to re-interpret "*"
679
+ # set.include? -1 #=> false, -1 is interpreted as "*"
680
+ #
681
+ # set = Net::IMAP::SequenceSet["5:10,100,111:*"]
682
+ # set.include? :* #=> true
683
+ # set.include? "*" #=> true
684
+ # set.include? -1 #=> true
685
+ # set.include?(200..) #=> true
686
+ # set.include?(100..) #=> false
687
+ #
688
+ # Related: #include_star?, #cover?, #===, #intersect?
689
+ def include?(element)
690
+ tuple = input_to_tuple element rescue nil
691
+ !!include_tuple?(tuple) if tuple
692
+ end
693
+
694
+ alias member? include?
695
+
696
+ # Returns +true+ when the set contains <tt>*</tt>.
697
+ def include_star?; @tuples.last&.last == STAR_INT end
698
+
699
+ # Returns +true+ if the set and a given object have any common elements,
700
+ # +false+ otherwise.
701
+ #
702
+ # Net::IMAP::SequenceSet["5:10"].intersect? "7,9,11" #=> true
703
+ # Net::IMAP::SequenceSet["5:10"].intersect? "11:33" #=> false
704
+ #
705
+ # Related: #intersection, #disjoint?, #cover?, #include?
706
+ def intersect?(other)
707
+ valid? && input_to_tuples(other).any? { intersect_tuple? _1 }
708
+ end
709
+ alias overlap? intersect?
710
+
711
+ # Returns +true+ if the set and a given object have no common elements,
712
+ # +false+ otherwise.
713
+ #
714
+ # Net::IMAP::SequenceSet["5:10"].disjoint? "7,9,11" #=> false
715
+ # Net::IMAP::SequenceSet["5:10"].disjoint? "11:33" #=> true
716
+ #
717
+ # Related: #intersection, #intersect?
718
+ def disjoint?(other)
719
+ empty? || input_to_tuples(other).none? { intersect_tuple? _1 }
720
+ end
721
+
722
+ # :call-seq:
723
+ # max(star: :*) => integer or star or nil
724
+ # max(count) => SequenceSet
725
+ #
726
+ # Returns the maximum value in +self+, +star+ when the set includes
727
+ # <tt>*</tt>, or +nil+ when the set is empty.
728
+ #
729
+ # When +count+ is given, a new SequenceSet is returned, containing only
730
+ # the last +count+ numbers. An empty SequenceSet is returned when +self+
731
+ # is empty. (+star+ is ignored when +count+ is given.)
732
+ #
733
+ # Related: #min, #minmax, #slice
734
+ def max(count = nil, star: :*)
735
+ if count
736
+ slice(-[count, size].min..) || remain_frozen_empty
737
+ elsif (val = @tuples.last&.last)
738
+ val == STAR_INT ? star : val
739
+ end
740
+ end
741
+
742
+ # :call-seq:
743
+ # min(star: :*) => integer or star or nil
744
+ # min(count) => SequenceSet
745
+ #
746
+ # Returns the minimum value in +self+, +star+ when the only value in the
747
+ # set is <tt>*</tt>, or +nil+ when the set is empty.
748
+ #
749
+ # When +count+ is given, a new SequenceSet is returned, containing only
750
+ # the first +count+ numbers. An empty SequenceSet is returned when +self+
751
+ # is empty. (+star+ is ignored when +count+ is given.)
752
+ #
753
+ # Related: #max, #minmax, #slice
754
+ def min(count = nil, star: :*)
755
+ if count
756
+ slice(0...count) || remain_frozen_empty
757
+ elsif (val = @tuples.first&.first)
758
+ val != STAR_INT ? val : star
759
+ end
760
+ end
761
+
762
+ # :call-seq: minmax(star: :*) => [min, max] or nil
763
+ #
764
+ # Returns a 2-element array containing the minimum and maximum numbers in
765
+ # +self+, or +nil+ when the set is empty. +star+ is handled the same way
766
+ # as by #min and #max.
767
+ #
768
+ # Related: #min, #max
769
+ def minmax(star: :*); [min(star: star), max(star: star)] unless empty? end
770
+
771
+ # Returns false when the set is empty.
772
+ def valid?; !empty? end
773
+
774
+ # Returns true if the set contains no elements
775
+ def empty?; @tuples.empty? end
776
+
777
+ # Returns true if the set contains every possible element.
778
+ def full?; @tuples == [[1, STAR_INT]] end
779
+
780
+ # :call-seq:
781
+ # self + other -> sequence set
782
+ # self | other -> sequence set
783
+ # union(other) -> sequence set
784
+ #
785
+ # Returns a new sequence set that has every number in the +other+ object
786
+ # added.
787
+ #
788
+ # +other+ may be any object that would be accepted by ::new.
789
+ #
790
+ # Net::IMAP::SequenceSet["1:5"] | 2 | [4..6, 99]
791
+ # #=> Net::IMAP::SequenceSet["1:6,99"]
792
+ #
793
+ # Related: #add, #merge, #&, #-, #^, #~
794
+ #
795
+ # ==== Set identities
796
+ #
797
+ # <tt>lhs | rhs</tt> is equivalent to:
798
+ # * <tt>rhs | lhs</tt> (commutative)
799
+ # * <tt>~(~lhs & ~rhs)</tt> (De Morgan's Law)
800
+ # * <tt>(lhs & rhs) ^ (lhs ^ rhs)</tt>
801
+ def |(other) remain_frozen dup.merge other end
802
+ alias :+ :|
803
+ alias union :|
804
+
805
+ # :call-seq:
806
+ # self - other -> sequence set
807
+ # difference(other) -> sequence set
808
+ #
809
+ # Returns a new sequence set built by duplicating this set and removing
810
+ # every number that appears in +other+.
811
+ #
812
+ # +other+ may be any object that would be accepted by ::new.
813
+ #
814
+ # Net::IMAP::SequenceSet[1..5] - 2 - 4 - 6
815
+ # #=> Net::IMAP::SequenceSet["1,3,5"]
816
+ #
817
+ # Related: #subtract, #|, #&, #^, #~
818
+ #
819
+ # ==== Set identities
820
+ #
821
+ # <tt>lhs - rhs</tt> is equivalent to:
822
+ # * <tt>~rhs - ~lhs</tt>
823
+ # * <tt>lhs & ~rhs</tt>
824
+ # * <tt>~(~lhs | rhs)</tt>
825
+ # * <tt>lhs & (lhs ^ rhs)</tt>
826
+ # * <tt>lhs ^ (lhs & rhs)</tt>
827
+ # * <tt>rhs ^ (lhs | rhs)</tt>
828
+ def -(other) remain_frozen dup.subtract other end
829
+ alias difference :-
830
+
831
+ # :call-seq:
832
+ # self & other -> sequence set
833
+ # intersection(other) -> sequence set
834
+ #
835
+ # Returns a new sequence set containing only the numbers common to this
836
+ # set and +other+.
837
+ #
838
+ # +other+ may be any object that would be accepted by ::new.
839
+ #
840
+ # Net::IMAP::SequenceSet[1..5] & [2, 4, 6]
841
+ # #=> Net::IMAP::SequenceSet["2,4"]
842
+ #
843
+ # Related: #intersect?, #|, #-, #^, #~
844
+ #
845
+ # ==== Set identities
846
+ #
847
+ # <tt>lhs & rhs</tt> is equivalent to:
848
+ # * <tt>rhs & lhs</tt> (commutative)
849
+ # * <tt>~(~lhs | ~rhs)</tt> (De Morgan's Law)
850
+ # * <tt>lhs - ~rhs</tt>
851
+ # * <tt>lhs - (lhs - rhs)</tt>
852
+ # * <tt>lhs - (lhs ^ rhs)</tt>
853
+ # * <tt>lhs ^ (lhs - rhs)</tt>
854
+ def &(other)
855
+ remain_frozen dup.subtract SequenceSet.new(other).complement!
856
+ end
857
+ alias intersection :&
858
+
859
+ # :call-seq:
860
+ # self ^ other -> sequence set
861
+ # xor(other) -> sequence set
862
+ #
863
+ # Returns a new sequence set containing numbers that are exclusive between
864
+ # this set and +other+.
865
+ #
866
+ # +other+ may be any object that would be accepted by ::new.
867
+ #
868
+ # Net::IMAP::SequenceSet[1..5] ^ [2, 4, 6]
869
+ # #=> Net::IMAP::SequenceSet["1,3,5:6"]
870
+ #
871
+ # Related: #|, #&, #-, #~
872
+ #
873
+ # ==== Set identities
874
+ #
875
+ # <tt>lhs ^ rhs</tt> is equivalent to:
876
+ # * <tt>rhs ^ lhs</tt> (commutative)
877
+ # * <tt>~lhs ^ ~rhs</tt>
878
+ # * <tt>(lhs | rhs) - (lhs & rhs)</tt>
879
+ # * <tt>(lhs - rhs) | (rhs - lhs)</tt>
880
+ # * <tt>(lhs ^ other) ^ (other ^ rhs)</tt>
881
+ def ^(other) remain_frozen (dup | other).subtract(self & other) end
882
+ alias xor :^
883
+
884
+ # :call-seq:
885
+ # ~ self -> sequence set
886
+ # complement -> sequence set
887
+ #
888
+ # Returns the complement of self, a SequenceSet which contains all numbers
889
+ # _except_ for those in this set.
890
+ #
891
+ # ~Net::IMAP::SequenceSet.full #=> Net::IMAP::SequenceSet.empty
892
+ # ~Net::IMAP::SequenceSet.empty #=> Net::IMAP::SequenceSet.full
893
+ # ~Net::IMAP::SequenceSet["1:5,100:222"]
894
+ # #=> Net::IMAP::SequenceSet["6:99,223:*"]
895
+ # ~Net::IMAP::SequenceSet["6:99,223:*"]
896
+ # #=> Net::IMAP::SequenceSet["1:5,100:222"]
897
+ #
898
+ # Related: #complement!, #|, #&, #-, #^
899
+ #
900
+ # ==== Set identities
901
+ #
902
+ # <tt>~set</tt> is equivalent to:
903
+ # * <tt>full - set</tt>, where "full" is Net::IMAP::SequenceSet.full
904
+ def ~; remain_frozen dup.complement! end
905
+ alias complement :~
906
+
907
+ # :call-seq:
908
+ # add(element) -> self
909
+ # self << other -> self
910
+ #
911
+ # Adds a range or number to the set and returns +self+.
912
+ #
913
+ # #string will be regenerated. Use #merge to add many elements at once.
914
+ #
915
+ # Use #append to append new elements to #string. See
916
+ # SequenceSet@Ordered+and+Normalized+sets.
917
+ #
918
+ # Related: #add?, #merge, #union, #append
919
+ def add(element)
920
+ modifying! # short-circuit before input_to_tuple
921
+ tuple_add input_to_tuple element
922
+ normalize!
923
+ end
924
+ alias << add
925
+
926
+ # Adds a range or number to the set and returns +self+.
927
+ #
928
+ # Unlike #add, #merge, or #union, the new value is appended to #string.
929
+ # This may result in a #string which has duplicates or is out-of-order.
930
+ #
931
+ # See SequenceSet@Ordered+and+Normalized+sets.
932
+ #
933
+ # Related: #add, #merge, #union
934
+ def append(entry)
935
+ modifying! # short-circuit before input_to_tuple
936
+ tuple = input_to_tuple entry
937
+ entry = tuple_to_str tuple
938
+ string unless empty? # write @string before tuple_add
939
+ tuple_add tuple
940
+ @string = -(@string ? "#{@string},#{entry}" : entry)
941
+ self
942
+ end
943
+
944
+ # :call-seq: add?(element) -> self or nil
945
+ #
946
+ # Adds a range or number to the set and returns +self+. Returns +nil+
947
+ # when the element is already included in the set.
948
+ #
949
+ # #string will be regenerated. Use #merge to add many elements at once.
950
+ #
951
+ # Related: #add, #merge, #union, #include?
952
+ def add?(element)
953
+ modifying! # short-circuit before include?
954
+ add element unless include? element
955
+ end
956
+
957
+ # :call-seq: delete(element) -> self
958
+ #
959
+ # Deletes the given range or number from the set and returns +self+.
960
+ #
961
+ # #string will be regenerated after deletion. Use #subtract to remove
962
+ # many elements at once.
963
+ #
964
+ # Related: #delete?, #delete_at, #subtract, #difference
965
+ def delete(element)
966
+ modifying! # short-circuit before input_to_tuple
967
+ tuple_subtract input_to_tuple element
968
+ normalize!
969
+ end
970
+
971
+ # :call-seq:
972
+ # delete?(number) -> integer or nil
973
+ # delete?(star) -> :* or nil
974
+ # delete?(range) -> sequence set or nil
975
+ #
976
+ # Removes a specified value from the set, and returns the removed value.
977
+ # Returns +nil+ if nothing was removed.
978
+ #
979
+ # Returns an integer when the specified +number+ argument was removed:
980
+ # set = Net::IMAP::SequenceSet.new [5..10, 20]
981
+ # set.delete?(7) #=> 7
982
+ # set #=> #<Net::IMAP::SequenceSet "5:6,8:10,20">
983
+ # set.delete?("20") #=> 20
984
+ # set #=> #<Net::IMAP::SequenceSet "5:6,8:10">
985
+ # set.delete?(30) #=> nil
986
+ #
987
+ # Returns <tt>:*</tt> when <tt>*</tt> or <tt>-1</tt> is specified and
988
+ # removed:
989
+ # set = Net::IMAP::SequenceSet.new "5:9,20,35,*"
990
+ # set.delete?(-1) #=> :*
991
+ # set #=> #<Net::IMAP::SequenceSet "5:9,20,35">
992
+ #
993
+ # And returns a new SequenceSet when a range is specified:
994
+ #
995
+ # set = Net::IMAP::SequenceSet.new [5..10, 20]
996
+ # set.delete?(9..) #=> #<Net::IMAP::SequenceSet "9:10,20">
997
+ # set #=> #<Net::IMAP::SequenceSet "5:8">
998
+ # set.delete?(21..) #=> nil
999
+ #
1000
+ # #string will be regenerated after deletion.
1001
+ #
1002
+ # Related: #delete, #delete_at, #subtract, #difference, #disjoint?
1003
+ def delete?(element)
1004
+ modifying! # short-circuit before input_to_tuple
1005
+ tuple = input_to_tuple element
1006
+ if tuple.first == tuple.last
1007
+ return unless include_tuple? tuple
1008
+ tuple_subtract tuple
1009
+ normalize!
1010
+ from_tuple_int tuple.first
1011
+ else
1012
+ copy = dup
1013
+ tuple_subtract tuple
1014
+ normalize!
1015
+ copy if copy.subtract(self).valid?
1016
+ end
1017
+ end
1018
+
1019
+ # :call-seq: delete_at(index) -> number or :* or nil
1020
+ #
1021
+ # Deletes a number the set, indicated by the given +index+. Returns the
1022
+ # number that was removed, or +nil+ if nothing was removed.
1023
+ #
1024
+ # #string will be regenerated after deletion.
1025
+ #
1026
+ # Related: #delete, #delete?, #slice!, #subtract, #difference
1027
+ def delete_at(index)
1028
+ slice! Integer(index.to_int)
1029
+ end
1030
+
1031
+ # :call-seq:
1032
+ # slice!(index) -> integer or :* or nil
1033
+ # slice!(start, length) -> sequence set or nil
1034
+ # slice!(range) -> sequence set or nil
1035
+ #
1036
+ # Deletes a number or consecutive numbers from the set, indicated by the
1037
+ # given +index+, +start+ and +length+, or +range+ of offsets. Returns the
1038
+ # number or sequence set that was removed, or +nil+ if nothing was
1039
+ # removed. Arguments are interpreted the same as for #slice or #[].
1040
+ #
1041
+ # #string will be regenerated after deletion.
1042
+ #
1043
+ # Related: #slice, #delete_at, #delete, #delete?, #subtract, #difference
1044
+ def slice!(index, length = nil)
1045
+ modifying! # short-circuit before slice
1046
+ deleted = slice(index, length) and subtract deleted
1047
+ deleted
1048
+ end
1049
+
1050
+ # Merges all of the elements that appear in any of the +sets+ into the
1051
+ # set, and returns +self+.
1052
+ #
1053
+ # The +sets+ may be any objects that would be accepted by ::new.
1054
+ #
1055
+ # #string will be regenerated after all sets have been merged.
1056
+ #
1057
+ # Related: #add, #add?, #union
1058
+ def merge(*sets)
1059
+ modifying! # short-circuit before input_to_tuples
1060
+ tuples_add input_to_tuples sets
1061
+ normalize!
1062
+ end
1063
+
1064
+ # Removes all of the elements that appear in any of the given +sets+ from
1065
+ # the set, and returns +self+.
1066
+ #
1067
+ # The +sets+ may be any objects that would be accepted by ::new.
1068
+ #
1069
+ # Related: #difference
1070
+ def subtract(*sets)
1071
+ tuples_subtract input_to_tuples sets
1072
+ normalize!
1073
+ end
1074
+
1075
+ # Returns an array of ranges and integers and <tt>:*</tt>.
1076
+ #
1077
+ # The entries are in the same order they appear in #string, with no
1078
+ # sorting, deduplication, or coalescing. When #string is in its
1079
+ # normalized form, this will return the same result as #elements.
1080
+ # This is useful when the given order is significant, for example in a
1081
+ # ESEARCH response to IMAP#sort.
1082
+ #
1083
+ # See SequenceSet@Ordered+and+Normalized+sets.
1084
+ #
1085
+ # Related: #each_entry, #elements
1086
+ def entries; each_entry.to_a end
1087
+
1088
+ # Returns an array of ranges and integers and <tt>:*</tt>.
1089
+ #
1090
+ # The returned elements are sorted and coalesced, even when the input
1091
+ # #string is not. <tt>*</tt> will sort last. See #normalize,
1092
+ # SequenceSet@Ordered+and+Normalized+sets.
1093
+ #
1094
+ # By itself, <tt>*</tt> translates to <tt>:*</tt>. A range containing
1095
+ # <tt>*</tt> translates to an endless range. Use #limit to translate both
1096
+ # cases to a maximum value.
1097
+ #
1098
+ # Net::IMAP::SequenceSet["2,5:9,6,*,12:11"].elements
1099
+ # #=> [2, 5..9, 11..12, :*]
1100
+ #
1101
+ # Related: #each_element, #ranges, #numbers
1102
+ def elements; each_element.to_a end
1103
+ alias to_a elements
1104
+
1105
+ # Returns an array of ranges
1106
+ #
1107
+ # The returned elements are sorted and coalesced, even when the input
1108
+ # #string is not. <tt>*</tt> will sort last. See #normalize,
1109
+ # SequenceSet@Ordered+and+Normalized+sets.
1110
+ #
1111
+ # <tt>*</tt> translates to an endless range. By itself, <tt>*</tt>
1112
+ # translates to <tt>:*..</tt>. Use #limit to set <tt>*</tt> to a maximum
1113
+ # value.
1114
+ #
1115
+ # Net::IMAP::SequenceSet["2,5:9,6,*,12:11"].ranges
1116
+ # #=> [2..2, 5..9, 11..12, :*..]
1117
+ # Net::IMAP::SequenceSet["123,999:*,456:789"].ranges
1118
+ # #=> [123..123, 456..789, 999..]
1119
+ #
1120
+ # Related: #each_range, #elements, #numbers, #to_set
1121
+ def ranges; each_range.to_a end
1122
+
1123
+ # Returns a sorted array of all of the number values in the sequence set.
1124
+ #
1125
+ # The returned numbers are sorted and de-duplicated, even when the input
1126
+ # #string is not. See #normalize, SequenceSet@Ordered+and+Normalized+sets.
1127
+ #
1128
+ # Net::IMAP::SequenceSet["2,5:9,6,12:11"].numbers
1129
+ # #=> [2, 5, 6, 7, 8, 9, 11, 12]
1130
+ #
1131
+ # If the set contains a <tt>*</tt>, RangeError is raised. See #limit.
1132
+ #
1133
+ # Net::IMAP::SequenceSet["10000:*"].numbers
1134
+ # #!> RangeError
1135
+ #
1136
+ # *WARNING:* Even excluding sets with <tt>*</tt>, an enormous result can
1137
+ # easily be created. An array with over 4 billion integers could be
1138
+ # returned, requiring up to 32GiB of memory on a 64-bit architecture.
1139
+ #
1140
+ # Net::IMAP::SequenceSet[10000..2**32-1].numbers
1141
+ # # ...probably freezes the process for a while...
1142
+ # #!> NoMemoryError (probably)
1143
+ #
1144
+ # For safety, consider using #limit or #intersection to set an upper
1145
+ # bound. Alternatively, use #each_element, #each_range, or even
1146
+ # #each_number to avoid allocation of a result array.
1147
+ #
1148
+ # Related: #elements, #ranges, #to_set
1149
+ def numbers; each_number.to_a end
1150
+
1151
+ # Yields each number or range in #string to the block and returns +self+.
1152
+ # Returns an enumerator when called without a block.
1153
+ #
1154
+ # The entries are yielded in the same order they appear in #string, with
1155
+ # no sorting, deduplication, or coalescing. When #string is in its
1156
+ # normalized form, this will yield the same values as #each_element.
1157
+ #
1158
+ # See SequenceSet@Ordered+and+Normalized+sets.
1159
+ #
1160
+ # Related: #entries, #each_element
1161
+ def each_entry(&block) # :yields: integer or range or :*
1162
+ return to_enum(__method__) unless block_given?
1163
+ each_entry_tuple do yield tuple_to_entry _1 end
1164
+ end
1165
+
1166
+ # Yields each number or range (or <tt>:*</tt>) in #elements to the block
1167
+ # and returns self. Returns an enumerator when called without a block.
1168
+ #
1169
+ # The returned numbers are sorted and de-duplicated, even when the input
1170
+ # #string is not. See #normalize, SequenceSet@Ordered+and+Normalized+sets.
1171
+ #
1172
+ # Related: #elements, #each_entry
1173
+ def each_element # :yields: integer or range or :*
1174
+ return to_enum(__method__) unless block_given?
1175
+ @tuples.each do yield tuple_to_entry _1 end
1176
+ self
1177
+ end
1178
+
1179
+ private
1180
+
1181
+ def each_entry_tuple(&block)
1182
+ return to_enum(__method__) unless block_given?
1183
+ if @string
1184
+ @string.split(",") do block.call str_to_tuple _1 end
1185
+ else
1186
+ @tuples.each(&block)
1187
+ end
1188
+ self
1189
+ end
1190
+
1191
+ def tuple_to_entry((min, max))
1192
+ if min == STAR_INT then :*
1193
+ elsif max == STAR_INT then min..
1194
+ elsif min == max then min
1195
+ else min..max
1196
+ end
1197
+ end
1198
+
1199
+ public
1200
+
1201
+ # Yields each range in #ranges to the block and returns self.
1202
+ # Returns an enumerator when called without a block.
1203
+ #
1204
+ # Related: #ranges
1205
+ def each_range # :yields: range
1206
+ return to_enum(__method__) unless block_given?
1207
+ @tuples.each do |min, max|
1208
+ if min == STAR_INT then yield :*..
1209
+ elsif max == STAR_INT then yield min..
1210
+ else yield min..max
1211
+ end
1212
+ end
1213
+ self
1214
+ end
1215
+
1216
+ # Yields each number in #numbers to the block and returns self.
1217
+ # If the set contains a <tt>*</tt>, RangeError will be raised.
1218
+ #
1219
+ # Returns an enumerator when called without a block (even if the set
1220
+ # contains <tt>*</tt>).
1221
+ #
1222
+ # Related: #numbers, #each_ordered_number
1223
+ def each_number(&block) # :yields: integer
1224
+ return to_enum(__method__) unless block_given?
1225
+ raise RangeError, '%s contains "*"' % [self.class] if include_star?
1226
+ @tuples.each do each_number_in_tuple _1, _2, &block end
1227
+ self
1228
+ end
1229
+
1230
+ # Yields each number in #entries to the block and returns self.
1231
+ # If the set contains a <tt>*</tt>, RangeError will be raised.
1232
+ #
1233
+ # Returns an enumerator when called without a block (even if the set
1234
+ # contains <tt>*</tt>).
1235
+ #
1236
+ # Related: #entries, #each_number
1237
+ def each_ordered_number(&block)
1238
+ return to_enum(__method__) unless block_given?
1239
+ raise RangeError, '%s contains "*"' % [self.class] if include_star?
1240
+ each_entry_tuple do each_number_in_tuple _1, _2, &block end
1241
+ end
1242
+
1243
+ private def each_number_in_tuple(min, max, &block)
1244
+ if min == STAR_INT then yield :*
1245
+ elsif min == max then yield min
1246
+ elsif max != STAR_INT then (min..max).each(&block)
1247
+ else
1248
+ raise RangeError, "#{SequenceSet} cannot enumerate range with '*'"
1249
+ end
1250
+ end
1251
+
1252
+ # Returns a Set with all of the #numbers in the sequence set.
1253
+ #
1254
+ # If the set contains a <tt>*</tt>, RangeError will be raised.
1255
+ #
1256
+ # See #numbers for the warning about very large sets.
1257
+ #
1258
+ # Related: #elements, #ranges, #numbers
1259
+ def to_set; Set.new(numbers) end
1260
+
1261
+ # Returns the count of #numbers in the set.
1262
+ #
1263
+ # <tt>*</tt> will be counted as <tt>2**32 - 1</tt> (the maximum 32-bit
1264
+ # unsigned integer value).
1265
+ #
1266
+ # Related: #count_with_duplicates
1267
+ def count
1268
+ @tuples.sum(@tuples.count) { _2 - _1 } +
1269
+ (include_star? && include?(UINT32_MAX) ? -1 : 0)
1270
+ end
1271
+
1272
+ alias size count
1273
+
1274
+ # Returns the count of numbers in the ordered #entries, including any
1275
+ # repeated numbers.
1276
+ #
1277
+ # <tt>*</tt> will be counted as <tt>2**32 - 1</tt> (the maximum 32-bit
1278
+ # unsigned integer value).
1279
+ #
1280
+ # When #string is normalized, this behaves the same as #count.
1281
+ #
1282
+ # Related: #entries, #count_duplicates, #has_duplicates?
1283
+ def count_with_duplicates
1284
+ return count unless @string
1285
+ each_entry_tuple.sum {|min, max|
1286
+ max - min + ((max == STAR_INT && min != STAR_INT) ? 0 : 1)
1287
+ }
1288
+ end
1289
+
1290
+ # Returns the count of repeated numbers in the ordered #entries, the
1291
+ # difference between #count_with_duplicates and #count.
1292
+ #
1293
+ # When #string is normalized, this is zero.
1294
+ #
1295
+ # Related: #entries, #count_with_duplicates, #has_duplicates?
1296
+ def count_duplicates
1297
+ return 0 unless @string
1298
+ count_with_duplicates - count
1299
+ end
1300
+
1301
+ # :call-seq: has_duplicates? -> true | false
1302
+ #
1303
+ # Returns whether or not the ordered #entries repeat any numbers.
1304
+ #
1305
+ # Always returns +false+ when #string is normalized.
1306
+ #
1307
+ # Related: #entries, #count_with_duplicates, #count_duplicates?
1308
+ def has_duplicates?
1309
+ return false unless @string
1310
+ count_with_duplicates != count
1311
+ end
1312
+
1313
+ # Returns the (sorted and deduplicated) index of +number+ in the set, or
1314
+ # +nil+ if +number+ isn't in the set.
1315
+ #
1316
+ # Related: #[], #at, #find_ordered_index
1317
+ def find_index(number)
1318
+ number = to_tuple_int number
1319
+ each_tuple_with_index(@tuples) do |min, max, idx_min|
1320
+ number < min and return nil
1321
+ number <= max and return from_tuple_int(idx_min + (number - min))
1322
+ end
1323
+ nil
1324
+ end
1325
+
1326
+ # Returns the first index of +number+ in the ordered #entries, or
1327
+ # +nil+ if +number+ isn't in the set.
1328
+ #
1329
+ # Related: #find_index
1330
+ def find_ordered_index(number)
1331
+ number = to_tuple_int number
1332
+ each_tuple_with_index(each_entry_tuple) do |min, max, idx_min|
1333
+ if min <= number && number <= max
1334
+ return from_tuple_int(idx_min + (number - min))
1335
+ end
1336
+ end
1337
+ nil
1338
+ end
1339
+
1340
+ private
1341
+
1342
+ def each_tuple_with_index(tuples)
1343
+ idx_min = 0
1344
+ tuples.each do |min, max|
1345
+ idx_max = idx_min + (max - min)
1346
+ yield min, max, idx_min, idx_max
1347
+ idx_min = idx_max + 1
1348
+ end
1349
+ idx_min
1350
+ end
1351
+
1352
+ def reverse_each_tuple_with_index(tuples)
1353
+ idx_max = -1
1354
+ tuples.reverse_each do |min, max|
1355
+ yield min, max, (idx_min = idx_max - (max - min)), idx_max
1356
+ idx_max = idx_min - 1
1357
+ end
1358
+ idx_max
1359
+ end
1360
+
1361
+ public
1362
+
1363
+ # :call-seq: at(index) -> integer or nil
1364
+ #
1365
+ # Returns the number at the given +index+ in the sorted set, without
1366
+ # modifying the set.
1367
+ #
1368
+ # +index+ is interpreted the same as in #[], except that #at only allows a
1369
+ # single integer argument.
1370
+ #
1371
+ # Related: #[], #slice, #ordered_at
1372
+ def at(index)
1373
+ lookup_number_by_tuple_index(tuples, index)
1374
+ end
1375
+
1376
+ # :call-seq: ordered_at(index) -> integer or nil
1377
+ #
1378
+ # Returns the number at the given +index+ in the ordered #entries, without
1379
+ # modifying the set.
1380
+ #
1381
+ # +index+ is interpreted the same as in #at (and #[]), except that
1382
+ # #ordered_at applies to the ordered #entries, not the sorted set.
1383
+ #
1384
+ # Related: #[], #slice, #ordered_at
1385
+ def ordered_at(index)
1386
+ lookup_number_by_tuple_index(each_entry_tuple, index)
1387
+ end
1388
+
1389
+ private def lookup_number_by_tuple_index(tuples, index)
1390
+ index = Integer(index.to_int)
1391
+ if index.negative?
1392
+ reverse_each_tuple_with_index(tuples) do |min, max, idx_min, idx_max|
1393
+ idx_min <= index and return from_tuple_int(min + (index - idx_min))
1394
+ end
1395
+ else
1396
+ each_tuple_with_index(tuples) do |min, _, idx_min, idx_max|
1397
+ index <= idx_max and return from_tuple_int(min + (index - idx_min))
1398
+ end
1399
+ end
1400
+ nil
1401
+ end
1402
+
1403
+ # :call-seq:
1404
+ # seqset[index] -> integer or :* or nil
1405
+ # slice(index) -> integer or :* or nil
1406
+ # seqset[start, length] -> sequence set or nil
1407
+ # slice(start, length) -> sequence set or nil
1408
+ # seqset[range] -> sequence set or nil
1409
+ # slice(range) -> sequence set or nil
1410
+ #
1411
+ # Returns a number or a subset from the _sorted_ set, without modifying
1412
+ # the set.
1413
+ #
1414
+ # When an Integer argument +index+ is given, the number at offset +index+
1415
+ # in the sorted set is returned:
1416
+ #
1417
+ # set = Net::IMAP::SequenceSet["10:15,20:23,26"]
1418
+ # set[0] #=> 10
1419
+ # set[5] #=> 15
1420
+ # set[10] #=> 26
1421
+ #
1422
+ # If +index+ is negative, it counts relative to the end of the sorted set:
1423
+ # set = Net::IMAP::SequenceSet["10:15,20:23,26"]
1424
+ # set[-1] #=> 26
1425
+ # set[-3] #=> 22
1426
+ # set[-6] #=> 15
1427
+ #
1428
+ # If +index+ is out of range, +nil+ is returned.
1429
+ #
1430
+ # set = Net::IMAP::SequenceSet["10:15,20:23,26"]
1431
+ # set[11] #=> nil
1432
+ # set[-12] #=> nil
1433
+ #
1434
+ # The result is based on the sorted and de-duplicated set, not on the
1435
+ # ordered #entries in #string.
1436
+ #
1437
+ # set = Net::IMAP::SequenceSet["12,20:23,11:16,21"]
1438
+ # set[0] #=> 11
1439
+ # set[-1] #=> 23
1440
+ #
1441
+ # Related: #at
1442
+ def [](index, length = nil)
1443
+ if length then slice_length(index, length)
1444
+ elsif index.is_a?(Range) then slice_range(index)
1445
+ else at(index)
1446
+ end
1447
+ end
1448
+
1449
+ alias slice :[]
1450
+
1451
+ private
1452
+
1453
+ def slice_length(start, length)
1454
+ start = Integer(start.to_int)
1455
+ length = Integer(length.to_int)
1456
+ raise ArgumentError, "length must be positive" unless length.positive?
1457
+ last = start + length - 1 unless start.negative? && start.abs <= length
1458
+ slice_range(start..last)
1459
+ end
1460
+
1461
+ def slice_range(range)
1462
+ first = range.begin || 0
1463
+ last = range.end || -1
1464
+ if range.exclude_end?
1465
+ return remain_frozen_empty if last.zero?
1466
+ last -= 1 if range.end && last != STAR_INT
1467
+ end
1468
+ if (first * last).positive? && last < first
1469
+ remain_frozen_empty
1470
+ elsif (min = at(first))
1471
+ max = at(last)
1472
+ max = :* if max.nil?
1473
+ if max == :* then self & (min..)
1474
+ elsif min <= max then self & (min..max)
1475
+ else remain_frozen_empty
1476
+ end
1477
+ end
1478
+ end
1479
+
1480
+ public
1481
+
1482
+ # Returns a copy of +self+ which only contains the numbers above +num+.
1483
+ #
1484
+ # Net::IMAP::SequenceSet["5,10:22,50"].above(10) # to_s => "11:22,50"
1485
+ # Net::IMAP::SequenceSet["5,10:22,50"].above(20) # to_s => "21:22,50
1486
+ # Net::IMAP::SequenceSet["5,10:22,50"].above(30) # to_s => "50"
1487
+ #
1488
+ # This returns the same result as #intersection with <tt>((num+1)..)</tt>
1489
+ # or #difference with <tt>(..num)</tt>.
1490
+ #
1491
+ # Net::IMAP::SequenceSet["5,10:22,50"] & (11..) # to_s => "11:22,50"
1492
+ # Net::IMAP::SequenceSet["5,10:22,50"] - (..10) # to_s => "11:22,50"
1493
+ # Net::IMAP::SequenceSet["5,10:22,50"] & (21..) # to_s => "21:22,50"
1494
+ # Net::IMAP::SequenceSet["5,10:22,50"] - (..20) # to_s => "21:22,50"
1495
+ #
1496
+ # Related: #above, #-, #&
1497
+ def above(num)
1498
+ NumValidator.valid_nz_number?(num) or
1499
+ raise ArgumentError, "not a valid sequence set number"
1500
+ difference(..num)
1501
+ end
1502
+
1503
+ # Returns a copy of +self+ which only contains numbers below +num+.
1504
+ #
1505
+ # Net::IMAP::SequenceSet["5,10:22,50"].below(10) # to_s => "5"
1506
+ # Net::IMAP::SequenceSet["5,10:22,50"].below(20) # to_s => "5,10:19"
1507
+ # Net::IMAP::SequenceSet["5,10:22,50"].below(30) # to_s => "5,10:22"
1508
+ #
1509
+ # This returns the same result as #intersection with <tt>(..(num-1))</tt>
1510
+ # or #difference with <tt>(num..)</tt>.
1511
+ #
1512
+ # Net::IMAP::SequenceSet["5,10:22,50"] & (..9) # to_s => "5"
1513
+ # Net::IMAP::SequenceSet["5,10:22,50"] - (10..) # to_s => "5"
1514
+ # Net::IMAP::SequenceSet["5,10:22,50"] & (..19) # to_s => "5,10:19"
1515
+ # Net::IMAP::SequenceSet["5,10:22,50"] - (20..) # to_s => "5,10:19"
1516
+ #
1517
+ # When the set does not contain <tt>*</tt>, #below is identical to #limit
1518
+ # with <tt>max: num - 1</tt>. When the set does contain <tt>*</tt>,
1519
+ # #below always drops it from the result. Use #limit when the IMAP
1520
+ # semantics for <tt>*</tt> must be enforced.
1521
+ #
1522
+ # Net::IMAP::SequenceSet["5,10:22,50"].below(30) # to_s => "5,10:22"
1523
+ # Net::IMAP::SequenceSet["5,10:22,50"].limit(max: 29) # to_s => "5,10:22"
1524
+ # Net::IMAP::SequenceSet["5,10:22,*"].below(30) # to_s => "5,10:22"
1525
+ # Net::IMAP::SequenceSet["5,10:22,*"].limit(max: 29) # to_s => "5,10:22,29"
1526
+ #
1527
+ # Related: #above, #-, #&, #limit
1528
+ def below(num)
1529
+ NumValidator.valid_nz_number?(num) or
1530
+ raise ArgumentError, "not a valid sequence set number"
1531
+ difference(num..)
1532
+ end
1533
+
1534
+ # Returns a frozen SequenceSet with <tt>*</tt> converted to +max+, numbers
1535
+ # and ranges over +max+ removed, and ranges containing +max+ converted to
1536
+ # end at +max+.
1537
+ #
1538
+ # Net::IMAP::SequenceSet["5,10:22,50"].limit(max: 20).to_s
1539
+ # #=> "5,10:20"
1540
+ #
1541
+ # <tt>*</tt> is always interpreted as the maximum value. When the set
1542
+ # contains <tt>*</tt>, it will be set equal to the limit.
1543
+ #
1544
+ # Net::IMAP::SequenceSet["*"].limit(max: 37)
1545
+ # #=> Net::IMAP::SequenceSet["37"]
1546
+ # Net::IMAP::SequenceSet["5:*"].limit(max: 37)
1547
+ # #=> Net::IMAP::SequenceSet["5:37"]
1548
+ # Net::IMAP::SequenceSet["500:*"].limit(max: 37)
1549
+ # #=> Net::IMAP::SequenceSet["37"]
1550
+ #
1551
+ # Related: #limit!
1552
+ def limit(max:)
1553
+ max = to_tuple_int(max)
1554
+ if empty? then self.class.empty
1555
+ elsif !include_star? && max < min then self.class.empty
1556
+ elsif max(star: STAR_INT) <= max then frozen? ? self : dup.freeze
1557
+ else dup.limit!(max: max).freeze
1558
+ end
1559
+ end
1560
+
1561
+ # Removes all members over +max+ and returns self. If <tt>*</tt> is a
1562
+ # member, it will be converted to +max+.
1563
+ #
1564
+ # Related: #limit
1565
+ def limit!(max:)
1566
+ modifying! # short-circuit, and normalize the error message for JRuby
1567
+ star = include_star?
1568
+ max = to_tuple_int(max)
1569
+ tuple_subtract [max + 1, STAR_INT]
1570
+ tuple_add [max, max ] if star
1571
+ normalize!
1572
+ end
1573
+
1574
+ # :call-seq: complement! -> self
1575
+ #
1576
+ # Converts the SequenceSet to its own #complement. It will contain all
1577
+ # possible values _except_ for those currently in the set.
1578
+ #
1579
+ # Related: #complement
1580
+ def complement!
1581
+ modifying! # short-circuit, and normalize the error message for JRuby
1582
+ return replace(self.class.full) if empty?
1583
+ return clear if full?
1584
+ flat = @tuples.flat_map { [_1 - 1, _2 + 1] }
1585
+ if flat.first < 1 then flat.shift else flat.unshift 1 end
1586
+ if STAR_INT < flat.last then flat.pop else flat.push STAR_INT end
1587
+ @tuples = flat.each_slice(2).to_a
1588
+ normalize!
1589
+ end
1590
+
1591
+ # Returns a new SequenceSet with a normalized string representation.
1592
+ #
1593
+ # The returned set's #string is sorted and deduplicated. Adjacent or
1594
+ # overlapping elements will be merged into a single larger range.
1595
+ # See SequenceSet@Ordered+and+Normalized+sets.
1596
+ #
1597
+ # Net::IMAP::SequenceSet["1:5,3:7,10:9,10:11"].normalize
1598
+ # #=> Net::IMAP::SequenceSet["1:7,9:11"]
1599
+ #
1600
+ # Related: #normalize!, #normalized_string
1601
+ def normalize
1602
+ str = normalized_string
1603
+ return self if frozen? && str == string
1604
+ remain_frozen dup.instance_exec { @string = str&.-@; self }
1605
+ end
1606
+
1607
+ # Resets #string to be sorted, deduplicated, and coalesced. Returns
1608
+ # +self+. See SequenceSet@Ordered+and+Normalized+sets.
1609
+ #
1610
+ # Related: #normalize, #normalized_string
1611
+ def normalize!
1612
+ modifying! # redundant check, to normalize the error message for JRuby
1613
+ @string = nil
1614
+ self
1615
+ end
1616
+
1617
+ # Returns a normalized +sequence-set+ string representation, sorted
1618
+ # and deduplicated. Adjacent or overlapping elements will be merged into
1619
+ # a single larger range. See SequenceSet@Ordered+and+Normalized+sets.
1620
+ #
1621
+ # Net::IMAP::SequenceSet["1:5,3:7,10:9,10:11"].normalized_string
1622
+ # #=> "1:7,9:11"
1623
+ #
1624
+ # Returns +nil+ when the set is empty.
1625
+ #
1626
+ # Related: #normalize!, #normalize
1627
+ def normalized_string
1628
+ @tuples.empty? ? nil : -@tuples.map { tuple_to_str _1 }.join(",")
1629
+ end
1630
+
1631
+ def inspect
1632
+ if empty?
1633
+ (frozen? ? "%s.empty" : "#<%s empty>") % [self.class]
1634
+ elsif frozen?
1635
+ "%s[%p]" % [self.class, to_s]
1636
+ else
1637
+ "#<%s %p>" % [self.class, to_s]
1638
+ end
1639
+ end
1640
+
1641
+ ##
1642
+ # :method: to_sequence_set
1643
+ # :call-seq: to_sequence_set -> self
1644
+ #
1645
+ # Returns +self+
1646
+ #
1647
+ # Related: ::try_convert
1648
+
1649
+ # :nodoc: (work around rdoc bug)
1650
+ alias to_sequence_set itself
1651
+
1652
+ # Unstable API: currently for internal use only (Net::IMAP#validate_data)
1653
+ def validate # :nodoc:
1654
+ empty? and raise DataFormatError, "empty sequence-set is invalid"
1655
+ self
1656
+ end
1657
+
1658
+ # Unstable API: for internal use only (Net::IMAP#send_data)
1659
+ def send_data(imap, tag) # :nodoc:
1660
+ imap.__send__(:put_string, valid_string)
1661
+ end
1662
+
1663
+ # For YAML serialization
1664
+ def encode_with(coder) # :nodoc:
1665
+ # we can perfectly reconstruct from the string
1666
+ coder['string'] = to_s
1667
+ end
1668
+
1669
+ # For YAML deserialization
1670
+ def init_with(coder) # :nodoc:
1671
+ @tuples = []
1672
+ self.string = coder['string']
1673
+ end
1674
+
1675
+ protected
1676
+
1677
+ attr_reader :tuples # :nodoc:
1678
+
1679
+ private
1680
+
1681
+ def remain_frozen(set) frozen? ? set.freeze : set end
1682
+ def remain_frozen_empty; frozen? ? SequenceSet.empty : SequenceSet.new end
1683
+
1684
+ # frozen clones are shallow copied
1685
+ def initialize_clone(other)
1686
+ other.frozen? ? super : initialize_dup(other)
1687
+ end
1688
+
1689
+ def initialize_dup(other)
1690
+ modifying! # redundant check, to normalize the error message for JRuby
1691
+ @tuples = other.tuples.map(&:dup)
1692
+ @string = other.string&.-@
1693
+ super
1694
+ end
1695
+
1696
+ def input_to_tuple(entry)
1697
+ entry = input_try_convert entry
1698
+ case entry
1699
+ when *STARS, Integer then [int = to_tuple_int(entry), int]
1700
+ when Range then range_to_tuple(entry)
1701
+ when String then str_to_tuple(entry)
1702
+ else
1703
+ raise DataFormatError, "expected number or range, got %p" % [entry]
1704
+ end
1705
+ end
1706
+
1707
+ def input_to_tuples(set)
1708
+ set = input_try_convert set
1709
+ case set
1710
+ when *STARS, Integer, Range then [input_to_tuple(set)]
1711
+ when String then str_to_tuples set
1712
+ when SequenceSet then set.tuples
1713
+ when Set then set.map { [to_tuple_int(_1)] * 2 }
1714
+ when Array then set.flat_map { input_to_tuples _1 }
1715
+ when nil then []
1716
+ else
1717
+ raise DataFormatError, "expected nz-number, range, '*', Set, Array; " \
1718
+ "got %p" % [set]
1719
+ end
1720
+ end
1721
+
1722
+ # unlike SequenceSet#try_convert, this returns an Integer, Range,
1723
+ # String, Set, Array, or... any type of object.
1724
+ def input_try_convert(input)
1725
+ SequenceSet.try_convert(input) ||
1726
+ Integer.try_convert(input) ||
1727
+ String.try_convert(input) ||
1728
+ input
1729
+ end
1730
+
1731
+ def range_to_tuple(range)
1732
+ first = to_tuple_int(range.begin || 1)
1733
+ last = to_tuple_int(range.end || :*)
1734
+ last -= 1 if range.exclude_end? && range.end && last != STAR_INT
1735
+ unless first <= last
1736
+ raise DataFormatError, "invalid range for sequence-set: %p" % [range]
1737
+ end
1738
+ [first, last]
1739
+ end
1740
+
1741
+ def to_tuple_int(obj) STARS.include?(obj) ? STAR_INT : nz_number(obj) end
1742
+ def from_tuple_int(num) num == STAR_INT ? :* : num end
1743
+
1744
+ def tuple_to_str(tuple) tuple.uniq.map{ from_tuple_int _1 }.join(":") end
1745
+ def str_to_tuples(str) str.split(",", -1).map! { str_to_tuple _1 } end
1746
+ def str_to_tuple(str)
1747
+ raise DataFormatError, "invalid sequence set string" if str.empty?
1748
+ str.split(":", 2).map! { to_tuple_int _1 }.minmax
1749
+ end
1750
+
1751
+ def include_tuple?((min, max)) range_gte_to(min)&.cover?(min..max) end
1752
+
1753
+ def intersect_tuple?((min, max))
1754
+ range = range_gte_to(min) and
1755
+ range.include?(min) || range.include?(max) || (min..max).cover?(range)
1756
+ end
1757
+
1758
+ def modifying!
1759
+ if frozen?
1760
+ raise FrozenError, "can't modify frozen #{self.class}: %p" % [self]
1761
+ end
1762
+ end
1763
+
1764
+ def tuples_add(tuples) tuples.each do tuple_add _1 end; self end
1765
+ def tuples_subtract(tuples) tuples.each do tuple_subtract _1 end; self end
1766
+
1767
+ #
1768
+ # --|=====| |=====new tuple=====| append
1769
+ # ?????????-|=====new tuple=====|-|===lower===|-- insert
1770
+ #
1771
+ # |=====new tuple=====|
1772
+ # ---------??=======lower=======??--------------- noop
1773
+ #
1774
+ # ---------??===lower==|--|==| join remaining
1775
+ # ---------??===lower==|--|==|----|===upper===|-- join until upper
1776
+ # ---------??===lower==|--|==|--|=====upper===|-- join to upper
1777
+ def tuple_add(tuple)
1778
+ modifying!
1779
+ min, max = tuple
1780
+ lower, lower_idx = tuple_gte_with_index(min - 1)
1781
+ if lower.nil? then tuples << [min, max]
1782
+ elsif (max + 1) < lower.first then tuples.insert(lower_idx, [min, max])
1783
+ else tuple_coalesce(lower, lower_idx, min, max)
1784
+ end
1785
+ end
1786
+
1787
+ def tuple_coalesce(lower, lower_idx, min, max)
1788
+ return if lower.first <= min && max <= lower.last
1789
+ lower[0] = [min, lower.first].min
1790
+ lower[1] = [max, lower.last].max
1791
+ lower_idx += 1
1792
+ return if lower_idx == tuples.count
1793
+ tmax_adj = lower.last + 1
1794
+ upper, upper_idx = tuple_gte_with_index(tmax_adj)
1795
+ if upper
1796
+ tmax_adj < upper.first ? (upper_idx -= 1) : (lower[1] = upper.last)
1797
+ end
1798
+ tuples.slice!(lower_idx..upper_idx)
1799
+ end
1800
+
1801
+ # |====tuple================|
1802
+ # --|====| no more 1. noop
1803
+ # --|====|---------------------------|====lower====|-- 2. noop
1804
+ # -------|======lower================|---------------- 3. split
1805
+ # --------|=====lower================|---------------- 4. trim beginning
1806
+ #
1807
+ # -------|======lower====????????????----------------- trim lower
1808
+ # --------|=====lower====????????????----------------- delete lower
1809
+ #
1810
+ # -------??=====lower===============|----------------- 5. trim/delete one
1811
+ # -------??=====lower====|--|====| no more 6. delete rest
1812
+ # -------??=====lower====|--|====|---|====upper====|-- 7. delete until
1813
+ # -------??=====lower====|--|====|--|=====upper====|-- 8. delete and trim
1814
+ def tuple_subtract(tuple)
1815
+ modifying!
1816
+ min, max = tuple
1817
+ lower, idx = tuple_gte_with_index(min)
1818
+ if lower.nil? then nil # case 1.
1819
+ elsif max < lower.first then nil # case 2.
1820
+ elsif max < lower.last then tuple_trim_or_split lower, idx, min, max
1821
+ else tuples_trim_or_delete lower, idx, min, max
1822
+ end
1823
+ end
1824
+
1825
+ def tuple_trim_or_split(lower, idx, tmin, tmax)
1826
+ if lower.first < tmin # split
1827
+ tuples.insert(idx, [lower.first, tmin - 1])
1828
+ end
1829
+ lower[0] = tmax + 1
1830
+ end
1831
+
1832
+ def tuples_trim_or_delete(lower, lower_idx, tmin, tmax)
1833
+ if lower.first < tmin # trim lower
1834
+ lower[1] = tmin - 1
1835
+ lower_idx += 1
1836
+ end
1837
+ if tmax == lower.last # case 5
1838
+ upper_idx = lower_idx
1839
+ elsif (upper, upper_idx = tuple_gte_with_index(tmax + 1))
1840
+ upper_idx -= 1 # cases 7 and 8
1841
+ upper[0] = tmax + 1 if upper.first <= tmax # case 8 (else case 7)
1842
+ end
1843
+ tuples.slice!(lower_idx..upper_idx)
1844
+ end
1845
+
1846
+ def tuple_gte_with_index(num)
1847
+ idx = tuples.bsearch_index { _2 >= num } and [tuples[idx], idx]
1848
+ end
1849
+
1850
+ def range_gte_to(num)
1851
+ first, last = tuples.bsearch { _2 >= num }
1852
+ first..last if first
1853
+ end
1854
+
1855
+ def nz_number(num)
1856
+ String === num && !/\A[1-9]\d*\z/.match?(num) and
1857
+ raise DataFormatError, "%p is not a valid nz-number" % [num]
1858
+ NumValidator.ensure_nz_number Integer num
1859
+ rescue TypeError # To catch errors from Integer()
1860
+ raise DataFormatError, $!.message
1861
+ end
1862
+
1863
+ # intentionally defined after the class implementation
1864
+
1865
+ EMPTY = new.freeze
1866
+ FULL = self["1:*"]
1867
+ private_constant :EMPTY, :FULL
1868
+
1869
+ end
1870
+ end
1871
+ end