lib/bencodr/parser.rb in bencodr-2.0.1 vs lib/bencodr/parser.rb in bencodr-3.0.0

- old
+ new

@@ -33,12 +33,13 @@ # 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}" - scanner.scan(/:/) or raise BEncodeError, "Invalid string: missing colon(:). #{scanner.pos}" - scanner.scan(/.{#{length}}/m) or raise BEncodeError, "Invalid string: length too long(#{length}) #{scanner.pos}." + length = scanner.scan(/[1-9][0-9]*|0/) 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.encode('UTF-8') rescue byte_string.force_encoding('UTF-8') end # This method parases a bencoded integer. # # scanner = StringScanner.new("i1e") \ No newline at end of file