Module: BinData::Integer

Defined in:
lib/bindata/int.rb

Overview

Provides a number of classes that contain an integer. The integer is defined by endian, signedness and number of bytes.

Class Method Summary collapse

Class Method Details

.create_int_methods(klass, nbits, endian) ⇒ Object

:nodoc: all



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bindata/int.rb', line 8

def self.create_int_methods(klass, nbits, endian)
  max = (1 << (nbits - 1)) - 1
  min = -(max + 1)
  clamp = "val = (val < #{min}) ? #{min} : (val > #{max}) ? #{max} : val"

  int2uint = "val = val & #{(1 << nbits) - 1}"
  uint2int = "val = ((val & #{1 << (nbits - 1)}).zero?) ? " +
             "val & #{max} : -(((~val) & #{max}) + 1)"

  read = create_read_code(nbits, endian)
  to_s = create_to_s_code(nbits, endian)

  define_methods(klass, nbits / 8, clamp, read, to_s, int2uint, uint2int)
end

.create_read_code(nbits, endian) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bindata/int.rb', line 34

def self.create_read_code(nbits, endian)
  c16 = (endian == :little) ? 'v' : 'n'
  c32 = (endian == :little) ? 'V' : 'N'
  b1  = (endian == :little) ? 0 : 1
  b2  = (endian == :little) ? 1 : 0

  case nbits
  when  8; "readbytes(io,1)[0]"
  when 16; "readbytes(io,2).unpack('#{c16}').at(0)"
  when 32; "readbytes(io,4).unpack('#{c32}').at(0)"
  when 64; "(a = readbytes(io,8).unpack('#{c32 * 2}'); " +
                 "(a.at(#{b2}) << 32) + a.at(#{b1}))"
  else
    raise "unknown nbits '#{nbits}'"
  end
end

.create_to_s_code(nbits, endian) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bindata/int.rb', line 51

def self.create_to_s_code(nbits, endian)
  c16 = (endian == :little) ? 'v' : 'n'
  c32 = (endian == :little) ? 'V' : 'N'
  v1  = (endian == :little) ? 'val' : '(val >> 32)'
  v2  = (endian == :little) ? '(val >> 32)' : 'val'

  case nbits
  when  8; "val.chr"
  when 16; "[val].pack('#{c16}')"
  when 32; "[val].pack('#{c32}')"
  when 64; "[#{v1} & 0xffffffff, #{v2} & 0xffffffff].pack('#{c32 * 2}')"
  else
    raise "unknown nbits '#{nbits}'"
  end
end

.create_uint_methods(klass, nbits, endian) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/bindata/int.rb', line 23

def self.create_uint_methods(klass, nbits, endian)
  min = 0
  max = (1 << nbits) - 1
  clamp = "val = (val < #{min}) ? #{min} : (val > #{max}) ? #{max} : val"

  read = create_read_code(nbits, endian)
  to_s = create_to_s_code(nbits, endian)

  define_methods(klass, nbits / 8, clamp, read, to_s)
end

.define_methods(klass, nbytes, clamp, read, to_s, int2uint = nil, uint2int = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bindata/int.rb', line 67

def self.define_methods(klass, nbytes, clamp, read, to_s,
                        int2uint = nil, uint2int = nil)
  # define methods in the given class
  klass.module_eval <<-END
    def value=(val)
      #{clamp}
      super(val)
    end

    def _num_bytes(ignored)
      #{nbytes}
    end

    #---------------
    private

    def sensible_default
      0
    end

    def val_to_str(val)
      #{clamp}
      #{int2uint unless int2uint.nil?}
      #{to_s}
    end

    def read_val(io)
      val = #{read}
      #{uint2int unless uint2int.nil?}
    end
  END
end