1 # This is a more or less straight translation of PyYAML3000 to Ruby
3 # the big difference in this implementation is that unicode support is not here...
10 # - checks if characters are in allowed range,
11 # - adds '\0' to the end.
14 # - a duck-typed IO object
16 def initialize_reader(stream)
30 @name = stream.respond_to?(:path) ? stream.path : stream.inspect
37 update(index+1) if @pointer+index+1 >= @buffer.length
38 @buffer[@pointer+index]
42 update(length) if @pointer+length >= @buffer.length
43 @buffer[@pointer...@pointer+length]
47 update(length+1) if @pointer+length+1 >= @buffer.length
49 ch = @buffer[@pointer]
52 if "\n\x85".include?(ch) || (ch == ?\r && @buffer[@pointer+1] != ?\n)
63 Mark.new(@name,@index,@line,@column,@buffer,@pointer)
65 Mark.new(@name,@index,@line,@column,nil,nil)
69 NON_PRINTABLE = /[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\xFF]/
70 def check_printable(data)
71 if NON_PRINTABLE =~ data
72 position = @index+@buffer.length-@pointer+($~.offset(0)[0])
73 raise ReaderError.new(@name, position, $&,"unicode","special characters are not allowed"),"special characters are not allowed"
78 return if @raw_buffer.nil?
79 @buffer = @buffer[@pointer..-1]
81 while @buffer.length < length
86 converted = data.length
89 @raw_buffer = @raw_buffer[converted..-1]
98 def update_raw(size=1024)
99 data = @stream.read(size)
100 if data && !data.empty?
102 @stream_pointer += data.length
109 class ReaderError < YAMLError
110 def initialize(name, position, character, encoding, reason)
113 @character = character
119 if String === @character
120 "'#{@encoding}' codec can't decode byte #x%02x: #{@reason}\n in \"#{@name}\", position #{@position}" % @character.to_i
122 "unacceptable character #x%04x: #{@reason}\n in \"#{@name}\", position #{@position}" % @character.to_i