diff options
author | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-05-14 17:26:41 +0000 |
---|---|---|
committer | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-05-14 17:26:41 +0000 |
commit | 7ceafcbdf5bd2155704839f97b869e689f66feeb (patch) | |
tree | 278d237513fff700906430468892ede20d3f76c1 /ext/psych/lib/psych/scalar_scanner.rb | |
parent | d7f06a665c8e5b4ec83b6baa4a7532fc7ad7f24a (diff) |
* ext/psych/lib/psych.rb: Adding Psych.safe_load for loading a user
defined, restricted subset of Ruby object types.
* ext/psych/lib/psych/class_loader.rb: A class loader for
encapsulating the logic for which objects are allowed to be
deserialized.
* ext/psych/lib/psych/deprecated.rb: Changes to use the class loader
* ext/psych/lib/psych/exception.rb: ditto
* ext/psych/lib/psych/json/stream.rb: ditto
* ext/psych/lib/psych/nodes/node.rb: ditto
* ext/psych/lib/psych/scalar_scanner.rb: ditto
* ext/psych/lib/psych/stream.rb: ditto
* ext/psych/lib/psych/streaming.rb: ditto
* ext/psych/lib/psych/visitors/json_tree.rb: ditto
* ext/psych/lib/psych/visitors/to_ruby.rb: ditto
* ext/psych/lib/psych/visitors/yaml_tree.rb: ditto
* ext/psych/psych_to_ruby.c: ditto
* test/psych/helper.rb: ditto
* test/psych/test_safe_load.rb: tests for restricted subset.
* test/psych/test_scalar_scanner.rb: ditto
* test/psych/visitors/test_to_ruby.rb: ditto
* test/psych/visitors/test_yaml_tree.rb: ditto
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40750 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/psych/lib/psych/scalar_scanner.rb')
-rw-r--r-- | ext/psych/lib/psych/scalar_scanner.rb | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/ext/psych/lib/psych/scalar_scanner.rb b/ext/psych/lib/psych/scalar_scanner.rb index 8aa594e333..5935e26b28 100644 --- a/ext/psych/lib/psych/scalar_scanner.rb +++ b/ext/psych/lib/psych/scalar_scanner.rb @@ -19,10 +19,13 @@ module Psych |[-+]?(?:0|[1-9][0-9_]*) (?# base 10) |[-+]?0x[0-9a-fA-F_]+ (?# base 16))$/x + attr_reader :class_loader + # Create a new scanner - def initialize + def initialize class_loader @string_cache = {} @symbol_cache = {} + @class_loader = class_loader end # Tokenize +string+ returning the ruby object @@ -63,7 +66,7 @@ module Psych when /^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/ require 'date' begin - Date.strptime(string, '%Y-%m-%d') + class_loader.date.strptime(string, '%Y-%m-%d') rescue ArgumentError string end @@ -75,9 +78,9 @@ module Psych Float::NAN when /^:./ if string =~ /^:(["'])(.*)\1/ - @symbol_cache[string] = $2.sub(/^:/, '').to_sym + @symbol_cache[string] = class_loader.symbolize($2.sub(/^:/, '')) else - @symbol_cache[string] = string.sub(/^:/, '').to_sym + @symbol_cache[string] = class_loader.symbolize(string.sub(/^:/, '')) end when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+$/ i = 0 @@ -117,6 +120,8 @@ module Psych ### # Parse and return a Time from +string+ def parse_time string + klass = class_loader.load 'Time' + date, time = *(string.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)?)?/) @@ -124,10 +129,10 @@ module Psych (hh, mm, ss) = md[1].split(':').map { |x| x.to_i } us = (md[2] ? Rational("0.#{md[2]}") : 0) * 1000000 - time = Time.utc(yy, m, dd, hh, mm, ss, us) + time = klass.utc(yy, m, dd, hh, mm, ss, us) return time if 'Z' == md[3] - return Time.at(time.to_i, us) unless md[3] + return klass.at(time.to_i, us) unless md[3] tz = md[3].match(/^([+\-]?\d{1,2})\:?(\d{1,2})?$/)[1..-1].compact.map { |digit| Integer(digit, 10) } offset = tz.first * 3600 @@ -138,7 +143,7 @@ module Psych offset += ((tz[1] || 0) * 60) end - Time.at((time - offset).to_i, us) + klass.at((time - offset).to_i, us) end end end |