Module:String: Difference between revisions
add support for finding the position of a string or pattern inside another string.
m>Anomie (Oh, the existing code is 0-based) |
m>Dragons flight (add support for finding the position of a string or pattern inside another string.) |
||
Line 19: | Line 19: | ||
end | end | ||
--[====[ | |||
str_find | |||
This function duplicates the behavior of {{str_find}}, including all of its quirks. | |||
This is provided in order to support existing templates, but is NOT RECOMMENDED for | |||
new code and templates. New code is recommended to use the "find" function instead. | |||
Returns the first index in "source" that is a match to "target". Indexing is 1-based, | |||
and the function returns -1 if the "target" string is not present in "source". Both | |||
strings have any leading or trailing whitespace removed before searching. | |||
Important Note: If the "target" string is empty / missing, this function returns a | |||
value of "1", which is generally unexpected behavior, and must be accounted for | |||
separatetly. | |||
]====] | |||
function str.str_find( frame ) | |||
local source_str = frame.args.source or ''; | |||
local target_str = frame.args.target or ''; | |||
if target_str == '' then | |||
return 1; | |||
end | |||
local start = mw.ustring.find( source_str, target_str, 1, true ) | |||
if start == nil then | |||
start = -1 | |||
end | |||
return start | |||
end | |||
--[====[ | |||
find | |||
This function allows one to search for a target string or pattern within another | |||
string. | |||
Parameters: | |||
source: The string to search | |||
target: The string or pattern to find within source | |||
start: The index within the source string to start the search, defaults to 1 | |||
plain: Boolean flag indicating that target should be understood as plain | |||
text and not as a Lua style regular expression, defaults to true | |||
This function returns the first index >= "start" where "target" can be found | |||
within "source". Indices are 1-based. If "target" is not found, then this | |||
function returns 0. If either "source" or "target" are missing / empty, this | |||
function also returns 0. | |||
Both "source" and "target" will be trimmed so that any leading or trailing | |||
whitespace is removed prior to searching. This function should be safe for | |||
UTF-8 strings. | |||
]====] | |||
function str.find( frame ) | |||
local source_str = frame.args.source or ''; | |||
local pattern = frame.args.target or ''; | |||
local start_pos = tonumber(frame.args.start) or 1; | |||
local plain = frame.args.plain or true; | |||
if source_str == '' or pattern == '' then | |||
return 0; | |||
end | |||
if type( plain ) == 'string' then | |||
plain = plain:lower(); | |||
if plain == 'false' or plain == 'no' or plain == '0' then | |||
plain = false; | |||
else | |||
plain = true; | |||
end | |||
end | |||
local start = mw.ustring.find( source_str, pattern, start_pos, plain ) | |||
if start == nil then | |||
start = 0 | |||
end | |||
return start | |||
end | |||
return str | return str |