diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2009-10-22 17:50:43 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2009-10-22 17:50:43 -0700 |
commit | 35fb005e028b42faff1ec0f01d84ed88d155df06 (patch) | |
tree | e2ed25d2feabf2614aed5b300efea297baa748df /lib | |
parent | 3854df000780465f6e78b084732d5ab324be941e (diff) | |
download | psych-35fb005e028b42faff1ec0f01d84ed88d155df06.zip |
speed improvement
Diffstat (limited to 'lib')
-rw-r--r-- | lib/psych/scalar_scanner.rb | 17 | ||||
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 54 |
2 files changed, 41 insertions, 30 deletions
diff --git a/lib/psych/scalar_scanner.rb b/lib/psych/scalar_scanner.rb index a15096f..f6412eb 100644 --- a/lib/psych/scalar_scanner.rb +++ b/lib/psych/scalar_scanner.rb @@ -15,6 +15,17 @@ module Psych return [:NULL, nil] if @string.empty? case @string + when /^[A-Za-z~]/ + case @string + when /^(null|~)$/i + [:NULL, nil] + when /^(y|yes|true|on)$/i + [:BOOLEAN, true] + when /^(n|no|false|off)$/i + [:BOOLEAN, false] + else + [:SCALAR, @string] + end when TIME [:TIME, @string] when /^\d{4}-\d{1,2}-\d{1,2}$/ @@ -25,12 +36,6 @@ module Psych [:NEGATIVE_INFINITY, -1 / 0.0] when /^\.nan$/i [:NAN, 0.0 / 0.0] - when /^(null|~)$/i - [:NULL, nil] - when /^(y|yes|true|on)$/i - [:BOOLEAN, true] - when /^(n|no|false|off)$/i - [:BOOLEAN, false] when /^:.+/i [:SYMBOL, @string.sub(/^:/, '').to_sym] when /^[-+]?[1-9][0-9_]*(:[0-5]?[0-9])+$/ diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index 66fefef..dd8805b 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -28,6 +28,7 @@ module Psych @st[o.anchor] = o.value if o.anchor return o.value if o.quoted + return resolve_unknown(o) unless o.tag case o.tag when '!binary', 'tag:yaml.org,2002:binary' @@ -61,30 +62,7 @@ module Psych args.push(args.delete_at(1) == '...') Range.new(*args) else - token = ScalarScanner.new(o.value).tokenize - - case token.first - when :DATE - require 'date' - Date.strptime token.last, '%Y-%m-%d' - when :TIME - lexeme = token.last - - date, time = *(lexeme.split(/[ tT]/, 2)) - (yy, m, dd) = date.split('-').map { |x| x.to_i } - md = time.match(/(\d+:\d+:\d+)(\.\d*)?\s*(Z|[-+]\d+(:\d\d)?)?/) - - (hh, mm, ss) = md[1].split(':').map { |x| x.to_i } - - time = Time.utc(yy, m, dd, hh, mm, ss) - - us = md[2] ? md[2].sub(/^\./, '').to_i : 0 - - tz = (!md[3] || md[3] == 'Z') ? 0 : Integer(md[3].split(':').first) - Time.at((time - (tz * 3600)).to_i, us) - else - token.last - end + resolve_unknown o end end @@ -178,6 +156,34 @@ module Psych def visit_Psych_Nodes_Alias o @st[o.anchor] end + + private + def resolve_unknown o + token = ScalarScanner.new(o.value).tokenize + + case token.first + when :DATE + require 'date' + Date.strptime token.last, '%Y-%m-%d' + when :TIME + lexeme = token.last + + date, time = *(lexeme.split(/[ tT]/, 2)) + (yy, m, dd) = date.split('-').map { |x| x.to_i } + md = time.match(/(\d+:\d+:\d+)(\.\d*)?\s*(Z|[-+]\d+(:\d\d)?)?/) + + (hh, mm, ss) = md[1].split(':').map { |x| x.to_i } + + time = Time.utc(yy, m, dd, hh, mm, ss) + + us = md[2] ? md[2].sub(/^\./, '').to_i : 0 + + tz = (!md[3] || md[3] == 'Z') ? 0 : Integer(md[3].split(':').first) + Time.at((time - (tz * 3600)).to_i, us) + else + token.last + end + end end end end |