class Reader

class Reader

lib/binary/reader.tya:2

Reader provides the binary/Reader standard library API.

Source
# Reader provides the binary/Reader standard library API.
class Reader
  # Reader.data stores instance state.
  # @type Array
  data: []

  # Reader.endian stores instance state.
  # @type Nil
  endian: nil

  # Reader.offset stores instance state.
  # @type Int
  offset: 0

  # Reader.initialize provides the binary/Reader standard library operation.
  # @param data Array data value.
  # @param options Dict options value.
  # @return Self the initialized object.
  initialize: data, options ->
    self.data = data
    self.offset = 0
    self.endian = self.option_endian(options)

  # Reader.eof? provides the binary/Reader standard library operation.
  # @return Boolean whether the condition is true.
  eof?: ->
    self.remaining() == 0

  # Reader.new provides the binary/Reader standard library operation.
  # @param data Array data value.
  # @param options Dict options value.
  # @return Self the resulting value.
  new: data, options ->
    Reader(data, options)

  # Reader.option_endian provides the binary/Reader standard library operation.
  # @param options Dict options value.
  # @return Any the resulting value.
  option_endian: options ->
    if options == nil
      return "big"
    if options.class != Dict
      raise error("binary.Reader: options must be a dict")
    if not options.has("endian")
      return "big"
    endian = options["endian"]
    if endian != "big" and endian != "little"
      raise error("binary.Reader: invalid endian")
    endian

  # Reader.position provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  position: ->
    self.offset

  # Reader.pow2 provides the binary/Reader standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  pow2: n ->
    out = 1
    i = 0
    while i < n
      out = out * 2
      i = i + 1
    out

  # Reader.pow256 provides the binary/Reader standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  pow256: n ->
    out = 1
    i = 0
    while i < n
      out = out * 256
      i = i + 1
    out

  # Reader.read_bytes provides the binary/Reader standard library operation.
  # @param count Int count value.
  # @return Any the resulting value.
  read_bytes: count ->
    if count == nil or count.class != Number or count < 0
      raise error("binary.Reader.read_bytes: invalid count")
    self.require(count)
    start = self.offset
    self.offset = self.offset + count
    bytes_slice(self.data, start, self.offset)

  # Reader.read_f32 provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_f32: ->
    self.read_float(4, self.endian)

  # Reader.read_f32_be provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_f32_be: ->
    self.read_float(4, "big")

  # Reader.read_f32_le provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_f32_le: ->
    self.read_float(4, "little")

  # Reader.read_f64 provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_f64: ->
    self.read_float(8, self.endian)

  # Reader.read_f64_be provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_f64_be: ->
    self.read_float(8, "big")

  # Reader.read_f64_le provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_f64_le: ->
    self.read_float(8, "little")

  # Reader.read_float provides the binary/Reader standard library operation.
  # @param width Int width value.
  # @param endian Any endian value.
  # @return Any the resulting value.
  read_float: width, endian ->
    self.require(width)
    start = self.offset
    self.offset = self.offset + width
    if width == 4
      return binary_read_f32(self.data, start, endian)
    binary_read_f64(self.data, start, endian)

  # Reader.read_i16 provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  read_i16: ->
    self.signed(self.read_u16(), 16)

  # Reader.read_i16_be provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_i16_be: ->
    self.signed(self.read_u16_be(), 16)

  # Reader.read_i16_le provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_i16_le: ->
    self.signed(self.read_u16_le(), 16)

  # Reader.read_i32 provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  read_i32: ->
    self.signed(self.read_u32(), 32)

  # Reader.read_i32_be provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_i32_be: ->
    self.signed(self.read_u32_be(), 32)

  # Reader.read_i32_le provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_i32_le: ->
    self.signed(self.read_u32_le(), 32)

  # Reader.read_i8 provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  read_i8: ->
    value = self.read_u8()
    if value >= 128
      return value - 256
    value

  # Reader.read_u16 provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  read_u16: ->
    self.read_uint(2, self.endian)

  # Reader.read_u16_be provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_u16_be: ->
    self.read_uint(2, "big")

  # Reader.read_u16_le provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_u16_le: ->
    self.read_uint(2, "little")

  # Reader.read_u32 provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  read_u32: ->
    self.read_uint(4, self.endian)

  # Reader.read_u32_be provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_u32_be: ->
    self.read_uint(4, "big")

  # Reader.read_u32_le provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_u32_le: ->
    self.read_uint(4, "little")

  # Reader.read_u8 provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  read_u8: ->
    self.require(1)
    value = self.data[self.offset]
    self.offset = self.offset + 1
    value

  # Reader.read_uint provides the binary/Reader standard library operation.
  # @param width Int width value.
  # @param endian Any endian value.
  # @return Any the resulting value.
  read_uint: width, endian ->
    self.require(width)
    value = 0
    i = 0
    while i < width
      b = self.data[self.offset + i]
      if endian == "big"
        value = value * 256 + b
      else
        value = value + b * self.pow256(i)
      i = i + 1
    self.offset = self.offset + width
    value

  # Reader.remaining provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  remaining: ->
    self.data.len() - self.offset

  # Reader.require provides the binary/Reader standard library operation.
  # @param count Int count value.
  # @return Any the resulting value.
  require: count ->
    if self.offset + count > self.data.len()
      raise error("binary.Reader: read past end")

  # Reader.seek provides the binary/Reader standard library operation.
  # @param offset Int offset value.
  # @return Any the resulting value.
  seek: offset ->
    if (
      offset == nil or offset.class != Number or offset < 0 or offset > self.data.len()
    )
      raise error("binary.Reader.seek: invalid offset")
    self.offset = offset
    self

  # Reader.signed provides the binary/Reader standard library operation.
  # @param value String value value.
  # @param bits Any bits value.
  # @return Any the resulting value.
  signed: value, bits ->
    limit = self.pow2(bits - 1)
    mod = self.pow2(bits)
    if value >= limit
      return value - mod
    value

  # Reader.skip provides the binary/Reader standard library operation.
  # @param count Int count value.
  # @return Any the resulting value.
  skip: count ->
    if count == nil or count.class != Number or count < 0
      raise error("binary.Reader.skip: invalid count")
    self.seek(self.offset + count)

