Sha256: c77a4ff4ea442429cd65f5eb8c7b6d7e8698ebf8a426f7113c4459c243a1ba13

Contents?: true

Size: 768 Bytes

Versions: 1

Compression:

Stored size: 768 Bytes

Contents

class Parser
  attr_reader :scanner

  def initialize(string)
    @scanner = StringScanner.new(string)
  end

  def parse
    return parse_string     if scanner.scan(/\d+/)
    return parse_integer    if scanner.scan(/i/)
    return parse_list       if scanner.scan(/l/)
    return parse_dictionary if scanner.scan(/d/)

    raise Fossyl::InvalidBencoding
  end

  private
  def parse_string
    length = scanner.matched.to_i
    scanner.getch # Skip the ':'
    length.times.map { scanner.getch }.join
  end

  def parse_integer
    scanner.scan_until(/e/).chop.to_i
  end

  def parse_list
    list = []
    list << parse until scanner.scan(/e/)
    list
  end

  def parse_dictionary
    hash = {}
    hash[parse] = parse until scanner.scan(/e/)
    hash
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fossyl-0.5.0 lib/fossyl/parser.rb