wppaste
WordPress

Text_Diff_Engine_string::parseContextDiff( array $diff ): array

Source
wp-includes/Text/Diff/Engine/string.php:146
Parses an array containing the context diff.

Compatibility

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$diffarray
Array of lines.

Return value

array
List of all diff operations.

Performance profile

How much work a call to Text_Diff_Engine_string::parseContextDiff() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
12

Executed per call on PHP 8.5. The body compiles to 254.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost Text_Diff_Engine_string::parseContextDiff() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$i12none

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev2541245
8.52541245
8.4254124569 fewer instructions than PHP 8.3
8.33231245
8.232312454 more instructions than PHP 8.1
8.13191245
7.43191245

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 4

Used by · 1

Source code

    function parseContextDiff(&$diff)    {        $edits = array();        $i = $max_i = $j = $max_j = 0;        $end = count($diff) - 1;        while ($i < $end && $j < $end) {            while ($i >= $max_i && $j >= $max_j) {                // Find the boundaries of the diff output of the two files                for ($i = $j;                     $i < $end && substr($diff[$i], 0, 3) == '***';                     $i++);                for ($max_i = $i;                     $max_i < $end && substr($diff[$max_i], 0, 3) != '---';                     $max_i++);                for ($j = $max_i;                     $j < $end && substr($diff[$j], 0, 3) == '---';                     $j++);                for ($max_j = $j;                     $max_j < $end && substr($diff[$max_j], 0, 3) != '***';                     $max_j++);            }             // find what hasn't been changed            $array = array();            while ($i < $max_i &&                   $j < $max_j &&                   strcmp($diff[$i], $diff[$j]) == 0) {                $array[] = substr($diff[$i], 2);                $i++;                $j++;            }             while ($i < $max_i && ($max_j-$j) <= 1) {                if ($diff[$i] != '' && substr($diff[$i], 0, 1) != ' ') {                    break;                }                $array[] = substr($diff[$i++], 2);            }             while ($j < $max_j && ($max_i-$i) <= 1) {                if ($diff[$j] != '' && substr($diff[$j], 0, 1) != ' ') {                    break;                }                $array[] = substr($diff[$j++], 2);            }            if (count($array) > 0) {                $edits[] = new Text_Diff_Op_copy($array);            }             if ($i < $max_i) {                $diff1 = array();                switch (substr($diff[$i], 0, 1)) {                case '!':                    $diff2 = array();                    do {                        $diff1[] = substr($diff[$i], 2);                        if ($j < $max_j && substr($diff[$j], 0, 1) == '!') {                            $diff2[] = substr($diff[$j++], 2);                        }                    } while (++$i < $max_i && substr($diff[$i], 0, 1) == '!');                    $edits[] = new Text_Diff_Op_change($diff1, $diff2);                    break;                 case '+':                    do {                        $diff1[] = substr($diff[$i], 2);                    } while (++$i < $max_i && substr($diff[$i], 0, 1) == '+');                    $edits[] = new Text_Diff_Op_add($diff1);                    break;                 case '-':                    do {                        $diff1[] = substr($diff[$i], 2);                    } while (++$i < $max_i && substr($diff[$i], 0, 1) == '-');                    $edits[] = new Text_Diff_Op_delete($diff1);                    break;                }            }             if ($j < $max_j) {

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/Text/Diff/Engine/string.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.