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