Class: JSON::Expect::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/json/expect/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(input, size) ⇒ Buffer

Returns a new instance of Buffer.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/json/expect/parser.rb', line 7

def initialize(input, size)
  unless 0 < size
    raise ArgumentError, "negative buffer_size"
  end
  @io = case input
  when String
    StringIO.new(input)
  when IO, StringIO
    input
  else
    raise ArgumentError, "#{input.class} is not supported"
  end
  @string = String.new
  @index = 0
  @size = size
end

Instance Method Details

#back(len = 1) ⇒ Object



50
51
52
# File 'lib/json/expect/parser.rb', line 50

def back(len = 1)
  @index -= len
end

#fetch(len = 0) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/json/expect/parser.rb', line 24

def fetch(len = 0)
  if str = @io.read(@size + len)
    @string = @string[@index..-1] << str
    @index = 0
  else
    nil
  end
end

#next(len = 1) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/json/expect/parser.rb', line 33

def next(len = 1)
  if @string.length - @index < len
    if str = @io.read(@size + len)
      @string = @string[@index..-1] << str
      @index = 0
    end
  end

  if @string.length <= @index
    nil
  else
    r = @string[@index, len]
    @index += r.length
    r
  end
end

#next_without_whitespaceObject



63
64
65
66
67
68
69
70
71
# File 'lib/json/expect/parser.rb', line 63

def next_without_whitespace
  while ch = self.next
    case ch
    when " ", "\t", "\n", "\r"
    else
      return ch
    end
  end
end

#rewindObject



73
74
75
76
77
# File 'lib/json/expect/parser.rb', line 73

def rewind
  @string.clear
  @index = 0
  @io.rewind
end

#scan(regexp) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/json/expect/parser.rb', line 54

def scan(regexp)
  if m = regexp.match(@string, @index)
    @index += m[0].length
    m[0]
  else
    nil
  end
end