Modulo:mchkparametrojn: Malsamoj inter versioj

Enhavo forigita Enhavo aldonita
Taylor 49 (diskuto | kontribuoj)
fixing bugs
Taylor 49 (diskuto | kontribuoj)
fix
Linio 1:
--[[
 
MODULE "MPIKSUBSTRINDmchkparametrojn" (pickcheck substring by indexparametrojn)
 
"eo.wiktionary.org/wiki/Modulo:mpiksubstrindmchkparametrojn"
 
Benefit: picksperforms a2 substringcomparisons fromin aorder comma-separatedto string,check thisvalidity
of the anonymous parameters submitted the caller, even
helps to pick a string from a two-dimensional table
empty parameters do count
 
Utilo: efektivigas 2 komparojn por kontroli validecon
Utilo: elprenas subsignocxenon el perkome apartigita signocxeno, tio
de la anonimaj parametroj transdonitaj al la vokanto, ecx
helpas elpreni signocxenon el dudimensia tabelo
malplenaj parametroj estas alsumigataj
 
Used by templates / Uzata far sxablonoj:
- multaj
- eltblkemele
- eltbllingvokodoinfo
- eltblmonato
- eltblnumeralo
- eltbltagodesemajno
 
Incoming: - string2 (lengthintegers with lower limit (must be >=1...9) char, comma-separatedand substrings,upper
emptylimit substrings(must dobigger countor asequal errorthe iflower thelimit and searchless reachesthan such64)
Returned: - string "1" if the parameters are OK, string "0" if
- index of the substring (ZERO-based, must be integer 0...63,
toothe highparameters indexsupplied ieto "notthe found"caller countsare asbad, error)or this
module itself became victim of misuze
Returned: - found substring if success or string "eraro" if something
goes wrong (when copying, possible leading and trailing
whitespace is being removed, while inner whitespace is
preserved but reduced and converted to exactly one space)
 
This module is unbreakable. Every/ imaginableCxi inputtiu willmodulo outputestas nerompebla.
either a useful result or the string "eraro".
 
Usage example / Ekzemplo de uzo:
Cxi tiu modulo estas nerompebla. Cxiu imagebla enigo eldonas aux
utilan rezulton aux signocxenon "eraro".
 
"{{#invoke:mchkparametrojn|ek|2|4}}"
Usage examples with results / Ekzemploj de uzo kun rezultoj:
 
"{{#invoke:mpiksubstrong}}"
 
-> Lua error
 
"{{#invoke:mpiksubstrind|fek}}"
 
-> Lua error
 
"{{#invoke:mpiksubstrind|ek}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek||}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|||}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|X}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|X|}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|X|999}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek||XX|}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|XX|XX|}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek||0}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|X|0}}"
 
-> "X"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,kvar,kvin|4}}"
 
-> "kvar"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,kvar,kvin|5}}"
 
-> "kvin"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,kvar,kvin|6}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek| nul ,unu,du , tri,kvar, kvin |5}}"
 
-> "kvin"
 
"{{#invoke:mpiksubstrind|ek| nul,unu, du
,
tri ,kvar,kvin |4}}"
 
-> "kvar"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,kvar,kv i
n|5}}"
 
-> "kv i n"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,kvar, |5}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,kvar,|5}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,kvar,,|5}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,,kvar,kvin|3}}"
 
-> "tri"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,,kvar,kvin|4}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,,kvar,kvin|5}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri, ,kvar,kvin|5}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,unu,kvin|4}}"
 
-> "unu"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,kvar,kvin|0}}"
 
-> "nul"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,kvar,kvin|00}}"
 
-> "nul"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,kvar,kvin|000}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,kvar,kvin|4.99}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|nul,unu,du,tri,kvar,kvin|-1}}"
 
-> "eraro"
 
"{{#invoke:mpiksubstrind|ek|a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,
a,b,x,y,z,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t|63}}"
 
-> "y"
 
"{{#invoke:mpiksubstrind|ek|a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,
a,b,x,y,z,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t|64}}"
 
-> "eraro"
 
]]
 
