-- Format substring but do not enclose in double quote marks.
-- The replacement is compatible with pretty-format package.

local function printSubstring(val: string): string
	val = val:gsub("(\\)", "\\%1")
	val = val:gsub('(")', "\\%1")
	return val
end

local function printReceivedStringContainExpectedSubstring(
	received: string,
	start: number,
	length: number -- not end
): string
	return RECEIVED_COLOR('"' .. printSubstring(received:sub(0, start - 1)))
		.. INVERTED_COLOR(printSubstring(received:sub(start, start + length - 1)))
		.. RECEIVED_COLOR(printSubstring(received:sub(start + length, #received)))
		.. '"'
end

local function printReceivedStringContainExpectedResult(received: string, result): string
	-- result passed in should be the result of making a call to RegExp:exec
	if result == nil then
		return printReceived(received)
	else
		return printReceivedStringContainExpectedSubstring(received, result.index, #result[1])
	end
end
