lopdf 0.46.0

A Rust library for PDF document manipulation.
Documentation
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
use super::{Dictionary, Object, ObjectId, Reader, Stream, StringFormat};
use crate::Error;
use crate::content::*;
use crate::error;
use crate::xref::*;
use std::collections::HashSet;
use std::str::{self, FromStr};

use nom::branch::alt;
use nom::bytes::complete::{tag, take, take_while, take_while_m_n, take_while1};
use nom::character::complete::multispace1;
use nom::character::complete::{digit0, digit1, one_of};
use nom::character::complete::{space0, space1};
use nom::combinator::cut;
use nom::combinator::{map, map_opt, map_res, opt, verify};
use nom::error::{ErrorKind, ParseError};
use nom::multi::{fold_many0, fold_many1, many0, many0_count};
use nom::sequence::{delimited, pair, preceded, separated_pair, terminated};
use nom::{AsChar, IResult, Input, Parser};

pub(crate) mod cmap_parser;

pub(crate) type ParserInput<'a> = &'a [u8];
// Change this to something else that implements ParseError to get a
// different error type out of nom.
pub(crate) type NomError<'a> = nom::error::Error<ParserInput<'a>>;

pub(crate) type NomResult<'a, O, E = NomError<'a>> = IResult<ParserInput<'a>, O, E>;

#[inline]
fn strip_nom<O>(r: NomResult<O>) -> Option<O> {
    r.ok().map(|(_, o)| o)
}

#[inline]
fn convert_result<O, E>(result: Result<O, E>, input: ParserInput, error_kind: ErrorKind) -> NomResult<O> {
    result.map(|o| (input, o)).map_err(|_| {
        // this is a unit bind if NomError = ()
        let err: NomError = nom::error::Error::from_error_kind(input, error_kind);
        nom::Err::Error(err)
    })
}

#[inline]
fn offset_stream(object: &mut Object, offset: usize) {
    if let Object::Stream(stream) = object {
        stream.start_position = stream.start_position.and_then(|sp| sp.checked_add(offset));
    }
}

pub(crate) fn eol(input: ParserInput) -> NomResult<ParserInput> {
    alt((tag(&b"\r\n"[..]), tag(&b"\n"[..]), tag(&b"\r"[..]))).parse(input)
}

pub(crate) fn comment(input: ParserInput) -> NomResult<()> {
    map((tag(&b"%"[..]), take_while(|c: u8| !b"\r\n".contains(&c)), eol), |_| ()).parse(input)
}

#[inline]
fn is_whitespace(c: u8) -> bool {
    b" \t\n\r\0\x0C".contains(&c)
}

#[inline]
fn is_delimiter(c: u8) -> bool {
    b"()<>[]{}/%".contains(&c)
}

#[inline]
fn is_regular(c: u8) -> bool {
    !is_whitespace(c) && !is_delimiter(c)
}

#[inline]
fn is_direct_literal_string(c: u8) -> bool {
    !b"()\\\r\n".contains(&c)
}

fn white_space(input: ParserInput) -> NomResult<()> {
    map(take_while(is_whitespace), |_| ()).parse(input)
}

/// White space and comments, as many as there are.
fn space(mut input: ParserInput) -> NomResult<()> {
    loop {
        let blanks = input.iter().take_while(|&&c| is_whitespace(c)).count();
        if blanks > 0 {
            input = &input[blanks..];
        } else if input.first() == Some(&b'%')
            && let Ok((rest, ())) = comment(input)
        {
            input = rest;
        } else {
            return Ok((input, ()));
        }
    }
}

/// The value of a run of ASCII digits, or `None` if it exceeds `u64`.
fn digits_value(digits: &[u8]) -> Option<u64> {
    digits.iter().try_fold(0u64, |value, &digit| {
        value.checked_mul(10)?.checked_add(u64::from(digit - b'0'))
    })
}

/// An optional `+` or `-` sign, and whether it was `-`.
fn sign(input: ParserInput) -> NomResult<bool> {
    match input.split_first() {
        Some((b'+', rest)) => Ok((rest, false)),
        Some((b'-', rest)) => Ok((rest, true)),
        _ => Ok((input, false)),
    }
}

fn integer(input: ParserInput) -> NomResult<i64> {
    let (i, (negative, digits)) = pair(sign, digit1).parse(input)?;

    let value = digits_value(digits).and_then(|magnitude| {
        if negative {
            0i64.checked_sub_unsigned(magnitude)
        } else {
            i64::try_from(magnitude).ok()
        }
    });
    convert_result(value.ok_or(()), i, ErrorKind::Digit)
}

fn real(input: ParserInput) -> NomResult<f32> {
    let (i, _) = pair(
        sign,
        alt((
            map((digit1, tag(&b"."[..]), digit0), |_| ()),
            map(pair(tag(&b"."[..]), digit1), |_| ()),
        )),
    )
    .parse(input)?;
    // Some producers write a minus sign inside the fraction, as in `0.00-4142502`. Like
    // pdf.js and Poppler, ignore it rather than splitting the number in two.
    let (i, fraction) = take_while(|c: u8| c == b'-' || c.is_ascii_digit()).parse(i)?;

    let float_input = &input[..input.len() - i.len()];
    let value = if fraction.contains(&b'-') {
        let (sign, rest) = float_input.split_at(1);
        let mut digits = sign.to_vec();
        digits.extend(rest.iter().filter(|&&c| c != b'-'));
        f32::from_str(str::from_utf8(&digits).unwrap())
    } else {
        f32::from_str(str::from_utf8(float_input).unwrap())
    };
    convert_result(value, i, ErrorKind::Digit)
}

/// The value of an ASCII hexadecimal digit.
#[inline]
fn hex_value(c: u8) -> Option<u8> {
    (c as char).to_digit(16).map(|value| value as u8)
}

pub(crate) fn hex_char(input: ParserInput) -> NomResult<u8> {
    map_opt(take(2usize), |h: ParserInput| {
        Some((hex_value(h[0])? << 4) | hex_value(h[1])?)
    })
    .parse(input)
}

fn oct_char(input: ParserInput) -> NomResult<u8> {
    map_res(
        take_while_m_n(1, 3, AsChar::is_oct_digit),
        // Spec requires us to ignore any overflow.
        |x: ParserInput| u16::from_str_radix(str::from_utf8(x).unwrap(), 8).map(|o| o as u8),
    )
    .parse(input)
}

pub(crate) fn name(input: ParserInput) -> NomResult<Vec<u8>> {
    let (mut input, _) = tag(&b"/"[..]).parse(input)?;
    let mut bytes = Vec::new();
    loop {
        // Copy regular characters a run at a time, up to the next `#` or the end of the name.
        let run = input
            .iter()
            .position(|&c| c == b'#' || !is_regular(c))
            .unwrap_or(input.len());
        bytes.extend_from_slice(&input[..run]);
        input = &input[run..];
        // `#` and two hexadecimal digits stand for one byte; any other `#` ends the name.
        match input.split_first() {
            Some((b'#', escape)) => match hex_char(escape) {
                Ok((rest, byte)) => {
                    bytes.push(byte);
                    input = rest;
                }
                Err(_) => return Ok((input, bytes)),
            },
            _ => return Ok((input, bytes)),
        }
    }
}

fn escape_sequence(input: ParserInput) -> NomResult<Option<u8>> {
    preceded(
        tag(&b"\\"[..]),
        alt((
            map(oct_char, Some),
            map(eol, |_| None),
            map(tag(&b"n"[..]), |_| Some(b'\n')),
            map(tag(&b"r"[..]), |_| Some(b'\r')),
            map(tag(&b"t"[..]), |_| Some(b'\t')),
            map(tag(&b"b"[..]), |_| Some(b'\x08')),
            map(tag(&b"f"[..]), |_| Some(b'\x0C')),
            map(take(1usize), |c: ParserInput| Some(c[0])),
        )),
    )
    .parse(input)
}

enum InnerLiteralString<'a> {
    Direct(ParserInput<'a>),
    Escape(Option<u8>),
    Eol,
    Nested(Vec<u8>),
}

