![]() |
VOOZH | about |
This is an exact copy of the built-in mw.text.gsplit, mw.text.split, and mw.text.trim functions, just using the significantly faster string library instead of the ustring library. As such, this won't work properly if you're trying to split a string by unicode characters, or trim unicode characters from a string. Unicode characters being in the string should be fine though.
-- Source: https://phabricator.wikimedia.org/diffusion/ELUA/browse/master/includes/Engines/LuaCommon/lualib/mw.text.lua localp={} functionp.gsplit(text,pattern,plain) locals,l=1,text:len() returnfunction() ifsthen locale,n=text:find(pattern,s,plain) localret ifnotethen ret=text:sub(s) s=nil elseifn<ethen -- Empty separator! ret=text:sub(s,e) ife<lthen s=e+1 else s=nil end else ret=e>sandtext:sub(s,e-1)or'' s=n+1 end returnret end end,nil,nil end functionp.split(text,pattern,plain) localret={} forminp.gsplit(text,pattern,plain)do ret[#ret+1]=m end returnret end functionp.trim(s,charset) ifnotcharsetthen returnstring.match(s,'^()%s*$')and''orstring.match(s,'^%s*(.*%S)') else return(string.gsub(s,'^['..charset..']*(.-)['..charset..']*$','%1')) end end returnp