Module: LZ77

Defined in:
lib/lz77/decoder.rb

Class Method Summary collapse

Class Method Details

.decode(string) ⇒ Object

Decodes a string with LZ77 compression.

Params:

string: (String) encoded string

Returns: (String) decoded string



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
# File 'lib/lz77/decoder.rb', line 10

def self.decode(string)
  result = ''
  i = 0

  while i < string.size
    b = string[i].ord

    if b == 0x00 or (0x09 <= b and b <= 0x7f)
      result << b.chr
    elsif 0x01 <= b and b <= 0x08
      b.times do |j|
        bb = string[i + j + 1]
        result << bb.chr
      end

      i += b
    elsif 0x80 <= b and b <= 0xbf
      b = (b << 8) + string[i + 1].ord
      s = result[(-1 * ((b & 0x3ff8) >> 3)), ((b & 0x0007) + 3)]

      s.each_byte do |bb|
        result << bb.chr
      end

      i += 1
    elsif 0xc0 <= b and b <= 0xff
      result << ' ' + (b ^ 0x80).chr
    end

    i += 1
  end

  return result
end