Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
File: scanf.rb
# scanf for Ruby
[0] Fix | Delete
#
[1] Fix | Delete
#--
[2] Fix | Delete
# $Release Version: 1.1.2 $
[3] Fix | Delete
# $Revision: 44961 $
[4] Fix | Delete
# $Id: scanf.rb 44961 2014-02-15 01:33:03Z ryan $
[5] Fix | Delete
# $Author: ryan $
[6] Fix | Delete
#++
[7] Fix | Delete
#
[8] Fix | Delete
# == Description
[9] Fix | Delete
#
[10] Fix | Delete
# scanf is an implementation of the C function scanf(3), modified as necessary
[11] Fix | Delete
# for Ruby compatibility.
[12] Fix | Delete
#
[13] Fix | Delete
# the methods provided are String#scanf, IO#scanf, and
[14] Fix | Delete
# Kernel#scanf. Kernel#scanf is a wrapper around STDIN.scanf. IO#scanf
[15] Fix | Delete
# can be used on any IO stream, including file handles and sockets.
[16] Fix | Delete
# scanf can be called either with or without a block.
[17] Fix | Delete
#
[18] Fix | Delete
# Scanf scans an input string or stream according to a <b>format</b>, as
[19] Fix | Delete
# described below in Conversions, and returns an array of matches between
[20] Fix | Delete
# the format and the input. The format is defined in a string, and is
[21] Fix | Delete
# similar (though not identical) to the formats used in Kernel#printf and
[22] Fix | Delete
# Kernel#sprintf.
[23] Fix | Delete
#
[24] Fix | Delete
# The format may contain <b>conversion specifiers</b>, which tell scanf
[25] Fix | Delete
# what form (type) each particular matched substring should be converted
[26] Fix | Delete
# to (e.g., decimal integer, floating point number, literal string,
[27] Fix | Delete
# etc.) The matches and conversions take place from left to right, and
[28] Fix | Delete
# the conversions themselves are returned as an array.
[29] Fix | Delete
#
[30] Fix | Delete
# The format string may also contain characters other than those in the
[31] Fix | Delete
# conversion specifiers. White space (blanks, tabs, or newlines) in the
[32] Fix | Delete
# format string matches any amount of white space, including none, in
[33] Fix | Delete
# the input. Everything else matches only itself.
[34] Fix | Delete
#
[35] Fix | Delete
# Scanning stops, and scanf returns, when any input character fails to
[36] Fix | Delete
# match the specifications in the format string, or when input is
[37] Fix | Delete
# exhausted, or when everything in the format string has been
[38] Fix | Delete
# matched. All matches found up to the stopping point are returned in
[39] Fix | Delete
# the return array (or yielded to the block, if a block was given).
[40] Fix | Delete
#
[41] Fix | Delete
#
[42] Fix | Delete
# == Basic usage
[43] Fix | Delete
#
[44] Fix | Delete
# require 'scanf'
[45] Fix | Delete
#
[46] Fix | Delete
# # String#scanf and IO#scanf take a single argument, the format string
[47] Fix | Delete
# array = a_string.scanf("%d%s")
[48] Fix | Delete
# array = an_io.scanf("%d%s")
[49] Fix | Delete
#
[50] Fix | Delete
# # Kernel#scanf reads from STDIN
[51] Fix | Delete
# array = scanf("%d%s")
[52] Fix | Delete
#
[53] Fix | Delete
# == Block usage
[54] Fix | Delete
#
[55] Fix | Delete
# When called with a block, scanf keeps scanning the input, cycling back
[56] Fix | Delete
# to the beginning of the format string, and yields a new array of
[57] Fix | Delete
# conversions to the block every time the format string is matched
[58] Fix | Delete
# (including partial matches, but not including complete failures). The
[59] Fix | Delete
# actual return value of scanf when called with a block is an array
[60] Fix | Delete
# containing the results of all the executions of the block.
[61] Fix | Delete
#
[62] Fix | Delete
# str = "123 abc 456 def 789 ghi"
[63] Fix | Delete
# str.scanf("%d%s") { |num,str| [ num * 2, str.upcase ] }
[64] Fix | Delete
# # => [[246, "ABC"], [912, "DEF"], [1578, "GHI"]]
[65] Fix | Delete
#
[66] Fix | Delete
# == Conversions
[67] Fix | Delete
#
[68] Fix | Delete
# The single argument to scanf is a format string, which generally
[69] Fix | Delete
# includes one or more conversion specifiers. Conversion specifiers
[70] Fix | Delete
# begin with the percent character ('%') and include information about
[71] Fix | Delete
# what scanf should next scan for (string, decimal number, single
[72] Fix | Delete
# character, etc.).
[73] Fix | Delete
#
[74] Fix | Delete
# There may be an optional maximum field width, expressed as a decimal
[75] Fix | Delete
# integer, between the % and the conversion. If no width is given, a
[76] Fix | Delete
# default of `infinity' is used (with the exception of the %c specifier;
[77] Fix | Delete
# see below). Otherwise, given a field width of <em>n</em> for a given
[78] Fix | Delete
# conversion, at most <em>n</em> characters are scanned in processing
[79] Fix | Delete
# that conversion. Before conversion begins, most conversions skip
[80] Fix | Delete
# white space in the input string; this white space is not counted
[81] Fix | Delete
# against the field width.
[82] Fix | Delete
#
[83] Fix | Delete
# The following conversions are available.
[84] Fix | Delete
#
[85] Fix | Delete
# [%]
[86] Fix | Delete
# Matches a literal `%'. That is, `%%' in the format string matches a
[87] Fix | Delete
# single input `%' character. No conversion is done, and the resulting
[88] Fix | Delete
# '%' is not included in the return array.
[89] Fix | Delete
#
[90] Fix | Delete
# [d]
[91] Fix | Delete
# Matches an optionally signed decimal integer.
[92] Fix | Delete
#
[93] Fix | Delete
# [u]
[94] Fix | Delete
# Same as d.
[95] Fix | Delete
#
[96] Fix | Delete
# [i]
[97] Fix | Delete
# Matches an optionally signed integer. The integer is read in base
[98] Fix | Delete
# 16 if it begins with `0x' or `0X', in base 8 if it begins with `0',
[99] Fix | Delete
# and in base 10 other- wise. Only characters that correspond to the
[100] Fix | Delete
# base are recognized.
[101] Fix | Delete
#
[102] Fix | Delete
# [o]
[103] Fix | Delete
# Matches an optionally signed octal integer.
[104] Fix | Delete
#
[105] Fix | Delete
# [x, X]
[106] Fix | Delete
# Matches an optionally signed hexadecimal integer,
[107] Fix | Delete
#
[108] Fix | Delete
# [a, e, f, g, A, E, F, G]
[109] Fix | Delete
# Matches an optionally signed floating-point number.
[110] Fix | Delete
#
[111] Fix | Delete
# [s]
[112] Fix | Delete
# Matches a sequence of non-white-space character. The input string stops at
[113] Fix | Delete
# white space or at the maximum field width, whichever occurs first.
[114] Fix | Delete
#
[115] Fix | Delete
# [c]
[116] Fix | Delete
# Matches a single character, or a sequence of <em>n</em> characters if a
[117] Fix | Delete
# field width of <em>n</em> is specified. The usual skip of leading white
[118] Fix | Delete
# space is suppressed. To skip white space first, use an explicit space in
[119] Fix | Delete
# the format.
[120] Fix | Delete
#
[121] Fix | Delete
# [[]
[122] Fix | Delete
# Matches a nonempty sequence of characters from the specified set
[123] Fix | Delete
# of accepted characters. The usual skip of leading white space is
[124] Fix | Delete
# suppressed. This bracketed sub-expression is interpreted exactly like a
[125] Fix | Delete
# character class in a Ruby regular expression. (In fact, it is placed as-is
[126] Fix | Delete
# in a regular expression.) The matching against the input string ends with
[127] Fix | Delete
# the appearance of a character not in (or, with a circumflex, in) the set,
[128] Fix | Delete
# or when the field width runs out, whichever comes first.
[129] Fix | Delete
#
[130] Fix | Delete
# === Assignment suppression
[131] Fix | Delete
#
[132] Fix | Delete
# To require that a particular match occur, but without including the result
[133] Fix | Delete
# in the return array, place the <b>assignment suppression flag</b>, which is
[134] Fix | Delete
# the star character ('*'), immediately after the leading '%' of a format
[135] Fix | Delete
# specifier (just before the field width, if any).
[136] Fix | Delete
#
[137] Fix | Delete
# == scanf for Ruby compared with scanf in C
[138] Fix | Delete
#
[139] Fix | Delete
# scanf for Ruby is based on the C function scanf(3), but with modifications,
[140] Fix | Delete
# dictated mainly by the underlying differences between the languages.
[141] Fix | Delete
#
[142] Fix | Delete
# === Unimplemented flags and specifiers
[143] Fix | Delete
#
[144] Fix | Delete
# * The only flag implemented in scanf for Ruby is '<tt>*</tt>' (ignore
[145] Fix | Delete
# upcoming conversion). Many of the flags available in C versions of
[146] Fix | Delete
# scanf(3) have to do with the type of upcoming pointer arguments, and are
[147] Fix | Delete
# meaningless in Ruby.
[148] Fix | Delete
#
[149] Fix | Delete
# * The <tt>n</tt> specifier (store number of characters consumed so far in
[150] Fix | Delete
# next pointer) is not implemented.
[151] Fix | Delete
#
[152] Fix | Delete
# * The <tt>p</tt> specifier (match a pointer value) is not implemented.
[153] Fix | Delete
#
[154] Fix | Delete
# === Altered specifiers
[155] Fix | Delete
#
[156] Fix | Delete
# [o, u, x, X]
[157] Fix | Delete
# In scanf for Ruby, all of these specifiers scan for an optionally signed
[158] Fix | Delete
# integer, rather than for an unsigned integer like their C counterparts.
[159] Fix | Delete
#
[160] Fix | Delete
# === Return values
[161] Fix | Delete
#
[162] Fix | Delete
# scanf for Ruby returns an array of successful conversions, whereas
[163] Fix | Delete
# scanf(3) returns the number of conversions successfully
[164] Fix | Delete
# completed. (See below for more details on scanf for Ruby's return
[165] Fix | Delete
# values.)
[166] Fix | Delete
#
[167] Fix | Delete
# == Return values
[168] Fix | Delete
#
[169] Fix | Delete
# Without a block, scanf returns an array containing all the conversions
[170] Fix | Delete
# it has found. If none are found, scanf will return an empty array. An
[171] Fix | Delete
# unsuccessful match is never ignored, but rather always signals the end
[172] Fix | Delete
# of the scanning operation. If the first unsuccessful match takes place
[173] Fix | Delete
# after one or more successful matches have already taken place, the
[174] Fix | Delete
# returned array will contain the results of those successful matches.
[175] Fix | Delete
#
[176] Fix | Delete
# With a block scanf returns a 'map'-like array of transformations from
[177] Fix | Delete
# the block -- that is, an array reflecting what the block did with each
[178] Fix | Delete
# yielded result from the iterative scanf operation. (See "Block
[179] Fix | Delete
# usage", above.)
[180] Fix | Delete
#
[181] Fix | Delete
# == Current limitations and bugs
[182] Fix | Delete
#
[183] Fix | Delete
# When using IO#scanf under Windows, make sure you open your files in
[184] Fix | Delete
# binary mode:
[185] Fix | Delete
#
[186] Fix | Delete
# File.open("filename", "rb")
[187] Fix | Delete
#
[188] Fix | Delete
# so that scanf can keep track of characters correctly.
[189] Fix | Delete
#
[190] Fix | Delete
# Support for character classes is reasonably complete (since it
[191] Fix | Delete
# essentially piggy-backs on Ruby's regular expression handling of
[192] Fix | Delete
# character classes), but users are advised that character class testing
[193] Fix | Delete
# has not been exhaustive, and that they should exercise some caution
[194] Fix | Delete
# in using any of the more complex and/or arcane character class
[195] Fix | Delete
# idioms.
[196] Fix | Delete
#
[197] Fix | Delete
# == License and copyright
[198] Fix | Delete
#
[199] Fix | Delete
# Copyright:: (c) 2002-2003 David Alan Black
[200] Fix | Delete
# License:: Distributed on the same licensing terms as Ruby itself
[201] Fix | Delete
#
[202] Fix | Delete
# == Warranty disclaimer
[203] Fix | Delete
#
[204] Fix | Delete
# This software is provided "as is" and without any express or implied
[205] Fix | Delete
# warranties, including, without limitation, the implied warranties of
[206] Fix | Delete
# merchantability and fitness for a particular purpose.
[207] Fix | Delete
#
[208] Fix | Delete
# == Credits and acknowledgements
[209] Fix | Delete
#
[210] Fix | Delete
# scanf was developed as the major activity of the Austin Ruby Codefest
[211] Fix | Delete
# (Austin, Texas, August 2002).
[212] Fix | Delete
#
[213] Fix | Delete
# Principal author:: David Alan Black (mailto:dblack@superlink.net)
[214] Fix | Delete
# Co-author:: Hal Fulton (mailto:hal9000@hypermetrics.com)
[215] Fix | Delete
# Project contributors:: Nolan Darilek, Jason Johnston
[216] Fix | Delete
#
[217] Fix | Delete
# Thanks to Hal Fulton for hosting the Codefest.
[218] Fix | Delete
#
[219] Fix | Delete
# Thanks to Matz for suggestions about the class design.
[220] Fix | Delete
#
[221] Fix | Delete
# Thanks to Gavin Sinclair for some feedback on the documentation.
[222] Fix | Delete
#
[223] Fix | Delete
# The text for parts of this document, especially the Description and
[224] Fix | Delete
# Conversions sections, above, were adapted from the Linux Programmer's
[225] Fix | Delete
# Manual manpage for scanf(3), dated 1995-11-01.
[226] Fix | Delete
#
[227] Fix | Delete
# == Bugs and bug reports
[228] Fix | Delete
#
[229] Fix | Delete
# scanf for Ruby is based on something of an amalgam of C scanf
[230] Fix | Delete
# implementations and documentation, rather than on a single canonical
[231] Fix | Delete
# description. Suggestions for features and behaviors which appear in
[232] Fix | Delete
# other scanfs, and would be meaningful in Ruby, are welcome, as are
[233] Fix | Delete
# reports of suspicious behaviors and/or bugs. (Please see "Credits and
[234] Fix | Delete
# acknowledgements", above, for email addresses.)
[235] Fix | Delete
[236] Fix | Delete
module Scanf
[237] Fix | Delete
# :stopdoc:
[238] Fix | Delete
[239] Fix | Delete
# ==Technical notes
[240] Fix | Delete
#
[241] Fix | Delete
# ===Rationale behind scanf for Ruby
[242] Fix | Delete
#
[243] Fix | Delete
# The impetus for a scanf implementation in Ruby comes chiefly from the fact
[244] Fix | Delete
# that existing pattern matching operations, such as Regexp#match and
[245] Fix | Delete
# String#scan, return all results as strings, which have to be converted to
[246] Fix | Delete
# integers or floats explicitly in cases where what's ultimately wanted are
[247] Fix | Delete
# integer or float values.
[248] Fix | Delete
#
[249] Fix | Delete
# ===Design of scanf for Ruby
[250] Fix | Delete
#
[251] Fix | Delete
# scanf for Ruby is essentially a <format string>-to-<regular
[252] Fix | Delete
# expression> converter.
[253] Fix | Delete
#
[254] Fix | Delete
# When scanf is called, a FormatString object is generated from the
[255] Fix | Delete
# format string ("%d%s...") argument. The FormatString object breaks the
[256] Fix | Delete
# format string down into atoms ("%d", "%5f", "blah", etc.), and from
[257] Fix | Delete
# each atom it creates a FormatSpecifier object, which it
[258] Fix | Delete
# saves.
[259] Fix | Delete
#
[260] Fix | Delete
# Each FormatSpecifier has a regular expression fragment and a "handler"
[261] Fix | Delete
# associated with it. For example, the regular expression fragment
[262] Fix | Delete
# associated with the format "%d" is "([-+]?\d+)", and the handler
[263] Fix | Delete
# associated with it is a wrapper around String#to_i. scanf itself calls
[264] Fix | Delete
# FormatString#match, passing in the input string. FormatString#match
[265] Fix | Delete
# iterates through its FormatSpecifiers; for each one, it matches the
[266] Fix | Delete
# corresponding regular expression fragment against the string. If
[267] Fix | Delete
# there's a match, it sends the matched string to the handler associated
[268] Fix | Delete
# with the FormatSpecifier.
[269] Fix | Delete
#
[270] Fix | Delete
# Thus, to follow up the "%d" example: if "123" occurs in the input
[271] Fix | Delete
# string when a FormatSpecifier consisting of "%d" is reached, the "123"
[272] Fix | Delete
# will be matched against "([-+]?\d+)", and the matched string will be
[273] Fix | Delete
# rendered into an integer by a call to to_i.
[274] Fix | Delete
#
[275] Fix | Delete
# The rendered match is then saved to an accumulator array, and the
[276] Fix | Delete
# input string is reduced to the post-match substring. Thus the string
[277] Fix | Delete
# is "eaten" from the left as the FormatSpecifiers are applied in
[278] Fix | Delete
# sequence. (This is done to a duplicate string; the original string is
[279] Fix | Delete
# not altered.)
[280] Fix | Delete
#
[281] Fix | Delete
# As soon as a regular expression fragment fails to match the string, or
[282] Fix | Delete
# when the FormatString object runs out of FormatSpecifiers, scanning
[283] Fix | Delete
# stops and results accumulated so far are returned in an array.
[284] Fix | Delete
[285] Fix | Delete
class FormatSpecifier
[286] Fix | Delete
[287] Fix | Delete
attr_reader :re_string, :matched_string, :conversion, :matched
[288] Fix | Delete
[289] Fix | Delete
private
[290] Fix | Delete
[291] Fix | Delete
def skip; /^\s*%\*/.match(@spec_string); end
[292] Fix | Delete
[293] Fix | Delete
def extract_float(s)
[294] Fix | Delete
return nil unless s &&! skip
[295] Fix | Delete
if /\A(?<sign>[-+]?)0[xX](?<frac>\.\h+|\h+(?:\.\h*)?)[pP](?<exp>[-+]\d+)/ =~ s
[296] Fix | Delete
f1, f2 = frac.split('.')
[297] Fix | Delete
f = f1.hex
[298] Fix | Delete
if f2
[299] Fix | Delete
len = f2.length
[300] Fix | Delete
if len > 0
[301] Fix | Delete
f += f2.hex / (16.0 ** len)
[302] Fix | Delete
end
[303] Fix | Delete
end
[304] Fix | Delete
(sign == ?- ? -1 : 1) * Math.ldexp(f, exp.to_i)
[305] Fix | Delete
elsif /\A([-+]?\d+)\.([eE][-+]\d+)/ =~ s
[306] Fix | Delete
($1 << $2).to_f
[307] Fix | Delete
else
[308] Fix | Delete
s.to_f
[309] Fix | Delete
end
[310] Fix | Delete
end
[311] Fix | Delete
def extract_decimal(s); s.to_i if s &&! skip; end
[312] Fix | Delete
def extract_hex(s); s.hex if s &&! skip; end
[313] Fix | Delete
def extract_octal(s); s.oct if s &&! skip; end
[314] Fix | Delete
def extract_integer(s); Integer(s) if s &&! skip; end
[315] Fix | Delete
def extract_plain(s); s unless skip; end
[316] Fix | Delete
[317] Fix | Delete
def nil_proc(s); nil; end
[318] Fix | Delete
[319] Fix | Delete
public
[320] Fix | Delete
[321] Fix | Delete
def to_s
[322] Fix | Delete
@spec_string
[323] Fix | Delete
end
[324] Fix | Delete
[325] Fix | Delete
def count_space?
[326] Fix | Delete
/(?:\A|\S)%\*?\d*c|%\d*\[/.match(@spec_string)
[327] Fix | Delete
end
[328] Fix | Delete
[329] Fix | Delete
def initialize(str)
[330] Fix | Delete
@spec_string = str
[331] Fix | Delete
h = '[A-Fa-f0-9]'
[332] Fix | Delete
[333] Fix | Delete
@re_string, @handler =
[334] Fix | Delete
case @spec_string
[335] Fix | Delete
[336] Fix | Delete
# %[[:...:]]
[337] Fix | Delete
when /%\*?(\[\[:[a-z]+:\]\])/
[338] Fix | Delete
[ "(#{$1}+)", :extract_plain ]
[339] Fix | Delete
[340] Fix | Delete
# %5[[:...:]]
[341] Fix | Delete
when /%\*?(\d+)(\[\[:[a-z]+:\]\])/
[342] Fix | Delete
[ "(#{$2}{1,#{$1}})", :extract_plain ]
[343] Fix | Delete
[344] Fix | Delete
# %[...]
[345] Fix | Delete
when /%\*?\[([^\]]*)\]/
[346] Fix | Delete
yes = $1
[347] Fix | Delete
if /^\^/.match(yes) then no = yes[1..-1] else no = '^' + yes end
[348] Fix | Delete
[ "([#{yes}]+)(?=[#{no}]|\\z)", :extract_plain ]
[349] Fix | Delete
[350] Fix | Delete
# %5[...]
[351] Fix | Delete
when /%\*?(\d+)\[([^\]]*)\]/
[352] Fix | Delete
yes = $2
[353] Fix | Delete
w = $1
[354] Fix | Delete
[ "([#{yes}]{1,#{w}})", :extract_plain ]
[355] Fix | Delete
[356] Fix | Delete
# %i
[357] Fix | Delete
when /%\*?i/
[358] Fix | Delete
[ "([-+]?(?:(?:0[0-7]+)|(?:0[Xx]#{h}+)|(?:[1-9]\\d*)))", :extract_integer ]
[359] Fix | Delete
[360] Fix | Delete
# %5i
[361] Fix | Delete
when /%\*?(\d+)i/
[362] Fix | Delete
n = $1.to_i
[363] Fix | Delete
s = "("
[364] Fix | Delete
if n > 1 then s += "[1-9]\\d{1,#{n-1}}|" end
[365] Fix | Delete
if n > 1 then s += "0[0-7]{1,#{n-1}}|" end
[366] Fix | Delete
if n > 2 then s += "[-+]0[0-7]{1,#{n-2}}|" end
[367] Fix | Delete
if n > 2 then s += "[-+][1-9]\\d{1,#{n-2}}|" end
[368] Fix | Delete
if n > 2 then s += "0[Xx]#{h}{1,#{n-2}}|" end
[369] Fix | Delete
if n > 3 then s += "[-+]0[Xx]#{h}{1,#{n-3}}|" end
[370] Fix | Delete
s += "\\d"
[371] Fix | Delete
s += ")"
[372] Fix | Delete
[ s, :extract_integer ]
[373] Fix | Delete
[374] Fix | Delete
# %d, %u
[375] Fix | Delete
when /%\*?[du]/
[376] Fix | Delete
[ '([-+]?\d+)', :extract_decimal ]
[377] Fix | Delete
[378] Fix | Delete
# %5d, %5u
[379] Fix | Delete
when /%\*?(\d+)[du]/
[380] Fix | Delete
n = $1.to_i
[381] Fix | Delete
s = "("
[382] Fix | Delete
if n > 1 then s += "[-+]\\d{1,#{n-1}}|" end
[383] Fix | Delete
s += "\\d{1,#{$1}})"
[384] Fix | Delete
[ s, :extract_decimal ]
[385] Fix | Delete
[386] Fix | Delete
# %x
[387] Fix | Delete
when /%\*?[Xx]/
[388] Fix | Delete
[ "([-+]?(?:0[Xx])?#{h}+)", :extract_hex ]
[389] Fix | Delete
[390] Fix | Delete
# %5x
[391] Fix | Delete
when /%\*?(\d+)[Xx]/
[392] Fix | Delete
n = $1.to_i
[393] Fix | Delete
s = "("
[394] Fix | Delete
if n > 3 then s += "[-+]0[Xx]#{h}{1,#{n-3}}|" end
[395] Fix | Delete
if n > 2 then s += "0[Xx]#{h}{1,#{n-2}}|" end
[396] Fix | Delete
if n > 1 then s += "[-+]#{h}{1,#{n-1}}|" end
[397] Fix | Delete
s += "#{h}{1,#{n}}"
[398] Fix | Delete
s += ")"
[399] Fix | Delete
[ s, :extract_hex ]
[400] Fix | Delete
[401] Fix | Delete
# %o
[402] Fix | Delete
when /%\*?o/
[403] Fix | Delete
[ '([-+]?[0-7]+)', :extract_octal ]
[404] Fix | Delete
[405] Fix | Delete
# %5o
[406] Fix | Delete
when /%\*?(\d+)o/
[407] Fix | Delete
[ "([-+][0-7]{1,#{$1.to_i-1}}|[0-7]{1,#{$1}})", :extract_octal ]
[408] Fix | Delete
[409] Fix | Delete
# %f
[410] Fix | Delete
when /%\*?[aefgAEFG]/
[411] Fix | Delete
[ '([-+]?(?:0[xX](?:\.\h+|\h+(?:\.\h*)?)[pP][-+]\d+|\d+(?![\d.])|\d*\.\d*(?:[eE][-+]?\d+)?))', :extract_float ]
[412] Fix | Delete
[413] Fix | Delete
# %5f
[414] Fix | Delete
when /%\*?(\d+)[aefgAEFG]/
[415] Fix | Delete
[ '(?=[-+]?(?:0[xX](?:\.\h+|\h+(?:\.\h*)?)[pP][-+]\d+|\d+(?![\d.])|\d*\.\d*(?:[eE][-+]?\d+)?))' +
[416] Fix | Delete
"(\\S{1,#{$1}})", :extract_float ]
[417] Fix | Delete
[418] Fix | Delete
# %5s
[419] Fix | Delete
when /%\*?(\d+)s/
[420] Fix | Delete
[ "(\\S{1,#{$1}})", :extract_plain ]
[421] Fix | Delete
[422] Fix | Delete
# %s
[423] Fix | Delete
when /%\*?s/
[424] Fix | Delete
[ '(\S+)', :extract_plain ]
[425] Fix | Delete
[426] Fix | Delete
# %c
[427] Fix | Delete
when /\s%\*?c/
[428] Fix | Delete
[ "\\s*(.)", :extract_plain ]
[429] Fix | Delete
[430] Fix | Delete
# %c
[431] Fix | Delete
when /%\*?c/
[432] Fix | Delete
[ "(.)", :extract_plain ]
[433] Fix | Delete
[434] Fix | Delete
# %5c (whitespace issues are handled by the count_*_space? methods)
[435] Fix | Delete
when /%\*?(\d+)c/
[436] Fix | Delete
[ "(.{1,#{$1}})", :extract_plain ]
[437] Fix | Delete
[438] Fix | Delete
# %%
[439] Fix | Delete
when /%%/
[440] Fix | Delete
[ '(\s*%)', :nil_proc ]
[441] Fix | Delete
[442] Fix | Delete
# literal characters
[443] Fix | Delete
else
[444] Fix | Delete
[ "(#{Regexp.escape(@spec_string)})", :nil_proc ]
[445] Fix | Delete
end
[446] Fix | Delete
[447] Fix | Delete
@re_string = '\A' + @re_string
[448] Fix | Delete
end
[449] Fix | Delete
[450] Fix | Delete
def to_re
[451] Fix | Delete
Regexp.new(@re_string,Regexp::MULTILINE)
[452] Fix | Delete
end
[453] Fix | Delete
[454] Fix | Delete
def match(str)
[455] Fix | Delete
@matched = false
[456] Fix | Delete
s = str.dup
[457] Fix | Delete
s.sub!(/\A\s+/,'') unless count_space?
[458] Fix | Delete
res = to_re.match(s)
[459] Fix | Delete
if res
[460] Fix | Delete
@conversion = send(@handler, res[1])
[461] Fix | Delete
@matched_string = @conversion.to_s
[462] Fix | Delete
@matched = true
[463] Fix | Delete
end
[464] Fix | Delete
res
[465] Fix | Delete
end
[466] Fix | Delete
[467] Fix | Delete
def letter
[468] Fix | Delete
@spec_string[/%\*?\d*([a-z\[])/, 1]
[469] Fix | Delete
end
[470] Fix | Delete
[471] Fix | Delete
def width
[472] Fix | Delete
w = @spec_string[/%\*?(\d+)/, 1]
[473] Fix | Delete
w && w.to_i
[474] Fix | Delete
end
[475] Fix | Delete
[476] Fix | Delete
def mid_match?
[477] Fix | Delete
return false unless @matched
[478] Fix | Delete
cc_no_width = letter == '[' &&! width
[479] Fix | Delete
c_or_cc_width = (letter == 'c' || letter == '[') && width
[480] Fix | Delete
width_left = c_or_cc_width && (matched_string.size < width)
[481] Fix | Delete
[482] Fix | Delete
return width_left || cc_no_width
[483] Fix | Delete
end
[484] Fix | Delete
[485] Fix | Delete
end
[486] Fix | Delete
[487] Fix | Delete
class FormatString
[488] Fix | Delete
[489] Fix | Delete
attr_reader :string_left, :last_spec_tried,
[490] Fix | Delete
:last_match_tried, :matched_count, :space
[491] Fix | Delete
[492] Fix | Delete
SPECIFIERS = 'diuXxofFeEgGscaA'
[493] Fix | Delete
REGEX = /
[494] Fix | Delete
# possible space, followed by...
[495] Fix | Delete
(?:\s*
[496] Fix | Delete
# percent sign, followed by...
[497] Fix | Delete
%
[498] Fix | Delete
# another percent sign, or...
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function