class JSON::Expect::Buffer

Public Class Methods

new(input, size) click to toggle source
# 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

Public Instance Methods

back(len = 1) click to toggle source
# File lib/json/expect/parser.rb, line 50
def back(len = 1)
  @index -= len
end
fetch(len = 0) click to toggle source
# 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) click to toggle source
# 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_whitespace() click to toggle source
# 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
rewind() click to toggle source
# File lib/json/expect/parser.rb, line 73
def rewind
  @string.clear
  @index = 0
  @io.rewind
end
scan(regexp) click to toggle source
# 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