Skip to content

Commit 2a56c55

Browse files
committed
🚧✨ Add `SequenceSet#xor!"
This is simply a mutating version of `^` or `xor`. TODO: * rdoc on method * rdoc in class "What's Here"
1 parent 0c41696 commit 2a56c55

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

‎lib/net/imap/sequence_set.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ def &(other) remain_frozen dup.intersect! other end
739739
# * <tt>(lhs | rhs) - (lhs & rhs)</tt>
740740
# * <tt>(lhs - rhs) | (rhs - lhs)</tt>
741741
# * <tt>(lhs ^ other) ^ (other ^ rhs)</tt>
742-
def ^(other) remain_frozen (dup | other).subtract(self & other) end
742+
def ^(other) remain_frozen dup.xor! other end
743743
alias xor :^
744744

745745
# :call-seq:
@@ -1451,6 +1451,15 @@ def intersect!(other) # :nodoc:
14511451
subtract SequenceSet.new(other).complement!
14521452
end
14531453

1454+
# TODO: document and directly test this
1455+
def xor!(other) # :nodoc:
1456+
modifying!
1457+
copy = dup
1458+
other = SequenceSet.new(other)
1459+
# subtract(other).merge(other.subtract(copy)) # (L - R) | (R - L)
1460+
merge(other).subtract(copy.intersect!(other)) # (L | R) - (L & R)
1461+
end
1462+
14541463
# Returns a new SequenceSet with a normalized string representation.
14551464
#
14561465
# The returned set's #string is sorted and deduplicated. Adjacent or

‎test/net/imap/test_sequence_set.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ def compare_to_reference_set(nums, set, seqset)
9999
assert_equal xor, (lhs | rhs) - (lhs & rhs)
100100
assert_equal xor, (lhs ^ mid) ^ (mid ^ rhs)
101101
assert_equal xor, ~lhs ^ ~rhs
102+
mutable = lhs.dup
103+
assert_equal xor, mutable.xor!(rhs)
104+
assert_equal xor, mutable
102105
end
103106
end
104107

0 commit comments

Comments
 (0)