Module: RuneRb::IO::NativeReadable

Defined in:
lib/rrb/io/native_readable.rb

Overview

Provides functions to read integer values from a string-based buffer using native ruby methods.

Since:

  • 0.0.1

Instance Method Summary collapse

Instance Method Details

#read_byte(mutation: :STD, signed: false) ⇒ Integer

Read a byte value from the Buffer#data

Parameters:

  • signed (Boolean) (defaults to: false)

    should the value be signed.

  • mutation (Symbol) (defaults to: :STD)

    mutation that should be applied to the byte value.

Returns:

  • (Integer)

Since:

  • 0.0.1



10
11
12
# File 'lib/rrb/io/native_readable.rb', line 10

def read_byte(mutation: :STD, signed: false)
  mutate(@data.slice!(0)&.unpack1(signed ? 'c' : 'C') || 0, mutation)
end

#read_int(signed: false, mutation: :STD, order: :BIG) ⇒ Integer

Reads a integer value from the Buffer#data

Parameters:

  • signed (Boolean) (defaults to: false)

    should the value be signed.

  • mutation (Symbol) (defaults to: :STD)

    mutation that should be applied to the integer value

  • order (Symbol) (defaults to: :BIG)

    they byte order to read the integer value

Returns:

  • (Integer)

Since:

  • 0.0.1



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rrb/io/native_readable.rb', line 61

def read_int(signed: false, mutation: :STD, order: :BIG)
  val = 0
  case order
  when :BIG
    val += mutate(@data.slice!(0..3).unpack1(signed ? 'i>' : 'I>'), mutation)
  when :MIDDLE
    val += read_byte(signed: signed) << 8
    val += read_byte(signed: signed, mutation: mutation)
    val += read_byte(signed: signed) << 24
    val += read_byte(signed: signed) << 16
    return val
  when :INVERSE_MIDDLE
    val += read_byte(signed: signed) << 16
    val += read_byte(signed: signed) << 24
    val += read_byte(signed: signed, mutation: mutation)
    val += read_byte(signed: signed) << 8
    return val
  when :LITTLE
    val += mutate(@data.slice!(0..3).unpack1(signed ? 'i<' : 'I<'), mutation)
  else read_int(signed: signed, mutation: mutation, order: :BIG)
  end
  val
end

#read_long(signed: false, mutation: :STD, order: :BIG) ⇒ Integer

Reads a long value from the Buffer#data

Parameters:

  • signed (Boolean) (defaults to: false)

    should the value be signed.

  • mutation (Symbol) (defaults to: :STD)

    mutation that should be applied to the long value

  • order (Symbol) (defaults to: :BIG)

    they byte order to read the long value

Returns:

  • (Integer)

Since:

  • 0.0.1



90
91
92
93
94
95
96
97
98
# File 'lib/rrb/io/native_readable.rb', line 90

def read_long(signed: false, mutation: :STD, order: :BIG)
  case order
  when :BIG
    mutate(@data.slice!(0..7).unpack1(signed ? 'q>' : 'Q>'), mutation)
  when :LITTLE
    mutate(@data.slice!(0..7).unpack1(signed ? 'q<' : 'Q<'), mutation)
  else read_long(signed: signed, mutation: mutation, order: :BIG)
  end
end

#read_medium(signed: false, mutation: :STD, order: :BIG) ⇒ Integer

Reads a medium value from the Buffer#data

Parameters:

  • signed (Boolean) (defaults to: false)

    should the value be signed.

  • mutation (Symbol) (defaults to: :STD)

    mutation that should be applied to the medium value

  • order (Symbol) (defaults to: :BIG)

    they byte order to read the medium value

Returns:

  • (Integer)

Since:

  • 0.0.1



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rrb/io/native_readable.rb', line 36

def read_medium(signed: false, mutation: :STD, order: :BIG)
  val = 0
  case order
  when :BIG
    val += read_byte(signed: signed) << 16
    val += read_byte(signed: signed) << 8
    val += read_byte(signed: signed, mutation: mutation)
  when :MIDDLE
    val += read_byte(signed: signed) << 8
    val += read_byte(signed: signed, mutation: mutation)
    val += read_byte(signed: signed) << 16
  when :LITTLE
    val += read_byte(signed: signed, mutation: mutation)
    val += read_byte(signed: signed) << 8
    val += read_byte(signed: signed) << 16
  else read_medium(signed: signed, mutation: mutation, order: :BIG)
  end
  val
end

#read_short(signed: false, mutation: :STD, order: :BIG) ⇒ Integer

Reads a short value from the Buffer#data

Parameters:

  • signed (Boolean) (defaults to: false)

    should the value be signed.

  • mutation (Symbol) (defaults to: :STD)

    mutation that should be applied to the short value

  • order (Symbol) (defaults to: :BIG)

    they byte order to read the short value

Returns:

  • (Integer)

Since:

  • 0.0.1



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rrb/io/native_readable.rb', line 19

def read_short(signed: false, mutation: :STD, order: :BIG)
  val = 0
  case order
  when :BIG
    val += mutate(@data.slice!(0..1).unpack1(signed ? 's>' : 'S>'), mutation)
  when :LITTLE
    val += mutate(@data.slice!(0..1).unpack1(signed ? 's<' : 'S<'), mutation)
  else read_short(signed: signed, mutation: mutation, order: :BIG)
  end
  val
end