raw = $raw; } // ################################################################### /** * This function initiates the parsing process and returns the output * in hunks, line-by-line * * @return array Array of outputted hunked information */ public function fetch() { global $viewsvn; $chunk = 0; $indexcounter = null; $curprop = ''; $delstack = array(); foreach ($this->raw AS $line) { if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits)) { $property = false; $delstack = array(); $this->diff["$index"][ ++$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4])); $lines['old'] = $this->diff["$index"]["$chunk"]['hunk']['old']['line'] - 1; $lines['new'] = $this->diff["$index"]["$chunk"]['hunk']['new']['line'] - 1; continue; } else if (preg_match('#^Property changes on: (.*?)$#', $line, $bits)) { $property = true; $index = $bits[1]; $this->diff["$index"]['props'] = array(); continue; } if ($indexcounter <= 3 AND $indexcounter !== null) { $indexcounter++; continue; } else if ($indexcounter == 3) { $indexcounter = null; continue; } if (preg_match('#^([\+\- ])(.*)#', $line, $matches) AND !$property) { $act = $matches[1]; $content = $matches[2]; if ($act == ' ') { $this->diff["$index"]["$chunk"][] = array( 'line' => $content, 'act' => '', 'oldlineno' => ++$lines['old'], 'newlineno' => ++$lines['new'] ); $delstack = array(); } else if ($act == '+') { // potential line delta if (sizeof($delstack) > 0) { $lastline = array_shift($delstack); if ($delta = @$this->_fetchDiffExtent($lastline['line'], $content)) { if (strlen($lastline['line']) > ($delta['start'] - $delta['end'])) { $end = strlen($lastline['line']) + $delta['end']; BSRegister::Debug("RM delta- = " . $end); $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}'; $this->diff["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end); } if (strlen($content) > $delta['start'] - $delta['end']) { $end = strlen($content) + $delta['end']; BSRegister::Debug("MK delta+ = " . $end); $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}'; $content = substr($content, 0, $delta['start']) . $change . substr($content, $end); } } } $this->diff["$index"]["$chunk"][] = array( 'line' => $content, 'act' => '+', 'oldlineno' => '', 'newlineno' => ++$lines['new'] ); } else if ($act == '-') { $this->diff["$index"]["$chunk"][] = $thearray = array( 'line' => $content, 'act' => '-', 'oldlineno' => ++$lines['old'], 'newlineno' => '' ); $key = sizeof($this->diff["$index"]["$chunk"]) - 2; $thearray['INDEX'] = $key; array_push($delstack, $thearray); } } // whitespace lines else { if (preg_match('#^Index: (.*?)$#', $line, $matches)) { $index = $matches[1]; $indexcounter = 1; $chunk = 0; continue; } if ($property) { if (preg_match('#^__*_$#', trim($line))) { BSRegister::Debug("skipping: $line"); continue; } if (preg_match('#Name: (.*?)$#', $line, $matches)) { $curprop = $matches[1]; BSRegister::Debug("prop: $curprop"); continue; } else { if (preg_match('#^\s+?\+(.*)#', $line, $matches)) { $mode = 'add'; $this->diff["$index"]['props']["$curprop"]['add'] .= $matches[1]; } else if (preg_match('#^\s+?\-(.*)#', $line, $matches)) { $mode = 'del'; $this->diff["$index"]['props']["$curprop"]['del'] .= $matches[1]; } else if (!preg_match('#^\s+[\+\- ](.*)#', $line) AND trim($line) != '') { $this->diff["$index"]['props']["$curprop"]["$mode"] .= "\n" . $line; } continue; } } $this->diff["$index"]["$chunk"][] = array( 'line' => '', 'act' => '', 'oldlineno' => ++$lines['old'], 'newlineno' => ++$lines['new'] ); $delstack = array(); } } return $this->diff; } // ################################################################### /** * Returns the amount of change that occured between two lines * * @param string Old line * @param string New line * * @return array Difference of positions */ private function _fetchDiffExtent($old, $new) { global $viewsvn; $start = 0; $min = min(strlen($old), strlen($new)); BSRegister::Debug("min1 = $min"); while ($start < $min AND $old["$start"] == $new["$start"]) { $start++; } $end = -1; $min = $min - $start; BSRegister::Debug("min2 = $min"); BSRegister::Debug("checking: " . $old[ strlen($old) + $end ] . ' == ' . $new[ strlen($new) + $end ]); while (-$end <= $min AND $old[ strlen($old) + $end ] == $new[ strlen($new) + $end ]) { $end--; } return array('start' => $start, 'end' => $end + 1); } } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>