function Util.SplitString(q,K)q=encodeControlChars(q)K=K or math.huge local k={}local B,f,H,z="\094\040\091\'\"\093\041","\040\091\'\"\093\041\036"for q in q:gmatch("\037\083\043")do q=Util.ParseEscapeSequences(q)local N=q:match(B)local G=q:match(f)local g=q:match("\040\092\042\041\091\'\"\093\036")if N and(not z and not G)then H,z=q,N elseif H and(G==z and#g%2==0)then q,H,z=H..(" "..q),nil,nil elseif H then H=H..(" "..q)end if not H then k[#k+(#k>K and 0 or 1)]=decodeControlChars((q:gsub(B,"")):gsub(f,""))end end if H then k[#k+(#k>K and 0 or 1)]=decodeControlChars(H)end return k end

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
