-
Notifications
You must be signed in to change notification settings - Fork 1
/
mystring.rb
62 lines (48 loc) · 1.19 KB
/
mystring.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#
# Some code found in the web
#
class String
def xor_slow(other)
if other.empty?
self
else
a1 = self.unpack("c*")
a2 = other.unpack("c*")
a2 *= a1.length/a2.length + 1
a1.zip(a2).collect{|c1,c2| c1^c2}.pack("c*")
end
end
def xor_fast(other)
if other.empty?
self
else
if other.length < self.length
div, mod = self.length.divmod(other.length)
other = other * div + other[0, mod]
end
a1 = NArray.to_na(self, "byte")
a2 = NArray.to_na(other, "byte")
(a1 ^ a2).to_s
end
end
# Convert a String to the HEX Representation
def hex
self.unpack('U'*self.length).collect {|x| x.to_s 16}.join
end
def hex2s
s=""
self.scan(/../).each { | tuple | s=s+tuple.to_i(16).chr }
s
end
# if narray not installed xor will use the slow version
begin
require "narray"
alias :xor :xor_fast
rescue LoadError
if $VERBOSE
$stderr.puts "I strongly advise to install the library
'narray'."
end
alias :xor :xor_slow
end
end