summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authornagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-02-19 16:38:03 +0000
committernagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-02-19 16:38:03 +0000
commitee2df505d8ee589a7778bf5723e701ec171362d3 (patch)
treea47c650bbf1ffaa6b2142c52ac14c7a7b1bacd74 /lib
parent0bdfc7243fe2d815aff62f4467663c52062cfbf2 (diff)
merge revision(s) r42844,r42845: [Backport #8865]
* lib/optparse.rb: The Integer acceptable now allows binary and hexadecimal numbers per the documentation. [ruby-trunk - Bug #8865] DecimalInteger, OctalInteger, DecimalNumeric now validate their input before converting to a number. [ruby-trunk - Bug #8865] * test/optparse/test_acceptable.rb: Tests for the above, tests for all numeric acceptables for existing behavior. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@45050 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/optparse.rb43
1 files changed, 34 insertions, 9 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb
index 469fde29ae..fb8b0278e8 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -1637,15 +1637,22 @@ XXX
decimal = '\d+(?:_\d+)*'
binary = 'b[01]+(?:_[01]+)*'
hex = 'x[\da-f]+(?:_[\da-f]+)*'
- octal = "0(?:[0-7]*(?:_[0-7]+)*|#{binary}|#{hex})"
+ octal = "0(?:[0-7]+(?:_[0-7]+)*|#{binary}|#{hex})?"
integer = "#{octal}|#{decimal}"
- accept(Integer, %r"\A[-+]?(?:#{integer})"io) {|s,| Integer(s) if s}
+
+ accept(Integer, %r"\A[-+]?(?:#{integer})\z"io) {|s,|
+ begin
+ Integer(s)
+ rescue ArgumentError
+ raise OptionParser::InvalidArgument, s
+ end if s
+ }
#
# Float number format, and converts to Float.
#
float = "(?:#{decimal}(?:\\.(?:#{decimal})?)?|\\.#{decimal})(?:E[-+]?#{decimal})?"
- floatpat = %r"\A[-+]?#{float}"io
+ floatpat = %r"\A[-+]?#{float}\z"io
accept(Float, floatpat) {|s,| s.to_f if s}
#
@@ -1653,7 +1660,7 @@ XXX
# for float format, and Rational for rational format.
#
real = "[-+]?(?:#{octal}|#{float})"
- accept(Numeric, /\A(#{real})(?:\/(#{real}))?/io) {|s, d, n|
+ accept(Numeric, /\A(#{real})(?:\/(#{real}))?\z/io) {|s, d, n|
if n
Rational(d, n)
elsif s
@@ -1664,22 +1671,40 @@ XXX
#
# Decimal integer format, to be converted to Integer.
#
- DecimalInteger = /\A[-+]?#{decimal}/io
- accept(DecimalInteger) {|s,| s.to_i if s}
+ DecimalInteger = /\A[-+]?#{decimal}\z/io
+ accept(DecimalInteger, DecimalInteger) {|s,|
+ begin
+ Integer(s)
+ rescue ArgumentError
+ raise OptionParser::InvalidArgument, s
+ end if s
+ }
#
# Ruby/C like octal/hexadecimal/binary integer format, to be converted to
# Integer.
#
- OctalInteger = /\A[-+]?(?:[0-7]+(?:_[0-7]+)*|0(?:#{binary}|#{hex}))/io
- accept(OctalInteger) {|s,| s.oct if s}
+ OctalInteger = /\A[-+]?(?:[0-7]+(?:_[0-7]+)*|0(?:#{binary}|#{hex}))\z/io
+ accept(OctalInteger, OctalInteger) {|s,|
+ begin
+ Integer(s, 8)
+ rescue ArgumentError
+ raise OptionParser::InvalidArgument, s
+ end if s
+ }
#
# Decimal integer/float number format, to be converted to Integer for
# integer format, Float for float format.
#
DecimalNumeric = floatpat # decimal integer is allowed as float also.
- accept(DecimalNumeric) {|s,| eval(s) if s}
+ accept(DecimalNumeric, floatpat) {|s,|
+ begin
+ eval(s)
+ rescue SyntaxError
+ raise OptionParser::InvalidArgument, s
+ end if s
+ }
#
# Boolean switch, which means whether it is present or not, whether it is