Module:String: Difference between revisions
From Test Wiki
Content deleted Content added
Created page with "--[[ This module is intended to provide access to basic string functions. Most of the functions provided here can be invoked with named parameters, unnamed parameters, or a mixture. If named parameters are used, Mediawiki will automatically remove any leading or trailing whitespace from the parameter. Depending on the intended use, it may be advantageous to either preserve or remove such whitespace. Global options ignore_errors: If set to 'true' or 1, any error c..." |
m 1 revision imported Tags: Mobile edit Mobile web edit |
||
| (6 intermediate revisions by 5 users not shown) | |||
| Line 59: | Line 59: | ||
Parameters |
Parameters |
||
s: The string to return a subset of |
s: The string to return a subset of |
||
i: The |
i: The first index of the substring to return, defaults to 1. |
||
j: The last index of the string to return, defaults to the last character. |
j: The last index of the string to return, defaults to the last character. |
||
| Line 97: | Line 97: | ||
--[[ |
--[[ |
||
This function implements that features of {{ |
This function implements that features of {{subr|s|skip|max}} and |
||
to maintain these older templates. |
is kept in order to maintain these older templates. |
||
Uses only numbered paramameters to preserve spaces (caller may trim |
|||
whitespaces by passing the parameter explicitly by numbering the |
|||
parameters). |
|||
Accepts negative values as relative to strng length. |
|||
No error is returned if attempting to read past end of string or |
|||
before start of string. |
|||
Currently does not parse and replace placeholders for nowiki tags. |
|||
]] |
]] |
||
function str.sublength( frame ) |
function str.sublength( frame ) |
||
local s = frame.args[1] or ''; |
|||
local skip = tonumber( frame.args[2] ) or 0; |
|||
local max = tonumber( frame.args[3] ) or 0; |
|||
| ⚫ | |||
local len = mw.ustring.len( s ); |
|||
-- convert negative values of max and skip as relative to string length |
|||
if max < 0 then |
|||
max = len + max |
|||
end |
|||
if skip < 0 then |
|||
skip = len + skip |
|||
end |
|||
-- check boundaries to reduce the requested max |
|||
if skip < 0 then -- if before start of string |
|||
max = max + skip |
|||
skip = 0 -- skip can't underflow now |
|||
end |
|||
if max > len - skip then -- if past end of string |
|||
max = len - skip -- max can't overflow now |
|||
end |
|||
if max <= 0 then |
|||
return '' -- nothing left to return |
|||
end |
|||
| ⚫ | |||
end |
end |
||
| Line 407: | Line 442: | ||
if plain then |
if plain then |
||
pattern = str._escapePattern( pattern ) |
pattern = str._escapePattern( pattern ) |
||
replace = |
replace = string.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences. |
||
end |
end |
||
| Line 512: | Line 547: | ||
end |
end |
||
-- findpagetext returns the position of a piece of text in a page |
|||
-- First positional parameter or |text is the search text |
|||
-- Optional parameter |title is the page title, defaults to current page |
|||
-- Optional parameter |plain is either true for plain search (default) or false for Lua pattern search |
|||
-- Optional parameter |nomatch is the return value when no match is found; default is nil |
|||
function str._findpagetext(args) |
|||
-- process parameters |
|||
local nomatch = args.nomatch or "" |
|||
if nomatch == "" then nomatch = nil end |
|||
-- |
|||
local text = mw.text.trim(args[1] or args.text or "") |
|||
if text == "" then return nil end |
|||
-- |
|||
local title = args.title or "" |
|||
local titleobj |
|||
if title == "" then |
|||
titleobj = mw.title.getCurrentTitle() |
|||
else |
|||
titleobj = mw.title.new(title) |
|||
end |
|||
-- |
|||
local plain = args.plain or "" |
|||
if plain:sub(1, 1) == "f" then plain = false else plain = true end |
|||
-- get the page content and look for 'text' - return position or nomatch |
|||
local content = titleobj and titleobj:getContent() |
|||
return content and mw.ustring.find(content, text, 1, plain) or nomatch |
|||
end |
|||
function str.findpagetext(frame) |
|||
local args = frame.args |
|||
local pargs = frame:getParent().args |
|||
for k, v in pairs(pargs) do |
|||
args[k] = v |
|||
end |
|||
if not (args[1] or args.text) then return nil end |
|||
-- just the first value |
|||
return (str._findpagetext(args)) |
|||
end |
|||
--[[ |
--[[ |
||
Helper function that populates the argument list given that user may need to use a mix of |
Helper function that populates the argument list given that user may need to use a mix of |
||
| Line 583: | Line 655: | ||
]] |
]] |
||
function str._escapePattern( pattern_str ) |
function str._escapePattern( pattern_str ) |
||
return |
return ( string.gsub( pattern_str, "[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%0" ) ) |
||
end |
end |
||