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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
use Range;
/// Content classification and placement of equivalent shortest edits.
///
/// The defaults enable indent-aware placement calibrated against Git CLI observations. Some
/// ambiguous placements still differ; see [`super::diff`]. Disable scoring to compare with
/// `git diff --no-indent-heuristic`; change-group compaction still applies.
///
/// ```
/// use std::sync::atomic::AtomicBool;
///
/// use girt::content_diff::{ContentDiff, DiffLimits, Options, diff_with_options};
///
/// let result = diff_with_options(
/// b"head\n\nhead\n body\n}\n}\n\n\n}\n",
/// b"head\n\nhead\n body\nhead\n body\n}\n}\n\n\n}\n",
/// Options::default().with_indent_heuristic(false),
/// DiffLimits::default(),
/// &AtomicBool::new(false),
/// )?;
/// let ContentDiff::Text(text) = result else {
/// panic!("expected text")
/// };
/// assert_eq!(text.edits()[0].new_lines, 4..6);
/// # Ok::<(), girt::content_diff::DiffError>(())
/// ```
/// Classification policy for [`super::diff`], independent of paths and Git attributes.
///
/// ```
/// use std::sync::atomic::AtomicBool;
///
/// use girt::content_diff::{BinaryMode, ContentDiff, DiffLimits, diff};
///
/// let cancel = AtomicBool::new(false);
/// let old = b"key\0old\n";
/// let new = b"key\0new\n";
///
/// assert!(matches!(
/// diff(old, new, BinaryMode::Auto, DiffLimits::default(), &cancel)?,
/// ContentDiff::Binary { .. }
/// ));
/// assert!(matches!(
/// diff(old, new, BinaryMode::Text, DiffLimits::default(), &cancel)?,
/// ContentDiff::Text(_)
/// ));
/// # Ok::<(), girt::content_diff::DiffError>(())
/// ```
/// Exact comparison outcome borrowing the caller's unchanged input buffers.
///
/// An empty-file addition can have unchanged content. Retain the structural [`crate::TreeChange`]
/// so a file's existence and mode are not lost when rendering the content outcome.
///
/// ```
/// use std::sync::atomic::AtomicBool;
///
/// use girt::content_diff::{BinaryMode, ContentDiff, DiffLimits, diff};
/// let result = diff(
/// b"same\0bytes",
/// b"same\0bytes",
/// BinaryMode::Binary,
/// DiffLimits::default(),
/// &AtomicBool::new(false),
/// )?;
/// assert_eq!(result, ContentDiff::Unchanged);
/// # Ok::<(), girt::content_diff::DiffError>(())
/// ```
/// Borrowed text inputs and owned edit ranges, constructed by [`super::diff`].
///
/// Edits are ordered, nonoverlapping and separated by at least one unchanged line. Replace each
/// original byte range by its corresponding new byte range to reconstruct the destination exactly.
/// Coordinates always refer to the original buffers, not progressively edited text. The retained
/// borrows prevent mutation or destruction of either input while these slices remain in use.
///
/// # Reconstruct the destination
///
/// Apply edits using the original coordinates. Copy unchanged gaps from the old buffer and
/// replacement ranges from the new buffer; do not adjust later offsets as you go.
///
/// ```
/// use std::sync::atomic::AtomicBool;
///
/// use girt::content_diff::{BinaryMode, ContentDiff, DiffLimits, diff};
///
/// let ContentDiff::Text(text) = diff(
/// b"one\ntwo\nthree\n",
/// b"ONE\ntwo\nTHREE",
/// BinaryMode::Text,
/// DiffLimits::default(),
/// &AtomicBool::new(false),
/// )?
/// else {
/// panic!("expected text edits");
/// };
///
/// let mut result = Vec::new();
/// let mut cursor = 0;
/// for edit in text.edits() {
/// result.extend_from_slice(&text.old_bytes()[cursor..edit.old_bytes.start]);
/// result.extend_from_slice(&text.new_bytes()[edit.new_bytes.clone()]);
/// cursor = edit.old_bytes.end;
/// }
/// result.extend_from_slice(&text.old_bytes()[cursor..]);
///
/// assert_eq!(result, text.new_bytes());
/// # Ok::<(), girt::content_diff::DiffError>(())
/// ```
///
/// # Reconstruct exact replacement bytes
///
/// Line ranges count LF-delimited records. Byte ranges preserve CRLF, non-UTF-8 bytes and an
/// unterminated final line; a patch renderer must not add a newline while slicing them.
///
/// ```
/// use std::sync::atomic::AtomicBool;
///
/// use girt::content_diff::{BinaryMode, ContentDiff, DiffLimits, diff};
///
/// let old = b"keep\r\nold\xff";
/// let new = b"keep\r\nnew\xff";
/// let ContentDiff::Text(text) = diff(
/// old,
/// new,
/// BinaryMode::Text,
/// DiffLimits::default(),
/// &AtomicBool::new(false),
/// )?
/// else {
/// panic!("requested text");
/// };
///
/// let mut rebuilt = Vec::new();
/// let mut cursor = 0;
/// for edit in text.edits() {
/// rebuilt.extend_from_slice(&old[cursor..edit.old_bytes.start]);
/// rebuilt.extend_from_slice(&new[edit.new_bytes.clone()]);
/// cursor = edit.old_bytes.end;
/// }
/// rebuilt.extend_from_slice(&old[cursor..]);
///
/// assert_eq!(rebuilt, new);
/// # Ok::<(), girt::content_diff::DiffError>(())
/// ```
/// One insertion, deletion or replacement in the original coordinate spaces.
///
/// All ranges are zero-based and half-open. An empty old range denotes insertion; an empty new
/// range denotes deletion. In results from [`super::diff`], at least one range is nonempty and
/// byte endpoints coincide with LF-delimited line boundaries (including end of input).
///
/// ```
/// use std::sync::atomic::AtomicBool;
///
/// use girt::content_diff::{BinaryMode, ContentDiff, DiffLimits, diff};
///
/// let ContentDiff::Text(text) = diff(
/// b"keep\n",
/// b"keep\ninserted",
/// BinaryMode::Auto,
/// DiffLimits::default(),
/// &AtomicBool::new(false),
/// )?
/// else {
/// panic!("expected text");
/// };
///
/// let edit = &text.edits()[0];
/// assert!(edit.old_bytes.is_empty());
/// assert_eq!(edit.old_lines, 1..1);
/// assert_eq!(&text.new_bytes()[edit.new_bytes.clone()], b"inserted");
/// # Ok::<(), girt::content_diff::DiffError>(())
/// ```
/// Deterministic resource bounds for one [`super::diff`] call; zero is a real bound.
///
/// Memory is O(lines + retained trace positions + edits); input bytes are borrowed. Vector spare
/// capacity and allocator overhead are not exact heap limits. Output contains at most the combined
/// line count of edits. Bounds are checked before line/trace growth; allocation failure follows
/// Rust's allocator behavior. Raising bounds can admit quadratic search/trace growth on unrelated
/// text. The default rejects that work rather than returning an approximate answer.
///
/// ```
/// use std::sync::atomic::AtomicBool;
///
/// use girt::content_diff::{BinaryMode, DiffError, DiffLimits, diff};
/// let limits = DiffLimits::default().with_max_input_bytes(3);
/// assert!(matches!(
/// diff(
/// b"ab",
/// b"ab",
/// BinaryMode::Auto,
/// limits,
/// &AtomicBool::new(false)
/// ),
/// Err(DiffError::Limit("input bytes"))
/// ));
/// ```
/// Content comparison failed without returning partial edits or changing external state.
///
/// A limit failure supplies no approximate edits. Choose a larger budget, offer a binary/raw
/// view, or let the user explicitly request more work. Cancellation requires a fresh call after
/// the application clears or replaces its flag.
///
/// ```
/// use std::sync::atomic::AtomicBool;
///
/// use girt::content_diff::{BinaryMode, DiffError, DiffLimits, diff};
/// assert!(matches!(
/// diff(
/// b"a",
/// b"b",
/// BinaryMode::Auto,
/// DiffLimits::default(),
/// &AtomicBool::new(true)
/// ),
/// Err(DiffError::Cancelled)
/// ));
/// ```