[yaml] reducing Hash in Array
From:
nobu.nokada@...
Date:
2004-07-05 05:43:10 UTC
List:
ruby-core #3131
Moin,
Is the latter better than the former, isn't it?
$ ruby -v -ryaml -e 'y [{"foo"=>10}]'
ruby 1.9.0 (2004-07-03) [i686-linux]
---
-
foo: 10
$ ./ruby -ryaml -e 'y [{"foo"=>10}]'
---
- foo: 10
And some cleanups.
* YAML::BaseEmitter#options doesn't allow @options to override
default values with false.
* Regexp with constants can be suffixed by 'o'.
* String#is_binary_data? always returns false unless it contains
only non-printable characters.
BTW, isn't this result curious?
$ ruby -ryaml -e 'puts s="---\n>\n foo";p YAML.load(s)'
---
>
foo
"foo\000"
Index: lib/yaml/baseemitter.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/lib/yaml/baseemitter.rb,v
retrieving revision 1.5
diff -U2 -p -d -w -r1.5 baseemitter.rb
--- lib/yaml/baseemitter.rb 25 May 2004 14:57:25 -0000 1.5
+++ lib/yaml/baseemitter.rb 5 Jul 2004 05:27:08 -0000
@@ -1,2 +1,3 @@
+# -*- mode: ruby; ruby-indent-level: 4 -*- vim: sw=4
#
# BaseEmitter
@@ -13,5 +14,5 @@ module YAML
def options( opt = nil )
if opt
- @options[opt] || YAML::DEFAULTS[opt]
+ @options.fetch(opt) {YAML::DEFAULTS[opt]}
else
@options
@@ -41,5 +42,5 @@ module YAML
if options(:UseBlock)
'|'
- elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{YAML::ESCAPE_CHAR}/
+ elsif not options(:UseFold) and /\n[ \t]/ =~ valx and /#{YAML::ESCAPE_CHAR}/o !~ valx
'|'
else
@@ -48,19 +49,18 @@ module YAML
indt = $&.to_i if block =~ /\d+/
- if valx =~ /(\A[ \t#]|^---\s+)/
+ if /(\A[ \t\#]|^---\s+)/ =~ valx
indt = options(:Indent) unless indt.to_i > 0
block += indt.to_s
end
- block +=
- if valx =~ /\n\Z\n/
- "+"
- elsif valx =~ /\Z\n/
- ""
+ case valx
+ when /\n\Z\n/
+ block << "+"
+ when /\Z\n/
else
- "-"
+ block << "-"
end
end
- if valx =~ /#{YAML::ESCAPE_CHAR}/
+ if /#{YAML::ESCAPE_CHAR}/o =~ valx
valx = YAML::escape( valx )
end
@@ -139,15 +139,15 @@ module YAML
val = Mapping.new
e.call( val )
- self << "#{type} " if type.length.nonzero?
+ self << "#{type} " unless type.empty?
#
# Empty hashes
#
- if val.length.zero?
+ if val.empty?
self << "{}"
@seq_map = false
else
# FIXME
- # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
+ # if @buffer.length == 1 and options(:UseHeader) == false and type.empty?
# @headless = 1
# end
@@ -163,6 +163,6 @@ module YAML
# Emit the key and value
#
- val.each { |v|
- seq_map_shortcut
+ val.each_with_index { |v, i|
+ seq_map_shortcut if i.nonzero?
if v[0].is_complex_yaml?
self << "? "
@@ -197,14 +197,14 @@ module YAML
val = Sequence.new
e.call( val )
- self << "#{type} " if type.length.nonzero?
+ self << "#{type} " unless type.empty?
#
# Empty arrays
#
- if val.length.zero?
+ if val.empty?
self << "[]"
else
# FIXME
- # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero?
+ # if @buffer.length == 1 and options(:UseHeader) == false and type.empty?
# @headless = 1
# end
Index: lib/yaml/basenode.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/lib/yaml/basenode.rb,v
retrieving revision 1.2
diff -U2 -p -d -w -r1.2 basenode.rb
--- lib/yaml/basenode.rb 30 May 2003 23:09:54 -0000 1.2
+++ lib/yaml/basenode.rb 5 Jul 2004 05:03:02 -0000
@@ -149,5 +149,5 @@ module YAML
case pred
when /^\.=/
- pred = $'
+ pred = Regexp.last_match.post_match
match_nodes.reject! { |n|
n.last.value != pred
Index: lib/yaml/rubytypes.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/lib/yaml/rubytypes.rb,v
retrieving revision 1.24
diff -U2 -p -d -w -r1.24 rubytypes.rb
--- lib/yaml/rubytypes.rb 25 May 2004 14:57:25 -0000 1.24
+++ lib/yaml/rubytypes.rb 5 Jul 2004 05:07:48 -0000
@@ -288,5 +288,5 @@ class String
end
def is_binary_data?
- ( self.count( "^ -~", "^\r\n" ) / self.size > 0.3 || self.count( "\x00" ) > 0 )
+ ( self.count( "^ -~", "^\r\n" ).to_f / self.size > 0.3 || self.count( "\x00" ) > 0 )
end
def to_yaml_type
@@ -327,5 +327,5 @@ class String
elsif empty?
"''"
- elsif self =~ /^[^#{YAML::WORD_CHAR}\/]| \#|#{YAML::ESCAPE_CHAR}|[#{YAML::SPACE_INDICATORS}]( |$)| $|\n|\'/
+ elsif self =~ /^[^#{YAML::WORD_CHAR}\/]| \#|#{YAML::ESCAPE_CHAR}|[#{YAML::SPACE_INDICATORS}]( |$)| $|\n|\'/o
"\"#{YAML.escape( self )}\""
elsif YAML.detect_implicit( self ) != 'str'
Index: lib/yaml/ypath.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/lib/yaml/ypath.rb,v
retrieving revision 1.2
diff -U2 -p -d -w -r1.2 ypath.rb
--- lib/yaml/ypath.rb 10 May 2003 09:43:54 -0000 1.2
+++ lib/yaml/ypath.rb 5 Jul 2004 05:20:36 -0000
@@ -1,2 +1,3 @@
+# -*- mode: ruby; ruby-indent-level: 4 -*- vim: sw=4
#
# YAML::YPath
@@ -11,13 +12,14 @@ module YAML
@predicates = []
@flags = nil
- while str =~ /^\/?(\/|[^\/\[]+)(?:\[([^\]]+)\])?/
+ pos = 0
+ while pos = str.index(/\G\/?(\/|[^\/\[]+)(?:\[([^\]]+)\])?/, pos)
@segments.push $1
@predicates.push $2
- str = $'
+ pos = $~.end
end
- unless str.to_s.empty?
- @segments += str.split( "/" )
+ if pos < str.size
+ @segments.concat str[pos..-1].split( "/" )
end
- if @segments.length == 0
+ if @segments.empty?
@segments.push "."
end
--
Nobu Nakada