function Util.SplitString(g,F)g=encodeControlChars(g)F=F or math.huge local b={}local J,v,M,P="\094\040\091\'\"\093\041","\040\091\'\"\093\041\036"for g in g:gmatch("\037\083\043")do g=Util.ParseEscapeSequences(g)local z=g:match(J)local S=g:match(v)local i=g:match("\040\092\042\041\091\'\"\093\036")if z and(not P and not S)then M,P=g,z elseif M and(S==P and#i%2==0)then g,M,P=M..(" "..g),nil,nil elseif M then M=M..(" "..g)end if not M then b[#b+(#b>F and 0 or 1)]=decodeControlChars((g:gsub(J,"")):gsub(v,""))end end if M then b[#b+(#b>F and 0 or 1)]=decodeControlChars(M)end return b end
--- Splits a string by space but taking into account quoted sequences which will be treated as a single argument.
function Util.SplitString(text, max)
	text = encodeControlChars(text)
	max = max or math.huge
	local t = {}
	local spat, epat, buf, quoted = [=[^(['"])]=], [=[(['"])$]=]
	for str in text:gmatch("%S+") do
		str = Util.ParseEscapeSequences(str)
		local squoted = str:match(spat)
		local equoted = str:match(epat)
		local escaped = str:match([=[(\*)['"]$]=])
		if squoted and not quoted and not equoted then
			buf, quoted = str, squoted
		elseif buf and equoted == quoted and #escaped % 2 == 0 then
			str, buf, quoted = buf .. " " .. str, nil, nil
		elseif buf then
			buf = buf .. " " .. str
		end
		if not buf then
			t[#t + (#t > max and 0 or 1)] = decodeControlChars(str:gsub(spat, ""):gsub(epat, ""))
		end
	end

	if buf then
		t[#t + (#t > max and 0 or 1)] = decodeControlChars(buf)
	end

	return t
end