Instance Variables

data

Reader.data

lib/binary/reader.tya:5

Reader.data stores instance state.

Source
  # Reader.data stores instance state.
  # @type Array
  data: []

endian

Reader.endian

lib/binary/reader.tya:9

Reader.endian stores instance state.

Source
  # Reader.endian stores instance state.
  # @type Nil
  endian: nil

offset

Reader.offset

lib/binary/reader.tya:13

Reader.offset stores instance state.

Source
  # Reader.offset stores instance state.
  # @type Int
  offset: 0

Methods

eof?

Reader.eof?()

lib/binary/reader.tya:26

Reader.eof? provides the binary/Reader standard library operation.

Source
  # Reader.eof? provides the binary/Reader standard library operation.
  # @return Boolean whether the condition is true.
  eof?: ->
    self.remaining() == 0

initialize

Reader.initialize(data, options)

lib/binary/reader.tya:19

Reader.initialize provides the binary/Reader standard library operation.

Source
  # Reader.initialize provides the binary/Reader standard library operation.
  # @param data Array data value.
  # @param options Dict options value.
  # @return Self the initialized object.
  initialize: data, options ->
    self.data = data
    self.offset = 0
    self.endian = self.option_endian(options)

new

Reader.new(data, options)

lib/binary/reader.tya:33

Reader.new provides the binary/Reader standard library operation.

Source
  # Reader.new provides the binary/Reader standard library operation.
  # @param data Array data value.
  # @param options Dict options value.
  # @return Self the resulting value.
  new: data, options ->
    Reader(data, options)

option_endian

Reader.option_endian(options)

lib/binary/reader.tya:39

Reader.option_endian provides the binary/Reader standard library operation.

Source
  # Reader.option_endian provides the binary/Reader standard library operation.
  # @param options Dict options value.
  # @return Any the resulting value.
  option_endian: options ->
    if options == nil
      return "big"
    if options.class != Dict
      raise error("binary.Reader: options must be a dict")
    if not options.has("endian")
      return "big"
    endian = options["endian"]
    if endian != "big" and endian != "little"
      raise error("binary.Reader: invalid endian")
    endian

position

Reader.position()

lib/binary/reader.tya:53

Reader.position provides the binary/Reader standard library operation.

Source
  # Reader.position provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  position: ->
    self.offset

pow2

Reader.pow2(n)

lib/binary/reader.tya:59

Reader.pow2 provides the binary/Reader standard library operation.

Source
  # Reader.pow2 provides the binary/Reader standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  pow2: n ->
    out = 1
    i = 0
    while i < n
      out = out * 2
      i = i + 1
    out

pow256

Reader.pow256(n)

lib/binary/reader.tya:70

