Module:Sarang/string
Jump to navigation
Jump to search
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules
Examples
[edit]Input | Output | Notes |
---|---|---|
{{#invoke:Sarang/string|unwrap|Test text string}}
|
Test text string | no match (text without delimiters) |
{{#invoke:Sarang/string|unwrap|Test text string"}}
|
Test text string" | only one match |
{{#invoke:Sarang/string|unwrap|"Test text string"}}
|
Test text string | both matching (default) |
{{#invoke:Sarang/string|unwrap|-Test text string-|-}}
|
Test text string | both matching (left specified, right defaulted with left one) |
{{#invoke:Sarang/string|unwrap|"Test text string°||°}}
|
"Test text string° | both matching (left defaulted, right specified) |
{{#invoke:Sarang/string|unwrap|(Test text string)|(|)}}
|
Test text string | both matching, both specified |
{{#invoke:Sarang/string|unwrap|«Test text string»|«|»}}
|
Test text string | both matching, both specified |
{{#invoke:Sarang/string|unwrap|오사랑시|오|시}}
|
오사랑시 | both matching; UTF-8 example |
{{#invoke:Sarang/string|unpeel|(Test text string)|(|)}}
|
Test text string | unwraps two characters, as above |
{{#invoke:Sarang/string|unpeel|aabbbcc|aa|cc}}
|
bbb | unpeels heading and trailing characters |
{{#invoke:Sarang/string|unpeel|abcdefg|abc|}}
|
defg | unpeel only heading (trail is "nil") |
{{#invoke:Sarang/string|unpeel|uvwxyz||yz}}
|
uvwx | unpeel only trailing (head is "nil") |
{{#invoke:Sarang/string|unpeel|total|tot|tal}}
|
unpeel total (overlap) | |
{{#invoke:Sarang/string|unpeel|total|total|}}
|
unpeel total (trail is "nil") | |
{{#invoke:Sarang/string|unpeel|toolong|toolongx|}}
|
toolong | too long compare |
{{#invoke:Sarang/string|unpeel|Test||}}
|
Test | both "nil" |
{{#invoke:Sarang/string|unlist|#abcdef}}
|
#abcdef | returns #abcdef |
{{#invoke:Sarang/string|unlist|*abcdef}}
|
*abcdef | returns *abcdef |
{{#invoke:Sarang/string|unlist|:abcdef}}
|
:abcdef | returns :abcdef |
{{#invoke:Sarang/string|unlist|;abcdef}}
|
;abcdef | returns ;abcdef |
{{#invoke:Sarang/string|unlist|$abcdef}}
|
$abcdef | returns $abcdef unchanged |
</noinclude>
Code
local str = {}
---------------------------
--[[
unwrap
This function can remove delimiters of a string.
Usage:
{{#invoke:String|unwrap|source_st|pattern_l|pattern_r}}
Parameters
source_st: The string in question
pattern_l: The one-byte-pattern of the first character, or left delimiter, of the string; default is '"'
pattern_r: The one-byte-pattern of the last character, or right delimiter, of the string; default is pattern_l
When both delimiters fit, they are removed; otherwise the string is returned as-it-is.
Examples:
{{#invoke:Sarang/sandbox|unwrap|"Test text string"}} returns Test text string
{{#invoke:Sarang/sandbox|unwrap|-Test text string-|-}} returns Test text string
{{#invoke:Sarang/sandbox|unwrap|"Test text string.||.}} returns Test text string
{{#invoke:Sarang/sandbox|unwrap|(Test text string)|(|)}} returns Test text string
{{#invoke:Sarang/sandbox|unwrap|«Test text string»|«|»}} returns Test text string
but no unwrapping occurs, when not both delimiters fit:
{{#invoke:String|unwrap|"Test text string.}} returns "Test text string.
This function should be safe for UTF-8 strings:
{{#invoke:Sarang/sandbox|unwrap|오사랑시|오|시}} returns 사랑
unpeel
This function can remove a heading and/or a trailing part of a string.
Usage:
{{#invoke:String|unpeel|source_st|begin-str|trail-str}}
Parameters
source_st: The string in question
begin-str: The string to compare with the begin of the source string; default is the "nil" value.
trail-str: The string to compare with the end of the source string; default is the "nil" value.
When both strings fit, unpeeling occurs; in the case of total unpeeling, nothing will be returned.
Examples:
{{#invoke:Sarang/sandbox|unpeel|(Test text string)|(|)}} returns Test text string
{{#invoke:Sarang/sandbox|unpeel|aabbbcc|aa|cc}} returns bbb
{{#invoke:Sarang/sandbox|unpeel|abcdefg|abc|}} returns defg
{{#invoke:Sarang/sandbox|unpeel|uvwxyz||yz}} returns uvwx
{{#invoke:Sarang/sandbox|unpeel|total|tot|tal}} returns "nil" unpeel total (overlap)
{{#invoke:Sarang/sandbox|unpeel|total|total|}} returns "nil" unpeel total
{{#invoke:Sarang/sandbox|unpeel|toolong|toolongx|}} returns toolong (no fit - no unpeel)
unlist
This function can replace a special character at the first byte of the string.
Usage:
{{#invoke:String|unlist|source_st}}
Parameter
source_st: The string in question
When the first byte is a special list character as "#", "*", ";" or ":" it is replaced by its Unicode notation.
Examples:
{{#invoke:Sarang/sandbox|unlist|#abcdef}} returns #abcdef
{{#invoke:Sarang/sandbox|unlist|*abcdef}} returns *abcdef
{{#invoke:Sarang/sandbox|unlist|:abcdef}} returns :abcdef
{{#invoke:Sarang/sandbox|unlist|;abcdef}} returns ;abcdef
{{#invoke:Sarang/sandbox|unlist|abcdefg}} returns abcdefg (no special character, returned unchanged)
led_segm
This function converts a string which may contain letters a...g to an 7-bytes bit pattern.
Usage:
{{#invoke:String|led_segm| {{BASEPAGE}} }}
]]
function str.unwrap( frame )
local new_args = str._getParameters( frame.args, {'st', 'pl', 'pr' } );
local str = new_args['st'] or ''
local pat = new_args['pl'] or '"'
local pex = new_args['pr'] or pat
if mw.ustring.byte ( str, 1 ) == mw.ustring.byte ( pat )
and mw.ustring.byte ( str, #str ) == mw.ustring.byte ( pex )
then
return mw.ustring.sub( str, 2, #str - 1 )
else
return str
end
end
function str.unpeel( frame )
local new_args = str._getParameters( frame.args, {'st', 'p1', 'p2' } );
local str = new_args['st'] or '';
local pp1 = new_args['p1'] or '';
local pp2 = new_args['p2'] or '';
if mw.ustring.sub ( str, 1, #pp1 ) == pp1
and mw.ustring.sub ( str, #str + 1 - #pp2, #str ) == pp2
then
return mw.ustring.sub( str, #pp1 + 1, #str - #pp2 );
else
return str;
end
end
function str.unlist( frame )
local new_args = str._getParameters( frame.args, { 'st' } );
local str = new_args['st'] or '';
if mw.ustring.sub ( str, 1, 1 ) == '#' then
return '#' .. mw.ustring.sub( str, 2, #str );
elseif mw.ustring.sub ( str, 1, 1 ) == '*' then
return '*' .. mw.ustring.sub( str, 2, #str );
elseif mw.ustring.sub ( str, 1, 1 ) == ':' then
return ':' .. mw.ustring.sub( str, 2, #str );
elseif mw.ustring.sub ( str, 1, 1 ) == ';' then
return ';' .. mw.ustring.sub( str, 2, #str );
elseif mw.ustring.sub ( str, 1, 1 ) == '|' then
return '|' .. mw.ustring.sub( str, 2, #str );
else
return str;
end
end
-- "File:DEK Deutsche Einheitskurzschrift - Verkehrsschrift - ?????.svg"
function str.dekname ( frame )
local args = frame.args;
return mw.ustring.sub ( args[1], 54, #args[1]-4 );
end
--
function str.led_segm ( frame )
local args = frame.args;
local part = mw.ustring.sub ( args[1], 11, #args[1]-4 ); -- "File:7-segment ???.svg"
local patt = '';
if mw.ustring.find( part, 'a') then patt = patt .. '1' else patt = patt .. '0'; end;
if mw.ustring.find( part, 'b') then patt = patt .. '1' else patt = patt .. '0'; end;
if mw.ustring.find( part, 'c') then patt = patt .. '1' else patt = patt .. '0'; end;
if mw.ustring.find( part, 'd') then patt = patt .. '1' else patt = patt .. '0'; end;
if mw.ustring.find( part, 'e') then patt = patt .. '1' else patt = patt .. '0'; end;
if mw.ustring.find( part, 'f') then patt = patt .. '1' else patt = patt .. '0'; end;
if mw.ustring.find( part, 'g') then patt = patt .. '1' else patt = patt .. '0'; end;
return patt;
end
---------------------------
function str._getParameters( frame_args, arg_list )
local new_args = {};
local index = 1;
local value;
for i,arg in ipairs( arg_list ) do
value = frame_args[arg]
if value == nil then
value = frame_args[index];
index = index + 1;
end
new_args[arg] = value;
end
return new_args;
end
function str.gsubst ( frame ) -- replace all "/" by "|"
local gpar = frame.args;
if gpar[1] == '' then
gpar = mw.getCurrentFrame():getParent().args;
end
local strg = gpar[1] or ' ';
local patt = gpar[2] or '/'; -- or '%/'
local repl = gpar[3] or '|';
if patt == '/+/' then patt = '|%+|' end;
return mw.ustring.gsub( strg, patt, repl ),_
end
return str