|
5 | 5 | class Bitwise
|
6 | 6 | attr_accessor :value
|
7 | 7 |
|
8 |
| - def initialize(size = 0) |
9 |
| - @value = "\x00" * size |
| 8 | + def initialize(value = "") |
| 9 | + @value = value.force_encoding(Encoding::ASCII_8BIT) |
10 | 10 | end
|
11 | 11 |
|
12 | 12 | def size
|
@@ -51,36 +51,32 @@ def get_byte(index)
|
51 | 51 | end
|
52 | 52 |
|
53 | 53 | def not
|
54 |
| - result = Bitwise.new |
55 |
| - result.value = Bitwise.string_not(self.value) |
56 |
| - result |
| 54 | + Bitwise.new(Bitwise.string_not(self.value)) |
57 | 55 | end
|
58 | 56 | alias :~ :not
|
59 | 57 |
|
60 | 58 | def intersect(other)
|
61 |
| - min, max = [ self.value, other.value ].sort_by{|i| i.bytesize } |
62 |
| - result = Bitwise.new |
63 |
| - result.value = Bitwise.string_intersect(max, min) |
64 |
| - result |
| 59 | + assign_max_and_min(other) |
| 60 | + Bitwise.new Bitwise.string_intersect(@max, @min) |
65 | 61 | end
|
66 | 62 | alias :& :intersect
|
67 | 63 |
|
68 | 64 | def union(other)
|
69 |
| - min, max = [ self.value, other.value ].sort_by{|i| i.bytesize } |
70 |
| - result = Bitwise.new |
71 |
| - result.value = Bitwise.string_union(max, min) |
72 |
| - result |
| 65 | + assign_max_and_min(other) |
| 66 | + Bitwise.new Bitwise.string_union(@max, @min) |
73 | 67 | end
|
74 | 68 | alias :| :union
|
75 | 69 |
|
76 | 70 | def xor(other)
|
77 |
| - min, max = [ self.value, other.value ].sort_by{|i| i.bytesize } |
78 |
| - result = Bitwise.new |
79 |
| - result.value = Bitwise.string_xor(max, min) |
80 |
| - result |
| 71 | + assign_max_and_min(other) |
| 72 | + Bitwise.new Bitwise.string_xor(@max, @min) |
81 | 73 | end
|
82 | 74 | alias :^ :xor
|
83 | 75 |
|
| 76 | + def assign_max_and_min(other) |
| 77 | + @min, @max = [ self.value, other.value ].sort_by{|i| i.bytesize } |
| 78 | + end |
| 79 | + |
84 | 80 | def value=(string)
|
85 | 81 | @value = string.force_encoding(Encoding::ASCII_8BIT)
|
86 | 82 | @value.bytesize
|
|
0 commit comments