|
4 | 4 |
|
5 | 5 | namespace Jfcherng\Diff;
|
6 | 6 |
|
| 7 | +use Jfcherng\Diff\Utility\Arr; |
| 8 | + |
7 | 9 | /**
|
8 | 10 | * A comprehensive library for generating differences between two strings
|
9 | 11 | * in multiple formats (unified, side by side HTML etc).
|
@@ -178,33 +180,31 @@ public function setOptions(array $options): self
|
178 | 180 | }
|
179 | 181 |
|
180 | 182 | /**
|
181 |
| - * Get a range of lines from $start to $end from the old string and return them as an array. |
182 |
| - * |
183 |
| - * If $end is null, it returns array sliced from the $start to the end. |
| 183 | + * Get a range of lines from $start to $end from the old. |
184 | 184 | *
|
185 |
| - * @param int $start the starting number. If null, the whole array will be returned. |
186 |
| - * @param null|int $end the ending number. If null, only the item in $start will be returned. |
| 185 | + * @param int $start the starting index (negative = count from backward) |
| 186 | + * @param null|int $end the ending index (negative = count from backward) |
| 187 | + * if is null, it returns a slice from $start to the end |
187 | 188 | *
|
188 | 189 | * @return string[] array of all of the lines between the specified range
|
189 | 190 | */
|
190 | 191 | public function getOld(int $start = 0, ?int $end = null): array
|
191 | 192 | {
|
192 |
| - return $this->getText($this->old, $start, $end); |
| 193 | + return Arr::getPartialByIndex($this->old, $start, $end); |
193 | 194 | }
|
194 | 195 |
|
195 | 196 | /**
|
196 |
| - * Get a range of lines from $start to $end from the new string and return them as an array. |
197 |
| - * |
198 |
| - * If $end is null, it returns array sliced from the $start to the end. |
| 197 | + * Get a range of lines from $start to $end from the new. |
199 | 198 | *
|
200 |
| - * @param int $start the starting number |
201 |
| - * @param null|int $end the ending number |
| 199 | + * @param int $start the starting index (negative = count from backward) |
| 200 | + * @param null|int $end the ending index (negative = count from backward) |
| 201 | + * if is null, it returns a slice from $start to the end |
202 | 202 | *
|
203 | 203 | * @return string[] array of all of the lines between the specified range
|
204 | 204 | */
|
205 | 205 | public function getNew(int $start = 0, ?int $end = null): array
|
206 | 206 | {
|
207 |
| - return $this->getText($this->new, $start, $end); |
| 207 | + return Arr::getPartialByIndex($this->new, $start, $end); |
208 | 208 | }
|
209 | 209 |
|
210 | 210 | /**
|
@@ -291,8 +291,8 @@ public function getGroupedOpcodesGnu(): array
|
291 | 291 |
|
292 | 292 | return $this->groupedOpcodesGnu = $this->sequenceMatcher
|
293 | 293 | ->setSequences(
|
294 |
| - $this->makeLinesGnuCompatible($this->old), |
295 |
| - $this->makeLinesGnuCompatible($this->new) |
| 294 | + $this->createGnuCompatibleLines($this->old), |
| 295 | + $this->createGnuCompatibleLines($this->new) |
296 | 296 | )
|
297 | 297 | ->getGroupedOpcodes($this->options['context']);
|
298 | 298 | }
|
@@ -335,61 +335,15 @@ private function resetCachedResults(): self
|
335 | 335 | return $this;
|
336 | 336 | }
|
337 | 337 |
|
338 |
| - /** |
339 |
| - * The work horse of getOld() and getNew(). |
340 |
| - * |
341 |
| - * If $end is null, it returns array sliced from the $start to the end. |
342 |
| - * |
343 |
| - * @param string[] $lines the array of lines |
344 |
| - * @param int $start the starting number |
345 |
| - * @param null|int $end the ending number |
346 |
| - * |
347 |
| - * @return string[] array of all of the lines between the specified range |
348 |
| - */ |
349 |
| - private function getText(array $lines, int $start = 0, ?int $end = null): array |
350 |
| - { |
351 |
| - $arrayLength = \count($lines); |
352 |
| - |
353 |
| - // make $end set |
354 |
| - $end = $end ?? $arrayLength; |
355 |
| - |
356 |
| - // make $start non-negative |
357 |
| - if ($start < 0) { |
358 |
| - $start += $arrayLength; |
359 |
| - |
360 |
| - if ($start < 0) { |
361 |
| - $start = 0; |
362 |
| - } |
363 |
| - } |
364 |
| - |
365 |
| - // may prevent from calling array_slice() |
366 |
| - if ($start === 0 && $end >= $arrayLength) { |
367 |
| - return $lines; |
368 |
| - } |
369 |
| - |
370 |
| - // make $end non-negative |
371 |
| - if ($end < 0) { |
372 |
| - $end += $arrayLength; |
373 |
| - |
374 |
| - if ($end < 0) { |
375 |
| - $end = 0; |
376 |
| - } |
377 |
| - } |
378 |
| - |
379 |
| - // now both $start and $end are non-negative |
380 |
| - // hence the length for array_slice() must be non-negative |
381 |
| - return \array_slice($lines, $start, \max(0, $end - $start)); |
382 |
| - } |
383 |
| - |
384 | 338 | /**
|
385 | 339 | * Make the lines to be prepared for GNU-style diff.
|
386 | 340 | *
|
387 | 341 | * This method checks whether $lines has no EOL at EOF and append a special
|
388 | 342 | * indicator to the last line.
|
389 | 343 | *
|
390 |
| - * @param string[] $lines the lines |
| 344 | + * @param string[] $lines the lines created by simply explode("\n", $string) |
391 | 345 | */
|
392 |
| - private function makeLinesGnuCompatible(array $lines): array |
| 346 | + private function createGnuCompatibleLines(array $lines): array |
393 | 347 | {
|
394 | 348 | // note that the $lines should not be empty at this point
|
395 | 349 | // they have at least one element "" in the array because explode("\n", "") === [""]
|
|
0 commit comments