48,403
edits
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
local m_string_utils = require("Module:string utilities") | |||
local str = {} | local str = {} | ||
local decode_uri = m_string_utils.decode_uri | |||
local | local gsub = string.gsub | ||
local pattern_escape = m_string_utils.pattern_escape | |||
local process_params = require("Module:parameters").process | |||
local replacement_escape = m_string_utils.replacement_escape | |||
local reverse = string.reverse | |||
local ufind = m_string_utils.find | |||
local ugmatch = m_string_utils.gmatch | |||
local ugsub = m_string_utils.gsub | |||
local ulen = m_string_utils.len | |||
local ulower = m_string_utils.lower | |||
local umatch = m_string_utils.match | |||
local unpack = unpack | |||
local usub = m_string_utils.sub | |||
--[[ | --[[ | ||
| Line 38: | Line 35: | ||
]] | ]] | ||
function str.len(frame) | function str.len(frame) | ||
return ulen(str._getParameters(frame.args, {"s"}).s or "") | |||
end | end | ||
| Line 51: | Line 46: | ||
]] | ]] | ||
function str.len_visible(frame) | function str.len_visible(frame) | ||
return ulen(ugsub(str._getParameters(frame.args, {"s"}).s or "", "<[^<>]+>", "")) | |||
end | end | ||
| Line 80: | Line 73: | ||
]] | ]] | ||
function str.sub(frame) | function str.sub(frame) | ||
local new_args = str._getParameters(frame.args, { | local new_args = str._getParameters(frame.args, {"s", "i", "j"}); | ||
local s = new_args[ | local s = new_args["s"] or ""; | ||
local i = tonumber(new_args[ | local i = tonumber(new_args["i"]) or 1; | ||
local j = tonumber(new_args[ | local j = tonumber(new_args["j"]) or -1; | ||
local len = | local len = ulen(s); | ||
-- Convert negatives for range checking | -- Convert negatives for range checking | ||
| Line 95: | Line 88: | ||
end | end | ||
if i > len or j > len | if i > len or j > len then | ||
return str._error('String subset index out of range'); | return str._error('String subset index out of range'); | ||
end | end | ||
| Line 102: | Line 95: | ||
end | end | ||
return | return usub(s, i, j) | ||
end | end | ||
| Line 112: | Line 105: | ||
local i = tonumber(frame.args.i) or 0 | local i = tonumber(frame.args.i) or 0 | ||
local len = tonumber(frame.args.len) | local len = tonumber(frame.args.len) | ||
return | return usub(frame.args.s, i + 1, len and (i + len)) | ||
end | end | ||
| Line 172: | Line 165: | ||
return str._error('Pattern string is empty'); | return str._error('Pattern string is empty'); | ||
end | end | ||
if math.abs(start) < 1 or math.abs(start) > | if math.abs(start) < 1 or math.abs(start) > ulen(s) then | ||
return str._error('Requested start is out of range'); | return str._error('Requested start is out of range'); | ||
end | end | ||
| Line 179: | Line 172: | ||
end | end | ||
if plain_flag then | if plain_flag then | ||
pattern = | pattern = pattern_escape(pattern); | ||
end | end | ||
| Line 185: | Line 178: | ||
if match_index == 1 then | if match_index == 1 then | ||
-- Find first match is simple case | -- Find first match is simple case | ||
result = | result = umatch(s, pattern, start) | ||
else | else | ||
if start > 1 then | if start > 1 then | ||
s = | s = usub(s, start); | ||
end | end | ||
local iterator = | local iterator = ugmatch(s, pattern); | ||
if match_index > 0 then | if match_index > 0 then | ||
-- Forward search | -- Forward search | ||
| Line 255: | Line 248: | ||
local pos = tonumber(new_args['pos']) or 0; | local pos = tonumber(new_args['pos']) or 0; | ||
if pos == 0 or math.abs(pos) > | if pos == 0 or math.abs(pos) > ulen(target_str) then | ||
return str._error('String index out of range'); | return str._error('String index out of range'); | ||
end | end | ||
return | return usub(target_str, pos, pos); | ||
end | end | ||
| Line 285: | Line 278: | ||
end | end | ||
local start = | local start = ufind(source_str, target_str, 1, true) | ||
if start == nil then | if start == nil then | ||
start = -1 | start = -1 | ||
| Line 323: | Line 316: | ||
]] | ]] | ||
function str.find(frame) | function str.find(frame) | ||
local | local main_param = {required = true, allow_empty = true} | ||
return ufind(unpack(process_params(frame.args, { | |||
[ | [1] = main_param, | ||
[3] = { type = "number" }, | [2] = main_param, | ||
[4] = { type = "boolean" }, | [3] = {type = "number"}, | ||
} | [4] = {type = "boolean"}, | ||
}))) | |||
end | end | ||
| Line 369: | Line 359: | ||
if plain then | if plain then | ||
pattern = | pattern = pattern_escape(pattern); | ||
replace = | replace = replacement_escape(replace); | ||
end | end | ||
local result; | local result; | ||
result = | result = ugsub(source_str, pattern, replace, count); | ||
return result; | return result; | ||
| Line 382: | Line 372: | ||
function str.gsub(frame) | function str.gsub(frame) | ||
local | local main_param = {required = true, allow_empty = true, no_trim = true} | ||
return (ugsub(unpack(process_params(frame.args, { | |||
[ | [1] = main_param, | ||
[3] = | [2] = main_param, | ||
[4] = { type = "number" }, | [3] = main_param, | ||
} | [4] = {type = "number"}, | ||
})))) | |||
end | end | ||
| Line 408: | Line 395: | ||
function str.lower(frame) | function str.lower(frame) | ||
return ulower(frame.args[1] or "") | |||
end | end | ||
str.lc = str.lower | str.lc = str.lower | ||
| Line 512: | Line 497: | ||
end | end | ||
return boolean_value | return boolean_value | ||
end | end | ||
| Line 556: | Line 509: | ||
if plain then | if plain then | ||
pattern = | pattern = pattern_escape(pattern) | ||
end | end | ||
local _, count = | local _, count = ugsub(text, pattern, "") | ||
return count | return count | ||
end | end | ||
| Line 621: | Line 531: | ||
} | } | ||
local args = | local args = process_params(frame.args, params) | ||
text = args[1] | text = args[1] | ||
| Line 636: | Line 546: | ||
local matches = {} | local matches = {} | ||
local i = 0 | local i = 0 | ||
for match in | for match in ugmatch(text, pattern) do | ||
i = i + 1 | i = i + 1 | ||
matches[i] = match | matches[i] = match | ||
| Line 689: | Line 599: | ||
pattern = "(" .. pattern .. ")" | pattern = "(" .. pattern .. ")" | ||
end | end | ||
local find = use_basic_Lua_function and string.find or | local find = use_basic_Lua_function and string.find or ufind | ||
return function() | return function() | ||
i = i + 1 | i = i + 1 | ||
| Line 702: | Line 612: | ||
end | end | ||
end | end | ||
end | end | ||
function str.URIdecode(frame) | function str.URIdecode(frame) | ||
return | return decode_uri(frame.args[1], frame.args[2] or "PATH") | ||
end | end | ||
function str:__index(k) | |||
function str | self[k] = package.loaders[2]("Module:string/" .. k)() | ||
return self[k] | |||
return | |||
end | end | ||
return str | return setmetatable(str, str) | ||