provenant-cli 0.0.33

Rust-based ScanCode-compatible scanner for licenses, package metadata, SBOMs, and provenance data.
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
// SPDX-FileCopyrightText: Provenant contributors
// SPDX-License-Identifier: Apache-2.0

//! Candidate line selection and grouping for copyright detection.
//!
//! Identifies lines that may contain copyright statements and groups them
//! into contiguous blocks for further analysis. Lines are selected based on:
//! - Presence of copyright hint markers (©, "Copyright", etc.)
//! - Year patterns (1960–2099)
//! - HTTP URLs
//! - Debian-style markup (`<s>`, `</s>`)
//! - End-of-statement markers ("all rights reserved")
//!
//! Multi-line continuation logic handles cases where a copyright statement
//! spans several lines, using trailing markers (years, commas, "and", "by")
//! to determine continuation.

use std::sync::LazyLock;

use regex::Regex;

use super::hints;
use super::prepare::prepare_text_line;

/// Code-like patterns that indicate a line is minified JS/CSS, not copyright text.
static CODE_PATTERNS: LazyLock<Vec<&'static str>> = LazyLock::new(|| {
    vec![
        "function(",
        "this.",
        ".prototype",
        "===",
        "!==",
        "var ",
        "return ",
        "typeof ",
        "undefined",
        "null)",
        "null,",
        ".apply(",
        ".call(",
        "addEventListener",
        "removeEventListener",
        "createElement",
        "appendChild",
        "innerHTML",
        "className",
        "setAttribute",
        "getAttribute",
    ]
});

/// Lines longer than this without strong copyright indicators are skipped.
///
/// Real copyright notices are never this long on a single line. Lines exceeding
/// this are invariably minified JS/CSS or binary data where regex operations
/// become pathologically slow (e.g., 624KB minified JS → 20s+ in prepare_text_line).
/// The 2000-char threshold is conservative — the longest known legitimate
/// single-line copyright notice in the golden test suite is ~3200 chars, but
/// those always contain strong indicators ("opyr"/"auth") and pass through.
const MAX_LINE_LENGTH: usize = 2_000;

/// Minimum line length to trigger code-line heuristic.
const CODE_LINE_MIN_LENGTH: usize = 200;

/// Minimum line length to trigger encoded-data detection.
///
/// Short lines are unlikely to be encoded data, and checking them would add
/// overhead without benefit. Uuencode full lines are 61 chars; base64 lines
/// are typically 76 chars. We use 40 as a conservative lower bound.
const ENCODED_LINE_MIN_LENGTH: usize = 40;

/// Minimum ratio of encoded characters (uuencode range 32–96 or base64 charset)
/// for a line to be classified as encoded data. Uuencode data lines are 100%
/// in this range; we use 90% to allow minor variations.
const ENCODED_CHAR_RATIO: f64 = 0.90;

/// Check whether a long line contains copyright-relevant content.
///
/// Returns `true` if the line has strong copyright indicators anywhere in it.
/// Strong indicators are: "opyr"/"opyl"/"auth" (case-insensitive), or "(c)"
/// followed by a digit (distinguishes copyright `(c)2024` from code `(c){var`).
///
/// Uses byte-level search to avoid allocating a lowercase copy of potentially
/// huge (100KB+) lines.
fn has_copyright_indicators(line: &str) -> bool {
    let bytes = line.as_bytes();
    contains_ascii_ci(bytes, b"opyr")
        || contains_ascii_ci(bytes, b"opyl")
        || contains_ascii_ci(bytes, b"auth")
        || has_c_sign_before_year(bytes)
}

/// Case-insensitive ASCII substring search without allocation.
fn contains_ascii_ci(haystack: &[u8], needle: &[u8]) -> bool {
    if needle.len() > haystack.len() {
        return false;
    }
    haystack
        .windows(needle.len())
        .any(|window| window.eq_ignore_ascii_case(needle))
}

fn has_c_sign_before_year(bytes: &[u8]) -> bool {
    for (i, window) in bytes.windows(3).enumerate() {
        if window[0] == b'(' && (window[1] == b'c' || window[1] == b'C') && window[2] == b')' {
            let rest = &bytes[i + 3..];
            for &b in rest {
                if b == b' ' || b == b'\t' {
                    continue;
                }
                if b.is_ascii_digit() {
                    return true;
                }
                break;
            }
        }
    }
    false
}

/// Detect lines that are encoded binary data (uuencode, base64, hex dumps).
///
/// These lines trigger false positives from weak hint markers like `@` but
/// never contain real copyright text. Skipping them avoids expensive regex
/// processing on thousands of data lines (e.g., 5,143 lines in a uuencode
/// file that each contain `@`).
fn is_encoded_data_line(line: &str) -> bool {
    let len = line.len();
    if len < ENCODED_LINE_MIN_LENGTH {
        return false;
    }

    // Quick check: if the line contains strong copyright indicators, never skip.
    if has_copyright_indicators(line) {
        return false;
    }

    let bytes = line.as_bytes();

    // Uuencode data lines: first byte is a length character (space to `_`, i.e.
    // ASCII 32–95), followed by encoded characters in the same range. Full lines
    // start with `M` (45 bytes = char 77) and are exactly 61 chars.
    if is_uuencode_data_line(bytes) {
        return true;
    }

    // Base64 data lines: consist entirely of [A-Za-z0-9+/] with optional `=`
    // padding and no spaces (pure data, not prose).
    if is_base64_data_line(bytes) {
        return true;
    }

    false
}