impl InnerLiteralString<'_> {
    fn push(&self, output: &mut Vec<u8>) {
        match self {
            InnerLiteralString::Direct(s) => output.extend_from_slice(s),
            InnerLiteralString::Eol => output.push(b'\n'),
            InnerLiteralString::Escape(e) => output.extend(e),
            InnerLiteralString::Nested(n) => output.extend_from_slice(n),
        }
    }
}

fn inner_literal_string(depth: usize) -> impl Fn(ParserInput) -> NomResult<Vec<u8>> {
    move |input| {
        fold_many0(
            alt((
                map(take_while1(is_direct_literal_string), InnerLiteralString::Direct),
                map(escape_sequence, InnerLiteralString::Escape),
                map(eol, |_| InnerLiteralString::Eol),
                map(nested_literal_string(depth), InnerLiteralString::Nested),
            )),
            Vec::new,
            |mut out: Vec<u8>, value| {
                value.push(&mut out);
                out
            },
        )
        .parse(input)
    }
}

fn nested_literal_string(depth: usize) -> impl Fn(ParserInput) -> NomResult<Vec<u8>> {
    move |input| {
        if depth == 0 {
            map(verify(tag(&b"too deep"[..]), |_: &[u8]| false), |_| vec![]).parse(input)
        } else {
            map(
                delimited(tag(&b"("[..]), inner_literal_string(depth - 1), tag(&b")"[..])),
                |mut content| {
                    content.insert(0, b'(');
                    content.push(b')');
                    content
                },
            )
            .parse(input)
        }
    }
}

fn literal_string(input: ParserInput) -> NomResult<Vec<u8>> {
    delimited(
        tag(&b"("[..]),
        inner_literal_string(crate::reader::MAX_BRACKET),
        tag(&b")"[..]),
    )
    .parse(input)
}

#[inline]
fn hex_digit(input: ParserInput) -> NomResult<u8> {
    map_opt(take(1usize), |c: ParserInput| hex_value(c[0])).parse(input)
}

fn hexadecimal_string(input: ParserInput) -> NomResult<Object> {
    map(
        delimited(
            tag(&b"<"[..]),
            terminated(
                fold_many0(
                    preceded(white_space, hex_digit),
                    || -> (Vec<u8>, bool) { (Vec::new(), false) },
                    |state, c| match state {
                        (mut out, false) => {
                            out.push(c << 4);
                            (out, true)
                        }
                        (mut out, true) => {
                            *out.last_mut().unwrap() |= c;
                            (out, false)
                        }
                    },
                ),
                white_space,
            ),
            tag(&b">"[..]),
        ),
        |(bytes, _)| Object::String(bytes, StringFormat::Hexadecimal),
    )
    .parse(input)
}

fn boolean(input: ParserInput) -> NomResult<Object> {
    alt((
        map(tag(&b"true"[..]), |_| Object::Boolean(true)),
        map(tag(&b"false"[..]), |_| Object::Boolean(false)),
    ))
    .parse(input)
}

fn null(input: ParserInput) -> NomResult<Object> {
    map(tag(&b"null"[..]), |_| Object::Null).parse(input)
}

fn array(depth: usize) -> impl Fn(ParserInput) -> NomResult<Vec<Object>> {
    move |input| {
        delimited(
            pair(tag(&b"["[..]), space),
            many0(_direct_object(depth)),
            tag(&b"]"[..]),
        )
        .parse(input)
    }
}

pub(crate) fn dictionary(input: ParserInput) -> NomResult<Dictionary> {
    _dictionary(crate::reader::MAX_NESTING_DEPTH)(input)
}

fn _dictionary(depth: usize) -> impl Fn(ParserInput) -> NomResult<Dictionary> {
    move |input| delimited(pair(tag(&b"<<"[..]), space), inner_dictionary(depth), tag(&b">>"[..])).parse(input)
}

fn inner_dictionary(depth: usize) -> impl Fn(ParserInput) -> NomResult<Dictionary> {
    move |input| {
        fold_many0(
            pair(terminated(name, space), _direct_object(depth)),
            Dictionary::new,
            |mut dict, (key, value)| {
                dict.set(key, value);
                dict
            },
        )
        .parse(input)
    }
}

pub(crate) fn dict_dup(input: ParserInput) -> NomResult<Dictionary> {
    delimited(
        (
            digit1,
            space1,
            tag(&b"dict"[..]),
            space1,
            tag(&b"dup"[..]),
            space1,
            tag(&b"begin"[..]),
            multispace1,
        ),
        fold_many0(
            terminated(
                pair(
                    terminated(name, space),
                    _direct_object(crate::reader::MAX_NESTING_DEPTH),
                ),
                pair(tag(&b"def"[..]), multispace1),
            ),
            Dictionary::new,
            |mut dict, (key, value)| {
                dict.set(key, value);
                dict
            },
        ),
        tag(&b"end"[..]),
    )
    .parse(input)
}

/// Recover the sole EOL-framed `endstream` immediately followed by `endobj`.
/// The caller must pass only bytes up to the current indirect-object boundary
/// so the scan cannot cross into a neighboring object.
fn recover_stream_length(input: ParserInput) -> Option<(ParserInput, ParserInput)> {
    const ENDSTREAM: &[u8] = b"endstream";
    let mut recovered = None;

    for (position, candidate) in input.windows(ENDSTREAM.len()).enumerate() {
        if candidate != ENDSTREAM {
            continue;
        }

        let data_end = if input[..position].ends_with(b"\r\n") {
            position - 2
        } else if input[..position].ends_with(b"\n") || input[..position].ends_with(b"\r") {
            position - 1
        } else {
            continue;
        };
        let after_endstream = &input[position + ENDSTREAM.len()..];
        let Ok((after_endobj, _)) = preceded(space, tag(&b"endobj"[..])).parse(after_endstream) else {
            continue;
        };
        if after_endobj.first().is_some_and(|&byte| !is_whitespace(byte)) {
            continue;
        }
        if recovered.is_some() {
            return None;
        }
        recovered = Some((after_endstream, &input[..data_end]));
    }

    recovered
}

/// The `stream` keyword and its end-of-line marker, which turn a dictionary into
/// the dictionary of a stream.
fn stream_keyword(input: ParserInput) -> NomResult<()> {
    map((space, tag(&b"stream"[..]), space0, eol), |_| ()).parse(input)
}

/// A stream whose dictionary `dict` starts `input`, with its data starting at `i`.
fn stream<'a>(
    input: ParserInput<'a>, i: ParserInput<'a>, dict: Dictionary, reader: &Reader,
    already_seen: &mut HashSet<ObjectId>, recover_length: bool, recovery_bound: Option<usize>,
) -> NomResult<'a, Object> {
    if let Ok(length) = dict.get(b"Length").and_then(|value| {
        if let Ok(id) = value.as_reference() {
            reader.get_object(id, already_seen).and_then(|value| value.as_i64())
        } else {
            value.as_i64()
        }
    }) {
        if length < 0 {
            // artificial error kind is created to allow descriptive nom errors
            return Err(nom::Err::Failure(NomError::from_error_kind(i, ErrorKind::LengthValue)));
        }
        let Ok(length) = usize::try_from(length) else {
            return Err(nom::Err::Failure(NomError::from_error_kind(i, ErrorKind::LengthValue)));
        };
        match terminated(take(length), pair(opt(eol), tag(&b"endstream"[..]))).parse(i) {
            Ok((remaining, data)) => Ok((remaining, Object::Stream(Stream::new(dict, data.to_vec())))),
            Err(_) if recover_length && !reader.strict => {
                // The scan must not cross into a neighbouring object, so it stops at the
                // xref-derived bound: the first `i.len() - bound` bytes of `i`.
                let scan_end = recovery_bound.map_or(i.len(), |bound| i.len().saturating_sub(bound));
                let Some((remaining, data)) = recover_stream_length(&i[..scan_end]) else {
                    return Err(nom::Err::Failure(NomError::from_error_kind(i, ErrorKind::LengthValue)));
                };
                log::warn!(
                    "Stream Length is {length}, but the unambiguous object boundary gives {} bytes; using the recovered length.",
                    data.len()
                );
                Ok((remaining, Object::Stream(Stream::new(dict, data.to_vec()))))
            }
            Err(_) => Err(nom::Err::Failure(NomError::from_error_kind(i, ErrorKind::LengthValue))),
        }
    } else {
        // Return position relative to the start of the stream dictionary.
        Ok((i, Object::Stream(Stream::with_position(dict, input.len() - i.len()))))
    }
}

