Class: Cabriolet::HLP::QuickHelp::CompressionStream
- Inherits:
-
Object
- Object
- Cabriolet::HLP::QuickHelp::CompressionStream
- 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
-
#eof? ⇒ Boolean
Check if at end of stream.
-
#initialize(input, keywords = []) ⇒ CompressionStream
constructor
Initialize compression stream decoder.
-
#read(length) ⇒ String
Read bytes from the decompressed stream.
Constructor Details
#initialize(input, keywords = []) ⇒ CompressionStream
Initialize compression stream decoder
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
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
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 |