/// Check if a line looks like a uuencode data line.
///
/// Uuencode format: each line starts with a length byte (ASCII 32 + number of
/// data bytes, so `M` = 77 = 32 + 45 for a full 45-byte line), followed by
/// encoded characters all in the printable range 32–96 (space through backtick).
/// Full data lines are exactly 61 characters.
///
/// To avoid false positives on comment decorators (e.g., `/*****/`), we require
/// at least 8 distinct byte values — real uuencode data has high character
/// diversity while decorators repeat 1-3 characters.
fn is_uuencode_data_line(bytes: &[u8]) -> bool {
    let first = bytes[0];
    if !(32..=95).contains(&first) {
        return false;
    }

    let uu_count = bytes.iter().filter(|&&b| (32..=96).contains(&b)).count();
    let ratio = uu_count as f64 / bytes.len() as f64;

    if ratio < ENCODED_CHAR_RATIO {
        return false;
    }

    // Reject lines with low character diversity (comment decorators like /****/).
    let mut seen = [false; 256];
    for &b in bytes {
        seen[b as usize] = true;
    }
    let distinct_count = seen.iter().filter(|&&s| s).count();
    if distinct_count < 8 {
        return false;
    }

    let space_count = bytes.iter().filter(|&&b| b == b' ').count();
    space_count <= 1
}

/// Check if a line looks like base64-encoded data.
///
/// Base64 uses only `[A-Za-z0-9+/=]` with no spaces. We require 100% base64
/// characters — URLs and file paths also lack spaces but contain `:`, `.`, `-`
/// which are not in the base64 alphabet, so a strict check avoids false positives.
fn is_base64_data_line(bytes: &[u8]) -> bool {
    if bytes.contains(&b' ') {
        return false;
    }

    bytes
        .iter()
        .all(|&b| b.is_ascii_alphanumeric() || b == b'+' || b == b'/' || b == b'=')
}

/// Check if a line looks like minified code where `(c)` is a false positive.
/// Returns true if the line should be skipped as a candidate.
fn is_code_line_with_false_c(line: &str) -> bool {
    if line.len() < CODE_LINE_MIN_LENGTH {
        return false;
    }

    let lower = line.to_lowercase();

    // Check if the ONLY copyright hint is `(c)` — if there's a real "copyright" word, keep it.
    if lower.contains("opyr") || lower.contains("opyl") || lower.contains("auth") {
        return false;
    }

    // Check for code-like patterns.
    let code_pattern_count = CODE_PATTERNS.iter().filter(|p| line.contains(**p)).count();

    code_pattern_count >= 2
}

fn is_swift_convention_c_signature_line(line: &str) -> bool {
    let trimmed = line.trim_start();
    trimmed.contains("@convention(c)")
        && !has_copyright_indicators(trimmed)
        && (trimmed.starts_with("let ")
            || trimmed.starts_with("var ")
            || trimmed.starts_with("typealias ")
            || trimmed.contains(" -> "))
}

/// Regex to remove all non-alphanumeric characters (for chars-only comparison).
static NON_CHARS_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[^a-zA-Z0-9]").unwrap());

/// Continuation suffixes: when the chars-only version of the previous line ends
/// with one of these, an empty/non-candidate line is treated as continuation
/// rather than a group break.
const CONTINUATION_SUFFIXES: &[&str] = &["copyright", "copyrights", "and", "by"];

/// Suffixes that indicate the end of a complete statement, causing an immediate
/// group yield.
const END_SUFFIXES: &[&str] = &["rightreserved", "rightsreserved"];

/// Remove all non-alphanumeric characters from a string.
fn chars_only(s: &str) -> String {
    NON_CHARS_RE.replace_all(&s.to_lowercase(), "").into_owned()
}

/// Check if a chars-only line marks the end of a copyright statement.
fn is_end_of_statement(chars: &str) -> bool {
    END_SUFFIXES.iter().any(|suffix| chars.ends_with(suffix))
}

pub(crate) fn versioned_banner_holder_from_prepared(prepared: &str) -> Option<String> {
    let trimmed = prepared.trim().trim_start_matches('!').trim_start();
    let c_idx = trimmed.find("(c)")?;
    let (head, tail_with_c) = trimmed.split_at(c_idx);
    let tail = tail_with_c["(c)".len()..].trim_start();

    let head_tokens: Vec<&str> = head.split_whitespace().collect();
    let has_version_token = head_tokens.iter().any(|token| {
        let token =
            token.trim_matches(|c: char| !c.is_ascii_alphanumeric() && c != '.' && c != '_');
        let stripped = token.strip_prefix('v').unwrap_or(token);
        !stripped.is_empty()
            && stripped.chars().next().is_some_and(|c| c.is_ascii_digit())
            && stripped.chars().any(|c| c == '.' || c == '-')
    });
    let has_project_token = head_tokens
        .iter()
        .any(|token| token.chars().any(|c| c.is_ascii_alphabetic()));
    if !(has_version_token && has_project_token) {
        return None;
    }

    let tail_lower = tail.to_ascii_lowercase();
    if !(tail_lower.contains(".org/license")
        || tail_lower.contains(".com/license")
        || tail_lower.contains(" licensed")
        || tail_lower.contains(" license")
        || tail_lower.contains(" released under"))
    {
        return None;
    }

    let tokens: Vec<&str> = tail.split_whitespace().collect();
    let license_token_idx = tokens.iter().enumerate().find_map(|(idx, token)| {
        let lower = token.to_ascii_lowercase();
        if lower.contains("/license") || lower.contains("/licenses") {
            return Some(idx);
        }
        if lower == "license" || lower == "licenses" || lower == "licensed" {
            return Some(idx);
        }
        if lower == "released"
            && tokens
                .get(idx + 1)
                .is_some_and(|next| next.eq_ignore_ascii_case("under"))
        {
            return Some(idx);
        }
        None
    })?;

    let holder_raw = tokens[..license_token_idx]
        .join(" ")
        .trim()
        .trim_matches(|c: char| c == '|' || c == ';' || c == ',' || c.is_whitespace())
        .to_string();
    if holder_raw.split_whitespace().count() < 2 {
        return None;
    }
    Some(holder_raw)
}

pub(crate) fn is_raw_versioned_project_banner_line(line: &str) -> bool {
    line.trim_start().starts_with("/*!")
        && versioned_banner_holder_from_prepared(&prepare_text_line(line)).is_some()
}

