Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/ffxcodec/core_ext/string.rb
Instance Method Summary collapse
-
#^(other) ⇒ String
XOR operation on String.
-
#bisect ⇒ Array<String>
Split down the middle into two parts (right-biased).
-
#prepad_zeros(length) ⇒ String
Prepend zeroes until string is of the given length.
Instance Method Details
#^(other) ⇒ String
XOR operation on String
rubocop:disable AbcSize
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/ffxcodec/core_ext/string.rb', line 8 def ^(other) # rubocop:disable RedundantSelf, SignalException b1 = self.unpack("C*") b2 = other.unpack("C*") raise ArgumentError, "Strings must be the same length" unless b1.size == b2.size longest = [b1.length, b2.length].max b1 = [0] * (longest - b1.length) + b1 b2 = [0] * (longest - b2.length) + b2 b1.zip(b2).map { |a, b| a ^ b }.pack("C*") end |
#bisect ⇒ Array<String>
Split down the middle into two parts (right-biased)
23 24 25 26 27 |
# File 'lib/ffxcodec/core_ext/string.rb', line 23 def bisect n = self.size l = n / 2 [self[0...l], self[l...n]] end |
#prepad_zeros(length) ⇒ String
Note:
if string was already longer than the given length, no action taken
Prepend zeroes until string is of the given length
35 36 37 38 39 |
# File 'lib/ffxcodec/core_ext/string.rb', line 35 def prepad_zeros(length) str = self str.insert(0, '0') while str.length < length str end |