lib/bencodr/parser.rb in bencodr-3.0.1 vs lib/bencodr/parser.rb in bencodr-3.0.2
- old
+ new
@@ -33,12 +33,15 @@
# BEncodr::Parser.parse_string(scanner) #=> "string"
#
# @param [StringScanner] scanner the scanner of a bencoded string
# @return [::String] the parsed string
def parse_string(scanner)
- length = scanner.scan(/[1-9][0-9]*|0/) or raise BEncodeError, "Invalid string: length invalid. #{scanner.pos}"
+ length = scanner.scan(/[1-9][0-9]*|0/).to_i or raise BEncodeError, "Invalid string: length invalid. #{scanner.pos}"
scanner.scan(/:/) or raise BEncodeError, "Invalid string: missing colon(:). #{scanner.pos}"
- byte_string = scanner.scan(/.{#{length}}/m) or raise BEncodeError, "Invalid string: length too long(#{length}) #{scanner.pos}."
+ byte_string = scanner.peek(length)
+ byte_string.length == length or raise BEncodeError, "Invalid string: length too long(#{length}) #{scanner.pos}."
+ scanner.pos = scanner.pos + length
+
if RUBY_VERSION =~ /1\.9/
byte_string.encode('UTF-8') rescue byte_string.force_encoding('UTF-8')
else
byte_string
end
\ No newline at end of file