/// Check if a chars-only previous line ends with a continuation marker
/// (copyright, copyrights, and, by, comma) or a trailing year.
fn ends_with_continuation(chars: &str) -> bool {
    if chars.is_empty() {
        return false;
    }
    // Check trailing comma in original (pre-chars-only) text would have been
    // stripped, but we check the original chars_only which only has alnum.
    // The Python code checks previous_chars.endswith(('copyright', 'copyrights', 'and', 'by', ',')).
    // Since chars_only strips commas, we only check the alpha suffixes here.
    // Trailing year check is separate.
    CONTINUATION_SUFFIXES
        .iter()
        .any(|suffix| chars.ends_with(suffix))
        || hints::has_trailing_year(chars)
}

fn is_tabular_noise_line(line: &str) -> bool {
    if !line.contains('\t') {
        return false;
    }
    if !line.contains("--") {
        return false;
    }
    let bytes = line.as_bytes();
    for i in 1..bytes.len().saturating_sub(1) {
        if bytes[i] == b'/' && bytes[i - 1].is_ascii_digit() && bytes[i + 1].is_ascii_digit() {
            return true;
        }
    }
    false
}

fn is_noncopyright_at_directive_line(prepared: &str) -> bool {
    let trimmed = prepared.trim_start();
    let mut chars = trimmed.chars();
    let starts_with_lowercase_directive =
        matches!(chars.next(), Some('@')) && chars.next().is_some_and(|c| c.is_ascii_lowercase());

    starts_with_lowercase_directive
        && !hints::has_year(trimmed)
        && !trimmed.contains("://")
        && !has_copyright_indicators(trimmed)
}

/// A numbered line: (1-based line number, prepared text).
pub type NumberedLine = (usize, String);

