package core

// PrefixDelta returns the portion of curr that follows the longest common
// prefix (LCP) shared with prev, comparing rune-by-rune to avoid splitting
// multi-byte characters.
//
// In the monotone streaming case (curr = prev + new), this returns exactly
// the new suffix. When the text diverges mid-way (reflow), it returns
// everything from the point of divergence to the end of curr.
func PrefixDelta(prev, curr string) string {
	prevRunes := []rune(prev)
	currRunes := []rune(curr)

	common := 0
	for common < len(prevRunes) && common < len(currRunes) {
		if prevRunes[common] != currRunes[common] {
			break
		}
		common++
	}

	return string(currRunes[common:])
}
