* lib/scanf.rb: support %a format. [ruby-dev:40650]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27139 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
7b93993b20
commit
2067e4eb30
@ -1,3 +1,7 @@
|
|||||||
|
Thu Apr 1 13:20:50 2010 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/scanf.rb: support %a format. [ruby-dev:40650]
|
||||||
|
|
||||||
Thu Apr 1 12:04:10 2010 NAKAMURA Usaku <usa@ruby-lang.org>
|
Thu Apr 1 12:04:10 2010 NAKAMURA Usaku <usa@ruby-lang.org>
|
||||||
|
|
||||||
* include/ruby/ruby.h (PRE_TIMET_PREFIX): define if configure doesn't
|
* include/ruby/ruby.h (PRE_TIMET_PREFIX): define if configure doesn't
|
||||||
|
29
lib/scanf.rb
29
lib/scanf.rb
@ -112,7 +112,7 @@ and <tt>tests/scanftests.rb</tt> for examples.)
|
|||||||
[x,X]
|
[x,X]
|
||||||
Matches an optionally signed hexadecimal integer,
|
Matches an optionally signed hexadecimal integer,
|
||||||
|
|
||||||
[f,g,e,E]
|
[a,e,f,g,A,E,F,G]
|
||||||
Matches an optionally signed floating-point number.
|
Matches an optionally signed floating-point number.
|
||||||
|
|
||||||
[s]
|
[s]
|
||||||
@ -309,7 +309,22 @@ module Scanf
|
|||||||
|
|
||||||
def skip; /^\s*%\*/.match(@spec_string); end
|
def skip; /^\s*%\*/.match(@spec_string); end
|
||||||
|
|
||||||
def extract_float(s); s.to_f if s &&! skip; end
|
def extract_float(s)
|
||||||
|
return nil unless s &&! skip
|
||||||
|
if /\A(?<sign>[-+]?)0[xX](?<frac>\.\h+|\h+(?:\.\h*)?)[pP](?<exp>[-+]\d+)/ =~ s
|
||||||
|
f1, f2 = frac.split('.')
|
||||||
|
f = f1.hex
|
||||||
|
if f2
|
||||||
|
len = f2.length
|
||||||
|
if len > 0
|
||||||
|
f += f2.hex / (16.0 ** len)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
(sign == ?- ? -1 : 1) * Math.ldexp(f, exp.to_i)
|
||||||
|
else
|
||||||
|
s.to_f
|
||||||
|
end
|
||||||
|
end
|
||||||
def extract_decimal(s); s.to_i if s &&! skip; end
|
def extract_decimal(s); s.to_i if s &&! skip; end
|
||||||
def extract_hex(s); s.hex if s &&! skip; end
|
def extract_hex(s); s.hex if s &&! skip; end
|
||||||
def extract_octal(s); s.oct if s &&! skip; end
|
def extract_octal(s); s.oct if s &&! skip; end
|
||||||
@ -409,12 +424,12 @@ module Scanf
|
|||||||
[ "([-+][0-7]{1,#{$1.to_i-1}}|[0-7]{1,#{$1}})", :extract_octal ]
|
[ "([-+][0-7]{1,#{$1.to_i-1}}|[0-7]{1,#{$1}})", :extract_octal ]
|
||||||
|
|
||||||
# %f
|
# %f
|
||||||
when /%\*?[efgEFG]/
|
when /%\*?[aefgAEFG]/
|
||||||
[ '([-+]?(?:\d+(?![\d.])|\d*\.\d*(?:[eE][-+]?\d+)?))', :extract_float ]
|
[ '([-+]?(?:0[xX](?:\.\h+|\h+(?:\.\h*)?)[pP][-+]\d+|\d+(?![\d.])|\d*\.\d*(?:[eE][-+]?\d+)?))', :extract_float ]
|
||||||
|
|
||||||
# %5f
|
# %5f
|
||||||
when /%\*?(\d+)[efgEFG]/
|
when /%\*?(\d+)[aefgAEFG]/
|
||||||
[ '(?=[-+]?(?:\d+(?![\d.])|\d*\.\d*(?:[eE][-+]?\d+)?))' +
|
[ '(?=[-+]?(?:0[xX](?:\.\h+|\h+(?:\.\h*)?)[pP][-+]\d+|\d+(?![\d.])|\d*\.\d*(?:[eE][-+]?\d+)?))' +
|
||||||
"(\\S{1,#{$1}})", :extract_float ]
|
"(\\S{1,#{$1}})", :extract_float ]
|
||||||
|
|
||||||
# %5s
|
# %5s
|
||||||
@ -491,7 +506,7 @@ module Scanf
|
|||||||
attr_reader :string_left, :last_spec_tried,
|
attr_reader :string_left, :last_spec_tried,
|
||||||
:last_match_tried, :matched_count, :space
|
:last_match_tried, :matched_count, :space
|
||||||
|
|
||||||
SPECIFIERS = 'diuXxofFeEgGsc'
|
SPECIFIERS = 'diuXxofFeEgGscaA'
|
||||||
REGEX = /
|
REGEX = /
|
||||||
# possible space, followed by...
|
# possible space, followed by...
|
||||||
(?:\s*
|
(?:\s*
|
||||||
|
@ -276,6 +276,8 @@ module ScanfTests
|
|||||||
[ "%g", "+3.25", [3.25] ],
|
[ "%g", "+3.25", [3.25] ],
|
||||||
[ "%G", "+3.25e2", [325.0] ],
|
[ "%G", "+3.25e2", [325.0] ],
|
||||||
[ "%f", "3.z", [3.0] ],
|
[ "%f", "3.z", [3.0] ],
|
||||||
|
[ "%a", "0X1P+10", [1024.0] ],
|
||||||
|
[ "%A", "0x1.deadbeefp+99", [1.1851510441583988e+30] ],
|
||||||
|
|
||||||
# Testing embedded matches including literal '[' behavior
|
# Testing embedded matches including literal '[' behavior
|
||||||
[",%d,%f", ",10,1.1", [10,1.1] ],
|
[",%d,%f", ",10,1.1", [10,1.1] ],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user