Reader.pow256 provides the binary/Reader standard library operation.

Source
  # Reader.pow256 provides the binary/Reader standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  pow256: n ->
    out = 1
    i = 0
    while i < n
      out = out * 256
      i = i + 1
    out

read_bytes

Reader.read_bytes(count)

lib/binary/reader.tya:81

Reader.read_bytes provides the binary/Reader standard library operation.

Source
  # Reader.read_bytes provides the binary/Reader standard library operation.
  # @param count Int count value.
  # @return Any the resulting value.
  read_bytes: count ->
    if count == nil or count.class != Number or count < 0
      raise error("binary.Reader.read_bytes: invalid count")
    self.require(count)
    start = self.offset
    self.offset = self.offset + count
    bytes_slice(self.data, start, self.offset)

read_f32

Reader.read_f32()

lib/binary/reader.tya:91

Reader.read_f32 provides the binary/Reader standard library operation.

Source
  # Reader.read_f32 provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_f32: ->
    self.read_float(4, self.endian)

read_f32_be

Reader.read_f32_be()

lib/binary/reader.tya:96

Reader.read_f32_be provides the binary/Reader standard library operation.

Source
  # Reader.read_f32_be provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_f32_be: ->
    self.read_float(4, "big")

read_f32_le

Reader.read_f32_le()

lib/binary/reader.tya:101

Reader.read_f32_le provides the binary/Reader standard library operation.

Source
  # Reader.read_f32_le provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_f32_le: ->
    self.read_float(4, "little")

read_f64

Reader.read_f64()

lib/binary/reader.tya:106

Reader.read_f64 provides the binary/Reader standard library operation.

Source
  # Reader.read_f64 provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_f64: ->
    self.read_float(8, self.endian)

read_f64_be

Reader.read_f64_be()

lib/binary/reader.tya:111

Reader.read_f64_be provides the binary/Reader standard library operation.

Source
  # Reader.read_f64_be provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_f64_be: ->
    self.read_float(8, "big")

read_f64_le

Reader.read_f64_le()

lib/binary/reader.tya:116

Reader.read_f64_le provides the binary/Reader standard library operation.

Source
  # Reader.read_f64_le provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_f64_le: ->
    self.read_float(8, "little")

read_float

Reader.read_float(width, endian)

lib/binary/reader.tya:123

Reader.read_float provides the binary/Reader standard library operation.

Source
  # Reader.read_float provides the binary/Reader standard library operation.
  # @param width Int width value.
  # @param endian Any endian value.
  # @return Any the resulting value.
  read_float: width, endian ->
    self.require(width)
    start = self.offset
    self.offset = self.offset + width
    if width == 4
      return binary_read_f32(self.data, start, endian)
    binary_read_f64(self.data, start, endian)

read_i16

Reader.read_i16()

lib/binary/reader.tya:133

Reader.read_i16 provides the binary/Reader standard library operation.

Source
  # Reader.read_i16 provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  read_i16: ->
    self.signed(self.read_u16(), 16)

read_i16_be

Reader.read_i16_be()

lib/binary/reader.tya:138

Reader.read_i16_be provides the binary/Reader standard library operation.

Source
  # Reader.read_i16_be provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_i16_be: ->
    self.signed(self.read_u16_be(), 16)

read_i16_le

Reader.read_i16_le()

lib/binary/reader.tya:143

Reader.read_i16_le provides the binary/Reader standard library operation.

Source
  # Reader.read_i16_le provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_i16_le: ->
    self.signed(self.read_u16_le(), 16)

read_i32

Reader.read_i32()

lib/binary/reader.tya:148

Reader.read_i32 provides the binary/Reader standard library operation.

Source
  # Reader.read_i32 provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  read_i32: ->
    self.signed(self.read_u32(), 32)

read_i32_be

Reader.read_i32_be()

lib/binary/reader.tya:153

Reader.read_i32_be provides the binary/Reader standard library operation.

Source
  # Reader.read_i32_be provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_i32_be: ->
    self.signed(self.read_u32_be(), 32)

read_i32_le

Reader.read_i32_le()

lib/binary/reader.tya:158

Reader.read_i32_le provides the binary/Reader standard library operation.

Source
  # Reader.read_i32_le provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_i32_le: ->
    self.signed(self.read_u32_le(), 32)

read_i8

Reader.read_i8()

lib/binary/reader.tya:163

Reader.read_i8 provides the binary/Reader standard library operation.

