src/Patch.php
public function addPadding(&$patches)
    {
        $paddingLength = $this->getMargin();
        $nullPadding = '';
        for ($i = 1; $i <= $paddingLength; $i++) {
            $nullPadding .= chr($i);
        }

        // Bump all the patches forward.
        foreach ($patches as &$patch) {
            $patch->setStart1($patch->getStart1() + $paddingLength);
            $patch->setStart2($patch->getStart2() + $paddingLength);
        }
        unset($patch);

        // Add some padding on start of first diff.
        $patch = &$patches[0];
        $diffs = $patch->getChanges();
        $firstChange = &$diffs[0];
        if (!$diffs || $firstChange[0] != Diff::EQUAL) {
            // Add nullPadding equality.
            array_unshift($diffs, array(Diff::EQUAL, $nullPadding));
            // Should be 0.
            $patch->setStart1($patch->getStart1() - $paddingLength);
            // Should be 0.
            $patch->setStart2($patch->getStart2() - $paddingLength);
            $patch->setLength1($patch->getLength1() + $paddingLength);
            $patch->setLength2($patch->getLength2() + $paddingLength);
        } elseif($paddingLength > mb_strlen($firstChange[1])) {
            // Grow first equality.
            $extraLength = $paddingLength - mb_strlen($firstChange[1]);
            $firstChange[1] = mb_substr($nullPadding, mb_strlen($firstChange[1])) . $firstChange[1];
            $patch->setStart1($patch->getStart1() - $extraLength);
            $patch->setStart2($patch->getStart2() - $extraLength);
            $patch->setLength1($patch->getLength1() + $extraLength);
            $patch->setLength2($patch->getLength2() + $extraLength);
        }
        $patch->setChanges($diffs);
        unset($patch, $firstChange);

        // Add some padding on end of last diff.
        $patch = &$patches[count($patches) - 1];
        $diffs = $patch->getChanges();
        $lastChange = &$diffs[count($diffs) - 1];
        if (!$diffs || $lastChange[0] != Diff::EQUAL) {
            // Add nullPadding equality.
            array_push($diffs, array(Diff::EQUAL, $nullPadding));
            $patch->setLength1($patch->getLength1() + $paddingLength);
            $patch->setLength2($patch->getLength2() + $paddingLength);
        } elseif($paddingLength > mb_strlen($lastChange[1])) {
            // Grow last equality.
            $extraLength = $paddingLength - mb_strlen($lastChange[1]);
            $lastChange[1] .= mb_substr($nullPadding, 0, $extraLength);
            $patch->setLength1($patch->getLength1() + $extraLength);
            $patch->setLength2($patch->getLength2() + $extraLength);
        }
        $patch->setChanges($diffs);
        unset($patch, $lastChange);

        return $nullPadding;
    }
public function addPadding(&$patches)
    {
        $paddingLength = $this->getMargin();
        $nullPadding = '';
        for ($i = 1; $i <= $paddingLength; $i++) {
            $nullPadding .= chr($i);
        }

        // Bump all the patches forward.
        foreach ($patches as &$patch) {
            $patch->setStart1($patch->getStart1() + $paddingLength);
            $patch->setStart2($patch->getStart2() + $paddingLength);
        }
        unset($patch);

        // Add some padding on start of first diff.
        $patch = &$patches[0];
        $diffs = $patch->getChanges();
        $firstChange = &$diffs[0];
        if (!$diffs || $firstChange[0] != Diff::EQUAL) {
            // Add nullPadding equality.
            array_unshift($diffs, array(Diff::EQUAL, $nullPadding));
            // Should be 0.
            $patch->setStart1($patch->getStart1() - $paddingLength);
            // Should be 0.
            $patch->setStart2($patch->getStart2() - $paddingLength);
            $patch->setLength1($patch->getLength1() + $paddingLength);
            $patch->setLength2($patch->getLength2() + $paddingLength);
        } elseif($paddingLength > mb_strlen($firstChange[1])) {
            // Grow first equality.
            $extraLength = $paddingLength - mb_strlen($firstChange[1]);
            $firstChange[1] = mb_substr($nullPadding, mb_strlen($firstChange[1])) . $firstChange[1];
            $patch->setStart1($patch->getStart1() - $extraLength);
            $patch->setStart2($patch->getStart2() - $extraLength);
            $patch->setLength1($patch->getLength1() + $extraLength);
            $patch->setLength2($patch->getLength2() + $extraLength);
        }
        $patch->setChanges($diffs);
        unset($patch, $firstChange);

        // Add some padding on end of last diff.
        $patch = &$patches[count($patches) - 1];
        $diffs = $patch->getChanges();
        $lastChange = &$diffs[count($diffs) - 1];
        if (!$diffs || $lastChange[0] != Diff::EQUAL) {
            // Add nullPadding equality.
            array_push($diffs, array(Diff::EQUAL, $nullPadding));
            $patch->setLength1($patch->getLength1() + $paddingLength);
            $patch->setLength2($patch->getLength2() + $paddingLength);
        } elseif($paddingLength > mb_strlen($lastChange[1])) {
            // Grow last equality.
            $extraLength = $paddingLength - mb_strlen($lastChange[1]);
            $lastChange[1] .= mb_substr($nullPadding, 0, $extraLength);
            $patch->setLength1($patch->getLength1() + $extraLength);
            $patch->setLength2($patch->getLength2() + $extraLength);
        }
        $patch->setChanges($diffs);
        unset($patch, $lastChange);

        return $nullPadding;
    }
Add some padding on text start and end so that edges can match something.
Intended to be called only from within patch_apply.
Modifies $patches. TODO try to fix it!

@param PatchObject[] $patches Array of PatchObjects.

@return string The padding string added to each side.