From e921ea2d78f693c57fcb289faff3fc126dc8617a Mon Sep 17 00:00:00 2001 From: why Date: Tue, 10 Jun 2003 14:15:27 +0000 Subject: * ext/syck/token.c: preserve newlines prepended to a block. * ext/syck/implicit.c (syck_match_implicit): added !merge and !default. * lib/yaml/constants.rb: remove '\z' escape. * lib/yaml/emitter.rb: ensure reset of @seq_map shortcut flag. * lib/yaml/encoding.rb: remove Unicode translation methods. * lib/yaml/rubytypes.rb: improved round-tripping of Strings. [ruby-core:1134] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3937 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/yaml/constants.rb | 12 ++----- lib/yaml/emitter.rb | 6 +++- lib/yaml/encoding.rb | 96 +-------------------------------------------------- lib/yaml/rubytypes.rb | 11 +++--- 4 files changed, 15 insertions(+), 110 deletions(-) (limited to 'lib') diff --git a/lib/yaml/constants.rb b/lib/yaml/constants.rb index 21aee7e0a6..fb833d3077 100644 --- a/lib/yaml/constants.rb +++ b/lib/yaml/constants.rb @@ -15,19 +15,19 @@ module YAML WORD_CHAR = 'A-Za-z0-9' PRINTABLE_CHAR = '-_A-Za-z0-9!?/()$\'". ' NOT_PLAIN_CHAR = '\x7f\x0-\x1f\x80-\x9f' - ESCAPE_CHAR = '[\\x00-\\x08\\x0b-\\x0d\\x0e-\\x1f]' + ESCAPE_CHAR = '[\\x00-\\x09\\x0b-\\x1f]' INDICATOR_CHAR = '*&!|\\\\^@%{}[]=' SPACE_INDICATORS = '-#:,?' RESTRICTED_INDICATORS = '#:,}]' DNS_COMP_RE = "\\w(?:[-\\w]*\\w)?" DNS_NAME_RE = "(?:(?:#{DNS_COMP_RE}\\.)+#{DNS_COMP_RE}|#{DNS_COMP_RE})" - ESCAPES = %w{\z \x01 \x02 \x03 \x04 \x05 \x06 \a + ESCAPES = %w{\x00 \x01 \x02 \x03 \x04 \x05 \x06 \a \x08 \t \n \v \f \r \x0e \x0f \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17 \x18 \x19 \x1a \e \x1c \x1d \x1e \x1f } UNESCAPES = { - 'z' => "\x00", 'a' => "\x07", 'b' => "\x08", 't' => "\x09", + 'a' => "\x07", 'b' => "\x08", 't' => "\x09", 'n' => "\x0a", 'v' => "\x0b", 'f' => "\x0c", 'r' => "\x0d", 'e' => "\x1b", '\\' => '\\', } @@ -41,11 +41,5 @@ module YAML :WidthType => 'absolute', :BestWidth => 80, :UseBlock => false, :UseFold => false, :Encoding => :None } - TRANSFER_DOMAINS = { - 'yaml.org,2002' => {}, - 'ruby.yaml.org,2002' => {} - } - PRIVATE_TYPES = {} - IMPLICIT_TYPES = [ 'null', 'bool', 'time', 'int', 'float' ] end diff --git a/lib/yaml/emitter.rb b/lib/yaml/emitter.rb index 4b8541c2c3..66c7a6e813 100644 --- a/lib/yaml/emitter.rb +++ b/lib/yaml/emitter.rb @@ -60,6 +60,7 @@ module YAML # Emit plain, normal flowing text # def node_text( value, block = '>' ) + @seq_map = false valx = value.dup if @options[:UseBlock] block = '|' @@ -89,6 +90,7 @@ module YAML # Emit a simple, unqouted string # def simple( value ) + @seq_map = false self << value.to_s end @@ -168,6 +170,7 @@ module YAML # if val.length.zero? self << "{}" + @seq_map = false else if @buffer.length == 1 and @options[:UseHeader] == false and type.length.zero? @headless = 1 @@ -213,6 +216,7 @@ module YAML # Quick sequence # def seq( type, &e ) + @seq_map = false val = Sequence.new e.call( val ) self << "#{type} " if type.length.nonzero? @@ -282,7 +286,7 @@ module YAML @buffer.push( "" ) #p [ self.id, @level, :END ] if @level < 0 - YAML.internal_to_utf( header + @buffer.to_s[@headless..-1], @options[:Encoding] ) + header + @buffer.to_s[@headless..-1] end end end diff --git a/lib/yaml/encoding.rb b/lib/yaml/encoding.rb index 59491b53b7..e361163ac6 100644 --- a/lib/yaml/encoding.rb +++ b/lib/yaml/encoding.rb @@ -4,100 +4,6 @@ module YAML - # - # Encodings ( $-K to ICONV ) - # - CHARSETS = { - 'NONE' => 'LATIN1', - 'ASCII' => 'US-ASCII', - 'UTF-8' => 'UTF-8', - 'EUC' => 'EUC-JP', - 'SJIS' => 'SHIFT-JIS' - } - - # - # YAML documents can be in UTF-8, UTF-16 or UTF-32 - # So let's read and write in Unicode - # - - @@unicode = false - begin - require 'iconv' - DEFAULTS[:Encoding] = :Utf8 - rescue LoadError - end - - def YAML.unicode; @@unicode; end - def YAML.unicode=( bool ); @@unicode = bool; end - - # - # Unicode conversion - # - - def YAML.utf_to_internal( str, from_enc ) - return unless str - to_enc = CHARSETS[$-K] - case from_enc - when :Utf32 - Iconv.iconv( to_enc, 'UTF-32', str )[0] - when :Utf16 - Iconv.iconv( to_enc, 'UTF-16', str )[0] - when :Utf8 - Iconv.iconv( to_enc, 'UTF-8', str )[0] - when :None - str - else - raise YAML::Error, ERROR_UNSUPPORTED_ENCODING % from_enc.inspect - end - end - - def YAML.internal_to_utf( str, to_enc ) - return unless str - from_enc = CHARSETS[$-K] - case to_enc - when :Utf32 - Iconv.iconv( 'UTF-32', from_enc, str )[0] - when :Utf16 - Iconv.iconv( 'UTF-16', from_enc, str )[0] - when :Utf8 - Iconv.iconv( 'UTF-8', from_enc, str )[0] - when :None - str - else - raise YAML::Error, ERROR_UNSUPPORTED_ENCODING % to_enc.inspect - end - end - - def YAML.sniff_encoding( str ) - unless YAML::unicode - :None - else - case str - when /^\x00\x00\xFE\xFF/ # UTF-32 - :Utf32 - when /^\xFE\xFF/ # UTF-32BE - :Utf16 - else - :Utf8 - end - end - end - - def YAML.enc_separator( enc ) - case enc - when :Utf32 - "\000\000\000\n" - when :Utf16 - "\000\n" - when :Utf8 - "\n" - when :None - "\n" - else - raise YAML::Error, ERROR_UNSUPPORTED_ENCODING % enc.inspect - end - end - # # Escape the string, condensing common escapes # @@ -109,7 +15,7 @@ module YAML # Unescape the condenses escapes # def YAML.unescape( value ) - value.gsub( /\\(?:([nevbr\\fartz])|0?x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ ) { |x| + value.gsub( /\\(?:([nevfbart\\])|0?x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ ) { |x| if $3 ["#$3".hex ].pack('U*') elsif $2 diff --git a/lib/yaml/rubytypes.rb b/lib/yaml/rubytypes.rb index 92aee82dae..b2ac615849 100644 --- a/lib/yaml/rubytypes.rb +++ b/lib/yaml/rubytypes.rb @@ -295,19 +295,20 @@ class String if complex if self.is_binary_data? out.binary_base64( self ) + elsif self =~ /^ |#{YAML::ESCAPE_CHAR}| $/ + complex = false else out.node_text( self ) end - else + end + if not complex ostr = if out.options[:KeepValue] self elsif empty? "''" - elsif YAML.detect_implicit( self ) != 'str' + elsif self =~ /^[^#{YAML::WORD_CHAR}]|#{YAML::ESCAPE_CHAR}|[#{YAML::SPACE_INDICATORS}]( |$)| $|\n|\'/ "\"#{YAML.escape( self )}\"" - elsif self =~ /#{YAML::ESCAPE_CHAR}|[#{YAML::SPACE_INDICATORS}] |\n|\'/ - "\"#{YAML.escape( self )}\"" - elsif self =~ /^[^#{YAML::WORD_CHAR}]/ + elsif YAML.detect_implicit( self ) != 'str' "\"#{YAML.escape( self )}\"" else self -- cgit v1.2.3