summaryrefslogtreecommitdiff
path: root/lib/mpeg/input.rb
blob: 7fd185b7c34b9be5b461797db358431b4a53a3de (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
module Mpeg
  class Input
    def initialize(string)
      @scanner = StringScanner.new(string)
    end

    def matches?(pattern)
      @scanner.match?(pattern)
    end

    def consume(characters)
      position = @scanner.pos
      slice = @scanner.scan(/(.|$){#{characters}}/m)
      Slice.new(position, slice)
    end

    def rewind
      transaction do |_|
        yield
      end
    end

    def transaction(&block)
      Transaction.new(@scanner).run(&block)
    end

    def end_of_string?
      @scanner.eos?
    end

    def position
      @scanner.pos
    end
  end
end