aboutsummaryrefslogtreecommitdiffstats
path: root/lib/plum/binary_string.rb
blob: 8011ea65b3e495f12f6bb426a4624f060bd59eef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Plum
  module BinaryString
    refine String do
      def uint8(pos = 0)
        byteslice(pos, 1).unpack("C")[0]
      end

      def uint16(pos = 0)
        byteslice(pos, 2).unpack("n")[0]
      end

      def uint24(pos = 0)
        (uint16(pos) << 8) | uint8(pos + 2)
      end

      def uint32(pos = 0)
        byteslice(pos, 4).unpack("N")[0]
      end

      def push_uint8(val)
        self << [val].pack("C")
      end

      def push_uint16(val)
        self << [val].pack("n")
      end

      def push_uint24(val)
        push_uint16(val >> 8)
        push_uint8(val & ((1 << 8) - 1))
      end

      def push_uint32(val)
        self << [val].pack("N")
      end

      alias push <<

      def shift(count)
        enc = self.encoding
        force_encoding(Encoding::BINARY)
        out = slice!(0, count)
        force_encoding(enc)
        out
      end
    end
  end
end