|
|
| വരി 407: |
വരി 407: |
| if plain then | | if plain then |
| pattern = str._escapePattern( pattern ) | | pattern = str._escapePattern( pattern ) |
| replace = mw.ustring.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences. | | replace = string.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences. |
| end | | end |
|
| |
|
| വരി 583: |
വരി 583: |
| ]] | | ]] |
| function str._escapePattern( pattern_str ) | | function str._escapePattern( pattern_str ) |
| return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" ) | | return ( string.gsub( pattern_str, "[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%0" ) ) |
| end | | end |
|
| |
| --[[
| |
| Split
| |
|
| |
| This function Splits a string based on a separator, returns nth substring based on count.
| |
|
| |
| Usage:
| |
| {{#invoke:StringFunc|split|source_string|separator|count}}
| |
|
| |
| Parameters:
| |
| source: The string to return a subset of
| |
| separator: The string to split on
| |
| count: The nth substring based on the separator to return
| |
| ]]
| |
| function str.split( frame )
| |
| local new_args = _getParameters( frame.args, {'source', 'separator', 'count'} )
| |
| local source_str = new_args['source'] or ''
| |
| local separator = (new_args['separator'] or ''):gsub('"', '')
| |
| local separator_len = mw.ustring.len(separator)
| |
| if source_str == '' or separator == '' then
| |
| return source_str;
| |
| end
| |
| local ret_count = tonumber( new_args['count'] ) or 1
| |
| if ret_count < 1 then
| |
| return ""
| |
| end
| |
| local start = 1
| |
| local iter = mw.ustring.find(source_str, separator, start, true)
| |
| if iter == nil then
| |
| if ret_count == 1 then
| |
| return source_str
| |
| else
| |
| return ""
| |
| end
| |
| else
| |
| iter = iter - 1
| |
| end
| |
| if ret_count == 1 then
| |
| return mw.ustring.sub( source_str, start, iter)
| |
| end
| |
| for i=2, ret_count do
| |
| start = iter+separator_len + 1
| |
| iter = mw.ustring.find(source_str, separator, start, true)
| |
| if iter == nil then
| |
| if ret_count == i then
| |
| return mw.ustring.sub(source_str, start, mw.ustring.len(source_str))
| |
| else
| |
| return ""
| |
| end
| |
| else
| |
| iter = iter - 1
| |
| end
| |
| end
| |
| return mw.ustring.sub( source_str,start,iter)
| |
| end
| |
|
| |
| function str.isNumber( frame )
| |
| local new_args = _getParameters( frame.args, {'source'} )
| |
| local source_str = new_args['source'] or ''
| |
| if source_str == '' then
| |
| return "false"
| |
| end
| |
| if tonumber(source_str) == nil and tonumber(string.gsub(source_str, ",", ".", 1) .. '') == nil then
| |
| return "false"
| |
| end
| |
| return "true"
| |
| end
| |
|
| |
|
| |
|
| return str | | return str |