local piksubstrindchkparametrojn = {}
function piksubstrindchkparametrojn.ek (frame)
local strargstrtmp = "" ; -- the long input string, also temp for decimal conveconversion
local strret = "0" ; -- output string
local numsimnumlong = 0 ; -- temp for decimal conversion (length of parameter)
local numsinnumuppm = 2550 ; -- indextemp offor thedecimal string,conversion also(no tempneed for decimalto converspreset)
local numlengthnumuppl = 0255 ; -- lengthupper oflimit the(preset inputto string, also temp for dec convinvalid)
local numcharnumlowl = 0 255 ; -- unicode codepointlower oflimit a(preset singleto charinvalid)
local numitenumiter = 0 ; -- iteration index, countsiterating unicodethrough chars,parameters ONE-basedof the caller
local boofnd = false ; -- multipurpose found flag
local boospc = false ; -- flag for whitespace reduction feature
local boouch = false ; -- flag for found a useful char when skipping string
if ((frame.args [1]) and (frame.args [2])) then
strargstrtmp = frame.args [21] ; -- indexlower oflimit substring(1 digit)
numlengthnumlong = mw.ustring.len (strargstrtmp) ; -- temp for decimal conversion
if ((numlengthnumlong==1) or (numlength==2)) then
numsinnumlowl = mw.ustring.codepoint (strargstrtmp,1,1) - 48 ; -- may be invalid
if ((numlowl<0) or (numlowl>9)) then
numlowl = 255 ; -- 255 is invalid, note that ZERO would be invalid too
end--if
end--if
strtmp = frame.args [2] ; -- upper limit (1 or 2 digits)
if (numlength==2) then
numsimnumlong = mw.ustring.codepointlen (strarg,2,2strtmp) - 48 ; -- maytemp for bedecimal invalidconversion
if ((numlong==1) or (numlong==2)) then
numuppl = mw.ustring.codepoint (strtmp,1,1) - 48 ; -- may be invalid
if ((numuppl<0) or (numuppl>9)) then
numuppl = 255 ; -- 255 is invalid, note that ZERO would be invalid too
end--if
end--if
if ((numsin<0) or (numsin>9) or (numsim<0) or (numsim>9)numlong==2) then
numuppm = mw.ustring.codepoint (strtmp,2,2) - 48 ; -- may be invalid
numsin = 255 ; -- 255 is invalid, note that ZERO would be valid
if ((numuppm<0) or (numuppm>9)) then
else
numuppl = 255 ; -- 255 is invalid, note that ZERO would be invalid too
if (numlength==2) then
else
numsin = numsin * 10 + numsim ; -- valid integer number 0...99 now
numuppl = numuppl * 10 + numuppm ; -- valid integer number 0...99 now
end--if
end--if
strarg = frame.args [1] ; -- the long input string
numlength = mw.ustring.len (strarg) ; -- length of the input string
end--if
if ((numlengthnumlowl>01) and (numsinnumlowl<10) and (numuppl>=numlowl) and (numuppl<64)) then
while (true) do -- iterate through substringsparameters inof orderthe to skip themcaller
if (not (mw.getCurrentFrame():getParent().args[numiter+1])) then
if (numsin==0) then -- nothing to skip anymore
break ; -- no more parameters, "numiter" holds amount of those found
boofnd = true ;
break ; -- exit the outer loop, nothing to skip anymore
end--if
numsin = numsin - 1 ;
boofnd = false ;
boouch = false ;
while (true) do -- iterate through whspace & chars mix looking for comma
if (numite>=numlength) then
break ; -- exit the inner loop, NOT found, give up
end--if
numite = numite + 1 ;
numchar = mw.ustring.codepoint (strarg,numite,numite) ;
if (numchar>32) and (numchar~=44) then
boouch = true ; -- found a valid char (empty field is an error)
end--if
if (numchar==44) then
if (boouch==true) then
boofnd = true ;
end--if
break ; -- exit the inner loop, found a comma, this is good or bad
end--if
end--while
if (boofnd==false) then
break ; -- exit the outer loop, NOT found or empty field, give up
end--if
numiter = numiter + 1 ;
end--while
if (boofnd(numiter>=numlowl) and (numiter<=truenumuppl)) then
strret = "1" ; -- output string (report success)
boofnd = false ;
while (true) do -- iterate through chars skipping whitespace
if (numite>=numlength) then
break ; -- exit the loop, NOT found, give up
end--if
numite = numite + 1 ;
numchar = mw.ustring.codepoint (strarg,numite,numite) ;
if (numchar>32) then
if (numchar==44) then
break ; -- exit the loop, found a comma, give up according to the rules
end--if
boofnd = true ;
break ; -- exit the loop, found valid char, start copying now
end--if
end--while
end--if
if (boofnd==true) then
while (true) do -- iterate through chars copying the found string
if (numchar==32) then
boospc=true ;
else
if (boospc==true) then
strret = strret .. " " ;
boospc = false ;
end--if
strret = strret .. mw.ustring.char (numchar) ;
end--if
if (numite>=numlength) then
break ; -- exit the loop, done
end--if
numite = numite + 1 ;
numchar = mw.ustring.codepoint (strarg,numite,numite) ;
if (numchar==44) then
break ; -- exit the loop, done
end--if
end--while
end--if
end--if
if (boofnd==false) then
strret = "eraro" ;
end--if
return strret
end--function
return piksubstrindchkparametrojn