fn unsigned_int<I: TryFrom<u64>>(input: ParserInput) -> NomResult<I> {
    map_opt(digit1, |digits: ParserInput| {
        digits_value(digits).and_then(|value| I::try_from(value).ok())
    })
    .parse(input)
}

fn object_id(input: ParserInput) -> NomResult<ObjectId> {
    pair(terminated(unsigned_int, space), terminated(unsigned_int, space)).parse(input)
}

fn reference(input: ParserInput) -> NomResult<Object> {
    map(terminated(object_id, tag(&b"R"[..])), Object::Reference).parse(input)
}

fn _direct_objects(depth: usize) -> impl Fn(ParserInput) -> NomResult<Object> {
    // The first byte tells the kinds of object apart, so it selects the parsers to try.
    // An integer also matches the start of a reference or a real, so it is tried last.
    move |input| match input.first() {
        Some(b'n') => null(input),
        Some(b't' | b'f') => boolean(input),
        Some(b'0'..=b'9') => alt((reference, map(real, Object::Real), map(integer, Object::Integer))).parse(input),
        Some(b'+' | b'-' | b'.') => alt((map(real, Object::Real), map(integer, Object::Integer))).parse(input),
        Some(b'/') => map(name, Object::Name).parse(input),
        Some(b'(') => map(literal_string, Object::string_literal).parse(input),
        Some(b'<') => alt((hexadecimal_string, map(_dictionary(depth), Object::Dictionary))).parse(input),
        Some(b'[') => map(array(depth), Object::Array).parse(input),
        _ => Err(nom::Err::Error(NomError::from_error_kind(input, ErrorKind::Alt))),
    }
}

fn _direct_object(depth: usize) -> impl Fn(ParserInput) -> NomResult<Object> {
    move |input| {
        if depth == 0 {
            return Err(nom::Err::Failure(NomError::from_error_kind(input, ErrorKind::TooLarge)));
        }
        terminated(_direct_objects(depth - 1), space).parse(input)
    }
}

pub fn direct_object(input: ParserInput) -> Option<Object> {
    strip_nom(_direct_object(crate::reader::MAX_NESTING_DEPTH)(input))
}

fn object<'a>(
    input: ParserInput<'a>, reader: &Reader, already_seen: &mut HashSet<ObjectId>, recover_stream_length: bool,
    recovery_bound: Option<usize>,
) -> NomResult<'a, Object> {
    let (i, object) = if input.starts_with(b"<<") {
        // A stream starts with its dictionary: parse the dictionary once, and let the
        // keyword after it decide whether the object is a stream.
        let (i, dict) = dictionary(input)?;
        match stream_keyword(i) {
            Ok((data, ())) => stream(
                input,
                data,
                dict,
                reader,
                already_seen,
                recover_stream_length,
                recovery_bound,
            )?,
            Err(_) => (i, Object::Dictionary(dict)),
        }
    } else {
        _direct_objects(crate::reader::MAX_NESTING_DEPTH)(input)?
    };
    let (i, ()) = space(i)?;
    Ok((i, object))
}

pub fn indirect_object(
    input: ParserInput, offset: usize, expected_id: Option<ObjectId>, reader: &Reader,
    already_seen: &mut HashSet<ObjectId>, recovery_bound: Option<usize>,
) -> crate::Result<(ObjectId, Object)> {
    // Every downstream slice is a suffix of `input`, so express the absolute
    // end offset as the maximum length a suffix may have.
    let recovery_bound = recovery_bound.map(|end| input.len().saturating_sub(end));
    let (id, mut object) = _indirect_object(
        input.take_from(offset),
        offset,
        expected_id,
        reader,
        already_seen,
        true,
        recovery_bound,
    )?;

    offset_stream(&mut object, offset);

    Ok((id, object))
}

fn _indirect_object<'a>(
    input: ParserInput<'a>, offset: usize, expected_id: Option<ObjectId>, reader: &Reader,
    already_seen: &mut HashSet<ObjectId>, recover_stream_length: bool, recovery_bound: Option<usize>,
) -> crate::Result<(ObjectId, Object)> {
    let (i, (_, object_id)) = terminated((space, object_id), pair(tag(&b"obj"[..]), space))
        .parse(input)
        .map_err(|_| Error::IndirectObject { offset })?;
    if let Some(expected_id) = expected_id
        && object_id != expected_id
    {
        return Err(crate::error::Error::ObjectIdMismatch);
    }

    let object_offset = input.len() - i.len();
    let (_, mut object) = terminated(
        |i: ParserInput<'a>| object(i, reader, already_seen, recover_stream_length, recovery_bound),
        (space, opt(tag(&b"endobj"[..])), space),
    )
    .parse(i)
    .map_err(|_| Error::IndirectObject { offset })?;

    offset_stream(&mut object, object_offset);

    Ok((object_id, object))
}

pub fn header(input: ParserInput, strict: bool) -> Option<String> {
    // Parse the version digits separately from trailing bytes: some generators put binary
    // markers on the header line. Strict mode rejects them, lenient mode skips them.
    let (_, (version_raw, trailing)) = delimited(
        tag(&b"%PDF-"[..]),
        pair(
            take_while(|c: u8| c.is_ascii_digit() || c == b'.'),
            take_while(|c: u8| !b"\r\n".contains(&c)),
        ),
        pair(eol, many0_count(comment)),
    )
    .parse(input)
    .ok()?;

    if strict && !trailing.is_empty() {
        return None;
    }

    let version = str::from_utf8(version_raw).ok()?.to_string();
    Some(version)
}

pub fn binary_mark(input: ParserInput) -> Option<Vec<u8>> {
    strip_nom(
        map_res(
            delimited(
                tag(&b"%"[..]),
                take_while(|c: u8| !b"\r\n".contains(&c)),
                pair(eol, many0_count(comment)),
            ),
            |v: ParserInput| Ok::<Vec<u8>, ()>(v.to_vec()),
        )
        .parse(input),
    )
}

/// Decode CrossReferenceTable
fn xref(input: ParserInput, strict: bool) -> NomResult<Xref> {
    // ISO 32000-1 s7.5.4 requires 20-byte entries ending in SP CR, SP LF or CR LF, but many
    // generators emit 19-byte entries ending in a bare LF, which qpdf, pikepdf, PDFium and
    // PDF.js accept. The conforming forms come first in the `alt` so a bare CR never matches
    // the CR of a CR LF and strands its LF.
    let xref_eol = move |i| {
        let conforming = alt((tag(&b" \r"[..]), tag(&b" \n"[..]), tag(&b"\r\n"[..])));
        if strict {
            map(conforming, |_| ()).parse(i)
        } else {
            map(alt((conforming, tag(&b"\n"[..]), tag(&b"\r"[..]))), |_| ()).parse(i)
        }
    };
    let xref_entry = pair(
        separated_pair(unsigned_int, tag(&b" "[..]), unsigned_int::<u32>),
        delimited(tag(&b" "[..]), map(one_of("nf"), |k| k == 'n'), xref_eol),
    );

    let xref_section = pair(
        separated_pair(unsigned_int::<usize>, tag(&b" "[..]), unsigned_int::<u32>),
        preceded(pair(opt(tag(&b" "[..])), eol), many0(xref_entry)),
    );

    delimited(
        pair(tag(&b"xref"[..]), preceded(opt(tag(&b" "[..])), eol)),
        fold_many1(
            xref_section,
            || -> Xref { Xref::new(0, XrefType::CrossReferenceTable) },
            |mut xref, ((start, _count), entries)| {
                let mut skipped = 0usize;
                for (index, ((offset, generation), is_normal)) in entries.into_iter().enumerate() {
                    if is_normal && let Ok(generation) = generation.try_into() {
                        // `start` is untrusted: `start + index` can overflow, and a `start`
                        // above u32::MAX would truncate into a valid-looking object number.
                        match start.checked_add(index).and_then(|id| u32::try_from(id).ok()) {
                            Some(id) => xref.insert(id, XrefEntry::Normal { offset, generation }),
                            None => skipped += 1,
                        }
                    }
                }
                if skipped > 0 {
                    log::warn!(
                        "cross-reference subsection starting at {start} has {skipped} entr{} with an object number beyond u32; skipping",
                        if skipped == 1 { "y" } else { "ies" }
                    );
                }
                xref
            },
        ),
        space,
    )
    .parse(input)
}

