-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced-scenarios.php
More file actions
66 lines (47 loc) · 1.7 KB
/
advanced-scenarios.php
File metadata and controls
66 lines (47 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
/*
* This file is part of the ALTO library.
*
* © 2025–present Simon André
*
* For full copyright and license information, please see
* the LICENSE file distributed with this source code.
*/
require __DIR__.'/../../vendor/autoload.php';
use Alto\Code\Diff\Diff;
use Alto\Code\Diff\Exception\BinaryInputException;
use Alto\Code\Diff\Exception\SizeLimitException;
use Alto\Code\Diff\Renderer\UnifiedRenderer;
echo "=== 1. Compare with Size Limits ===\n";
$largeFile1 = str_repeat('A', 200000); // 200KB
$largeFile2 = str_repeat('B', 200000);
// Set limit to 100KB (100000 bytes)
$diff = Diff::build()->maxBytes(100000);
try {
$result = $diff->compare($largeFile1, $largeFile2);
} catch (SizeLimitException $e) {
echo "Caught expected exception: {$e->getMessage()}\n";
}
echo "\n=== 2. Binary File Detection ===\n";
$file1 = 'Text content';
$file2 = "Binary \0 content";
try {
$result = Diff::build()->compare($file1, $file2);
} catch (BinaryInputException $e) {
echo "Caught expected exception: {$e->getMessage()}\n";
}
echo "\n=== 3. Custom Context Lines ===\n";
$old = implode("\n", range(1, 10));
$new = implode("\n", array_merge(range(1, 4), ['5 modified'], range(6, 10)));
$result = Diff::build()
->contextLines(1)
->compare($old, $new);
echo (new UnifiedRenderer())->render($result)."\n";
echo "\n=== 4. No Trailing Newline ===\n";
$old = "line1\nline2";
$new = "line1\nline2\n";
$result = Diff::build()->compare($old, $new);
echo 'Old has trailing newline: '.($result->oldHasTrailingNewline ? 'yes' : 'no')."\n";
echo 'New has trailing newline: '.($result->newHasTrailingNewline ? 'yes' : 'no')."\n";
echo (new UnifiedRenderer())->render($result)."\n";