FANDOM


local txt = {}
 
function txt.esplit(text, pattern, exceptin, plain, capture)
    if not exceptin then return txt.split(text, pattern, plain, capture) end
    local ret, a = {}
	for m, n in txt.gsplit( text, exceptin, plain ) do
    	for o, p in txt.gsplit( m, pattern, plain ) do
    	    if a then
    	        ret[a] = ret[a] .. o
    	        a = nil
    	    else
    		    ret[#ret+1] = o
    		end
    		if capture then ret[#ret+1] = p end
    	end
		if n then
	        ret[#ret] = ret[#ret] .. n
	        a = #ret
	    end
	end
	return ret
end
function txt.split(text, pattern, plain, capture)
	local ret = {}
	for m, n in txt.gsplit( text, pattern, plain ) do
		ret[#ret+1] = m
		if capture then ret[#ret+1] = n end
	end
	return ret
end
function txt.gsplit( text, pattern, plain )
	local s, l = 1, mw.ustring.len( text )
	return function ()
		if s then
			local e, n = mw.ustring.find( text, pattern, s, plain )
			local ret1, ret2
			if not e then
				ret1 = mw.ustring.sub( text, s )
				s = nil
			elseif n < e then
				-- Empty separator!
				ret1 = mw.ustring.sub( text, s, e )
				ret2 = ''
				if e < l then
					s = e + 1
				else
					s = nil
				end
			else
				ret1 = e > s and mw.ustring.sub( text, s, e - 1 ) or ''
				ret2 = mw.ustring.sub( text, e, n )
				s = n + 1
			end
			return ret1, ret2
		end
	end, nil, nil
end
function txt.gsub(text, pattern, replacement, n, exceptin)
    if not exceptin then return mw.ustring.gsub(text, pattern, replacement, n) end
    local res = txt.split(text, exceptin, false, true)
    local sum, no = 0
    for i=1,#res,2 do
        res[i], no = mw.ustring.gsub(res[i], pattern, replacement, n)
        sum = sum + no
        if n and n-sum < 1 then break end
    end
 
    return table.concat(res, ''), sum
end
 
return txt

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.