/// Collect groups of candidate lines from numbered input lines.
///
/// Each group is a `Vec<NumberedLine>` representing a contiguous block of lines
/// that may contain copyright/author information. The caller then processes each
/// group through the lexer/parser pipeline.
///
/// This mirrors the Python `collect_candidate_lines()` function.
pub fn collect_candidate_lines<T>(
    numbered_lines: impl IntoIterator<Item = (usize, T)>,
) -> Vec<Vec<NumberedLine>>
where
    T: AsRef<str>,
{
    let mut groups: Vec<Vec<NumberedLine>> = Vec::new();
    let mut candidates: Vec<NumberedLine> = Vec::new();

    // `in_copyright` is a countdown: when > 0, we're inside a copyright block
    // and will include non-candidate continuation lines. Starts at 2 when a
    // candidate is found, decrements for each non-candidate continuation line.
    let mut in_copyright: u32 = 0;
    let mut previous_chars: Option<String> = None;
    let mut debian_like: bool = false;
    let mut debian_header_only_copyright_next_copy_needs_prefix: bool = false;

    let mut prev_prepared_is_copy_start_with_year = false;

    for (ln, line) in numbered_lines {
        let line = line.as_ref();
        let lower_trim = line.trim_start();
        let lower_trim = lower_trim
            .trim_start_matches(['*', '/', '#', ';', '!'])
            .trim_start()
            .to_ascii_lowercase();

        let is_author_header_line = (lower_trim == "authors"
            || lower_trim.starts_with("authors:")
            || lower_trim.starts_with("author(s):")
            || lower_trim.starts_with("author:")
            || lower_trim.starts_with("author(s)"))
            && !hints::has_year(line)
            && !lower_trim.contains("copyright")
            && !lower_trim.contains("(c)")
            && !lower_trim.contains("copr");

        if is_author_header_line {
            if !candidates.is_empty() {
                groups.push(std::mem::take(&mut candidates));
            }
            in_copyright = 0;
            previous_chars = None;
            prev_prepared_is_copy_start_with_year = false;
            continue;
        }

        if lower_trim.starts_with("format-specification:") || lower_trim.starts_with("files:") {
            debian_like = true;
        }

        if lower_trim.starts_with("files:") || lower_trim.starts_with("license:") {
            debian_header_only_copyright_next_copy_needs_prefix = false;
        }

        // Skip long lines without copyright indicators (minified JS, binary data).
        if line.len() > MAX_LINE_LENGTH && !has_copyright_indicators(line) {
            if in_copyright > 0 {
                in_copyright -= 1;
                if in_copyright == 0 && !candidates.is_empty() {
                    groups.push(std::mem::take(&mut candidates));
                    previous_chars = None;
                }
            }
            continue;
        }

        // Skip encoded data lines (uuencode, base64) that trigger false
        // positives from weak hint markers like `@`.
        if is_encoded_data_line(line) {
            if in_copyright > 0 {
                in_copyright -= 1;
                if in_copyright == 0 && !candidates.is_empty() {
                    groups.push(std::mem::take(&mut candidates));
                    previous_chars = None;
                }
            }
            continue;
        }

        let co = chars_only(line);

        if is_end_of_statement(&co) {
            let prepared = prepare_text_line(line);

            let prepared_is_copy_start_with_year = is_copy_marker_start(prepared.trim_start());

            if prepared_is_copy_start_with_year && prev_prepared_is_copy_start_with_year {
                let prev_is_year_only = candidates
                    .last()
                    .is_some_and(|(_, prev)| is_year_only_copy_marker_line(prev.as_str()));
                let current_is_year_only = is_year_only_copy_marker_line(prepared.as_str());

                if !prev_is_year_only && !current_is_year_only && !candidates.is_empty() {
                    groups.push(std::mem::take(&mut candidates));
                }
            }

            if debian_like
                && is_copy_marker_start(prepared.trim_start())
                && candidates
                    .last()
                    .is_some_and(|(_, prev)| is_copy_marker_start(prev.trim_start()))
            {
                groups.push(std::mem::take(&mut candidates));
            }

            candidates.push((ln, prepared));
            groups.push(std::mem::take(&mut candidates));
            in_copyright = 0;
            previous_chars = None;
            prev_prepared_is_copy_start_with_year = prepared_is_copy_start_with_year;
        } else if hints::is_candidate(line)
            || co.contains("http")
            || is_raw_versioned_project_banner_line(line)
        {
            if is_swift_convention_c_signature_line(line) {
                continue;
            }

            if is_code_line_with_false_c(line) {
                continue;
            }

            let prepared = prepare_text_line(line);
            if is_noncopyright_at_directive_line(&prepared) {
                if !candidates.is_empty() {
                    groups.push(std::mem::take(&mut candidates));
                }
                in_copyright = 0;
                previous_chars = None;
                prev_prepared_is_copy_start_with_year = false;
                continue;
            }

            let prepared_chars = chars_only(&prepared);

            let prepared_is_copy_start_with_year = is_copy_marker_start(prepared.trim_start());
            if prepared_is_copy_start_with_year && prev_prepared_is_copy_start_with_year {
                let prev_is_year_only = candidates
                    .last()
                    .is_some_and(|(_, prev)| is_year_only_copy_marker_line(prev.as_str()));
                let current_is_year_only = is_year_only_copy_marker_line(prepared.as_str());

                if !prev_is_year_only && !current_is_year_only && !candidates.is_empty() {
                    groups.push(std::mem::take(&mut candidates));
                }
            }

            if debian_like
                && (prepared_chars == "copyright" || prepared_chars == "copyrights")
                && prepared
                    .split_once(':')
                    .map(|(_, tail)| tail.trim())
                    .is_some_and(|tail| tail.is_empty())
            {
                debian_header_only_copyright_next_copy_needs_prefix = true;
            }

            if debian_like
                && is_copy_marker_start(prepared.trim_start())
                && candidates
                    .last()
                    .is_some_and(|(_, prev)| is_copy_marker_start(prev.trim_start()))
            {
                groups.push(std::mem::take(&mut candidates));
            }

            if is_standalone_comment_line(line)
                && versioned_banner_holder_from_prepared(&prepared).is_some()
            {
                candidates.push((ln, prepared));
                groups.push(std::mem::take(&mut candidates));
                in_copyright = 0;
                previous_chars = None;
                prev_prepared_is_copy_start_with_year = prepared_is_copy_start_with_year;
                continue;
            }

            if (prepared_chars == "copyright" || prepared_chars == "copyrights")
                && prepared
                    .split_once(':')
                    .map(|(_, tail)| tail.trim())
                    .is_some_and(|tail| {
                        !tail.is_empty() && tail.chars().all(|c| !c.is_ascii_alphanumeric())
                    })
            {
                in_copyright = 2;
                previous_chars = Some(prepared_chars);
                continue;
            }

            in_copyright = 2;

            if debian_header_only_copyright_next_copy_needs_prefix {
                let trimmed = prepared.trim_start();
                let lower = trimmed.to_ascii_lowercase();
                let is_copy_start = starts_with_c_sign(trimmed) || lower.starts_with("copyright");
                if is_copy_start && hints::has_year(trimmed) {
                    debian_header_only_copyright_next_copy_needs_prefix = false;
                    if starts_with_c_sign(trimmed) {
                        let prefixed = format!("Copyright {trimmed}");
                        let prefixed_chars = chars_only(&prefixed);
                        candidates.push((ln, prefixed));
                        previous_chars = Some(prefixed_chars);
                        continue;
                    }
                }
            }

            candidates.push((ln, prepared));
            previous_chars = Some(prepared_chars);
            prev_prepared_is_copy_start_with_year = prepared_is_copy_start_with_year;
        } else if in_copyright > 0 {
            // Inside a copyright block — check if we should continue or break.
            let prepared = prepare_text_line(line);
            let trimmed = prepared.trim_start();
            let lower = trimmed.to_ascii_lowercase();

            if is_noncopyright_at_directive_line(trimmed) {
                if !candidates.is_empty() {
                    groups.push(std::mem::take(&mut candidates));
                }
                in_copyright = 0;
                previous_chars = None;
                prev_prepared_is_copy_start_with_year = false;
                continue;
            }

            let is_author_header = (lower == "authors"
                || lower.starts_with("authors:")
                || lower.starts_with("author(s):")
                || lower.starts_with("author:")
                || lower.starts_with("author(s)"))
                && !hints::has_year(trimmed)
                && !lower.contains("copyright")
                && !lower.contains("(c)")
                && !lower.contains("copr");

            if is_author_header {
                if !candidates.is_empty() {
                    groups.push(std::mem::take(&mut candidates));
                }
                in_copyright = 0;
                previous_chars = None;
                prev_prepared_is_copy_start_with_year = false;
                continue;
            }
            if is_obvious_code_line(line) && !has_copyright_indicators(line) {
                if !candidates.is_empty() {
                    groups.push(std::mem::take(&mut candidates));
                }
                in_copyright = 0;
                previous_chars = None;
                prev_prepared_is_copy_start_with_year = false;
                continue;
            }

            let is_standalone_comment_without_indicators =
                is_standalone_comment_line(line) && !has_copyright_indicators(&prepared);
            let is_indented_standalone_comment = line.trim_start().starts_with("/*")
                && line.trim_end().ends_with("*/")
                && !line.starts_with("/*");

            if is_standalone_comment_without_indicators && is_indented_standalone_comment {
                if !candidates.is_empty() {
                    groups.push(std::mem::take(&mut candidates));
                }
                in_copyright = 0;
                previous_chars = None;
                prev_prepared_is_copy_start_with_year = false;
                continue;
            }
            if co.is_empty() {
                // Empty line: continue only if previous line ends with a
                // continuation marker or a trailing year.
                if let Some(ref prev) = previous_chars {
                    if !ends_with_continuation(prev) {
                        // Break the group.
                        if !candidates.is_empty() {
                            groups.push(std::mem::take(&mut candidates));
                        }
                        in_copyright = 0;
                        previous_chars = None;
                        prev_prepared_is_copy_start_with_year = false;
                    } else {
                        candidates.push((ln, prepare_text_line(line)));
                        in_copyright -= 1;
                    }
                } else {
                    // No previous chars recorded — break.
                    if !candidates.is_empty() {
                        groups.push(std::mem::take(&mut candidates));
                    }
                    in_copyright = 0;
                    previous_chars = None;
                    prev_prepared_is_copy_start_with_year = false;
                }
            } else if is_tabular_noise_line(line) {
                if !candidates.is_empty() {
                    groups.push(std::mem::take(&mut candidates));
                }
                in_copyright = 0;
                previous_chars = None;
                prev_prepared_is_copy_start_with_year = false;
            } else {
                candidates.push((ln, prepare_text_line(line)));
                in_copyright -= 1;
            }
        } else if !candidates.is_empty() {
            // Not in copyright and line is not a candidate — yield what we have.
            groups.push(std::mem::take(&mut candidates));
            in_copyright = 0;
            previous_chars = None;
            prev_prepared_is_copy_start_with_year = false;
        }
    }

    // Yield any remaining candidates.
    if !candidates.is_empty() {
        groups.push(candidates);
    }

    groups
}

