Skip to content

Commit f117dbd

Browse files
committed
✅ Short-circuit frozen SequenceSet modifications
Short-circuit frozen SequenceSet modifications does two things: * avoids unnecessarily coercing inputs or calling query methods * normalizes the output for JRuby
1 parent c8f55e8 commit f117dbd

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

lib/net/imap/sequence_set.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,11 @@ def full; FULL end
383383
def initialize(input = nil) input ? replace(input) : clear end
384384

385385
# Removes all elements and returns self.
386-
def clear; @tuples, @string = [], nil; self end
386+
def clear
387+
modifying! # redundant check, to normalize the error message for JRuby
388+
@tuples, @string = [], nil
389+
self
390+
end
387391

388392
# Replace the contents of the set with the contents of +other+ and returns
389393
# +self+.
@@ -439,11 +443,13 @@ def string=(str)
439443
if str.nil?
440444
clear
441445
else
446+
modifying! # redundant check, to normalize the error message for JRuby
442447
str = String.try_convert(str) or raise ArgumentError, "not a string"
443448
tuples = str_to_tuples str
444449
@tuples, @string = [], -str
445450
tuples_add tuples
446451
end
452+
str
447453
end
448454

449455
# Returns the \IMAP +sequence-set+ string representation, or an empty
@@ -780,6 +786,7 @@ def ~; remain_frozen dup.complement! end
780786
#
781787
# Related: #add?, #merge, #union, #append
782788
def add(element)
789+
modifying! # short-circuit before input_to_tuple
783790
tuple_add input_to_tuple element
784791
normalize!
785792
end
@@ -794,7 +801,7 @@ def add(element)
794801
#
795802
# Related: #add, #merge, #union
796803
def append(entry)
797-
modifying!
804+
modifying! # short-circuit before input_to_tuple
798805
tuple = input_to_tuple entry
799806
entry = tuple_to_str tuple
800807
string unless empty? # write @string before tuple_add
@@ -812,6 +819,7 @@ def append(entry)
812819
#
813820
# Related: #add, #merge, #union, #include?
814821
def add?(element)
822+
modifying! # short-circuit before include?
815823
add element unless include? element
816824
end
817825

@@ -824,6 +832,7 @@ def add?(element)
824832
#
825833
# Related: #delete?, #delete_at, #subtract, #difference
826834
def delete(element)
835+
modifying! # short-circuit before input_to_tuple
827836
tuple_subtract input_to_tuple element
828837
normalize!
829838
end
@@ -861,6 +870,7 @@ def delete(element)
861870
#
862871
# Related: #delete, #delete_at, #subtract, #difference, #disjoint?
863872
def delete?(element)
873+
modifying! # short-circuit before input_to_tuple
864874
tuple = input_to_tuple element
865875
if tuple.first == tuple.last
866876
return unless include_tuple? tuple
@@ -901,6 +911,7 @@ def delete_at(index)
901911
#
902912
# Related: #slice, #delete_at, #delete, #delete?, #subtract, #difference
903913
def slice!(index, length = nil)
914+
modifying! # short-circuit before slice
904915
deleted = slice(index, length) and subtract deleted
905916
deleted
906917
end
@@ -916,6 +927,7 @@ def slice!(index, length = nil)
916927
#
917928
# Related: #add, #add?, #union
918929
def merge(*sets)
930+
modifying! # short-circuit before input_to_tuples
919931
tuples_add input_to_tuples sets
920932
normalize!
921933
end
@@ -1424,6 +1436,7 @@ def limit(max:)
14241436
#
14251437
# Related: #limit
14261438
def limit!(max:)
1439+
modifying! # short-circuit, and normalize the error message for JRuby
14271440
star = include_star?
14281441
max = to_tuple_int(max)
14291442
tuple_subtract [max + 1, STAR_INT]
@@ -1438,6 +1451,7 @@ def limit!(max:)
14381451
#
14391452
# Related: #complement
14401453
def complement!
1454+
modifying! # short-circuit, and normalize the error message for JRuby
14411455
return replace(self.class.full) if empty?
14421456
return clear if full?
14431457
flat = @tuples.flat_map { [_1 - 1, _2 + 1] }
@@ -1468,6 +1482,7 @@ def normalize
14681482
#
14691483
# Related: #normalize, #normalized_string
14701484
def normalize!
1485+
modifying! # redundant check, to normalize the error message for JRuby
14711486
@string = nil
14721487
self
14731488
end
@@ -1537,6 +1552,7 @@ def initialize_clone(other)
15371552
end
15381553

15391554
def initialize_dup(other)
1555+
modifying! # redundant check, to normalize the error message for JRuby
15401556
@tuples = other.tuples.map(&:dup)
15411557
@string = other.string&.-@
15421558
super

0 commit comments

Comments
 (0)