fn trailer(input: ParserInput) -> NomResult<Dictionary> {
    delimited(pair(tag(&b"trailer"[..]), space), dictionary, space).parse(input)
}

pub fn xref_and_trailer(input: ParserInput, reader: &Reader) -> crate::Result<(Xref, Dictionary)> {
    let xref_trailer = map(pair(|i| xref(i, reader.strict), trailer), |(mut xref, trailer)| {
        xref.size = trailer
            .get(b"Size")
            .and_then(Object::as_i64)
            .map_err(|_| error::ParseError::InvalidTrailer)? as u32;
        Ok((xref, trailer))
    });
    alt((
        xref_trailer,
        (|input| {
            _indirect_object(input, 0, None, reader, &mut HashSet::new(), false, None)
                .map(|(_, obj)| {
                    let res = match obj {
                        Object::Stream(stream) => decode_xref_stream_with_limit(stream, reader.max_decompressed_size),
                        _ => Err(crate::error::ParseError::InvalidXref.into()),
                    };
                    (input, res)
                })
                .map_err(|_| {
                    // artificial error kind is created to allow descriptive nom errors
                    nom::Err::Error(NomError::from_error_kind(input, ErrorKind::Fail))
                })
        }),
    ))
    .parse(input)
    .map(|(_, o)| o)
    .map_err(|_| error::ParseError::InvalidTrailer)?
}

pub fn xref_start(input: ParserInput) -> Option<i64> {
    strip_nom(
        delimited(
            pair(tag(&b"startxref"[..]), preceded(opt(tag(&b" "[..])), eol)),
            trim_spaces(integer),
            (eol, tag(&b"%%EOF"[..]), space),
        )
        .parse(input),
    )
}

fn trim_spaces<'a, O>(
    p: impl Parser<ParserInput<'a>, Output = O, Error = NomError<'a>>,
) -> impl Parser<ParserInput<'a>, Output = O, Error = NomError<'a>> {
    delimited(many0(tag(" ")), p, many0(tag(" ")))
}

// The following code create parser to parse content stream.

fn content_space(input: ParserInput) -> NomResult<()> {
    map(take_while(is_whitespace), |_| ()).parse(input)
}

fn operator(input: ParserInput) -> NomResult<String> {
    map_res(
        // Digits may follow the first character, as in the Type 3 glyph operators d0 and d1.
        verify(
            take_while1(|c: u8| c.is_ascii_alphanumeric() || b"*'\"".contains(&c)),
            |op: ParserInput| !op[0].is_ascii_digit(),
        ),
        |op: ParserInput| str::from_utf8(op).map(Into::into),
    )
    .parse(input)
}

fn operand(input: ParserInput) -> NomResult<Object> {
    terminated(
        alt((
            null,
            boolean,
            map(real, Object::Real),
            map(integer, Object::Integer),
            map(name, Object::Name),
            map(literal_string, Object::string_literal),
            hexadecimal_string,
            map(array(crate::reader::MAX_NESTING_DEPTH), Object::Array),
            map(dictionary, Object::Dictionary),
        )),
        content_space,
    )
    .parse(input)
}

fn operation(input: ParserInput) -> NomResult<Operation> {
    map(
        preceded(
            // A comment consumes its end-of-line marker, so also skip any
            // white space that follows it (e.g. an indented next line).
            many0(terminated(comment, content_space)),
            alt((inline_image, terminated(pair(many0(operand), operator), content_space))),
        ),
        |(operands, operator)| Operation { operator, operands },
    )
    .parse(input)
}

fn inline_image(input: ParserInput) -> NomResult<(Vec<Object>, String)> {
    preceded(pair(tag(&b"BI"[..]), content_space), cut(inline_image_impl)).parse(input)
}

/// The single white-space character between `ID` and the image data (ISO 32000-2, 8.9.7),
/// with a CR LF pair read as one end-of-line. White space after it is image data: a gray
/// level of 32 is a space byte.
fn inline_image_data_separator(input: ParserInput) -> NomResult<()> {
    map(opt(alt((eol, take_while_m_n(1, 1, is_whitespace)))), |_| ()).parse(input)
}

fn inline_image_impl(input: ParserInput) -> NomResult<(Vec<Object>, String)> {
    let (input, stream_dict) = inner_dictionary(crate::reader::MAX_NESTING_DEPTH).parse(input)?;
    let (input, _) = pair(tag(&b"ID"[..]), inline_image_data_separator).parse(input)?;
    match image_data_stream(input, stream_dict) {
        Ok((input, stream)) => {
            let (input, _) = (content_space, tag(&b"EI"[..]), content_space).parse(input)?;
            Ok((input, (vec![Object::Stream(stream)], String::from("BI"))))
        }
        Err(e) => {
            // Skip to EI marker so the rest of the content stream can still be parsed.
            log::warn!("Skipping unparseable inline image: {e}");
            let bytes = input;
            // EI must appear after whitespace to distinguish from data bytes.
            let ei_pos = bytes
                .windows(4)
                .position(|w| {
                    (w[0] == b' ' || w[0] == b'\n' || w[0] == b'\r')
                        && w[1] == b'E'
                        && w[2] == b'I'
                        && (w[3] == b' ' || w[3] == b'\n' || w[3] == b'\r')
                })
                .ok_or_else(|| {
                    let err: NomError = nom::error::Error::from_error_kind(input, ErrorKind::Fail);
                    nom::Err::Failure(err)
                })?;
            let (input, _) = take(ei_pos + 3).parse(input).map_err(|_: nom::Err<()>| {
                let err: NomError = nom::error::Error::from_error_kind(input, ErrorKind::Fail);
                nom::Err::Failure(err)
            })?;
            let (input, _) = content_space(input)?;
            Ok((input, (vec![], String::from("BI"))))
        }
    }
}

fn image_data_stream(input: ParserInput, stream_dict: Dictionary) -> crate::Result<(ParserInput, Stream)> {
    let get_abbr = |key_abbr: &[u8], key: &[u8]| stream_dict.get(key_abbr).or_else(|_| stream_dict.get(key));
    let invalid_size = || Error::InvalidInlineImage(String::from("image size out of range"));
    let get_size = |key_abbr: &[u8], key: &[u8]| -> crate::Result<usize> {
        usize::try_from(get_abbr(key_abbr, key)?.as_i64()?).map_err(|_| invalid_size())
    };
    let width = get_size(b"W", b"Width")?;
    let height = get_size(b"H", b"Height")?;
    let bpc = get_size(b"BPC", b"BitsPerComponent")?;
    let im = get_abbr(b"IM", b"ImageMask").and_then(|x| x.as_bool());
    let num_colors = match im {
        // If we have an image mask then we don't have a colorspace
        Ok(true) => 1,
        _ => match get_abbr(b"CS", b"ColorSpace")? {
            // An Indexed colour space is an array, and each sample is an index into its table.
            Object::Array(array) if matches!(array.first().map(Object::as_name), Some(Ok(b"Indexed" | b"I"))) => 1,
            colorspace => match colorspace.as_name()? {
                b"DeviceGray" | b"G" | b"Gray" => 1,
                b"DeviceRGB" | b"RGB" => 3,
                b"DeviceRGBA" | b"RGBA" => 4,
                b"DeviceCMYK" | b"CMYK" => 4,
                b"Pattern" => {
                    log::warn!("Pattern colorspace is not allowed in inline images");
                    return Err(Error::InvalidInlineImage(String::from(
                        "Pattern colorspace is not allowed in inline images",
                    )));
                }
                _ => {
                    log::warn!("Colorspace of inline image not recognized / not yet implemented");
                    return Err(Error::Unimplemented("inline image colorspaces"));
                }
            },
        },
    };

    let length = bpc
        .checked_mul(num_colors)
        .and_then(|bits_per_pixel| width.checked_mul(bits_per_pixel))
        .and_then(|row_bits| row_bits.div_ceil(8).checked_mul(height))
        .ok_or_else(invalid_size)?;

    let (input, content) = match get_abbr(b"F", b"Filter") {
        Err(_) => {
            // no decompression needed as no filter was applied
            take(length)
                .parse(input)
                .map_err(|_: nom::Err<()>| crate::error::ParseError::EndOfInput)?
        }
        Ok(Object::Name(_filter)) => {
            log::warn!("Filters for inline images are not yet implemented");
            return Err(Error::Unimplemented("filters for inline images"));
        }
        Ok(Object::Array(_filters)) => {
            log::warn!("Filters for inline images are not yet implemented");
            return Err(Error::Unimplemented("filters for inline images"));
        }
        Ok(obj) => {
            log::warn!("Filter must be either a Name or and Array.");
            return Err(Error::ObjectType {
                expected: "Name or Array",
                found: obj.enum_variant(),
            });
        }
    };
    Ok((input, Stream::new(stream_dict, content.to_vec())))
}