fn is_copy_marker_start(s: &str) -> bool {
    let mut t = s.trim_start();

    if let Some(prefix) = t.get(.."portions".len())
        && prefix.eq_ignore_ascii_case("portions")
        && t.as_bytes()
            .get("portions".len())
            .is_none_or(|b| b.is_ascii_whitespace() || b.is_ascii_punctuation())
    {
        t = t
            .get("portions".len()..)
            .unwrap_or("")
            .trim_start_matches(|c: char| c.is_ascii_punctuation() || c.is_whitespace());
    }

    (starts_with_c_sign(t)
        || t.get(.."copyright".len())
            .is_some_and(|p| p.eq_ignore_ascii_case("copyright")))
        && hints::has_year(t)
}

fn starts_with_c_sign(s: &str) -> bool {
    let s = s.trim_start();
    let b = s.as_bytes();
    b.len() >= 3 && b[0] == b'(' && (b[1] == b'c' || b[1] == b'C') && b[2] == b')'
}

fn is_obvious_code_line(line: &str) -> bool {
    let t = line.trim_start();
    if t.is_empty() {
        return false;
    }

    let lower = t.to_ascii_lowercase();
    if lower == "public" || lower == "private" || lower == "protected" {
        return true;
    }
    if lower.starts_with("public ")
        || lower.starts_with("private ")
        || lower.starts_with("protected ")
        || lower.starts_with("static ")
        || lower.starts_with("const ")
        || lower.starts_with("class ")
        || lower.starts_with("struct ")
        || lower.starts_with("enum ")
        || lower.starts_with("interface ")
        || lower.starts_with("impl ")
        || lower.starts_with("fn ")
        || lower.starts_with("let ")
        || lower.starts_with("var ")
        || lower.starts_with("function ")
        || lower.starts_with("#define")
        || lower.starts_with("#include")
    {
        return true;
    }

    let stripped = t.trim_matches(|c: char| c.is_ascii_punctuation() || c.is_whitespace());
    matches!(stripped, "{" | "}" | ";")
}

fn is_standalone_comment_line(line: &str) -> bool {
    let t = line.trim();
    if t.is_empty() {
        return false;
    }
    let lower = t.to_ascii_lowercase();
    if lower.starts_with("/*") && lower.ends_with("*/") {
        return true;
    }
    false
}

fn is_year_only_copy_marker_line(prepared: &str) -> bool {
    let mut s = prepared.trim_start();

    if s.get(.."copyright".len())
        .is_some_and(|prefix| prefix.eq_ignore_ascii_case("copyright"))
    {
        s = s.get("copyright".len()..).unwrap_or("");
    }

    s = s.trim_start();

    if starts_with_c_sign(s) {
        s = s.get(3..).unwrap_or("");
    } else {
        s = s.trim_start_matches(|c: char| c.is_ascii_punctuation() || c.is_whitespace());
        if starts_with_c_sign(s) {
            s = s.get(3..).unwrap_or("");
        }
    }

    s = s.trim_start_matches(|c: char| c.is_ascii_punctuation() || c.is_whitespace());

    let s = s.trim();
    if s.is_empty() {
        return true;
    }

    s.chars().all(|c| {
        c.is_ascii_digit()
            || c.is_whitespace()
            || matches!(c, ',' | '-' | '+' | '(' | ')' | '.' | ';' | ':')
    })
}

