diff options
author | Jean Boussier <jean.boussier@gmail.com> | 2019-04-26 19:38:43 +0200 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2019-04-26 19:38:43 +0200 |
commit | 7d04834b79aa6677b1bff42161311cb79809ed7d (patch) | |
tree | 3b6c934a64f8660c0bbc5091c1cb58b342a23a26 /lib | |
parent | a28037330ff1507fe014b85a44b96034c24ba48d (diff) | |
download | psych-7d04834b79aa6677b1bff42161311cb79809ed7d.zip |
Do not allocate a string to check if a scalar is an integer
Diffstat (limited to 'lib')
-rw-r--r-- | lib/psych/scalar_scanner.rb | 16 |
1 files changed, 7 insertions, 9 deletions
diff --git a/lib/psych/scalar_scanner.rb b/lib/psych/scalar_scanner.rb index e92fe27..cea2a45 100644 --- a/lib/psych/scalar_scanner.rb +++ b/lib/psych/scalar_scanner.rb @@ -14,10 +14,10 @@ module Psych |\.(nan|NaN|NAN)(?# not a number))$/x # Taken from http://yaml.org/type/int.html - INTEGER = /^(?:[-+]?0b[0-1_]+ (?# base 2) - |[-+]?0[0-7_]+ (?# base 8) - |[-+]?(?:0|[1-9][0-9_]*) (?# base 10) - |[-+]?0x[0-9a-fA-F_]+ (?# base 16))$/x + INTEGER = /^(?:[-+]?0b[0-1_,]+ (?# base 2) + |[-+]?0[0-7_,]+ (?# base 8) + |[-+]?(?:0|[1-9][0-9_,]*) (?# base 10) + |[-+]?0x[0-9a-fA-F_,]+ (?# base 16))$/x attr_reader :class_loader @@ -91,10 +91,9 @@ module Psych else Float(string.gsub(/[,_]|\.([Ee]|$)/, '\1')) end + elsif string.match?(INTEGER) + parse_int string else - int = parse_int string.gsub(/[,_]/, '') - return int if int - string end end @@ -102,8 +101,7 @@ module Psych ### # Parse and return an int from +string+ def parse_int string - return unless INTEGER === string - Integer(string) + Integer(string.gsub(/[,]/, '')) end ### |