Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ffxcodec/core_ext/string.rb

Instance Method Summary collapse

Instance Method Details

#^(other) ⇒ String

XOR operation on String

rubocop:disable AbcSize

Parameters:

  • other (String)

    string to XOR with

Returns:

  • (String)

    result of XOR operation

Raises:

  • (ArgumentError)


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

#bisectArray<String>

Split down the middle into two parts (right-biased)

Returns:

  • (Array<String>)

    the original string split into two. If length was odd, then the second string will have an extra character.



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

Parameters:

  • length (Integer)

    we want the resulting string to be

Returns:

  • (String)

    prepended with ‘0’s until the given length is reached



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