peterc-rsmaz 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/lib/rsmaz.rb +12 -10
- data/spec/rsmaz_spec.rb +4 -1
- metadata +1 -1
data/History.txt
CHANGED
data/lib/rsmaz.rb
CHANGED
@@ -3,10 +3,12 @@ $:.unshift(File.dirname(__FILE__)) unless
|
|
3
3
|
|
4
4
|
require 'strscan'
|
5
5
|
|
6
|
-
#
|
6
|
+
# Silly hack to allow usage of String#ord in Ruby 1.9 without breaking Ruby 1.8
|
7
|
+
class Fixnum; def ord; self; end; end
|
7
8
|
|
9
|
+
# RSmaz is too small to bother splitting into separate files, so I'll be lazy..
|
8
10
|
module RSmaz
|
9
|
-
VERSION = '0.0.
|
11
|
+
VERSION = '0.0.2'
|
10
12
|
|
11
13
|
# From http://github.com/antirez/smaz/blob/4b913924e15b7663ee0240af19cedfd266052aab/smaz.c
|
12
14
|
CODEBOOK = ["\002s,\266", "\003had\232\002leW", "\003on \216", "", "\001yS",
|
@@ -89,9 +91,9 @@ module RSmaz
|
|
89
91
|
# This algorithm has been ported to Ruby from C and only
|
90
92
|
# slightly Rubyized.. still a lonnnng way to go. Wanna give it a crack?
|
91
93
|
while (input && input.length > 0)
|
92
|
-
h1 = h2 = input[0] << 3
|
93
|
-
h2 += input[1] if (input.length > 1)
|
94
|
-
h3 = h2 ^ input[2] if (input.length > 2)
|
94
|
+
h1 = h2 = input[0].ord << 3
|
95
|
+
h2 += input[1].ord if (input.length > 1)
|
96
|
+
h3 = h2 ^ input[2].ord if (input.length > 2)
|
95
97
|
q = []
|
96
98
|
|
97
99
|
[input.length, 7].min.downto(1) do |j2|
|
@@ -104,11 +106,11 @@ module RSmaz
|
|
104
106
|
end
|
105
107
|
|
106
108
|
while (slot && slot[0]) do
|
107
|
-
if (slot[0] == j2 && (slot[1,j2] == input[0,j2]))
|
109
|
+
if (slot[0].ord == j2 && (slot[1,j2] == input[0,j2]))
|
108
110
|
# Match found in hash table
|
109
111
|
q << verb
|
110
112
|
verb = ""
|
111
|
-
q << slot[slot[0]+1]
|
113
|
+
q << slot[slot[0].ord+1].ord
|
112
114
|
input = input[j2..-1]
|
113
115
|
else
|
114
116
|
slot = slot[2..-1]
|
@@ -118,7 +120,7 @@ module RSmaz
|
|
118
120
|
|
119
121
|
# No queue? It means we matched nothing, so add the current byte to the verbatim buffer
|
120
122
|
if q.empty?
|
121
|
-
verb << input[0]
|
123
|
+
verb << input[0].ord if input[0]
|
122
124
|
input = input[1..-1]
|
123
125
|
end
|
124
126
|
|
@@ -146,11 +148,11 @@ module RSmaz
|
|
146
148
|
out = ""
|
147
149
|
s = StringScanner.new(input)
|
148
150
|
until s.eos?
|
149
|
-
bv = s.get_byte[0]
|
151
|
+
bv = s.get_byte[0].ord
|
150
152
|
if (bv == 254)
|
151
153
|
out << s.get_byte
|
152
154
|
elsif (bv == 255)
|
153
|
-
len = s.get_byte[0] + 1
|
155
|
+
len = s.get_byte[0].ord + 1
|
154
156
|
len.times do
|
155
157
|
out << s.get_byte
|
156
158
|
end
|
data/spec/rsmaz_spec.rb
CHANGED
@@ -10,7 +10,7 @@ describe RSmaz do
|
|
10
10
|
RSmaz.compress("thex").length.should == 3
|
11
11
|
end
|
12
12
|
|
13
|
-
it "should compress and decompress strings to the same
|
13
|
+
it "should compress and decompress strings back to the same string" do
|
14
14
|
[
|
15
15
|
"This is a test.",
|
16
16
|
"This is a test",
|
@@ -20,4 +20,7 @@ describe RSmaz do
|
|
20
20
|
].each { |str| RSmaz.decompress(RSmaz.compress(str)).should == str }
|
21
21
|
end
|
22
22
|
|
23
|
+
it "should properly decode a reference compression (so the internal coding doesn't change)" do
|
24
|
+
RSmaz.decompress("\020\230`A\376o\f\026\030").should == "hello world"
|
25
|
+
end
|
23
26
|
end
|