3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function doCharDiff($from_text, $to_text) { $result = []; $jobs = [[0, strlen($from_text), 0, strlen($to_text)]]; while ($job = array_pop($jobs)) { // get the segments which must be diff'ed list($from_segment_start, $from_segment_end, $to_segment_start, $to_segment_end) = $job; $from_segment_len = $from_segment_end - $from_segment_start; $to_segment_len = $to_segment_end - $to_segment_start; // catch easy cases first if (!$from_segment_len || !$to_segment_len) { if ($from_segment_len) $result[$from_segment_start * 4 + 0] = new FineDiffDeleteOp($from_segment_len); else if ($to_segment_len) $result[$from_segment_start * 4 + 1] = new FineDiffInsertOp(substr($to_text, $to_segment_start, $to_segment_len)); continue; } if ($from_segment_len >= $to_segment_len) { $copy_len = $to_segment_len; while ($copy_len) { $to_copy_start = $to_segment_start; $to_copy_start_max = $to_segment_end - $copy_len; while ($to_copy_start <= $to_copy_start_max) { $from_copy_start = strpos(substr($from_text, $from_segment_start, $from_segment_len), substr($to_text, $to_copy_start, $copy_len)); if ($from_copy_start !== false) { $from_copy_start += $from_segment_start; break 2; } $to_copy_start++; } $copy_len--; } } else { $copy_len = $from_segment_len; while ($copy_len) { $from_copy_start = $from_segment_start; $from_copy_start_max = $from_segment_end - $copy_len; while ($from_copy_start <= $from_copy_start_max) { $to_copy_start = strpos(substr($to_text, $to_segment_start, $to_segment_len), substr($from_text, $from_copy_start, $copy_len)); if ($to_copy_start !== false) { $to_copy_start += $to_segment_start; break 2; } $from_copy_start++; } $copy_len--; } } // match found if ($copy_len) { $jobs[] = [$from_segment_start, $from_copy_start, $to_segment_start, $to_copy_start]; $result[$from_copy_start * 4 + 2] = new FineDiffCopyOp($copy_len); $jobs[] = [$from_copy_start + $copy_len, $from_segment_end, $to_copy_start + $copy_len, $to_segment_end]; } // no match, so delete all, insert all else { $result[$from_segment_start * 4] = new FineDiffReplaceOp($from_segment_len, substr($to_text, $to_segment_start, $to_segment_len)); } } ksort($result, SORT_NUMERIC); return array_values($result); }
based on ZY0MT
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename:       /in/NnOk1
function name:  (null)
number of ops:  2
compiled vars:  none
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   2     0  E >   NOP                                                      
  79     1      > RETURN                                                   1

Function dochardiff:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = 154
Branch analysis from position: 154
Jump found. Position 1 = 159, Position 2 = 12
Branch analysis from position: 159
Jump found. Position 1 = -2
Branch analysis from position: 12
Jump found. Position 1 = 26, Position 2 = 28
Branch analysis from position: 26
Jump found. Position 1 = 29, Position 2 = 52
Branch analysis from position: 29
Jump found. Position 1 = 30, Position 2 = 38
Branch analysis from position: 30
Jump found. Position 1 = 51
Branch analysis from position: 51
Jump found. Position 1 = 154
Branch analysis from position: 154
Branch analysis from position: 38
Jump found. Position 1 = 39, Position 2 = 51
Branch analysis from position: 39
Jump found. Position 1 = 154
Branch analysis from position: 154
Branch analysis from position: 51
Branch analysis from position: 52
Jump found. Position 1 = 54, Position 2 = 87
Branch analysis from position: 54
Jump found. Position 1 = 85
Branch analysis from position: 85
Jump found. Position 1 = 86, Position 2 = 56
Branch analysis from position: 86
Jump found. Position 1 = 119
Branch analysis from position: 119
Jump found. Position 1 = 120, Position 2 = 142
Branch analysis from position: 120
Jump found. Position 1 = 154
Branch analysis from position: 154
Branch analysis from position: 142
Jump found. Position 1 = 159, Position 2 = 12
Branch analysis from position: 159
Branch analysis from position: 12
Branch analysis from position: 56
Jump found. Position 1 = 81
Branch analysis from position: 81
Jump found. Position 1 = 83, Position 2 = 60
Branch analysis from position: 83
Jump found. Position 1 = 86, Position 2 = 56
Branch analysis from position: 86
Branch analysis from position: 56
Branch analysis from position: 60
Jump found. Position 1 = 77, Position 2 = 79
Branch analysis from position: 77
Jump found. Position 1 = 86
Branch analysis from position: 86
Branch analysis from position: 79
Jump found. Position 1 = 83, Position 2 = 60
Branch analysis from position: 83
Branch analysis from position: 60
Branch analysis from position: 87
Jump found. Position 1 = 118
Branch analysis from position: 118
Jump found. Position 1 = 119, Position 2 = 89
Branch analysis from position: 119
Branch analysis from position: 89
Jump found. Position 1 = 114
Branch analysis from position: 114
Jump found. Position 1 = 116, Position 2 = 93
Branch analysis from position: 116
Jump found. Position 1 = 119, Position 2 = 89
Branch analysis from position: 119
Branch analysis from position: 89
Branch analysis from position: 93
Jump found. Position 1 = 110, Position 2 = 112
Branch analysis from position: 110
Jump found. Position 1 = 119
Branch analysis from position: 119
Branch analysis from position: 112
Jump found. Position 1 = 116, Position 2 = 93
Branch analysis from position: 116
Branch analysis from position: 93
Branch analysis from position: 28
filename:       /in/NnOk1
function name:  doCharDiff
number of ops:  168
compiled vars:  !0 = $from_text, !1 = $to_text, !2 = $result, !3 = $jobs, !4 = $job, !5 = $from_segment_start, !6 = $from_segment_end, !7 = $to_segment_start, !8 = $to_segment_end, !9 = $from_segment_len, !10 = $to_segment_len, !11 = $copy_len, !12 = $to_copy_start, !13 = $to_copy_start_max, !14 = $from_copy_start, !15 = $from_copy_start_max
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   2     0  E >   RECV                                             !0      
         1        RECV                                             !1      
   4     2        ASSIGN                                                   !2, <array>
   5     3        INIT_ARRAY                                       ~17     0
         4        STRLEN                                           ~18     !0
         5        ADD_ARRAY_ELEMENT                                ~17     ~18
         6        ADD_ARRAY_ELEMENT                                ~17     0
         7        STRLEN                                           ~19     !1
         8        ADD_ARRAY_ELEMENT                                ~17     ~19
         9        INIT_ARRAY                                       ~20     ~17
        10        ASSIGN                                                   !3, ~20
   6    11      > JMP                                                      ->154
   9    12    >   FETCH_LIST                                       $22     !4, 0
        13        ASSIGN                                                   !5, $22
        14        FETCH_LIST                                       $24     !4, 1
        15        ASSIGN                                                   !6, $24
        16        FETCH_LIST                                       $26     !4, 2
        17        ASSIGN                                                   !7, $26
        18        FETCH_LIST                                       $28     !4, 3
        19        ASSIGN                                                   !8, $28
  10    20        SUB                                              ~30     !6, !5
        21        ASSIGN                                                   !9, ~30
  11    22        SUB                                              ~32     !8, !7
        23        ASSIGN                                                   !10, ~32
  14    24        BOOL_NOT                                         ~34     !9
        25      > JMPNZ_EX                                         ~34     ~34, ->28
        26    >   BOOL_NOT                                         ~35     !10
        27        BOOL                                             ~34     ~35
        28    > > JMPZ                                                     ~34, ->52
  16    29    > > JMPZ                                                     !9, ->38
  17    30    >   MUL                                              ~36     !5, 4
        31        ADD                                              ~37     ~36, 0
        32        NEW                                              $39     :3
        33        SEND_VAR_EX                                              !9
        34        DO_FCALL                                      0          
        35        ASSIGN_DIM                                               !2, ~37
        36        OP_DATA                                                  $39
        37      > JMP                                                      ->51
  18    38    > > JMPZ                                                     !10, ->51
  19    39    >   MUL                                              ~41     !5, 4
        40        ADD                                              ~42     ~41, 1
        41        NEW                                              $44     :7
        42        INIT_FCALL                                               'substr'
        43        SEND_VAR                                                 !1
        44        SEND_VAR                                                 !7
        45        SEND_VAR                                                 !10
        46        DO_ICALL                                         $45     
        47        SEND_VAR_NO_REF                               4          $45
        48        DO_FCALL                                      0          
        49        ASSIGN_DIM                                               !2, ~42
        50        OP_DATA                                                  $44
  21    51    > > JMP                                                      ->154
  24    52    >   IS_SMALLER_OR_EQUAL                              ~47     !10, !9
        53      > JMPZ                                                     ~47, ->87
  26    54    >   ASSIGN                                                   !11, !10
  27    55      > JMP                                                      ->85
  29    56    >   ASSIGN                                                   !12, !7
  30    57        SUB                                              ~50     !8, !11
        58        ASSIGN                                                   !13, ~50
  31    59      > JMP                                                      ->81
  33    60    >   INIT_FCALL                                               'strpos'
        61        INIT_FCALL                                               'substr'
        62        SEND_VAR                                                 !0
        63        SEND_VAR                                                 !5
        64        SEND_VAR                                                 !9
        65        DO_ICALL                                         $52     
        66        SEND_VAR                                                 $52
        67        INIT_FCALL                                               'substr'
        68        SEND_VAR                                                 !1
        69        SEND_VAR                                                 !12
        70        SEND_VAR                                                 !11
        71        DO_ICALL                                         $53     
        72        SEND_VAR                                                 $53
        73        DO_ICALL                                         $54     
        74        ASSIGN                                                   !14, $54
  34    75        IS_NOT_IDENTICAL                                 ~56     !14, <false>
        76      > JMPZ                                                     ~56, ->79
  36    77    >   ASSIGN_ADD                                    0          !14, !5
  37    78      > JMP                                                      ->86
  39    79    >   POST_INC                                         ~58     !12
        80        FREE                                                     ~58
  31    81    >   IS_SMALLER_OR_EQUAL                              ~59     !12, !13
        82      > JMPNZ                                                    ~59, ->60
  41    83    >   POST_DEC                                         ~60     !11
        84        FREE                                                     ~60
  27    85    > > JMPNZ                                                    !11, ->56
        86    > > JMP                                                      ->119
  46    87    >   ASSIGN                                                   !11, !9
  47    88      > JMP                                                      ->118
  49    89    >   ASSIGN                                                   !14, !5
  50    90        SUB                                              ~63     !6, !11
        91        ASSIGN                                                   !15, ~63
  51    92      > JMP                                                      ->114
  53    93    >   INIT_FCALL                                               'strpos'
        94        INIT_FCALL                                               'substr'
        95        SEND_VAR                                                 !1
        96        SEND_VAR                                                 !7
        97        SEND_VAR                                                 !10
        98        DO_ICALL                                         $65     
        99        SEND_VAR                                                 $65
       100        INIT_FCALL                                               'substr'
       101        SEND_VAR                                                 !0
       102        SEND_VAR                                                 !14
       103        SEND_VAR                                                 !11
       104        DO_ICALL                                         $66     
       105        SEND_VAR                                                 $66
       106        DO_ICALL                                         $67     
       107        ASSIGN                                                   !12, $67
  54   108        IS_NOT_IDENTICAL                                 ~69     !12, <false>
       109      > JMPZ                                                     ~69, ->112
  56   110    >   ASSIGN_ADD                                    0          !12, !7
  57   111      > JMP                                                      ->119
  59   112    >   POST_INC                                         ~71     !14
       113        FREE                                                     ~71
  51   114    >   IS_SMALLER_OR_EQUAL                              ~72     !14, !15
       115      > JMPNZ                                                    ~72, ->93
  61   116    >   POST_DEC                                         ~73     !11
       117        FREE                                                     ~73
  47   118    > > JMPNZ                                                    !11, ->89
  65   119    > > JMPZ                                                     !11, ->142
  67   120    >   INIT_ARRAY                                       ~75     !5
       121        ADD_ARRAY_ELEMENT                                ~75     !14
       122        ADD_ARRAY_ELEMENT                                ~75     !7
       123        ADD_ARRAY_ELEMENT                                ~75     !12
       124        ASSIGN_DIM                                               !3
       125        OP_DATA                                                  ~75
  68   126        MUL                                              ~76     !14, 4
       127        ADD                                              ~77     ~76, 2
       128        NEW                                              $79     :20
       129        SEND_VAR_EX                                              !11
       130        DO_FCALL                                      0          
       131        ASSIGN_DIM                                               !2, ~77
       132        OP_DATA                                                  $79
  69   133        ADD                                              ~82     !14, !11
       134        INIT_ARRAY                                       ~83     ~82
       135        ADD_ARRAY_ELEMENT                                ~83     !6
       136        ADD                                              ~84     !12, !11
       137        ADD_ARRAY_ELEMENT                                ~83     ~84
       138        ADD_ARRAY_ELEMENT                                ~83     !8
       139        ASSIGN_DIM                                               !3
       140        OP_DATA                                                  ~83
       141      > JMP                                                      ->154
  74   142    >   MUL                                              ~85     !5, 4
       143        NEW                                              $87     :23
       144        SEND_VAR_EX                                              !9
       145        INIT_FCALL                                               'substr'
       146        SEND_VAR                                                 !1
       147        SEND_VAR                                                 !7
       148        SEND_VAR                                                 !10
       149        DO_ICALL                                         $88     
       150        SEND_VAR_NO_REF                               4          $88
       151        DO_FCALL                                      0          
       152        ASSIGN_DIM                                               !2, ~85
       153        OP_DATA                                                  $87
   6   154    >   INIT_FCALL                                               'array_pop'
       155        SEND_REF                                                 !3
       156        DO_ICALL                                         $90     
       157        ASSIGN                                           $91     !4, $90
       158      > JMPNZ                                                    $91, ->12
  77   159    >   INIT_FCALL                                               'ksort'
       160        SEND_REF                                                 !2
       161        SEND_VAL                                                 1
       162        DO_ICALL                                                 
  78   163        INIT_FCALL                                               'array_values'
       164        SEND_VAR                                                 !2
       165        DO_ICALL                                         $93     
       166      > RETURN                                                   $93
  79   167*     > RETURN                                                   null

End of function dochardiff

Generated using Vulcan Logic Dumper, using php 7.3.0