fn _content(input: ParserInput) -> NomResult<Content<Vec<Operation>>> {
    delimited(
        content_space,
        map(many0(operation), |operations| Content { operations }),
        many0(terminated(comment, content_space)),
    )
    .parse(input)
}

pub fn content(input: ParserInput) -> Option<Content<Vec<Operation>>> {
    strip_nom(_content.parse(input))
}

/// The operations of a content stream, decoded one at a time with the grammar of
/// [`content`].
///
/// Yields exactly the operations `content` collects, in order, then an error where
/// `content` fails, so the operations of a large stream need not all be held at once.
pub struct ContentOperations<'a> {
    input: ParserInput<'a>,
    started: bool,
    finished: bool,
}

impl<'a> ContentOperations<'a> {
    pub(crate) fn new(input: ParserInput<'a>) -> Self {
        ContentOperations {
            input,
            started: false,
            finished: false,
        }
    }

    fn fail(&mut self) -> Option<crate::Result<Operation>> {
        self.finished = true;
        Some(Err(error::ParseError::InvalidContentStream.into()))
    }
}

impl Iterator for ContentOperations<'_> {
    type Item = crate::Result<Operation>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.finished {
            return None;
        }
        if !self.started {
            self.started = true;
            match content_space(self.input) {
                Ok((input, ())) => self.input = input,
                Err(_) => return self.fail(),
            }
        }
        let len = self.input.len();
        match operation(self.input) {
            // `many0` fails when its parser succeeds without consuming input.
            Ok((input, _)) if input.len() == len => self.fail(),
            Ok((input, operation)) => {
                self.input = input;
                Some(Ok(operation))
            }
            // `many0` stops at a recoverable error, and `content` ignores the rest.
            Err(nom::Err::Error(_)) => {
                self.finished = true;
                None
            }
            Err(_) => self.fail(),
        }
    }
}

