Module:CommaSplit

From Terra Invicta Official Wiki
Revision as of 05:02, 19 January 2026 by Redwah (talk | contribs) (Created page with "-- Helper Module for splitting lines. local p = {} local slen = string.len local sfind = string.find local ssub = string.sub -- Splits the line on the commas. -- Special handling made for one set of double quotations. -- Someone better at coding than I am please generalize this to handle multiple quotations and nested quotations. function p.CommaSplit(text) local ret = {} local s, l = 1, slen( text ) while s do local e, n = sfind( text, ',', s, true)...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:CommaSplit/doc

-- Helper Module for splitting lines.
local p = {}


local slen = string.len
local sfind = string.find
local ssub = string.sub

-- Splits the line on the commas.
-- Special handling made for one set of double quotations.
-- Someone better at coding than I am please generalize this to handle multiple quotations and nested quotations.
function p.CommaSplit(text)
    local ret = {}
    local s, l = 1, slen( text )
    while s do
    	local e, n = sfind( text, ',', s, true)
    	
    	--Deal with one possible double quote. If there are more or even nested quotes... then I don't know.
    	local qe, qn = sfind( text, '"', s, true)
    	if qe and e and qe < e then
    		local qqe, qqn = sfind( text, '"', qe+1, true)
    		e,n = sfind( text, ',', qqe+1, true)
    	end
    	
    	if not e then
    		ret[#ret+1] = ssub ( text, s )
    		s = nil
    	else
    		ret[#ret+1] = e > s and ssub( text, s, e - 1 ) or ''
    		s = e + 1
    	end
    end
    return ret
end

return p