Source
  # Reader.read_i8 provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  read_i8: ->
    value = self.read_u8()
    if value >= 128
      return value - 256
    value

read_u16

Reader.read_u16()

lib/binary/reader.tya:171

Reader.read_u16 provides the binary/Reader standard library operation.

Source
  # Reader.read_u16 provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  read_u16: ->
    self.read_uint(2, self.endian)

read_u16_be

Reader.read_u16_be()

lib/binary/reader.tya:176

Reader.read_u16_be provides the binary/Reader standard library operation.

Source
  # Reader.read_u16_be provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_u16_be: ->
    self.read_uint(2, "big")

read_u16_le

Reader.read_u16_le()

lib/binary/reader.tya:181

Reader.read_u16_le provides the binary/Reader standard library operation.

Source
  # Reader.read_u16_le provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_u16_le: ->
    self.read_uint(2, "little")

read_u32

Reader.read_u32()

lib/binary/reader.tya:186

Reader.read_u32 provides the binary/Reader standard library operation.

Source
  # Reader.read_u32 provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  read_u32: ->
    self.read_uint(4, self.endian)

read_u32_be

Reader.read_u32_be()

lib/binary/reader.tya:191

Reader.read_u32_be provides the binary/Reader standard library operation.

Source
  # Reader.read_u32_be provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_u32_be: ->
    self.read_uint(4, "big")

read_u32_le

Reader.read_u32_le()

lib/binary/reader.tya:196

Reader.read_u32_le provides the binary/Reader standard library operation.

Source
  # Reader.read_u32_le provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  read_u32_le: ->
    self.read_uint(4, "little")

read_u8

Reader.read_u8()

lib/binary/reader.tya:201

Reader.read_u8 provides the binary/Reader standard library operation.

Source
  # Reader.read_u8 provides the binary/Reader standard library operation.
  # @return Int the resulting value.
  read_u8: ->
    self.require(1)
    value = self.data[self.offset]
    self.offset = self.offset + 1
    value

read_uint

Reader.read_uint(width, endian)

lib/binary/reader.tya:211

Reader.read_uint provides the binary/Reader standard library operation.

Source
  # Reader.read_uint provides the binary/Reader standard library operation.
  # @param width Int width value.
  # @param endian Any endian value.
  # @return Any the resulting value.
  read_uint: width, endian ->
    self.require(width)
    value = 0
    i = 0
    while i < width
      b = self.data[self.offset + i]
      if endian == "big"
        value = value * 256 + b
      else
        value = value + b * self.pow256(i)
      i = i + 1
    self.offset = self.offset + width
    value

remaining

Reader.remaining()

lib/binary/reader.tya:227

Reader.remaining provides the binary/Reader standard library operation.

Source
  # Reader.remaining provides the binary/Reader standard library operation.
  # @return Any the resulting value.
  remaining: ->
    self.data.len() - self.offset

require

Reader.require(count)

lib/binary/reader.tya:233

Reader.require provides the binary/Reader standard library operation.

Source
  # Reader.require provides the binary/Reader standard library operation.
  # @param count Int count value.
  # @return Any the resulting value.
  require: count ->
    if self.offset + count > self.data.len()
      raise error("binary.Reader: read past end")

seek

Reader.seek(offset)

lib/binary/reader.tya:240

Reader.seek provides the binary/Reader standard library operation.

Source
  # Reader.seek provides the binary/Reader standard library operation.
  # @param offset Int offset value.
  # @return Any the resulting value.
  seek: offset ->
    if (
      offset == nil or offset.class != Number or offset < 0 or offset > self.data.len()
    )
      raise error("binary.Reader.seek: invalid offset")
    self.offset = offset
    self

signed

Reader.signed(value, bits)

lib/binary/reader.tya:252

Reader.signed provides the binary/Reader standard library operation.

Source
  # Reader.signed provides the binary/Reader standard library operation.
  # @param value String value value.
  # @param bits Any bits value.
  # @return Any the resulting value.
  signed: value, bits ->
    limit = self.pow2(bits - 1)
    mod = self.pow2(bits)
    if value >= limit
      return value - mod
    value

skip

Reader.skip(count)

lib/binary/reader.tya:262

Reader.skip provides the binary/Reader standard library operation.

Source
  # Reader.skip provides the binary/Reader standard library operation.
  # @param count Int count value.
  # @return Any the resulting value.
  skip: count ->
    if count == nil or count.class != Number or count < 0
      raise error("binary.Reader.skip: invalid count")
    self.seek(self.offset + count)