DiffMatchPatch\Diff::xIndex PHP Method

xIndex() public method

e.g. "The cat" vs "The big cat", 1->1, 5->8
public xIndex ( integer $loc ) : integer
$loc integer Location within text1.
return integer Location within text2.
    public function xIndex($loc)
    {
        $diffs = $this->getChanges();
        $chars1 = 0;
        $chars2 = 0;
        $last_chars1 = 0;
        $last_chars2 = 0;
        $i = 0;
        foreach ($diffs as $change) {
            $op = $change[0];
            $text = $change[1];
            // Equality or deletion.
            if ($op != self::INSERT) {
                $chars1 += mb_strlen($text);
            }
            // Equality or insertion.
            if ($op != self::DELETE) {
                $chars2 += mb_strlen($text);
            }
            // Overshot the location.
            if ($chars1 > $loc) {
                break;
            }
            $last_chars1 = $chars1;
            $last_chars2 = $chars2;
            $i++;
        }
        // The location was deleted.
        if (count($diffs) != $i && $diffs[$i][0] == self::DELETE) {
            return $last_chars2;
        }
        return $loc + $last_chars2 - $last_chars1;
    }