-- Compare two strings character-by-character.
-- Format as comparison lines in which changed substrings have inverse colors.

local function diffStringsUnified(a: string, b: string, options: DiffOptions?): string
	if a ~= b and #a ~= 0 and #b ~= 0 then
		local isMultiline = a:find("\n") ~= nil or b:find("\n") ~= nil

		-- getAlignedDiffs assumes that a newline was appended to the strings.
		local diffs = diffStringsRaw(
			isMultiline and a .. "\n" or a,
			isMultiline and b .. "\n" or b,
			true -- cleanupSemantic
		)

		if hasCommonDiff(diffs, isMultiline) then
			local optionsNormalized = normalizeDiffOptions(options)
			local lines = getAlignedDiffs(diffs, optionsNormalized.changeColor)
			return printDiffLines(lines, optionsNormalized)
		end
	end

	-- Fall back to line-by-line diff.
	return diffLinesUnified(a:split("\n"), b:split("\n"), options)
end