pub fn content_strict(input: ParserInput) -> Result<Content<Vec<Operation>>, error::ParseError> {
    let (rest, content) = _content
        .parse(input)
        .map_err(|_| error::ParseError::InvalidContentStream)?;
    if !rest.is_empty() {
        return Err(error::ParseError::InvalidContentStream);
    }
    Ok(content)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_span(s: &'_ [u8]) -> ParserInput<'_> {
        s
    }

    fn tstrip<O>(r: NomResult<O>) -> Option<O> {
        r.ok().and_then(|(i, o)| if !i.is_empty() { None } else { Some(o) })
    }

    #[test]
    fn content_operations_match_content() {
        let streams: [&[u8]; 6] = [
            b"",
            b"q 1 0 0 1 72 720 cm BT /F1 12 Tf [(A) -120 (B)] TJ ET Q",
            b"  % leading comment\n0 0 m 10 10 l S\n% trailing comment\n",
            b"BT /F1 12 Tf (text) Tj ET\n) unparsable trailing input is ignored",
            b"BI /W 1 /H 1 /BPC 8 /CS /G ID X EI 0 0 m",
            b"/P <</MCID 0>> BDC BT (marked) Tj ET EMC",
        ];
        for stream in streams {
            let expected = content(test_span(stream)).expect("content decodes");
            let operations = ContentOperations::new(test_span(stream))
                .collect::<crate::Result<Vec<_>>>()
                .expect("operations decode");
            assert_eq!(
                format!("{:?}", operations),
                format!("{:?}", expected.operations),
                "stream: {:?}",
                String::from_utf8_lossy(stream)
            );
        }
    }

    #[test]
    fn content_operations_fail_where_content_fails() {
        // Without EI the inline image cannot be skipped, so the whole decode fails.
        let stream = test_span(b"BT (before) Tj ET\nBI /W 1 /H 1 /BPC 8 /CS /G ID X\nBT (after) Tj ET");
        assert!(content(stream).is_none());

        let mut operations = ContentOperations::new(stream);
        for operator in ["BT", "Tj", "ET"] {
            assert_eq!(operations.next().unwrap().unwrap().operator, operator);
        }
        assert!(matches!(
            operations.next(),
            Some(Err(Error::Parse(error::ParseError::InvalidContentStream)))
        ));
        assert!(operations.next().is_none());
    }

    #[test]
    fn parse_real_number() {
        let real = |i| tstrip(real(i));

        assert_eq!(real(test_span(b"0.12")), Some(0.12));
        assert_eq!(real(test_span(b"-.12")), Some(-0.12));
        assert_eq!(real(test_span(b"10.")), Some(10.0));
        // A minus sign inside the fraction is ignored.
        assert_eq!(real(test_span(b"0.00-4142502")), Some(0.004142502));
        assert_eq!(real(test_span(b"-1.5-5")), Some(-1.55));
    }

    #[test]
    fn parse_string() {
        let literal_string = |i| tstrip(literal_string(i));

        let data = vec![
            ("()", ""),
            ("(text())", "text()"),
            ("(text\r\n\\\\(nested\\t\\b\\f))", "text\n\\(nested\t\x08\x0C)"),
            // An unescaped end-of-line marker of any kind reads as a single line feed.
            ("(a\rb\nc\r\n\r\nd\n\re)", "a\nb\nc\n\nd\n\ne"),
            ("(text\\0\\53\\053\\0053)", "text\0++\x053"),
            ("(text line\\\n())", "text line()"),
        ];

        for (input, expected) in data {
            assert_eq!(
                literal_string(test_span(input.as_bytes())),
                Some(expected.as_bytes().to_vec()),
                "input: {:?} output: {:?}",
                input,
                expected,
            );
        }
    }

    #[test]
    fn parse_name() {
        let (text, expected) = (b"/ABC#5f", b"ABC\x5F");
        let result = tstrip(name(test_span(text)));
        assert_eq!(result, Some(expected.to_vec()));

        let (text, expected) = (b"/#cb#ce#cc#e5", b"\xcb\xce\xcc\xe5");
        let result = tstrip(name(test_span(text)));
        assert_eq!(result, Some(expected.to_vec()));
    }

    #[test]
    /// Run `cargo test -- --nocapture` to see output
    fn parse_content() {
        let stream = b"
2 J
BT
/F1 12 Tf
0 Tc
0 Tw
72.5 712 TD
[(Unencoded streams can be read easily) 65 (,) ] TJ
0 -14 TD
[(b) 20 (ut generally tak) 10 (e more space than \\311)] TJ
T* (encoded streams.) Tj
		";
        let content = tstrip(_content(test_span(stream)));
        assert!(content.is_some());
    }

    #[test]
    fn hex_partial() {
        // Example from PDF specification.
        let out = tstrip(hexadecimal_string(test_span(b"<901FA>")));

        match out {
            Some(Object::String(s, _)) => assert_eq!(s, b"\x90\x1F\xA0".to_vec()),
            _ => panic!("unexpected {:?}", out),
        }
    }

    #[test]
    fn hex_separated() {
        let out = tstrip(hexadecimal_string(test_span(b"<9 01F A>")));

        match out {
            Some(Object::String(s, _)) => assert_eq!(s, b"\x90\x1F\xA0".to_vec()),
            _ => panic!("unexpected {:?}", out),
        }
    }

    #[test]
    fn big_generation_value() {
        let input = b"xref
0 1
0000000000 65536 f\x20
0 16
0000000000 65535 f\x20
0000153238 00000 n\x20
0000000019 00000 n\x20
0000000313 00000 n\x20
0000000333 00000 n\x20
0000145531 00000 n\x20
0000153407 00000 n\x20
0000145554 00000 n\x20
0000152303 00000 n\x20
0000152324 00000 n\x20
0000152514 00000 n\x20
0000152880 00000 n\x20
0000153106 00000 n\x20
0000153139 00000 n\x20
0000153532 00000 n\x20
0000153629 00000 n\x20
trailer
<</Size 16/Root 14 0 R
/Info 15 0 R
/ID [ <9DDC4B621B3F485FF5ED0F57D00A028F>
<9DDC4B621B3F485FF5ED0F57D00A028F> ]
/DocChecksum /2BCC3C7DE26E6BF3573E4A6E8362221F
>>
startxref
153804\x20
%%EOF
";
        match xref(test_span(input), false) {
            Ok((_, re)) => assert_eq!(re.entries.len(), 15),
            Err(err) => panic!("unexpected {:?}", err),
        }
    }

    #[test]
    fn space_in_startxref_number() {
        let input = b"startxref
153804\x20
%%EOF
";
        match xref_start(test_span(input)) {
            Some(num) => assert_eq!(num, 153804),
            None => panic!("could not parse number in startxref"),
        }
    }

    #[test]
    fn header_standard() {
        // Standard header with proper EOL
        let input = b"%PDF-1.7\n%\xe2\xe3\xcf\xd3\n";
        assert_eq!(header(test_span(input), false), Some("1.7".to_string()));
    }

    #[test]
    fn header_with_binary_bytes_on_same_line() {
        // Some generators (e.g. ImageMill) place binary marker bytes on the
        // header line without a separating newline or '%' prefix.
        let input = b"%PDF-1.3 \xb0\x9f\x92\x9c\x9f\xd4\xe0\xce\xd0\xd0\xd0\r1 0 obj\r";
        assert_eq!(header(test_span(input), false), Some("1.3".to_string()));
    }

    #[test]
    fn header_with_binary_bytes_strict_rejects() {
        // In strict mode, binary bytes on the header line should cause a
        // parse failure (the raw bytes are not valid UTF-8).
        let input = b"%PDF-1.3 \xb0\x9f\x92\x9c\x9f\xd4\xe0\xce\xd0\xd0\xd0\r1 0 obj\r";
        assert_eq!(header(test_span(input), true), None);
    }

    #[test]
    fn header_cr_line_ending() {
        // CR-only line ending (common in older PDFs)
        let input = b"%PDF-1.3\r%\xe2\xe3\xcf\xd3\r";
        assert_eq!(header(test_span(input), false), Some("1.3".to_string()));
    }

    #[test]
    fn header_crlf_line_ending() {
        // CRLF line ending (common on Windows-generated PDFs)
        let input = b"%PDF-1.7\r\n%\xe2\xe3\xcf\xd3\r\n";
        assert_eq!(header(test_span(input), false), Some("1.7".to_string()));
    }

    #[test]
    fn header_pdf_2_0() {
        let input = b"%PDF-2.0\n%\xe2\xe3\xcf\xd3\n";
        assert_eq!(header(test_span(input), false), Some("2.0".to_string()));
    }

    #[test]
    fn content_with_comments() {
        // It should be processed as usual but ignoring the comments
        let input = b"0.5 0.5 0.5 setrgbcolor
% This is a comment
100 100 moveto
(Hello, world!) show
% Another comment
";
        let out = content(test_span(input)).unwrap();
        let out_strict = content_strict(test_span(input)).unwrap();
        assert_eq!(out.operations.len(), out_strict.operations.len());
        assert_eq!(out.operations.len(), 3);
    }

    #[test]
    fn content_with_comment_followed_by_indented_line() {
        // A comment is a single whitespace character (ISO 32000-2, 7.2.4), so an indented
        // line right after one must not stop the parser (issue #535).
        let input = b"BT /F1 24 Tf
% comment
  100 100 Td (Hello World) Tj ET";
        let out = content(test_span(input)).unwrap();
        let out_strict = content_strict(test_span(input)).unwrap();
        assert_eq!(out.operations.len(), out_strict.operations.len());
        let ops: Vec<&str> = out.operations.iter().map(|o| o.operator.as_str()).collect();
        assert_eq!(ops, vec!["BT", "Tf", "Td", "Tj", "ET"]);
    }

    #[test]
    fn inline_image_unknown_colorspace_skipped() {
        // Inline image with an unrecognized colorspace ("ICCBased" is not handled).
        // The parser should skip it and still parse the surrounding operations.
        let input = b"q 100 100 moveto
BI /W 2 /H 2 /CS /ICCBased /BPC 8
ID
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
EI
(Hello) Tj Q";
        let out = content(test_span(input)).unwrap();
        // Should have: q, moveto, BI (skipped), Tj, Q = 5 operations
        let ops: Vec<&str> = out.operations.iter().map(|o| o.operator.as_str()).collect();
        assert!(ops.contains(&"q"), "missing q, got: {:?}", ops);
        assert!(ops.contains(&"Tj"), "missing Tj, got: {:?}", ops);
        assert!(ops.contains(&"Q"), "missing Q, got: {:?}", ops);
    }

    #[test]
    fn inline_image_with_invalid_dimensions_skipped() {
        // Negative dimensions or a byte count that doesn't fit in usize must not overflow.
        for dims in [
            &b"/W -1 /H 1 /BPC 8"[..],
            b"/W 4294967296 /H 4294967296 /BPC 8",
            b"/W 1 /H 1 /BPC 9223372036854775807",
        ] {
            let mut input = b"q BI ".to_vec();
            input.extend_from_slice(dims);
            input.extend_from_slice(b" /CS /DeviceRGB ID \x00\x00\x00 EI (Hello) Tj Q");
            let out = content(test_span(&input)).unwrap();
            let ops: Vec<&str> = out.operations.iter().map(|o| o.operator.as_str()).collect();
            assert_eq!(ops, vec!["q", "BI", "Tj", "Q"]);
            assert!(out.operations[1].operands.is_empty());
        }
    }

    #[test]
    fn inline_image() {
        let _ = env_logger::try_init();
        let input = b"BI /W 4 /H 4 /CS /RGB /BPC 8
ID
00000z0z00zzz00z0zzz0zzzEI aazazaazzzaazazzzazzz
EI";
        let out = super::inline_image(test_span(input)).unwrap().1;
        assert_eq!(&out.1, "BI");
        assert_eq!(
            &out.0[0].as_stream().unwrap().content,
            b"00000z0z00zzz00z0zzz0zzzEI aazazaazzzaazazzzazzz"
        )
    }

    #[test]
    fn inline_image_abbreviated_color_spaces() {
        // 2x1 images using the abbreviated colour space names allowed in inline images.
        for (color_space, data) in [
            (&b"/G"[..], &b"\x00\xff"[..]),
            (b"/RGB", b"\x00\x00\x00\xff\xff\xff"),
            (b"/CMYK", b"\x00\x00\x00\x00\xff\xff\xff\xff"),
            (b"[/I /RGB 1 <000000FFFFFF>]", b"\x00\x01"),
            (b"[/Indexed /DeviceRGB 1 <000000FFFFFF>]", b"\x00\x01"),
        ] {
            let mut input = b"q BI /W 2 /H 1 /BPC 8 /CS ".to_vec();
            input.extend_from_slice(color_space);
            input.extend_from_slice(b" ID ");
            input.extend_from_slice(data);
            input.extend_from_slice(b" EI Q");
            let out = content(test_span(&input)).unwrap();
            let ops: Vec<&str> = out.operations.iter().map(|o| o.operator.as_str()).collect();
            assert_eq!(ops, vec!["q", "BI", "Q"]);
            let image = out.operations[1].operands[0].as_stream().unwrap();
            assert_eq!(image.content, data);
        }
    }

    #[test]
    fn inline_image_data_starting_with_white_space() {
        // A 3x1 gray image whose three pixels have level 32, i.e. space bytes. Only the
        // first white-space character after ID separates it from the data.
        let input = b"q\rBI\r/CS/DeviceGray/W 3/H 1/BPC 8\rID\r   \nEI\rQ\r";
        let out = content_strict(test_span(input)).unwrap();
        let ops: Vec<&str> = out.operations.iter().map(|o| o.operator.as_str()).collect();
        assert_eq!(ops, vec!["q", "BI", "Q"]);
        assert_eq!(&out.operations[1].operands[0].as_stream().unwrap().content, b"   ");
    }

    #[test]
    fn inline_image_data_after_crlf() {
        // A CR LF after ID is one end-of-line, so the data starts after the LF.
        let input = b"BI /CS /DeviceGray /W 2 /H 1 /BPC 8 ID\r\n\n\x80\nEI";
        let out = super::inline_image(test_span(input)).unwrap().1;
        assert_eq!(&out.0[0].as_stream().unwrap().content, b"\n\x80");
    }

    #[test]
    fn xref_trailing_space_after_keyword() {
        // Some PDF generators emit "xref \n" with a trailing space.
        let input = b"xref \n0 3\n0000000000 65535 f \n0000000017 00000 n \n0000000081 00000 n \ntrailer\n<</Size 3/Root 1 0 R>>\nstartxref\n175\n%%EOF\n";
        match xref(test_span(input), false) {
            Ok((_, re)) => assert_eq!(re.entries.len(), 2),
            Err(err) => panic!("xref with trailing space should parse: {:?}", err),
        }
    }

    #[test]
    fn xref_entries_with_bare_eol_terminator() {
        // 19-byte entries with a bare "\n" terminator are non-conforming but accepted by
        // qpdf, pikepdf, PDFium and PDF.js, so lenient parsing accepts them too.
        for (name, input) in [
            (
                "bare LF",
                &b"xref\n0 3\n0000000000 65535 f\n0000000017 00000 n\n0000000081 00000 n\ntrailer\n<</Size 3/Root 1 0 R>>\n"[..],
            ),
            (
                "bare CR",
                &b"xref\n0 3\n0000000000 65535 f\r0000000017 00000 n\r0000000081 00000 n\rtrailer\n<</Size 3/Root 1 0 R>>\n"[..],
            ),
        ] {
            match xref(test_span(input), false) {
                Ok((_, re)) => assert_eq!(re.entries.len(), 2, "{name} should yield both normal entries"),
                Err(err) => panic!("19-byte entries ({name}) should parse when lenient: {err:?}"),
            }
        }
    }

    #[test]
    fn xref_entries_with_bare_eol_rejected_when_strict() {
        // Rejection is indirect: the entries are not recognised, leaving an empty section
        // whose unconsumed lines then displace `trailer`.
        let input =
            b"xref\n0 3\n0000000000 65535 f\n0000000017 00000 n\n0000000081 00000 n\ntrailer\n<</Size 3/Root 1 0 R>>\n";
        if let Ok((_, re)) = xref(test_span(input), true) {
            assert_eq!(re.entries.len(), 0, "strict must not accept 19-byte entries");
        }

        // The 20-byte build of the same document is the control: it must load in both
        // modes, so any strict rejection is attributable to the terminator alone.
        let load = |bytes: &[u8], strict: bool| {
            crate::Document::load_mem_with_options(
                bytes,
                crate::LoadOptions {
                    strict,
                    ..Default::default()
                },
            )
        };

        for (name, term, strict_should_load) in [("19-byte", "\n", false), ("20-byte", " \n", true)] {
            let header = "%PDF-1.7\n";
            let obj1 = "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n";
            let obj2 = "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n";
            let obj3 = "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] >>\nendobj\n";
            let o1 = header.len();
            let o2 = o1 + obj1.len();
            let o3 = o2 + obj2.len();
            let body = format!("{header}{obj1}{obj2}{obj3}");
            let xref_pos = body.len();
            let doc = format!(
                "{body}xref\n0 4\n0000000000 65535 f{term}{o1:010} 00000 n{term}{o2:010} 00000 n{term}\
                 {o3:010} 00000 n{term}trailer\n<< /Size 4 /Root 1 0 R >>\nstartxref\n{xref_pos}\n%%EOF\n"
            );

            assert!(
                load(doc.as_bytes(), false).is_ok(),
                "{name} entries should load when lenient"
            );
            assert_eq!(
                load(doc.as_bytes(), true).is_ok(),
                strict_should_load,
                "{name} entries under strict parsing"
            );
        }
    }

    #[test]
    fn xref_entries_with_conforming_terminators() {
        // The three 20-byte terminators of s7.5.4 must keep parsing in both modes.
        for (name, input) in [
            (
                "SP LF",
                &b"xref\n0 3\n0000000000 65535 f \n0000000017 00000 n \n0000000081 00000 n \ntrailer\n<</Size 3/Root 1 0 R>>\n"[..],
            ),
            (
                "SP CR",
                &b"xref\n0 3\n0000000000 65535 f \r0000000017 00000 n \r0000000081 00000 n \rtrailer\n<</Size 3/Root 1 0 R>>\n"[..],
            ),
            (
                "CR LF",
                &b"xref\n0 3\n0000000000 65535 f\r\n0000000017 00000 n\r\n0000000081 00000 n\r\ntrailer\n<</Size 3/Root 1 0 R>>\n"[..],
            ),
        ] {
            for strict in [false, true] {
                match xref(test_span(input), strict) {
                    Ok((_, re)) => assert_eq!(re.entries.len(), 2, "{name} (strict={strict}) lost an entry"),
                    Err(err) => panic!("conforming {name} entries should parse (strict={strict}): {err:?}"),
                }
            }
        }
    }

    #[test]
    fn xref_subsection_start_near_usize_max_is_skipped() {
        // A start of usize::MAX made `start + index` overflow: a panic where overflow checks
        // are on, and a silent wrap to object 0 where they are not.
        let input = &b"xref\n18446744073709551615 2\n0000000000 65535 f \n0000000009 00000 n \ntrailer\n<</Size 2/Root 1 0 R>>\n"[..];
        for strict in [false, true] {
            match xref(test_span(input), strict) {
                Ok((_, re)) => assert!(
                    re.entries.is_empty(),
                    "unrepresentable object number was inserted (strict={strict}): {:?}",
                    re.entries
                ),
                Err(err) => panic!("xref should still parse (strict={strict}): {err:?}"),
            }
        }
    }

    #[test]
    fn xref_subsection_start_beyond_u32_does_not_displace_entries() {
        // A start above u32::MAX truncated into a valid-looking number (4294967297 becomes 1),
        // overwriting a legitimate entry. No overflow occurs, so a checked add alone misses it.
        let input = &b"xref\n0 2\n0000000000 65535 f \n0000000009 00000 n \n4294967297 1\n0000000999 00000 n \ntrailer\n<</Size 2/Root 1 0 R>>\n"[..];
        for strict in [false, true] {
            match xref(test_span(input), strict) {
                Ok((_, re)) => {
                    assert_eq!(
                        re.entries.len(),
                        1,
                        "(strict={strict}) unexpected entries: {:?}",
                        re.entries
                    );
                    assert!(
                        matches!(
                            re.get(1),
                            Some(XrefEntry::Normal {
                                offset: 9,
                                generation: 0
                            })
                        ),
                        "entry for object 1 was displaced (strict={strict}): {:?}",
                        re.get(1)
                    );
                }
                Err(err) => panic!("xref should still parse (strict={strict}): {err:?}"),
            }
        }
    }

    #[test]
    fn startxref_trailing_space_after_keyword() {
        // Some PDF generators emit "startxref \n" with a trailing space.
        let input = b"startxref \n135738\n%%EOF\n";
        match xref_start(test_span(input)) {
            Some(num) => assert_eq!(num, 135738),
            None => panic!("startxref with trailing space should parse"),
        }
    }

    #[test]
    fn content_silently_truncates_corrupted_data() {
        // Corrupted data with unterminated string literal
        let data = b"q 1 0 0 1 10 10 cm (corrupted Q";

        let content = content(data).unwrap();

        // Operations before the corruption returned without an error.
        // Trailing Q was silently dropped.
        assert_eq!(content.operations.len(), 2);
        assert_eq!(content.operations[0].operator, "q");
        assert_eq!(content.operations[1].operator, "cm");
    }

    #[test]
    fn content_strict_rejects_corrupted_data() {
        let data = b"q 1 0 0 1 10 10 cm (corrupted Q";
        assert!(content_strict(data).is_err());
    }

    fn on_big_stack(f: impl FnOnce() + Send + 'static) {
        std::thread::Builder::new()
            .stack_size(64 * 1024 * 1024)
            .spawn(f)
            .unwrap()
            .join()
            .unwrap();
    }

    #[test]
    fn deeply_nested_array_is_rejected() {
        on_big_stack(|| {
            let depth = 200_000;
            let mut input = Vec::with_capacity(depth * 2);
            input.extend(std::iter::repeat_n(b'[', depth));
            input.extend(std::iter::repeat_n(b']', depth));
            let result = _direct_object(crate::reader::MAX_NESTING_DEPTH)(input.as_slice());
            assert!(result.is_err());
        });
    }

    #[test]
    fn deeply_nested_dictionary_is_rejected() {
        on_big_stack(|| {
            let depth = 200_000;
            let mut input = Vec::with_capacity(depth * 6);
            for _ in 0..depth {
                input.extend_from_slice(b"<</K ");
            }
            for _ in 0..depth {
                input.extend_from_slice(b">>");
            }
            let result = _direct_object(crate::reader::MAX_NESTING_DEPTH)(input.as_slice());
            assert!(result.is_err());
        });
    }

    #[test]
    fn modestly_nested_array_still_parses() {
        let input = b"[[[[[1]]]]]";
        let obj = _direct_object(crate::reader::MAX_NESTING_DEPTH)(test_span(input));
        assert!(obj.is_ok());
    }

    #[test]
    fn content_treats_nul_and_form_feed_as_white_space() {
        // Some generators (e.g. Polaris Office) pad between operators with NUL bytes.
        let data = b"BT\n/F1 12 Tf\n[(a)] TJ\n\0\0\0\0ET\x0Cq\0Q";
        let content = content_strict(data).unwrap();
        let operators: Vec<_> = content.operations.iter().map(|op| op.operator.as_str()).collect();
        assert_eq!(operators, ["BT", "Tf", "TJ", "ET", "q", "Q"]);
    }

    #[test]
    fn content_parses_type3_glyph_operators() {
        // Type 3 glyph descriptions start with d0 or d1, whose names end in a digit.
        let data = b"750 0 d0\n0 0 m 10 10 l S\n500 0 0 -10 480 700 d1\n0 0 480 700 re f";
        let content = content_strict(data).unwrap();
        let operators: Vec<_> = content.operations.iter().map(|op| op.operator.as_str()).collect();
        assert_eq!(operators, ["d0", "m", "l", "S", "d1", "re", "f"]);
        assert_eq!(
            content.operations[0].operands,
            [Object::Integer(750), Object::Integer(0)]
        );
        assert_eq!(content.operations[4].operands.len(), 6);
    }

    #[test]
    fn content_reads_minus_sign_inside_real_as_one_operand() {
        let data = b"q\n0.99999994\n0.00-4142502\n0\n1.0000001\n106.729645\n232.27554\ncm\nQ";
        let content = content_strict(data).unwrap();
        let operators: Vec<_> = content.operations.iter().map(|op| op.operator.as_str()).collect();
        assert_eq!(operators, ["q", "cm", "Q"]);
        assert_eq!(content.operations[1].operands.len(), 6);
        assert_eq!(content.operations[1].operands[1], Object::Real(0.004142502));
    }

    #[test]
    fn space_skips_white_space_and_comments() {
        let rest = |input| space(test_span(input)).unwrap().0;
        assert_eq!(rest(b" \t\r\n\0\x0C/A"), b"/A");
        assert_eq!(rest(b"% one\n%two\r\n \0% three\r/A"), b"/A");
        assert_eq!(rest(b"/A"), b"/A");
        assert_eq!(rest(b""), b"");
        // A comment ends with an end-of-line marker, without which it is left unread.
        assert_eq!(rest(b" % unterminated"), b"% unterminated");
    }

    #[test]
    fn parse_name_ends_at_delimiters_and_invalid_escapes() {
        for (input, expected, rest) in [
            (&b"/Type/Page"[..], &b"Type"[..], &b"/Page"[..]),
            (b"/A B", b"A", b" B"),
            (b"/A(text)", b"A", b"(text)"),
            (b"/A#42C", b"ABC", b""),
            (b"/#41#4a#4A", b"AJJ", b""),
            (b"/a#20b#00", b"a b\0", b""),
            // A `#` without two hexadecimal digits ends the name.
            (b"/A#4", b"A", b"#4"),
            (b"/A#4G", b"A", b"#4G"),
            (b"/A#", b"A", b"#"),
            (b"/A# 1", b"A", b"# 1"),
            (b"/", b"", b""),
            (b"/\xe5\x90\x8d ", b"\xe5\x90\x8d", b" "),
        ] {
            assert_eq!(
                name(test_span(input)),
                Ok((rest, expected.to_vec())),
                "{:?}",
                String::from_utf8_lossy(input)
            );
        }
    }

    #[test]
    fn hexadecimal_string_accepts_only_hexadecimal_digits() {
        let hex = |input| match tstrip(hexadecimal_string(test_span(input))) {
            Some(Object::String(bytes, StringFormat::Hexadecimal)) => Some(bytes),
            _ => None,
        };
        assert_eq!(hex(b"<09aFAf>"), Some(b"\x09\xaf\xaf".to_vec()));
        assert_eq!(hex(b"<>"), Some(vec![]));
        for input in [&b"<4G>"[..], b"<+1>", b"<\xc3\xa9>", b"<41"] {
            assert_eq!(hex(input), None, "{:?}", String::from_utf8_lossy(input));
        }
    }

    #[test]
    fn parse_integers_and_references_at_their_limits() {
        for (input, expected) in [
            (&b"9223372036854775807"[..], Some(Object::Integer(i64::MAX))),
            (b"-9223372036854775808", Some(Object::Integer(i64::MIN))),
            (b"+0042", Some(Object::Integer(42))),
            (b"-0", Some(Object::Integer(0))),
            // An integer outside i64 is not an object.
            (b"9223372036854775808", None),
            (b"-9223372036854775809", None),
            (b"99999999999999999999999999", None),
            (b"4294967295 65535 R", Some(Object::Reference((u32::MAX, u16::MAX)))),
            (b"0000000000000000000000001 0R", Some(Object::Reference((1, 0)))),
            (b"1 %comment\n0 R", Some(Object::Reference((1, 0)))),
            // Beyond u32 or u16, the numbers of a reference are read as integers.
            (b"4294967296 0 R", Some(Object::Integer(4_294_967_296))),
            (b"1 65536 R", Some(Object::Integer(1))),
        ] {
            assert_eq!(
                direct_object(test_span(input)),
                expected,
                "{:?}",
                String::from_utf8_lossy(input)
            );
        }
    }

    #[test]
    fn parse_each_kind_of_direct_object() {
        for (input, expected) in [
            (&b"null"[..], Object::Null),
            (b"true", Object::Boolean(true)),
            (b"false", Object::Boolean(false)),
            (b"12 0 R", Object::Reference((12, 0))),
            (b"12 0", Object::Integer(12)),
            (b"1.5", Object::Real(1.5)),
            (b"-.5", Object::Real(-0.5)),
            (b"+7", Object::Integer(7)),
            (b"/N", Object::Name(b"N".to_vec())),
            (b"(s)", Object::string_literal("s")),
            (b"<73>", Object::String(b"s".to_vec(), StringFormat::Hexadecimal)),
            (b"<</N 1>>", Object::Dictionary(crate::dictionary! { "N" => 1 })),
            (
                b"[1 2.5 /N]",
                Object::Array(vec![1.into(), Object::Real(2.5), "N".into()]),
            ),
        ] {
            assert_eq!(
                direct_object(test_span(input)),
                Some(expected),
                "{:?}",
                String::from_utf8_lossy(input)
            );
        }
        for input in [&b""[..], b"nul", b"True", b"R", b")", b">>", b"]", b"{", b"-", b"."] {
            assert_eq!(
                direct_object(test_span(input)),
                None,
                "{:?}",
                String::from_utf8_lossy(input)
            );
        }
    }
}