Class: Cabriolet::HLP::QuickHelp::CompressionStream

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/hlp/quickhelp/compression_stream.rb

Overview

Compression stream decoder for QuickHelp topics

Handles dictionary substitution (keyword compression) and run-length encoding as specified in the QuickHelp format.

Control bytes 0x10-0x1A have special meanings:

  • 0x10-0x17: Dictionary entry (with optional space append)

  • 0x18: Run of spaces

  • 0x19: Run of bytes

  • 0x1A: Escape byte

Instance Method Summary collapse

Constructor Details

#initialize(input, keywords = []) ⇒ CompressionStream

Initialize compression stream decoder

Parameters:

  • input (String, IO)

    Input data (compressed)

  • keywords (Array<String>) (defaults to: [])

    Keyword dictionary



23
24
25
26
27
28
# File 'lib/cabriolet/hlp/quickhelp/compression_stream.rb', line 23

def initialize(input, keywords = [])
  @input = input.is_a?(String) ? StringIO.new(input) : input
  @keywords = keywords || []
  @buffer = ""
  @buffer_pos = 0
end

Instance Method Details

#eof?Boolean

Check if at end of stream

Returns:

  • (Boolean)

    true if EOF



57
58
59
# File 'lib/cabriolet/hlp/quickhelp/compression_stream.rb', line 57

def eof?
  @buffer_pos >= @buffer.bytesize && @input.eof?
end

#read(length) ⇒ String

Read bytes from the decompressed stream

Parameters:

  • length (Integer)

    Number of bytes to read

Returns:

  • (String)

    Decompressed data



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cabriolet/hlp/quickhelp/compression_stream.rb', line 34

def read(length)
  result = String.new(encoding: Encoding::BINARY)

  while result.bytesize < length
    # Fill buffer if needed
    fill_buffer(length - result.bytesize) if @buffer_pos >= @buffer.bytesize

    # Check for EOF
    break if @buffer_pos >= @buffer.bytesize

    # Copy from buffer
    available = @buffer.bytesize - @buffer_pos
    to_copy = [length - result.bytesize, available].min
    result << @buffer[@buffer_pos, to_copy]
    @buffer_pos += to_copy
  end

  result
end