Module: Bencoding::Parser
Constant Summary collapse
- DICTIONARY_TOKEN =
d # :nodoc:
100- LIST_TOKEN =
l # :nodoc:
108- INTEGER_TOKEN =
i # :nodoc:
105- TERMINATOR_TOKEN =
e # :nodoc:
101- SEPERATOR_TOKEN =
: # :nodoc:
58
Instance Method Summary collapse
- #load(io) ⇒ Object
-
#parse_anytype(io, typechar = nil) ⇒ Object
:nodoc:.
-
#parse_dictionary(io) ⇒ Object
:nodoc:.
-
#parse_integer(io, terminator = TERMINATOR_TOKEN) ⇒ Object
:nodoc:.
-
#parse_list(io) ⇒ Object
:nodoc:.
-
#parse_string(io) ⇒ Object
:nodoc:.
Instance Method Details
#load(io) ⇒ Object
10 11 12 |
# File 'lib/bencoding/parser.rb', line 10 def load(io) parse_anytype(io) end |
#parse_anytype(io, typechar = nil) ⇒ Object
:nodoc:
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/bencoding/parser.rb', line 14 def parse_anytype(io, typechar=nil) # :nodoc: typechar ||= io.getc case typechar when DICTIONARY_TOKEN then parse_dictionary(io) when LIST_TOKEN then parse_list(io) when INTEGER_TOKEN then parse_integer(io) else io.ungetc typechar parse_string(io) end end |
#parse_dictionary(io) ⇒ Object
:nodoc:
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bencoding/parser.rb', line 26 def parse_dictionary(io) # :nodoc: dictionary = ::Hash.new until (c = io.getc) == TERMINATOR_TOKEN io.ungetc c key = parse_string(io) val = parse_anytype(io) dictionary[key] = val end dictionary end |
#parse_integer(io, terminator = TERMINATOR_TOKEN) ⇒ Object
:nodoc:
46 47 48 49 50 51 52 |
# File 'lib/bencoding/parser.rb', line 46 def parse_integer(io, terminator=TERMINATOR_TOKEN) # :nodoc: integer_string = "" until (c = io.getc) == terminator integer_string << c.chr end integer_string.to_i end |
#parse_list(io) ⇒ Object
:nodoc:
37 38 39 40 41 42 43 44 |
# File 'lib/bencoding/parser.rb', line 37 def parse_list(io) # :nodoc: list = ::Array.new until (c = io.getc) == TERMINATOR_TOKEN val = parse_anytype(io, c) list << val end list end |
#parse_string(io) ⇒ Object
:nodoc:
54 55 56 57 |
# File 'lib/bencoding/parser.rb', line 54 def parse_string(io) # :nodoc: length = parse_integer(io, SEPERATOR_TOKEN) io.read(length) end |