יחידה:String: הבדלים בין גרסאות בדף

adds updated match support with wider parameter choices, whitespace handling, etc.
מ 16 גרסאות של הדף wikipedia:he:יחידה:String יובאו
 
(42 גרסאות ביניים של 13 משתמשים אינן מוצגות)
שורה 113: שורה 113:


Usage:
Usage:
{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag}}
{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}
OR
OR
{{#invoke:String|pos|s=source_string|pattern=pattern_string|start=start_index
{{#invoke:String|pos|s=source_string|pattern=pattern_string|start=start_index
     |match=match_number|plain=plain_flag}}
     |match=match_number|plain=plain_flag|nomatch=nomatch_output}}


Parameters
Parameters
שורה 128: שורה 128:
         counting from the last match.  Hence match = -1 is the same as requesting
         counting from the last match.  Hence match = -1 is the same as requesting
         the last match.  Defaults to 1.
         the last match.  Defaults to 1.
     plain_flag: A flag indicating that the pattern should be understood as plain
     plain: A flag indicating that the pattern should be understood as plain
         text.  Defaults to false.
         text.  Defaults to false.
    nomatch: If no match is found, output the "nomatch" value rather than an error.


If invoked using named parameters, Mediawiki will automatically remove any leading or
If invoked using named parameters, Mediawiki will automatically remove any leading or
שורה 148: שורה 149:
]]
]]
function str.match( frame )
function str.match( frame )
     local new_args = str._getParameters( frame.args, {'s', 'pattern', 'start', 'match', 'plain'} );
     local new_args = str._getParameters( frame.args, {'s', 'pattern', 'start', 'match', 'plain', 'nomatch'} );
     local s = new_args['s'] or '';
     local s = new_args['s'] or '';
     local start = tonumber( new_args['start'] ) or 1;
     local start = tonumber( new_args['start'] ) or 1;
שורה 154: שורה 155:
     local pattern = new_args['pattern'] or '';
     local pattern = new_args['pattern'] or '';
     local match_index = math.floor( tonumber(new_args['match']) or 1 );
     local match_index = math.floor( tonumber(new_args['match']) or 1 );
    local nomatch = new_args['nomatch'];
      
      
     if s == '' then
     if s == '' then
שורה 204: שורה 206:
      
      
     if result == nil then
     if result == nil then
         return str._error( 'Match not found' );
         if nomatch == nil then
            return str._error( 'Match not found' );
        else
            return nomatch;
        end
     else
     else
         return result;
         return result;
שורה 376: שורה 382:
     return result;
     return result;
end
end
--[[
"Reverse" reverses a string, a'la string.reverse()
]]
str.reverse = function( frame )
    local tab = mw.text.split( frame.args[1] or '', '' )
    local restab = {}
    for i = #tab, 1, -1 do table.insert( restab, tab[i] ) end
    return table.concat( restab )
end
str['היפוך'] = str.reverse


--[[
--[[
שורה 405: שורה 423:
function str._error( error_str )
function str._error( error_str )
     local frame = mw.getCurrentFrame();
     local frame = mw.getCurrentFrame();
     local error_category = frame.args.error_category or 'Errors reported by Module String';
     local error_category = frame.args.error_category or 'שגיאות טיפול במחרוזת';
     local ignore_errors = frame.args.ignore_errors or false;
     local ignore_errors = frame.args.ignore_errors or false;
     local no_category = frame.args.no_category or false;
     local no_category = frame.args.no_category or false;
שורה 415: שורה 433:
     local error_str = '<strong class="error">String Module Error: ' .. error_str .. '</strong>';
     local error_str = '<strong class="error">String Module Error: ' .. error_str .. '</strong>';
     if error_category ~= '' and not str._getBoolean( no_category ) then
     if error_category ~= '' and not str._getBoolean( no_category ) then
        error_str = '[[Category:' .. error_category .. ']]' .. error_str;
    if mw.title.getCurrentTitle().namespace ~= 10 then
        error_str = '[[Category:' .. error_category .. ']]' .. error_str;
        end
     end         
     end         
      
      
שורה 449: שורה 469:
function str._escapePattern( pattern_str )
function str._escapePattern( pattern_str )
     return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" );
     return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" );
end
--[[
contains_any
This function returns the first matched element or empty string
Usage:
{{#invoke:String|contains_any|string|search1|search2|search3...}}
Parameters
    1: string
    2...: strings to search within the first string
]]
function str.contains_any( frame )
for i,v in pairs(frame.args) do
if i>1 then
local m=mw.ustring.find( frame.args[1], v, 0, true)
if m then
    return v
    end
end
    end
    return
end
--[[
contains_any_csv
This function returns the first matched element or empty string.
Usage:
{{#invoke:String|contains_any_csv|string|search1,search2,search3}}
Parameters
    1: string
    2: comma seperated list of strings to search within the first string
]]
function str.contains_any_csv( frame )
for word in mw.ustring.gmatch(frame.args[2], '([^,]+)') do
local m=mw.ustring.find( frame.args[1], word, 0, true)
if m then
    return word
  end
    end
    return
end
str.rep = function( frame )
local strval, repval = frame.args[1] or '', tonumber( frame.args[2] )
if not repval then return '' end
return string.rep( strval, repval )
end
str.join = function(frame)
local res = {}
local sep = ', '
for k,v in pairs(frame.args) do
if k=='sep' then
sep = v
else
if #v>0 then -- avoid empty string
table.insert(res, v)
end
end
end
return table.concat( res, sep)
end
str.encode = function(frame)
return mw.text.encode( frame.args[1] or "" )
end
str["קידוד"] = str.encode
str['מיצוי מספר'] = function(frame)
local s = frame.args[1]
local def = frame.args[2] or ''
if not s then return def end
local int, frac = s:match('[^%d.-]*(%-?[%d,]*)(%.%d+)')
if int or frac then
int = string.gsub(int or '', ',', '') -- remove commas
frac = frac or ''
return int .. frac
end
int = s:match('[^%d.-]*(%-?[%d,]*)')
int = int and string.gsub(int or '', ',', '')
return int or def
end
end


return str
return str