--[[
* Determine the common prefix of two strings.
* @param {string} text1 First string.
* @param {string} text2 Second string.
* @return {number} The number of characters common to the start of each
*    string.
--]]

local function _diff_commonPrefix(text1: string, text2: string): number
	-- Quick check for common null cases.
	if #text1 == 0 or #text2 == 0 or strbyte(text1, 1, 1) ~= strbyte(text2, 1, 1) then
		return 0
	end
	-- Binary search.
	-- Performance analysis: https://neil.fraser.name/news/2007/10/09/
	local pointermin = 1
	local pointermax = min(#text1, #text2)
	local pointermid = pointermax
	local pointerstart = 1
	while pointermin < pointermid do
		if strsub(text1, pointerstart, pointermid) == strsub(text2, pointerstart, pointermid) then
			pointermin = pointermid
			pointerstart = pointermin
		else
			pointermax = pointermid
		end
		pointermid = floor(pointermin + (pointermax - pointermin) / 2)
	end
	return pointermid
end
