lib/asciipack/unpacker.rb in asciipack-0.1.2 vs lib/asciipack/unpacker.rb in asciipack-0.1.3

- old
+ new

@@ -1,45 +1,65 @@ -require 'asciipack/typemap.rb' - module AsciiPack class Unpacker + @@fixmap = { + "0" => 0x0, + "1" => 0x1, + "2" => 0x2, + "3" => 0x3, + "4" => 0x4, + "5" => 0x5, + "6" => 0x6, + "7" => 0x7, + "8" => 0x8, + "9" => 0x9, + "A" => 0xa, + "B" => 0xb, + "C" => 0xc, + "D" => 0xd, + "E" => 0xe, + "F" => 0xf, + "W" => nil, + "X" => false, + "Y" => true, + }.freeze + def initialize (ap) @ap = ap - @ret = nil @at = 0 @ch = @ap[0] end def unpack move + + if @@fixmap.key?(@ch) + return @@fixmap[@ch] + end + case @ch - when /[0-9A-F]/; positive_fixint + when "a"; int4 + when "b"; int8 + when "c"; int16 + when "d"; int32 + when "e"; int64 + when "g"; uint8 + when "h"; uint16 + when "i"; uint32 + when "j"; uint64 + when "l"; float64 + when "n"; str8 + when "o"; str16 + when "p"; str32 + when "r"; map4 + when "s"; map8 + when "t"; map16 + when "u"; map32 + when "v"; array4 + when "w"; array8 + when "x"; array16 + when "y"; array32 when /[G-V]/; fixstr - when TypeMap.int4; int4 - when TypeMap.int8; int8 - when TypeMap.int16; int16 - when TypeMap.int32; int32 - when TypeMap.int64; int64 - when TypeMap.uint8; uint8 - when TypeMap.uint16; uint16 - when TypeMap.uint32; uint32 - when TypeMap.uint64; uint64 - when TypeMap.float64; float64 - when TypeMap.map4; map4 - when TypeMap.map8; map8 - when TypeMap.map16; map16 - when TypeMap.map32; map32 - when TypeMap.array4; array4 - when TypeMap.array8; array8 - when TypeMap.array16; array16 - when TypeMap.array32; array32 - when TypeMap.str8; str8 - when TypeMap.str16; str16 - when TypeMap.str32; str32 - when TypeMap.nil; nil - when TypeMap.false; false - when TypeMap.true; true else raise "undefined type " + @ch.to_s end end private @@ -49,14 +69,14 @@ @at += 1 @ch end def cut (len) - @ret = @ap[@at...(@at + len)] + ret = @ap[@at...(@at + len)] @at += len @ch = @ap[@at] - @ret + ret end def int4 move i = @ch.to_i(16) @@ -85,14 +105,10 @@ c = cut(16) i = c.to_i(16) (c[0].to_i(16) < 0x8) ? i : i - 0x10000000000000000; end - def positive_fixint - @ch.to_i(16) - end - def uint8 cut(2).to_i(16) end def uint16 @@ -154,10 +170,10 @@ def array8; array(2) end def array16; array(4) end def array32; array(8) end def fixstr - len = @ch.ord - 71 # 71 = TypeMap.fixstr_0.ord + len = @ch.ord - 71 # 71 = "G".ord cut(len) end def str8 len = cut(2).to_i(16)