Изменения

+ функция subcount для подсчета количества подстрок внутри строки по запросу на СО модуля
Строка 24: Строка 24:     
local str = {}
 
local str = {}
 +
 +
--[[
 +
subcount
 +
 +
This function returns the count of substring in source string.
 +
 +
Usage:
 +
{{#invoke:String|subcount|source_string|substring|plain_flag}}
 +
OR
 +
{{#invoke:String|subcount|s=source_string|pattern=substring|plain=plain_flag}}
 +
 +
Parameters
 +
    s: The string to search
 +
    pattern: The pattern or string to find within the string
 +
    plain: A flag indicating that the substring should be understood as plain
 +
        text. Defaults to true.
 +
 +
If invoked using named parameters, Mediawiki will automatically remove any leading or
 +
trailing whitespace from the target string. 
 +
]]
 +
function str.subcount( frame )
 +
    local new_args = str._getParameters( frame.args, {'s', 'pattern', 'plain'} );
 +
    local s = new_args['s'] or '';
 +
    local plain_flag = str._getBoolean( new_args['plain'] or true );
 +
    local pattern = new_args['pattern'] or '';
 +
    if s == '' then
 +
    return 0;
 +
    end
 +
   
 +
    if plain_flag then
 +
        pattern = str._escapePattern( pattern );
 +
    end
 +
   
 +
    local _, count = mw.ustring.gsub(s, pattern, "")
 +
    return count;
 +
end
    
--[[
 
--[[
Анонимный участник