function Util.ParseEscapeSequences(Q)return((Q:gsub("\092\040\046\041",{t="\t",n="\n"})):gsub("\092\117\040\037\120\037\120\037\120\037\120\041",charCode)):gsub("\092\120\040\037\120\037\120\041",charCode)end function Util.EncodeEscapedOperator(Q,x)local H=x:sub(1,1)local q=x:gsub("\046","\037\037\037\049")local E="\037"..H return Q:gsub("\040"..(E..("\043\041\040"..(q.."\041"))),function(Q,x)return(Q:sub(1,#Q-1)..x):gsub("\046",function(Q)return"\092\117"..string.format("\037\048\052\120",string.byte(Q),16)end)end)end local Q={"\038\038";"\124\124";"\059"}function Util.EncodeEscapedOperators(x)for Q,H in ipairs(Q)do x=Util.EncodeEscapedOperator(x,H)end return x end local function x(Q)return(((Q:gsub("\092\092","\095\095\095\033\067\077\068\082\095\069\083\067\065\080\069\033\095\095\095")):gsub("\092\"","\095\095\095\033\067\077\068\082\095\081\085\079\084\069\033\095\095\095")):gsub("\092\'","\095\095\095\033\067\077\068\082\095\083\081\085\079\084\069\033\095\095\095")):gsub("\092\n","\095\095\095\033\067\077\068\082\095\078\076\033\095\095\095")end local function H(Q)return((Q:gsub("\095\095\095\033\067\077\068\082\095\069\083\067\065\080\069\033\095\095\095","\092")):gsub("\095\095\095\033\067\077\068\082\095\081\085\079\084\069\033\095\095\095","\"")):gsub("\095\095\095\033\067\077\068\082\095\078\076\033\095\095\095","\n")end
--- Parses escape sequences into their fully qualified characters
function Util.ParseEscapeSequences(text)
	return text:gsub("\\(.)", {
		t = "\t",
		n = "\n",
	})
		:gsub("\\u(%x%x%x%x)", charCode)
		:gsub("\\x(%x%x)", charCode)
end

function Util.EncodeEscapedOperator(text, op)
	local first = op:sub(1, 1)
	local escapedOp = op:gsub(".", "%%%1")
	local escapedFirst = "%" .. first

	return text:gsub("(" .. escapedFirst .. "+)(" .. escapedOp .. ")", function(esc, op)
		return (esc:sub(1, #esc - 1) .. op):gsub(".", function(char)
			return "\\u" .. string.format("%04x", string.byte(char), 16)
		end)
	end)
end

local OPERATORS = { "&&", "||", ";" }
function Util.EncodeEscapedOperators(text)
	for _, operator in ipairs(OPERATORS) do
		text = Util.EncodeEscapedOperator(text, operator)
	end

	return text
end

local function encodeControlChars(text)
	return (
		text:gsub("\\\\", "___!CMDR_ESCAPE!___")
			:gsub('\\"', "___!CMDR_QUOTE!___")
			:gsub("\\'", "___!CMDR_SQUOTE!___")
			:gsub("\\\n", "___!CMDR_NL!___")
	)
end

local function decodeControlChars(text)
	return (text:gsub("___!CMDR_ESCAPE!___", "\\"):gsub("___!CMDR_QUOTE!___", '"'):gsub("___!CMDR_NL!___", "\n"))
end