/// Strip balanced leading and trailing parentheses from a string.
///
/// Only strips if the parentheses are truly wrapping (no inner parens).
///
/// For example, `(Hello World)` unwraps to `Hello World`, while unbalanced
/// input such as `(Hello (World)` is left unchanged.
pub fn strip_balanced_edge_parens(s: &str) -> &str {
    if s.starts_with('(') && s.ends_with(')') {
        let inner = &s[1..s.len() - 1];
        if !inner.contains('(')
            && !inner.contains(')')
            && !inner.to_ascii_lowercase().contains("http://")
            && !inner.to_ascii_lowercase().contains("https://")
            && !inner.to_ascii_lowercase().contains("ftp://")
            && !inner.to_ascii_lowercase().contains("www.")
            && !inner.contains('@')
        {
            return inner;
        }
    }
    s
}

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

    // ── chars_only ──────────────────────────────────────────────────

    #[test]
    fn test_chars_only_basic() {
        assert_eq!(chars_only("Hello, World! 123"), "helloworld123");
    }

    #[test]
    fn test_chars_only_empty() {
        assert_eq!(chars_only(""), "");
    }

    #[test]
    fn test_chars_only_only_punct() {
        assert_eq!(chars_only("---...!!!"), "");
    }

    // ── is_end_of_statement ─────────────────────────────────────────

    #[test]
    fn test_eos_rights_reserved() {
        assert!(is_end_of_statement("allrightsreserved"));
    }

    #[test]
    fn test_eos_right_reserved() {
        assert!(is_end_of_statement("allrightreserved"));
    }

    #[test]
    fn test_eos_negative() {
        assert!(!is_end_of_statement("copyright2024"));
    }

    // ── ends_with_continuation ──────────────────────────────────────

    #[test]
    fn test_continuation_copyright() {
        assert!(ends_with_continuation("somecopyright"));
    }

    #[test]
    fn test_continuation_and() {
        assert!(ends_with_continuation("fooand"));
    }

    #[test]
    fn test_continuation_by() {
        assert!(ends_with_continuation("writtenby"));
    }

    #[test]
    fn test_continuation_year() {
        assert!(ends_with_continuation("text2024"));
    }

    #[test]
    fn test_continuation_negative() {
        assert!(!ends_with_continuation("justtext"));
    }

    #[test]
    fn test_continuation_empty() {
        assert!(!ends_with_continuation(""));
    }

    // ── strip_balanced_edge_parens ──────────────────────────────────

    #[test]
    fn test_strip_balanced_simple() {
        assert_eq!(strip_balanced_edge_parens("(Hello World)"), "Hello World");
    }

    #[test]
    fn test_strip_balanced_unmatched_start() {
        assert_eq!(strip_balanced_edge_parens("(Hello World"), "(Hello World");
    }

    #[test]
    fn test_strip_balanced_unmatched_end() {
        assert_eq!(strip_balanced_edge_parens("Hello World)"), "Hello World)");
    }

    #[test]
    fn test_strip_balanced_inner_parens() {
        assert_eq!(
            strip_balanced_edge_parens("(Hello (World)"),
            "(Hello (World)"
        );
    }

    #[test]
    fn test_strip_balanced_nested_parens() {
        // Inner contains both ( and ), so don't strip.
        assert_eq!(
            strip_balanced_edge_parens("(Hello (World))"),
            "(Hello (World))"
        );
    }

    #[test]
    fn test_strip_balanced_no_parens() {
        assert_eq!(strip_balanced_edge_parens("Hello World"), "Hello World");
    }

    // ── collect_candidate_lines ─────────────────────────────────────

    #[test]
    fn test_collect_empty_input() {
        let lines: Vec<(usize, String)> = vec![];
        let groups = collect_candidate_lines(lines);
        assert!(groups.is_empty());
    }

    #[test]
    fn test_collect_single_copyright_line() {
        let lines = vec![(1, "Copyright 2024 Acme Inc.".to_string())];
        let groups = collect_candidate_lines(lines);
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].len(), 1);
        assert_eq!(groups[0][0].0, 1);
    }

    #[test]
    fn test_collect_non_candidate_lines() {
        let lines = vec![
            (1, "This is just code".to_string()),
            (2, "More code here".to_string()),
        ];
        let groups = collect_candidate_lines(lines);
        assert!(groups.is_empty());
    }

    #[test]
    fn test_collect_two_separate_copyrights() {
        // With in_copyright countdown of 2, lines 2 and 3 are continuation
        // lines (decrementing 2→1→0), so we need 3+ gap lines to split.
        let lines = vec![
            (1, "Copyright 2020 Foo".to_string()),
            (2, "some random code".to_string()),
            (3, "not related".to_string()),
            (4, "also not related".to_string()),
            (5, "Copyright 2024 Bar".to_string()),
        ];
        let groups = collect_candidate_lines(lines);
        assert_eq!(groups.len(), 2, "groups: {groups:?}");
    }

    #[test]
    fn test_collect_end_of_statement_yields_immediately() {
        let lines = vec![
            (1, "Copyright 2024 Acme Inc.".to_string()),
            (2, "All rights reserved.".to_string()),
            (3, "Some other text".to_string()),
        ];
        let groups = collect_candidate_lines(lines);
        assert_eq!(groups.len(), 1, "groups: {groups:?}");
        // Both copyright line and "all rights reserved" should be in same group.
        assert_eq!(groups[0].len(), 2);
    }

    #[test]
    fn test_collect_continuation_with_trailing_year() {
        // A copyright line followed by an empty line should continue if the
        // previous line ends with a year.
        let lines = vec![
            (1, "Copyright 2024".to_string()),
            (2, "".to_string()),
            (3, "Acme Inc.".to_string()),
        ];
        let groups = collect_candidate_lines(lines);
        // Line 1 ends with year → empty line 2 continues → line 3 is included.
        assert_eq!(groups.len(), 1, "groups: {groups:?}");
    }

    #[test]
    fn test_collect_break_on_empty_without_continuation() {
        // A non-continuation line followed by empty → group break.
        let lines = vec![
            (1, "Copyright 2024 Acme Inc.".to_string()),
            (2, "Some additional text".to_string()),
            (3, "".to_string()),
            (4, "Copyright 2025 Bar".to_string()),
        ];
        let groups = collect_candidate_lines(lines);
        // Line 2 is continuation (in_copyright=2), line 3 is empty but
        // previous doesn't end with continuation marker → break.
        assert_eq!(groups.len(), 2, "groups: {groups:?}");
    }

    #[test]
    fn test_collect_http_as_candidate() {
        let lines = vec![(1, "http://example.com/copyright".to_string())];
        let groups = collect_candidate_lines(lines);
        assert_eq!(groups.len(), 1, "groups: {groups:?}");
    }

    #[test]
    fn test_collect_debian_markup() {
        let lines = vec![(1, "<s>John Doe</s>".to_string())];
        let groups = collect_candidate_lines(lines);
        assert_eq!(groups.len(), 1, "groups: {groups:?}");
    }

    #[test]
    fn test_collect_multiline_copyright() {
        let lines = vec![
            (1, "Copyright (C) 2020-2024".to_string()),
            (2, "  Acme Corporation".to_string()),
            (3, "  All rights reserved.".to_string()),
        ];
        let groups = collect_candidate_lines(lines);
        // Line 1 is candidate, line 2 continuation (in_copyright=2→1),
        // line 3 is end-of-statement → yields group.
        assert_eq!(groups.len(), 1, "groups: {groups:?}");
        assert_eq!(groups[0].len(), 3);
    }

    #[test]
    fn test_noncopyright_at_directive_line_detection() {
        assert!(is_noncopyright_at_directive_line(
            "@lint-ignore-every FBOBJCIMPORTORDER1 METHOD_BRACKETSMETHOD_BRACKETS"
        ));
        assert!(!is_noncopyright_at_directive_line("@author Jane Doe"));
        assert!(!is_noncopyright_at_directive_line(
            "@copyright 2024 Example Corp."
        ));
        assert!(!is_noncopyright_at_directive_line("@History:"));
    }

    #[test]
    fn test_collect_breaks_before_noncopyright_at_directive_line() {
        let lines = vec![
            (
                1,
                "// (c) Example Corp. and affiliates. Confidential and proprietary.".to_string(),
            ),
            (
                2,
                "// @lint-ignore-every FBOBJCIMPORTORDER1 METHOD_BRACKETSMETHOD_BRACKETS"
                    .to_string(),
            ),
        ];

        let groups = collect_candidate_lines(lines);
        assert_eq!(groups.len(), 1, "groups: {groups:?}");
        assert_eq!(groups[0].len(), 1, "groups: {groups:?}");
        assert_eq!(
            groups[0][0].1,
            "(c) Example Corp. and affiliates. Confidential and proprietary."
        );
    }

    #[test]
    fn test_collect_remaining_candidates_at_end() {
        let lines = vec![
            (1, "Some preamble".to_string()),
            (2, "Copyright 2024 Acme".to_string()),
        ];
        let groups = collect_candidate_lines(lines);
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0][0].0, 2);
    }

    // ── has_copyright_indicators ────────────────────────────────────

    #[test]
    fn test_indicators_copyright_word() {
        assert!(has_copyright_indicators("blah Copyright 2024 blah"));
    }

    #[test]
    fn test_indicators_copyleft() {
        assert!(has_copyright_indicators("Copyleft notice here"));
    }

    #[test]
    fn test_indicators_author() {
        assert!(has_copyright_indicators("@author John Doe"));
    }

    #[test]
    fn test_indicators_c_sign_with_year() {
        assert!(has_copyright_indicators("(c)2024 Acme Inc."));
        assert!(has_copyright_indicators("(C) 1996 Id Software"));
        assert!(has_copyright_indicators("(c) 2020 Foo"));
    }

    #[test]
    fn test_indicators_c_sign_code_pattern() {
        assert!(!has_copyright_indicators("if(c){var r=[]}"));
        assert!(!has_copyright_indicators("function(c){return c.length}"));
    }

    #[test]
    fn test_indicators_no_match() {
        assert!(!has_copyright_indicators("var x = 42; function foo() {}"));
        assert!(!has_copyright_indicators(
            "just some random @ text with right margin"
        ));
    }

    #[test]
    fn test_collect_skips_swift_convention_c_signature_lines() {
        let lines = vec![
            (
                1,
                "let invokeSuperSetter: @convention(c) (NSObject, AnyClass, Selector, AnyObject?) -> Void = { object, superclass, selector, delegate in".to_string(),
            ),
            (
                2,
                "typealias Setter = @convention(c) (NSObject, Selector, AnyObject?) -> Void"
                    .to_string(),
            ),
        ];

        assert!(collect_candidate_lines(lines).is_empty());
    }

    // ── has_c_sign_before_year ──────────────────────────────────────

    #[test]
    fn test_c_sign_year_adjacent() {
        assert!(has_c_sign_before_year(b"(c)2024"));
    }

    #[test]
    fn test_c_sign_year_with_space() {
        assert!(has_c_sign_before_year(b"(c) 1996"));
    }

    #[test]
    fn test_c_sign_year_uppercase() {
        assert!(has_c_sign_before_year(b"(C)2024"));
    }

    #[test]
    fn test_c_sign_code_brace() {
        assert!(!has_c_sign_before_year(b"(c){var}"));
    }

    #[test]
    fn test_c_sign_code_dot() {
        assert!(!has_c_sign_before_year(b"(c).length"));
    }

    #[test]
    fn test_c_sign_empty_after() {
        assert!(!has_c_sign_before_year(b"(c)"));
    }

    // ── collect_candidate_lines with long lines ─────────────────────

    #[test]
    fn test_collect_skips_long_line_without_indicators() {
        let long_line = "x".repeat(3000);
        let lines = vec![
            (1, "Copyright 2024 Acme".to_string()),
            (2, long_line),
            (3, "Copyright 2025 Bar".to_string()),
        ];
        let groups = collect_candidate_lines(lines);
        assert!(
            !groups.is_empty(),
            "Should still detect copyrights: {groups:?}"
        );
    }

    #[test]
    fn test_collect_keeps_long_line_with_copyright() {
        let mut long_line = "x".repeat(2500);
        long_line.push_str(" Copyright 2024 Acme Inc. ");
        long_line.push_str(&"y".repeat(500));
        let lines = vec![(1, long_line)];
        let groups = collect_candidate_lines(lines);
        assert_eq!(
            groups.len(),
            1,
            "Should detect copyright in long line: {groups:?}"
        );
    }

    #[test]
    fn test_collect_keeps_long_line_with_c_sign_year() {
        let mut long_line = "x".repeat(2500);
        long_line.push_str(" (c)1996 Id Software ");
        long_line.push_str(&"y".repeat(500));
        let lines = vec![(1, long_line)];
        let groups = collect_candidate_lines(lines);
        assert_eq!(
            groups.len(),
            1,
            "Should detect (c)year in long line: {groups:?}"
        );
    }

    // ── contains_ascii_ci ───────────────────────────────────────────

    #[test]
    fn test_contains_ascii_ci_found() {
        assert!(contains_ascii_ci(b"Hello World", b"world"));
        assert!(contains_ascii_ci(b"CoPyRiGhT", b"opyr"));
    }

    #[test]
    fn test_contains_ascii_ci_not_found() {
        assert!(!contains_ascii_ci(b"Hello World", b"xyz"));
    }

    #[test]
    fn test_contains_ascii_ci_needle_longer() {
        assert!(!contains_ascii_ci(b"Hi", b"Hello"));
    }

    // ── is_uuencode_data_line ───────────────────────────────────────

    #[test]
    fn test_uuencode_full_data_line() {
        // Real uuencode data line from golden test (starts with M = 45 bytes)
        let line = b"M?T5,1@$\"`0`````````````!``@````!`````````````]'0```0`0`T````";
        assert!(is_uuencode_data_line(line));
    }

    #[test]
    fn test_uuencode_short_data_line() {
        // Shorter uuencode line (last line of a block) — still matches the
        // uuencode character pattern, but is_encoded_data_line() rejects it
        // due to the minimum length check.
        let line = b"1`@``*%P```(\"```H8````@(`";
        assert!(is_uuencode_data_line(line));
        assert!(!is_encoded_data_line(std::str::from_utf8(line).unwrap()));
    }

    #[test]
    fn test_uuencode_not_natural_text() {
        let line = b"This is a normal English sentence with spaces between words here";
        assert!(!is_uuencode_data_line(line));
    }

    #[test]
    fn test_uuencode_not_copyright_line() {
        let line = b" * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting, Atheros";
        assert!(!is_uuencode_data_line(line));
    }

    #[test]
    fn test_uuencode_not_comment_decorator() {
        let line = b"/************************************************************************/";
        assert!(!is_uuencode_data_line(line));
    }

    #[test]
    fn test_uuencode_not_star_line() {
        let line = b"************************************************************";
        assert!(!is_uuencode_data_line(line));
    }

    #[test]
    fn test_uuencode_not_dash_line() {
        let line = b"------------------------------------------------------------";
        assert!(!is_uuencode_data_line(line));
    }

    // ── is_base64_data_line ─────────────────────────────────────────

    #[test]
    fn test_base64_typical_line() {
        let line = b"SGVsbG8gV29ybGQhIFRoaXMgaXMgYSBiYXNlNjQgZW5jb2RlZCBzdHJpbmc=";
        assert!(is_base64_data_line(line));
    }

    #[test]
    fn test_base64_with_plus_slash() {
        let line = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        assert!(is_base64_data_line(line));
    }

    #[test]
    fn test_base64_not_text_with_spaces() {
        let line = b"This is not base64 because it has spaces in it right here";
        assert!(!is_base64_data_line(line));
    }

    #[test]
    fn test_base64_not_url() {
        let line = b"http://code.google.com/apis/protocolbuffers/";
        assert!(!is_base64_data_line(line));
    }

    #[test]
    fn test_base64_not_file_path() {
        let line = b"/usr/local/lib/python3.11/site-packages/some_package/module.py";
        assert!(!is_base64_data_line(line));
    }

    // ── is_encoded_data_line (integration) ──────────────────────────

    #[test]
    fn test_encoded_skips_short_lines() {
        assert!(!is_encoded_data_line("short"));
        assert!(!is_encoded_data_line("M`@``")); // uuencode-like but too short
    }

    #[test]
    fn test_encoded_preserves_copyright_indicators() {
        let line = "M".to_string() + &"`".repeat(20) + "Copyright" + &"`".repeat(30);
        assert!(!is_encoded_data_line(&line));
    }

    #[test]
    fn test_encoded_detects_uuencode() {
        let line = "M?T5,1@$\"`0`````````````!``@````!`````````````]'0```0`0`T````";
        assert!(is_encoded_data_line(line));
    }

    #[test]
    fn test_encoded_detects_base64() {
        let line = "SGVsbG8gV29ybGQhIFRoaXMgaXMgYSBiYXNlNjQgZW5jb2RlZCBzdHJpbmc=";
        assert!(is_encoded_data_line(line));
    }

    #[test]
    fn test_encoded_preserves_normal_text() {
        assert!(!is_encoded_data_line(
            "This is a normal line of source code with various characters"
        ));
    }

    #[test]
    fn test_encoded_preserves_email_line() {
        assert!(!is_encoded_data_line(
            "Contact us at support@example.com for more information about this"
        ));
    }

    // ── collect_candidate_lines with encoded data ───────────────────

    #[test]
    fn test_collect_skips_uuencode_data_lines() {
        let uu_line = "M?T5,1@$\"`0`````````````!``@````!`````````````]'0```0`0`T````";
        let lines = vec![
            (1, "Copyright 2024 Acme".to_string()),
            (2, uu_line.to_string()),
            (3, uu_line.to_string()),
            (4, uu_line.to_string()),
            (5, "Copyright 2025 Bar".to_string()),
        ];
        let groups = collect_candidate_lines(lines);
        assert_eq!(groups.len(), 2, "Should detect both copyrights: {groups:?}");
    }

    #[test]
    fn test_collect_preserves_copyright_in_uuencode_file() {
        // Simulates the real uuencode golden test: copyright header followed by data
        let uu_line = "M?T5,1@$\"`0`````````````!``@````!`````````````]'0```0`0`T````";
        let mut lines: Vec<(usize, String)> = vec![
            (
                1,
                " * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting, Atheros".to_string(),
            ),
            (
                2,
                " * Communications, Inc.  All rights reserved.".to_string(),
            ),
        ];
        // Add 100 uuencode data lines
        for i in 3..103 {
            lines.push((i, uu_line.to_string()));
        }
        let groups = collect_candidate_lines(lines);
        assert!(!groups.is_empty(), "Should detect copyright header");
        assert_eq!(groups[0].len(), 2, "Copyright group should have 2 lines");
    }

    #[test]
    fn test_collect_keeps_versioned_project_banner_with_mixed_case_brand_holder() {
        let lines = vec![(
            1,
            "/*! jQuery v2.2.0 | (c) jQuery Foundation | jquery.org/license */".to_string(),
        )];
        let groups = collect_candidate_lines(lines);
        assert_eq!(groups.len(), 1, "groups: {groups:?}");
        assert_eq!(groups[0].len(), 1, "groups: {groups:?}");
    }

    #[test]
    fn test_versioned_banner_holder_from_prepared_extracts_holder() {
        let prepared =
            "! jQuery v3.7.1 (c) OpenJS Foundation and other contributors jquery.org/license";
        assert_eq!(
            versioned_banner_holder_from_prepared(prepared),
            Some("OpenJS Foundation and other contributors".to_string())
        );
    }
}