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
//! # Byte-span provenance — the foundation of round-trip / minimal-diff
//!
//! This module exists because of the single most important architecture
//! decision in pdfcer: **provenance is a first-class field, not an
//! optimization** (`docs/decisions/001-oxidize-pdf-adopt-vs-build.md`
//! §6.1 item 1, enacting `docs/ARCHITECTURE.md` §5).
//!
//! ## The contract
//!
//! Every syntactic entity parsed out of a PDF buffer — every token, every
//! COS object, every content-stream operator — records the exact byte
//! range of the source buffer it was parsed from, as a [`ByteSpan`]. The
//! source buffer itself is retained for the lifetime of the loaded
//! document. At save time the rule is:
//!
//! > An entity that is span-backed and structurally unmodified since the
//! > base revision re-emits its **source bytes verbatim** (full rewrite)
//! > or is **omitted entirely** (incremental save — the default mode).
//!
//! This is what makes pdfcer's editing signature-safe: an incremental save
//! appends a new revision and leaves every prior byte untouched, so a
//! digital signature's `/ByteRange` over the earlier revision stays
//! valid (ISO 32000-1 §7.5.6; see `iso32000__s__7.5.6.md` in the spec
//! RAG).
//!
//! ## Why re-encoding is NOT equivalent — concrete spec cases
//!
//! PDF syntax is deliberately non-canonical; a decoded value does not
//! determine its source bytes. Cases the spec RAG documents explicitly:
//!
//! - **Names** (§7.3.5 NOTE 1): `/A#42` and `/AB` are the *same name*.
//! Re-emitting the decoded value changes bytes.
//! - **Literal strings** (§7.3.4.2): a bare CRLF inside a literal string
//! decodes to a single 0Ah byte — decode-then-re-encode is lossy by
//! specification.
//! - **Numerics** (§7.3.3): `4.`, `+17`, `0.40` all carry formatting a
//! parsed value can't reproduce.
//!
//! Hence every lossy-decoded entity keeps its span, and the span — not
//! the decoded value — is the unit of re-emission for untouched content.
//!
//! ## Offsets are absolute
//!
//! Spans are offsets into **the retained source buffer, from byte 0 of
//! that buffer** — not relative to the `%PDF-` header (which may not be
//! at byte 0; see the header-offset open question in `lib.rs`). One
//! buffer, one coordinate system, no ambiguity.
use fmt;
use Range;
/// A half-open byte range `[start, start + len)` into a retained source
/// buffer.
///
/// See the module docs for why this type is load-bearing rather than a
/// convenience. It is deliberately a plain value type (`Copy`, ordered,
/// hashable) so it can be embedded in every token and object without
/// ceremony.
///
/// # Examples
///
/// ```
/// use pdfcer_model::span::ByteSpan;
///
/// let buf = b"12 0 obj";
/// let span = ByteSpan::new(0, 2); // the "12" token
/// assert_eq!(span.slice(buf), Some(&b"12"[..]));
/// assert_eq!(span.end(), 2);
/// ```