go-brrr 0.1.0

Token-efficient code analysis for LLMs - Rust implementation
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
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
//! Lines of Code (LOC) metrics calculation using AST-aware analysis.
//!
//! This module provides accurate line counting that distinguishes between:
//! - Physical LOC: Total lines including blanks and comments
//! - Source LOC (SLOC): Lines containing actual code
//! - Logical LOC: Number of statements (AST-based)
//! - Comment LOC (CLOC): Lines containing comments
//! - Blank lines: Empty or whitespace-only lines
//!
//! # AST-Aware Counting
//!
//! Unlike naive line counting, this module uses AST analysis to:
//! - Not count multi-line string literals as code lines (they're data)
//! - Properly count statements for logical LOC
//! - Identify comment vs code lines accurately
//! - Handle mixed code-and-comment lines (count as code)
//!
//! # Example
//!
//! ```ignore
//! use go_brrr::metrics::loc::{analyze_loc, LOCAnalysis};
//!
//! let result = analyze_loc("./src", Some("python"), None)?;
//! println!("Total SLOC: {}", result.stats.total_sloc);
//! println!("Code to comment ratio: {:.2}", result.stats.code_to_comment_ratio);
//! ```

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use tracing::debug;
use tree_sitter::{Node, Tree};
use wide::{u8x32, u32x8, CmpEq};

use crate::callgraph::scanner::{ProjectScanner, ScanConfig};
use crate::error::{Result, BrrrError};
use crate::lang::LanguageRegistry;

// =============================================================================
// TYPES
// =============================================================================

/// Lines of Code metrics for a code unit (function, file, or project).
///
/// These metrics provide different views of code size:
/// - Physical: Raw line count
/// - Source: Lines with actual code (excludes blanks and pure comments)
/// - Logical: Statement count (semantic size)
/// - Comment: Documentation/comment coverage
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct LOCMetrics {
    /// Total physical lines (including blanks and comments)
    pub physical: u32,
    /// Source lines of code (lines with actual code, excluding pure comments and blanks)
    pub source: u32,
    /// Logical lines of code (statement count from AST)
    pub logical: u32,
    /// Comment lines (lines that are purely comments or documentation)
    pub comment: u32,
    /// Blank lines (empty or whitespace only)
    pub blank: u32,
    /// Ratio of code lines to comment lines (SLOC/CLOC, or 0 if no comments)
    pub code_to_comment_ratio: f64,
}

impl LOCMetrics {
    /// Calculate metrics from raw counts.
    #[must_use]
    pub fn from_counts(physical: u32, source: u32, logical: u32, comment: u32, blank: u32) -> Self {
        let code_to_comment_ratio = if comment > 0 {
            f64::from(source) / f64::from(comment)
        } else {
            0.0
        };

        Self {
            physical,
            source,
            logical,
            comment,
            blank,
            code_to_comment_ratio,
        }
    }

    /// Calculate comment density (CLOC / SLOC as percentage).
    #[must_use]
    pub fn comment_density(&self) -> f64 {
        if self.source > 0 {
            f64::from(self.comment) / f64::from(self.source) * 100.0
        } else {
            0.0
        }
    }

    /// Calculate blank line ratio (blank / physical as percentage).
    #[must_use]
    pub fn blank_ratio(&self) -> f64 {
        if self.physical > 0 {
            f64::from(self.blank) / f64::from(self.physical) * 100.0
        } else {
            0.0
        }
    }
}

/// Size metrics for a single function.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionSize {
    /// Function name (may include class prefix for methods)
    pub name: String,
    /// File containing the function
    pub file: PathBuf,
    /// Starting line number (1-indexed)
    pub line: usize,
    /// Ending line number (1-indexed)
    pub end_line: usize,
    /// Source lines of code within the function
    pub sloc: u32,
    /// Number of statements in the function
    pub statements: u32,
    /// Comment density within the function (percentage)
    pub comment_density: f64,
    /// Whether function exceeds recommended size (> 50 SLOC)
    pub is_too_long: bool,
}

impl FunctionSize {
    /// Default threshold for "too long" functions (50 SLOC).
    pub const DEFAULT_THRESHOLD: u32 = 50;
}

/// LOC metrics for a single file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileLOC {
    /// File path
    pub file: PathBuf,
    /// Detected language
    pub language: Option<String>,
    /// LOC metrics for the file
    pub metrics: LOCMetrics,
    /// Function sizes within the file
    pub functions: Vec<FunctionSize>,
    /// Number of functions exceeding size threshold
    pub oversized_functions: usize,
}

/// LOC metrics grouped by language.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LanguageLOC {
    /// Language name
    pub language: String,
    /// Number of files
    pub file_count: usize,
    /// Aggregated LOC metrics
    pub metrics: LOCMetrics,
    /// Average SLOC per file
    pub avg_sloc_per_file: f64,
    /// Average statements per function
    pub avg_statements_per_function: f64,
}

/// Distribution statistics for LOC metrics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LOCDistribution {
    /// Total SLOC across all files
    pub total_sloc: u32,
    /// Total physical lines
    pub total_physical: u32,
    /// Total logical lines (statements)
    pub total_logical: u32,
    /// Total comment lines
    pub total_comment: u32,
    /// Total blank lines
    pub total_blank: u32,
    /// Overall code-to-comment ratio
    pub code_to_comment_ratio: f64,
    /// Overall blank line ratio
    pub blank_ratio: f64,
    /// Average SLOC per file
    pub avg_sloc_per_file: f64,
    /// Maximum SLOC in any file
    pub max_sloc: u32,
    /// Minimum SLOC in any file (non-empty)
    pub min_sloc: u32,
    /// Median SLOC
    pub median_sloc: u32,
    /// Total functions analyzed
    pub total_functions: usize,
    /// Average function size (SLOC)
    pub avg_function_size: f64,
    /// Functions exceeding size threshold
    pub oversized_function_count: usize,
}

/// Complete LOC analysis result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LOCAnalysis {
    /// Path that was analyzed
    pub path: PathBuf,
    /// Language filter applied (if any)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,
    /// Per-file metrics
    pub files: Vec<FileLOC>,
    /// Metrics by language
    pub by_language: Vec<LanguageLOC>,
    /// Aggregate statistics
    pub stats: LOCDistribution,
    /// Largest files by SLOC
    pub largest_files: Vec<FileRanking>,
    /// Functions exceeding size threshold
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub oversized_functions: Vec<FunctionSize>,
    /// Errors encountered during analysis
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub errors: Vec<LOCError>,
}

/// File ranking by size.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileRanking {
    /// File path
    pub file: PathBuf,
    /// Source lines of code
    pub sloc: u32,
    /// Percentage of total project SLOC
    pub percentage: f64,
}

/// Error during LOC analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LOCError {
    /// File where error occurred
    pub file: PathBuf,
    /// Error message
    pub message: String,
}

// =============================================================================
// LINE CLASSIFICATION
// =============================================================================

/// Classification of a source line.
///
/// Uses `#[repr(u8)]` with explicit discriminants to enable SIMD-accelerated
/// counting via byte-level comparisons in [`LineClassifier::count`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
enum LineType {
    /// Empty or whitespace-only line
    Blank = 0,
    /// Line containing only comments
    CommentOnly = 1,
    /// Line containing code (may also have trailing comment)
    Code = 2,
    /// Line inside a multi-line string literal (not counted as code)
    StringLiteral = 3,
}

/// Tracks line classifications for a file.
struct LineClassifier {
    /// Line type for each line (0-indexed)
    line_types: Vec<LineType>,
    /// Total line count
    total_lines: usize,
}

impl LineClassifier {
    /// Create a new classifier for a file with given line count.
    fn new(total_lines: usize) -> Self {
        // Default all lines to Code, then refine
        Self {
            line_types: vec![LineType::Code; total_lines],
            total_lines,
        }
    }

    /// Mark a line as blank.
    fn mark_blank(&mut self, line: usize) {
        if line < self.total_lines {
            self.line_types[line] = LineType::Blank;
        }
    }

    /// Mark a range of lines as comment-only.
    ///
    /// Note: Currently unused as process_comment handles line marking directly,
    /// but kept for potential future use in more sophisticated line classification.
    #[allow(dead_code)]
    fn mark_comment_only(&mut self, start_line: usize, end_line: usize) {
        for line in start_line..=end_line.min(self.total_lines.saturating_sub(1)) {
            // Only mark as comment if not already classified as code
            if self.line_types[line] != LineType::Code {
                self.line_types[line] = LineType::CommentOnly;
            }
        }
    }

    /// Mark a range of lines as inside a string literal.
    ///
    /// Note: Currently unused as walk_node handles string literal marking directly,
    /// but kept for potential future use in more sophisticated line classification.
    #[allow(dead_code)]
    fn mark_string_literal(&mut self, start_line: usize, end_line: usize) {
        for line in start_line..=end_line.min(self.total_lines.saturating_sub(1)) {
            // String literals only override blank or default
            if self.line_types[line] == LineType::Blank {
                self.line_types[line] = LineType::StringLiteral;
            }
        }
    }

    /// Count lines by type using SIMD acceleration.
    ///
    /// Processes 32 line types at a time using `u8x32` vectors for parallel comparison.
    /// Each enum value (0-3) is compared against all 32 elements simultaneously,
    /// then matches are counted via bitmask popcount (x86_64 AVX2) or byte summation (fallback).
    ///
    /// # Returns
    ///
    /// Tuple of `(blank, comment, code+string, string_literal)` counts.
    ///
    /// # Performance
    ///
    /// For files with N lines:
    /// - SIMD path: O(N/32) vector operations + O(N%32) scalar operations
    /// - Achieves ~8-16x speedup on x86_64 with AVX2 for large files
    fn count(&self) -> (u32, u32, u32, u32) {
        // Safety: LineType is #[repr(u8)] with values 0-3, guaranteeing identical
        // memory layout to &[u8]. This transmutation is safe because:
        // 1. LineType has explicit #[repr(u8)] representation
        // 2. All enum variants have values 0, 1, 2, 3 which are valid u8
        // 3. The slice length and alignment are preserved
        let bytes: &[u8] = unsafe {
            std::slice::from_raw_parts(
                self.line_types.as_ptr().cast::<u8>(),
                self.line_types.len(),
            )
        };

        let mut blank = 0u32;
        let mut comment = 0u32;
        let mut code = 0u32;
        let mut string_literal = 0u32;

        // Splat each enum discriminant for SIMD comparison
        let blank_v = u8x32::splat(LineType::Blank as u8);
        let comment_v = u8x32::splat(LineType::CommentOnly as u8);
        let code_v = u8x32::splat(LineType::Code as u8);
        let string_v = u8x32::splat(LineType::StringLiteral as u8);

        let chunks = bytes.chunks_exact(32);
        let remainder = chunks.remainder();

        for chunk in chunks {
            // Load 32 bytes into SIMD register
            let arr: [u8; 32] = chunk.try_into().unwrap_or([0u8; 32]);
            let data = u8x32::from(arr);

            // Parallel comparison: each lane becomes 0xFF (match) or 0x00 (no match)
            let blank_m = data.cmp_eq(blank_v);
            let comment_m = data.cmp_eq(comment_v);
            let code_m = data.cmp_eq(code_v);
            let string_m = data.cmp_eq(string_v);

            // Count matches via bitmask extraction and popcount
            blank += Self::count_mask_matches(blank_m);
            comment += Self::count_mask_matches(comment_m);
            code += Self::count_mask_matches(code_m);
            string_literal += Self::count_mask_matches(string_m);
        }

        // Scalar fallback for remainder (< 32 elements)
        for &byte in remainder {
            match byte {
                0 => blank += 1,
                1 => comment += 1,
                2 => code += 1,
                3 => string_literal += 1,
                _ => {} // Invalid discriminant - should never happen
            }
        }

        // String literals count as code lines since they're part of the program
        (blank, comment, code + string_literal, string_literal)
    }

    /// Count set lanes in a comparison mask using platform-optimal intrinsics.
    ///
    /// On x86_64 with AVX2: uses `_mm256_movemask_epi8` + `popcnt` for O(1) counting.
    /// Fallback: converts 0xFF masks to 0x01 via bitwise AND, then sums bytes.
    ///
    /// # Arguments
    ///
    /// * `mask` - SIMD vector where matching lanes are 0xFF, non-matching are 0x00
    ///
    /// # Returns
    ///
    /// Count of set (0xFF) lanes in the mask (0..=32)
    #[inline(always)]
    #[allow(clippy::transmute_ptr_to_ref)]
    fn count_mask_matches(mask: wide::u8x32) -> u32 {
        Self::count_mask_matches_impl(mask)
    }
}

/// x86_64 AVX2 fast path: extract bitmask and count ones.
///
/// The `movemask` instruction extracts the high bit of each byte into a 32-bit integer,
/// which for comparison masks (0xFF or 0x00) gives us exactly one bit per match.
#[cfg(all(target_arch = "x86_64", target_feature = "avx2"))]
impl LineClassifier {
    #[inline(always)]
    fn count_mask_matches_impl(mask: wide::u8x32) -> u32 {
        use std::arch::x86_64::{__m256i, _mm256_movemask_epi8};
        // Safety: wide::u8x32 on AVX2 is internally backed by __m256i with same layout.
        // Both are 256-bit, 32-byte aligned SIMD registers.
        #[allow(unsafe_code)]
        unsafe {
            let m: __m256i = std::mem::transmute(mask);
            let bits = _mm256_movemask_epi8(m) as u32;
            bits.count_ones()
        }
    }
}

/// Portable fallback: convert mask bytes to counts and sum.
///
/// Since comparison results are 0xFF (all bits set) for matches, we AND with 0x01
/// to normalize to 0x00 or 0x01, then sum all 32 bytes.
#[cfg(not(all(target_arch = "x86_64", target_feature = "avx2")))]
impl LineClassifier {
    #[inline(always)]
    fn count_mask_matches_impl(mask: wide::u8x32) -> u32 {
        // Convert 0xFF -> 0x01 by AND with splat(1), then horizontal sum
        let ones = wide::u8x32::splat(1);
        let counted = mask & ones;
        let arr: [u8; 32] = counted.into();
        // Sum all bytes - max value is 32, well within u32
        arr.iter().fold(0u32, |acc, &x| acc + u32::from(x))
    }
}

// =============================================================================
// SIMD AGGREGATE SUMMATION
// =============================================================================

/// SIMD-accelerated u32 array summation using `u32x8`.
///
/// Processes 8 elements per SIMD iteration, achieving ~4-8x speedup over scalar
/// summation for large arrays on x86_64 with AVX2 or aarch64 with NEON.
///
/// # Algorithm
///
/// 1. Initialize 8-wide accumulator to zero
/// 2. Load 8 consecutive u32 values into SIMD register
/// 3. Add to accumulator (8 parallel additions per iteration)
/// 4. Repeat for all 8-element chunks
/// 5. Horizontal reduce: sum 8 accumulator lanes to single value
/// 6. Add remainder elements (0-7) via scalar fallback
///
/// # Performance
///
/// For N elements:
/// - SIMD iterations: N/8
/// - Scalar remainder: N % 8 (max 7)
/// - Horizontal reduce: O(1) - 7 additions
/// - Total: O(N/8) vs O(N) for scalar = ~8x theoretical speedup
#[inline]
fn simd_sum_u32(slice: &[u32]) -> u32 {
    if slice.is_empty() {
        return 0;
    }

    let chunks = slice.chunks_exact(8);
    let remainder = chunks.remainder();

    // 8-wide accumulator for partial sums
    let mut acc = u32x8::splat(0);

    for chunk in chunks {
        // Load 8 u32 values into SIMD register
        // SAFETY: chunks_exact guarantees exactly 8 elements
        let arr: [u32; 8] = chunk.try_into().unwrap_or([0u32; 8]);
        let v = u32x8::from(arr);
        acc += v;
    }

    // Horizontal reduction: sum all 8 lanes
    // Extract to array and sum - compiler may optimize to phadd on x86
    let arr: [u32; 8] = acc.into();
    let mut sum = arr[0]
        .wrapping_add(arr[1])
        .wrapping_add(arr[2])
        .wrapping_add(arr[3])
        .wrapping_add(arr[4])
        .wrapping_add(arr[5])
        .wrapping_add(arr[6])
        .wrapping_add(arr[7]);

    // Handle remainder (0-7 elements) via scalar
    for &val in remainder {
        sum = sum.wrapping_add(val);
    }

    sum
}

// =============================================================================
// SIMD WHITESPACE DETECTION
// =============================================================================

/// Whitespace byte constants for SIMD comparison.
const WS_SPACE: u8 = b' ';    // 0x20
const WS_TAB: u8 = b'\t';     // 0x09
const WS_NEWLINE: u8 = b'\n'; // 0x0A
const WS_CR: u8 = b'\r';      // 0x0D

/// Check if a byte is ASCII whitespace (space, tab, newline, carriage return).
#[inline(always)]
fn is_ascii_ws(byte: u8) -> bool {
    matches!(byte, WS_SPACE | WS_TAB | WS_NEWLINE | WS_CR)
}

/// Find the index of the first non-whitespace byte using SIMD.
///
/// Compares each byte against space (0x20), tab (0x09), newline (0x0A),
/// and carriage return (0x0D) using `u8x32` parallel comparison.
///
/// # Algorithm
///
/// 1. Load 32 bytes into SIMD register
/// 2. Compare against each whitespace value (4 comparisons)
/// 3. OR masks together to get "is whitespace" bitmap
/// 4. Find first zero in mask (first non-whitespace)
/// 5. Repeat for subsequent chunks, or fall back to scalar for remainder
///
/// # Performance
///
/// - SIMD path: O(N/32) vector operations with AVX2 `movemask` + `trailing_zeros`
/// - Scalar fallback: O(N%32) for remainder elements
/// - Early exit on first non-whitespace found
///
/// # Returns
///
/// `Some(index)` of first non-whitespace byte, or `None` if all whitespace.
#[inline]
fn find_first_nonws(bytes: &[u8]) -> Option<usize> {
    if bytes.is_empty() {
        return None;
    }

    // Splat whitespace values for parallel comparison
    let space_v = u8x32::splat(WS_SPACE);
    let tab_v = u8x32::splat(WS_TAB);
    let nl_v = u8x32::splat(WS_NEWLINE);
    let cr_v = u8x32::splat(WS_CR);

    let chunks = bytes.chunks_exact(32);
    let remainder = chunks.remainder();
    let chunk_count = bytes.len() / 32;

    // SIMD pass: process 32 bytes at a time
    for (chunk_idx, chunk) in chunks.enumerate() {
        let arr: [u8; 32] = chunk.try_into().unwrap_or([0u8; 32]);
        let data = u8x32::from(arr);

        // Check if each byte is any whitespace character
        // Each lane becomes 0xFF for whitespace, 0x00 for non-whitespace
        let is_ws = data.cmp_eq(space_v)
            | data.cmp_eq(tab_v)
            | data.cmp_eq(nl_v)
            | data.cmp_eq(cr_v);

        // Find first non-whitespace (first 0x00 in is_ws mask)
        if let Some(offset) = find_first_zero_in_ws_mask(is_ws) {
            return Some(chunk_idx * 32 + offset);
        }
    }

    // Scalar fallback for remainder (< 32 bytes)
    let base_offset = chunk_count * 32;
    for (i, &byte) in remainder.iter().enumerate() {
        if !is_ascii_ws(byte) {
            return Some(base_offset + i);
        }
    }

    None
}

/// Find the index of the last non-whitespace byte using SIMD.
///
/// Scans backwards through the buffer, processing remainder first (scalar),
/// then 32-byte chunks in reverse order using SIMD.
///
/// # Algorithm
///
/// 1. Check remainder bytes (tail) with scalar loop
/// 2. For each 32-byte chunk (reverse order):
///    - Load into SIMD register
///    - Compare against whitespace values
///    - Find last zero in mask (last non-whitespace)
/// 3. Return immediately upon finding non-whitespace
///
/// # Performance
///
/// - Early exit on first (last positionally) non-whitespace found
/// - SIMD path processes 32 bytes per iteration
/// - Optimizes for common case where content ends near buffer end
///
/// # Returns
///
/// `Some(index)` of last non-whitespace byte, or `None` if all whitespace.
#[inline]
fn find_last_nonws(bytes: &[u8]) -> Option<usize> {
    if bytes.is_empty() {
        return None;
    }

    let space_v = u8x32::splat(WS_SPACE);
    let tab_v = u8x32::splat(WS_TAB);
    let nl_v = u8x32::splat(WS_NEWLINE);
    let cr_v = u8x32::splat(WS_CR);

    let len = bytes.len();
    let chunk_count = len / 32;
    let remainder_len = len % 32;

    // First, check remainder (tail bytes) with scalar - most content ends here
    if remainder_len > 0 {
        let remainder_start = chunk_count * 32;
        for i in (0..remainder_len).rev() {
            if !is_ascii_ws(bytes[remainder_start + i]) {
                return Some(remainder_start + i);
            }
        }
    }

    // SIMD pass: process 32-byte chunks in reverse order
    for chunk_idx in (0..chunk_count).rev() {
        let start = chunk_idx * 32;
        let chunk = &bytes[start..start + 32];
        let arr: [u8; 32] = chunk.try_into().unwrap_or([0u8; 32]);
        let data = u8x32::from(arr);

        let is_ws = data.cmp_eq(space_v)
            | data.cmp_eq(tab_v)
            | data.cmp_eq(nl_v)
            | data.cmp_eq(cr_v);

        // Find last non-whitespace (last 0x00 in is_ws mask)
        if let Some(offset) = find_last_zero_in_ws_mask(is_ws) {
            return Some(start + offset);
        }
    }

    None
}

/// x86_64 AVX2 fast path: find first zero byte in whitespace mask.
///
/// Uses `movemask` to extract 32-bit bitmap where each bit indicates
/// if corresponding lane is 0xFF (whitespace). Inverts and finds first
/// set bit (first non-whitespace).
#[cfg(all(target_arch = "x86_64", target_feature = "avx2"))]
#[inline(always)]
fn find_first_zero_in_ws_mask(mask: u8x32) -> Option<usize> {
    use std::arch::x86_64::{__m256i, _mm256_movemask_epi8};
    #[allow(unsafe_code)]
    unsafe {
        let m: __m256i = std::mem::transmute(mask);
        let bits = _mm256_movemask_epi8(m) as u32;
        // Invert: 1 bits become non-whitespace positions
        let inverted = !bits;
        if inverted == 0 {
            None // All 32 bytes are whitespace
        } else {
            Some(inverted.trailing_zeros() as usize)
        }
    }
}

/// x86_64 AVX2 fast path: find last zero byte in whitespace mask.
///
/// Uses `movemask` + inversion + `leading_zeros` to find the highest
/// bit position that was non-whitespace.
#[cfg(all(target_arch = "x86_64", target_feature = "avx2"))]
#[inline(always)]
fn find_last_zero_in_ws_mask(mask: u8x32) -> Option<usize> {
    use std::arch::x86_64::{__m256i, _mm256_movemask_epi8};
    #[allow(unsafe_code)]
    unsafe {
        let m: __m256i = std::mem::transmute(mask);
        let bits = _mm256_movemask_epi8(m) as u32;
        let inverted = !bits;
        if inverted == 0 {
            None // All 32 bytes are whitespace
        } else {
            // Last set bit position = 31 - leading zeros
            Some(31 - inverted.leading_zeros() as usize)
        }
    }
}

/// Portable fallback: find first zero byte by linear scan.
#[cfg(not(all(target_arch = "x86_64", target_feature = "avx2")))]
#[inline(always)]
fn find_first_zero_in_ws_mask(mask: u8x32) -> Option<usize> {
    let arr: [u8; 32] = mask.into();
    arr.iter().position(|&x| x == 0)
}

/// Portable fallback: find last zero byte by reverse linear scan.
#[cfg(not(all(target_arch = "x86_64", target_feature = "avx2")))]
#[inline(always)]
fn find_last_zero_in_ws_mask(mask: u8x32) -> Option<usize> {
    let arr: [u8; 32] = mask.into();
    arr.iter().rposition(|&x| x == 0)
}

/// Aggregate LOC metrics collector for SIMD-accelerated summation.
///
/// Collects metrics into contiguous arrays (single pass over input),
/// then uses `u32x8` SIMD to sum each array efficiently.
///
/// # Memory Layout
///
/// Each field stored contiguously for optimal cache locality during SIMD summation:
/// ```text
/// physical: [p0, p1, p2, p3, p4, p5, p6, p7, ...]  <- SIMD sum
/// source:   [s0, s1, s2, s3, s4, s5, s6, s7, ...]  <- SIMD sum
/// logical:  [l0, l1, l2, l3, l4, l5, l6, l7, ...]  <- SIMD sum
/// comment:  [c0, c1, c2, c3, c4, c5, c6, c7, ...]  <- SIMD sum
/// blank:    [b0, b1, b2, b3, b4, b5, b6, b7, ...]  <- SIMD sum
/// ```
///
/// # Performance
///
/// For N files:
/// - Collection: O(N) single pass, 5 pushes per iteration
/// - Summation: 5 x O(N/8) SIMD operations
/// - Total: O(N + 0.625N) = O(1.625N) vs O(5N) naive = ~3x speedup
struct AggregateMetricsCollector {
    physical: Vec<u32>,
    source: Vec<u32>,
    logical: Vec<u32>,
    comment: Vec<u32>,
    blank: Vec<u32>,
}

impl AggregateMetricsCollector {
    /// Create collector with pre-allocated capacity.
    fn with_capacity(capacity: usize) -> Self {
        Self {
            physical: Vec::with_capacity(capacity),
            source: Vec::with_capacity(capacity),
            logical: Vec::with_capacity(capacity),
            comment: Vec::with_capacity(capacity),
            blank: Vec::with_capacity(capacity),
        }
    }

    /// Collect metrics from FileLOC slice in single pass.
    fn collect_from_file_locs(files: &[FileLOC]) -> Self {
        let mut collector = Self::with_capacity(files.len());
        for f in files {
            collector.physical.push(f.metrics.physical);
            collector.source.push(f.metrics.source);
            collector.logical.push(f.metrics.logical);
            collector.comment.push(f.metrics.comment);
            collector.blank.push(f.metrics.blank);
        }
        collector
    }

    /// Collect metrics from FileLOC references in single pass.
    fn collect_from_file_loc_refs(files: &[&FileLOC]) -> Self {
        let mut collector = Self::with_capacity(files.len());
        for f in files {
            collector.physical.push(f.metrics.physical);
            collector.source.push(f.metrics.source);
            collector.logical.push(f.metrics.logical);
            collector.comment.push(f.metrics.comment);
            collector.blank.push(f.metrics.blank);
        }
        collector
    }

    /// Sum all fields using SIMD-accelerated summation.
    ///
    /// # Returns
    ///
    /// Tuple of `(physical, source, logical, comment, blank)` sums.
    #[inline]
    fn sum_all(&self) -> (u32, u32, u32, u32, u32) {
        (
            simd_sum_u32(&self.physical),
            simd_sum_u32(&self.source),
            simd_sum_u32(&self.logical),
            simd_sum_u32(&self.comment),
            simd_sum_u32(&self.blank),
        )
    }
}

// =============================================================================
// AST ANALYSIS
// =============================================================================

/// Node types that represent comments in various languages.
const COMMENT_NODE_TYPES: &[&str] = &[
    "comment",       // Python, Go, TypeScript/JavaScript, Java
    "line_comment",  // Rust, Java, C/C++
    "block_comment", // Rust, Java, C/C++
];

/// Node types that represent string literals (potential multi-line).
const STRING_NODE_TYPES: &[&str] = &[
    "string",              // Python
    "string_literal",      // Rust, C, C++, Java
    "raw_string_literal",  // Rust, C++
    "template_string",     // TypeScript/JavaScript
    "interpreted_string_literal", // Go
    "raw_string",          // Python raw strings
];

/// Node types that represent statements (for logical LOC).
const STATEMENT_NODE_TYPES: &[&str] = &[
    // Python
    "expression_statement", "return_statement", "if_statement", "for_statement",
    "while_statement", "try_statement", "with_statement", "assert_statement",
    "raise_statement", "pass_statement", "break_statement", "continue_statement",
    "import_statement", "import_from_statement", "global_statement", "nonlocal_statement",
    "delete_statement", "future_import_statement", "match_statement",
    // TypeScript/JavaScript
    "expression_statement", "return_statement", "if_statement", "switch_statement",
    "for_statement", "for_in_statement", "while_statement", "do_statement",
    "try_statement", "with_statement", "throw_statement", "break_statement",
    "continue_statement", "import_statement", "export_statement", "variable_declaration",
    "lexical_declaration",
    // Rust
    "expression_statement", "let_declaration", "return_expression", "if_expression",
    "match_expression", "for_expression", "while_expression", "loop_expression",
    "break_expression", "continue_expression", "macro_invocation",
    // Go
    "expression_statement", "return_statement", "if_statement", "switch_statement",
    "for_statement", "go_statement", "select_statement", "defer_statement",
    "var_declaration", "short_var_declaration", "assignment_statement",
    // Java
    "expression_statement", "return_statement", "if_statement", "switch_expression",
    "for_statement", "enhanced_for_statement", "while_statement", "do_statement",
    "try_statement", "throw_statement", "break_statement", "continue_statement",
    "local_variable_declaration", "assert_statement",
    // C/C++
    "expression_statement", "return_statement", "if_statement", "switch_statement",
    "for_statement", "while_statement", "do_statement", "break_statement",
    "continue_statement", "goto_statement", "declaration", "compound_statement",
];

/// Function-defining node types.
const FUNCTION_NODE_TYPES: &[&str] = &[
    // Python
    "function_definition",
    // TypeScript/JavaScript
    "function_declaration", "method_definition", "arrow_function",
    // Rust
    "function_item",
    // Go
    "function_declaration", "method_declaration",
    // Java
    "method_declaration", "constructor_declaration",
    // C/C++
    "function_definition",
];

/// Analyze a parsed AST to extract LOC information.
struct ASTAnalyzer<'a> {
    source: &'a [u8],
    lines: Vec<&'a str>,
    classifier: LineClassifier,
    statement_count: u32,
    function_ranges: Vec<(String, usize, usize)>, // (name, start, end)
}

impl<'a> ASTAnalyzer<'a> {
    /// Create a new analyzer for the given source.
    fn new(source: &'a [u8]) -> Self {
        let source_str = std::str::from_utf8(source).unwrap_or("");
        let lines: Vec<&str> = source_str.lines().collect();
        let total_lines = lines.len();

        let mut classifier = LineClassifier::new(total_lines);

        // First pass: mark blank lines
        for (i, line) in lines.iter().enumerate() {
            if line.trim().is_empty() {
                classifier.mark_blank(i);
            }
        }

        Self {
            source,
            lines,
            classifier,
            statement_count: 0,
            function_ranges: Vec::new(),
        }
    }

    /// Analyze an AST tree.
    fn analyze(&mut self, tree: &Tree, lang_name: &str) {
        self.walk_node(tree.root_node(), lang_name, 0);
    }

    /// Recursively walk the AST.
    fn walk_node(&mut self, node: Node, lang_name: &str, depth: usize) {
        let kind = node.kind();
        let start_line = node.start_position().row;
        let end_line = node.end_position().row;

        // Track comments
        if COMMENT_NODE_TYPES.contains(&kind) {
            self.process_comment(node, start_line, end_line);
        }

        // Track multi-line strings (but not docstrings)
        if STRING_NODE_TYPES.contains(&kind) && !self.is_docstring(node, lang_name) {
            if end_line > start_line {
                // Multi-line string: mark intermediate lines as string literal
                // Keep first and last lines as code (they contain the quotes)
                for line in (start_line + 1)..end_line {
                    if line < self.classifier.total_lines {
                        self.classifier.line_types[line] = LineType::StringLiteral;
                    }
                }
            }
        }

        // Count statements
        if STATEMENT_NODE_TYPES.contains(&kind) {
            self.statement_count += 1;
        }

        // Track function definitions
        if FUNCTION_NODE_TYPES.contains(&kind) {
            let name = self.extract_function_name(node);
            self.function_ranges.push((name, start_line, end_line));
        }

        // Recurse into children
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            self.walk_node(child, lang_name, depth + 1);
        }
    }

    /// Process a comment node.
    fn process_comment(&mut self, node: Node, start_line: usize, end_line: usize) {
        // Check if comment is the only thing on its line(s)
        for line_idx in start_line..=end_line.min(self.classifier.total_lines.saturating_sub(1)) {
            if self.is_line_only_comment(line_idx, node) {
                self.classifier.line_types[line_idx] = LineType::CommentOnly;
            }
        }
    }

    /// Check if a line contains only the comment (and whitespace).
    ///
    /// Uses SIMD-accelerated whitespace detection via `find_first_nonws` and
    /// `find_last_nonws` for efficient content boundary identification.
    fn is_line_only_comment(&self, line_idx: usize, comment_node: Node) -> bool {
        if line_idx >= self.lines.len() {
            return false;
        }

        let line = self.lines[line_idx];
        let line_bytes = line.as_bytes();
        let line_start_byte = self.line_start_byte(line_idx);

        let comment_start = comment_node.start_byte();
        let comment_end = comment_node.end_byte();

        // SIMD-accelerated: find first and last non-whitespace positions
        // Returns None if line is all whitespace (blank line)
        let first_nonws = match find_first_nonws(line_bytes) {
            Some(idx) => idx,
            None => return false, // All whitespace = blank line, not comment-only
        };

        // Find last non-whitespace (guaranteed to exist if first exists)
        let last_nonws = find_last_nonws(line_bytes).unwrap_or(first_nonws);

        // Calculate absolute byte positions for comparison with AST node
        let content_start = line_start_byte + first_nonws;
        let content_end = line_start_byte + last_nonws + 1; // +1 for exclusive end

        // Comment covers the line if it includes all non-whitespace content
        comment_start <= content_start && comment_end >= content_end
    }

    /// Get the byte offset of a line.
    fn line_start_byte(&self, line_idx: usize) -> usize {
        let mut offset = 0;
        for (i, line) in self.lines.iter().enumerate() {
            if i == line_idx {
                return offset;
            }
            offset += line.len() + 1; // +1 for newline
        }
        offset
    }

    /// Check if a string node is a docstring (Python-specific).
    fn is_docstring(&self, node: Node, lang_name: &str) -> bool {
        if lang_name != "python" {
            return false;
        }

        // Python docstrings are expression_statement nodes containing just a string
        // as the first statement in a function/class/module
        if let Some(parent) = node.parent() {
            if parent.kind() == "expression_statement" {
                if let Some(grandparent) = parent.parent() {
                    let gp_kind = grandparent.kind();
                    if gp_kind == "block" || gp_kind == "module" {
                        // Check if this is the first non-comment statement
                        let mut cursor = grandparent.walk();
                        for (idx, child) in grandparent.children(&mut cursor).enumerate() {
                            if child.kind() == "expression_statement" && child.id() == parent.id() {
                                // It's a docstring if it's among the first statements
                                return idx < 3; // Allow for decorators/comments before
                            }
                            if STATEMENT_NODE_TYPES.contains(&child.kind()) && child.id() != parent.id() {
                                // Found a different statement before this one
                                return false;
                            }
                        }
                    }
                }
            }
        }
        false
    }

    /// Extract function name from a function node.
    fn extract_function_name(&self, node: Node) -> String {
        // Look for identifier or name child
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            match child.kind() {
                "identifier" | "name" | "property_identifier" => {
                    return child.utf8_text(self.source)
                        .unwrap_or("")
                        .to_string();
                }
                "function_declarator" | "declarator" => {
                    // C/C++ style: recurse into declarator
                    let mut inner_cursor = child.walk();
                    for inner in child.children(&mut inner_cursor) {
                        if inner.kind() == "identifier" {
                            return inner.utf8_text(self.source)
                                .unwrap_or("")
                                .to_string();
                        }
                    }
                }
                _ => {}
            }
        }
        "<anonymous>".to_string()
    }

    /// Get final metrics.
    fn get_metrics(&self) -> LOCMetrics {
        let (blank, comment, source, _) = self.classifier.count();
        let physical = self.classifier.total_lines as u32;
        LOCMetrics::from_counts(physical, source, self.statement_count, comment, blank)
    }

    /// Get function-level metrics.
    fn get_function_metrics(&self, file: &Path) -> Vec<FunctionSize> {
        self.function_ranges.iter().map(|(name, start, end)| {
            let sloc = self.count_sloc_in_range(*start, *end);
            let statements = self.count_statements_in_range(*start, *end);
            let comment_lines = self.count_comments_in_range(*start, *end);
            let comment_density = if sloc > 0 {
                f64::from(comment_lines) / f64::from(sloc) * 100.0
            } else {
                0.0
            };

            FunctionSize {
                name: name.clone(),
                file: file.to_path_buf(),
                line: *start + 1, // 1-indexed
                end_line: *end + 1,
                sloc,
                statements,
                comment_density,
                is_too_long: sloc > FunctionSize::DEFAULT_THRESHOLD,
            }
        }).collect()
    }

    /// Count SLOC in a line range using SIMD-accelerated comparison.
    ///
    /// Uses `wide::u8x32` to process 32 line types simultaneously.
    /// Since `LineType` is `#[repr(u8)]` with Code=2 and StringLiteral=3,
    /// we check for equality with these values to identify SLOC lines.
    ///
    /// # Performance
    ///
    /// - SIMD path: O(N/32) vector operations with AVX2 `movemask` + `popcnt`
    /// - Scalar fallback: O(N%32) for remainder elements
    /// - Achieves ~8-16x speedup on x86_64 with AVX2 for large ranges
    fn count_sloc_in_range(&self, start: usize, end: usize) -> u32 {
        let end_idx = end.min(self.classifier.total_lines.saturating_sub(1));
        if start > end_idx {
            return 0;
        }

        let slice = &self.classifier.line_types[start..=end_idx];

        // SAFETY: LineType is #[repr(u8)] with discriminants 0-3,
        // so the memory layout is identical to &[u8].
        let bytes: &[u8] = unsafe {
            std::slice::from_raw_parts(slice.as_ptr().cast::<u8>(), slice.len())
        };

        let mut count = 0u32;

        // SLOC line type values: Code=2, StringLiteral=3
        let code_val = u8x32::splat(LineType::Code as u8);
        let str_val = u8x32::splat(LineType::StringLiteral as u8);

        // Use chunks_exact for cleaner SIMD iteration
        let chunks = bytes.chunks_exact(32);
        let remainder = chunks.remainder();

        // SIMD pass: process 32 bytes at a time
        for chunk in chunks {
            let arr: [u8; 32] = chunk.try_into().unwrap_or([0u8; 32]);
            let data = u8x32::from(arr);

            // Compare for Code(2) and StringLiteral(3), then OR the masks
            // Each comparison returns 0xFF for match, 0x00 for non-match
            let mask_code = data.cmp_eq(code_val);
            let mask_str = data.cmp_eq(str_val);
            let mask = mask_code | mask_str;

            // Use LineClassifier's optimized mask counting (AVX2 movemask+popcnt or fallback)
            count += LineClassifier::count_mask_matches(mask);
        }

        // Scalar fallback for remainder (< 32 bytes)
        for &byte in remainder {
            if byte == LineType::Code as u8 || byte == LineType::StringLiteral as u8 {
                count += 1;
            }
        }

        count
    }

    /// Count statements in a line range (approximate by lines that changed).
    fn count_statements_in_range(&self, start: usize, end: usize) -> u32 {
        // This is a simplified count - ideally we'd track statement positions
        // For now, estimate based on SLOC (most lines are statements)
        let sloc = self.count_sloc_in_range(start, end);
        // Rough estimate: ~80% of SLOC are actual statements
        (f64::from(sloc) * 0.8).round() as u32
    }

    /// Count comment lines in a range.
    fn count_comments_in_range(&self, start: usize, end: usize) -> u32 {
        let mut count = 0u32;
        for i in start..=end.min(self.classifier.total_lines.saturating_sub(1)) {
            if self.classifier.line_types[i] == LineType::CommentOnly {
                count += 1;
            }
        }
        count
    }
}

// =============================================================================
// ANALYSIS FUNCTIONS
// =============================================================================

/// Analyze LOC metrics for a file or directory.
///
/// # Arguments
///
/// * `path` - Path to file or directory to analyze
/// * `language` - Optional language filter
/// * `function_threshold` - Optional threshold for "too long" functions (default: 50)
///
/// # Returns
///
/// Complete LOC analysis with per-file, per-language, and aggregate metrics.
pub fn analyze_loc(
    path: impl AsRef<Path>,
    language: Option<&str>,
    function_threshold: Option<u32>,
) -> Result<LOCAnalysis> {
    let path = path.as_ref();
    let threshold = function_threshold.unwrap_or(FunctionSize::DEFAULT_THRESHOLD);

    if !path.exists() {
        return Err(BrrrError::Io(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            format!("Path not found: {}", path.display()),
        )));
    }

    if path.is_file() {
        return analyze_file_loc(path, threshold);
    }

    // Directory analysis
    let path_str = path.to_str().ok_or_else(|| {
        BrrrError::InvalidArgument("Invalid path encoding".to_string())
    })?;

    let scanner = ProjectScanner::new(path_str)?;

    let config = if let Some(lang) = language {
        ScanConfig::for_language(lang)
    } else {
        ScanConfig::default()
    };

    let scan_result = scanner.scan_with_config(&config)?;

    if scan_result.files.is_empty() {
        return Err(BrrrError::InvalidArgument(format!(
            "No source files found in {} (filter: {:?})",
            path.display(),
            language
        )));
    }

    debug!("Analyzing {} files for LOC metrics", scan_result.files.len());

    // Analyze files in parallel
    let results: Vec<std::result::Result<FileLOC, LOCError>> = scan_result
        .files
        .par_iter()
        .map(|file| analyze_single_file(file, threshold))
        .collect();

    // Separate successes and errors
    let mut files = Vec::new();
    let mut errors = Vec::new();

    for result in results {
        match result {
            Ok(file_loc) => files.push(file_loc),
            Err(e) => errors.push(e),
        }
    }

    // Aggregate by language
    let by_language = aggregate_by_language(&files);

    // Calculate distribution stats
    let stats = calculate_distribution(&files);

    // Get largest files
    let largest_files = get_largest_files(&files, &stats, 10);

    // Collect oversized functions
    let mut oversized_functions: Vec<FunctionSize> = files
        .iter()
        .flat_map(|f| f.functions.iter())
        .filter(|f| f.is_too_long)
        .cloned()
        .collect();
    oversized_functions.sort_by(|a, b| b.sloc.cmp(&a.sloc));

    Ok(LOCAnalysis {
        path: path.to_path_buf(),
        language: language.map(String::from),
        files,
        by_language,
        stats,
        largest_files,
        oversized_functions,
        errors,
    })
}

/// Analyze a single file for LOC metrics.
pub fn analyze_file_loc(
    path: impl AsRef<Path>,
    function_threshold: u32,
) -> Result<LOCAnalysis> {
    let path = path.as_ref();

    if !path.exists() {
        return Err(BrrrError::Io(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            format!("File not found: {}", path.display()),
        )));
    }

    let file_loc = analyze_single_file(path, function_threshold)
        .map_err(|e| BrrrError::InvalidArgument(e.message))?;

    let by_language = aggregate_by_language(&[file_loc.clone()]);
    let stats = calculate_distribution(&[file_loc.clone()]);
    let largest_files = get_largest_files(&[file_loc.clone()], &stats, 1);

    let oversized_functions: Vec<FunctionSize> = file_loc.functions
        .iter()
        .filter(|f| f.is_too_long)
        .cloned()
        .collect();

    Ok(LOCAnalysis {
        path: path.to_path_buf(),
        language: file_loc.language.clone(),
        files: vec![file_loc],
        by_language,
        stats,
        largest_files,
        oversized_functions,
        errors: Vec::new(),
    })
}

/// Analyze a single file (internal).
fn analyze_single_file(path: &Path, threshold: u32) -> std::result::Result<FileLOC, LOCError> {
    let source = std::fs::read(path).map_err(|e| LOCError {
        file: path.to_path_buf(),
        message: format!("Failed to read file: {}", e),
    })?;

    let registry = LanguageRegistry::global();
    let lang = registry.detect_language(path);

    let lang_name = lang.map(|l| l.name().to_string());

    // Parse with tree-sitter if language is detected
    let (metrics, functions) = if let Some(ref lang_impl) = lang {
        let mut parser = lang_impl.parser().map_err(|e| LOCError {
            file: path.to_path_buf(),
            message: format!("Failed to create parser: {}", e),
        })?;

        let tree = parser.parse(&source, None).ok_or_else(|| LOCError {
            file: path.to_path_buf(),
            message: "Failed to parse file".to_string(),
        })?;

        let mut analyzer = ASTAnalyzer::new(&source);
        analyzer.analyze(&tree, lang_impl.name());

        let metrics = analyzer.get_metrics();
        let mut functions = analyzer.get_function_metrics(path);

        // Apply threshold
        for func in &mut functions {
            func.is_too_long = func.sloc > threshold;
        }

        (metrics, functions)
    } else {
        // Fallback: simple line counting without AST
        let metrics = simple_line_count(&source);
        (metrics, Vec::new())
    };

    let oversized_functions = functions.iter().filter(|f| f.is_too_long).count();

    Ok(FileLOC {
        file: path.to_path_buf(),
        language: lang_name,
        metrics,
        functions,
        oversized_functions,
    })
}

/// Simple line counting without AST (fallback for unknown languages).
fn simple_line_count(source: &[u8]) -> LOCMetrics {
    let source_str = std::str::from_utf8(source).unwrap_or("");
    let lines: Vec<&str> = source_str.lines().collect();

    let physical = lines.len() as u32;
    let blank = lines.iter().filter(|l| l.trim().is_empty()).count() as u32;
    let source = physical - blank;

    // Without AST, we can't distinguish comments reliably
    LOCMetrics::from_counts(physical, source, source, 0, blank)
}

/// Aggregate file metrics by language using SIMD-accelerated summation.
///
/// Groups files by language, then uses `AggregateMetricsCollector` with `u32x8`
/// SIMD to efficiently sum metrics for each language group.
///
/// # Performance
///
/// For L languages with N_l files each:
/// - Grouping: O(N) where N = sum of all N_l
/// - Per-language SIMD sum: O(1.625 * N_l) vs O(5 * N_l) naive
/// - Total speedup: ~3x for metric aggregation
fn aggregate_by_language(files: &[FileLOC]) -> Vec<LanguageLOC> {
    let mut by_lang: HashMap<String, Vec<&FileLOC>> = HashMap::new();

    for file in files {
        let lang = file.language.clone().unwrap_or_else(|| "unknown".to_string());
        by_lang.entry(lang).or_default().push(file);
    }

    let mut result: Vec<LanguageLOC> = by_lang
        .into_iter()
        .map(|(lang, lang_files)| {
            let file_count = lang_files.len();

            // SIMD-accelerated metric aggregation:
            // 1. Single-pass collection into contiguous arrays
            // 2. u32x8 parallel summation (8 elements per iteration)
            let collector = AggregateMetricsCollector::collect_from_file_loc_refs(&lang_files);
            let (total_physical, total_source, total_logical, total_comment, total_blank) =
                collector.sum_all();

            let metrics = LOCMetrics::from_counts(
                total_physical,
                total_source,
                total_logical,
                total_comment,
                total_blank,
            );

            let total_functions: usize = lang_files.iter().map(|f| f.functions.len()).sum();
            let total_statements: u32 = lang_files
                .iter()
                .flat_map(|f| f.functions.iter())
                .map(|func| func.statements)
                .sum();

            let avg_sloc_per_file = if file_count > 0 {
                f64::from(total_source) / file_count as f64
            } else {
                0.0
            };

            let avg_statements_per_function = if total_functions > 0 {
                f64::from(total_statements) / total_functions as f64
            } else {
                0.0
            };

            LanguageLOC {
                language: lang,
                file_count,
                metrics,
                avg_sloc_per_file,
                avg_statements_per_function,
            }
        })
        .collect();

    result.sort_by(|a, b| b.metrics.source.cmp(&a.metrics.source));
    result
}

/// Calculate distribution statistics using SIMD-accelerated summation.
///
/// Uses `AggregateMetricsCollector` with `u32x8` SIMD to efficiently sum
/// the 5 LOC metric fields across all files.
///
/// # Performance
///
/// For N files:
/// - SIMD collection + sum: O(1.625N) vs O(5N) naive = ~3x speedup
/// - Additional passes for functions/median are not SIMD-optimized (less frequent)
fn calculate_distribution(files: &[FileLOC]) -> LOCDistribution {
    if files.is_empty() {
        return LOCDistribution {
            total_sloc: 0,
            total_physical: 0,
            total_logical: 0,
            total_comment: 0,
            total_blank: 0,
            code_to_comment_ratio: 0.0,
            blank_ratio: 0.0,
            avg_sloc_per_file: 0.0,
            max_sloc: 0,
            min_sloc: 0,
            median_sloc: 0,
            total_functions: 0,
            avg_function_size: 0.0,
            oversized_function_count: 0,
        };
    }

    // SIMD-accelerated metric aggregation:
    // 1. Single-pass collection into contiguous arrays (5 pushes per file)
    // 2. u32x8 parallel summation (8 elements per SIMD iteration)
    // Result: ~3x speedup over 5 separate iter().sum() calls
    let collector = AggregateMetricsCollector::collect_from_file_locs(files);
    let (total_physical, total_sloc, total_logical, total_comment, total_blank) =
        collector.sum_all();

    let code_to_comment_ratio = if total_comment > 0 {
        f64::from(total_sloc) / f64::from(total_comment)
    } else {
        0.0
    };

    let blank_ratio = if total_physical > 0 {
        f64::from(total_blank) / f64::from(total_physical) * 100.0
    } else {
        0.0
    };

    let file_count = files.len();
    let avg_sloc_per_file = f64::from(total_sloc) / file_count as f64;

    let mut sloc_values: Vec<u32> = files.iter().map(|f| f.metrics.source).collect();
    sloc_values.sort_unstable();

    let max_sloc = *sloc_values.last().unwrap_or(&0);
    let min_sloc = *sloc_values.first().unwrap_or(&0);
    let median_sloc = if file_count % 2 == 0 && file_count >= 2 {
        (sloc_values[file_count / 2 - 1] + sloc_values[file_count / 2]) / 2
    } else {
        sloc_values.get(file_count / 2).copied().unwrap_or(0)
    };

    let total_functions: usize = files.iter().map(|f| f.functions.len()).sum();
    let total_func_sloc: u32 = files
        .iter()
        .flat_map(|f| f.functions.iter())
        .map(|func| func.sloc)
        .sum();
    let avg_function_size = if total_functions > 0 {
        f64::from(total_func_sloc) / total_functions as f64
    } else {
        0.0
    };

    let oversized_function_count: usize = files.iter().map(|f| f.oversized_functions).sum();

    LOCDistribution {
        total_sloc,
        total_physical,
        total_logical,
        total_comment,
        total_blank,
        code_to_comment_ratio,
        blank_ratio,
        avg_sloc_per_file,
        max_sloc,
        min_sloc,
        median_sloc,
        total_functions,
        avg_function_size,
        oversized_function_count,
    }
}

/// Get largest files by SLOC.
fn get_largest_files(files: &[FileLOC], stats: &LOCDistribution, limit: usize) -> Vec<FileRanking> {
    let mut sorted: Vec<&FileLOC> = files.iter().collect();
    sorted.sort_by(|a, b| b.metrics.source.cmp(&a.metrics.source));

    sorted
        .into_iter()
        .take(limit)
        .map(|f| FileRanking {
            file: f.file.clone(),
            sloc: f.metrics.source,
            percentage: if stats.total_sloc > 0 {
                f64::from(f.metrics.source) / f64::from(stats.total_sloc) * 100.0
            } else {
                0.0
            },
        })
        .collect()
}

// =============================================================================
// TESTS
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    fn create_temp_file(content: &str, extension: &str) -> NamedTempFile {
        let mut file = tempfile::Builder::new()
            .suffix(extension)
            .tempfile()
            .expect("Failed to create temp file");
        file.write_all(content.as_bytes())
            .expect("Failed to write to temp file");
        file
    }

    #[test]
    fn test_loc_metrics_basic() {
        let source = r#"
# Comment
def hello():
    """Docstring"""
    print("Hello")

# Another comment
def world():
    pass
"#;
        let file = create_temp_file(source, ".py");
        let result = analyze_file_loc(file.path(), 50);

        assert!(result.is_ok(), "Analysis should succeed");
        let analysis = result.unwrap();

        // Should have some physical lines
        assert!(analysis.stats.total_physical > 0);
        // Should detect comments
        assert!(analysis.stats.total_comment > 0 || analysis.stats.total_sloc > 0);
    }

    #[test]
    fn test_loc_metrics_empty_file() {
        let source = "";
        let file = create_temp_file(source, ".py");
        let result = analyze_file_loc(file.path(), 50);

        assert!(result.is_ok());
        let analysis = result.unwrap();
        assert_eq!(analysis.stats.total_sloc, 0);
    }

    #[test]
    fn test_loc_metrics_blank_lines() {
        let source = "def foo():\n    pass\n\n\n\ndef bar():\n    pass\n";
        let file = create_temp_file(source, ".py");
        let result = analyze_file_loc(file.path(), 50);

        assert!(result.is_ok());
        let analysis = result.unwrap();
        assert!(analysis.stats.total_blank > 0, "Should detect blank lines");
    }

    #[test]
    fn test_loc_metrics_comments_only() {
        let source = r#"# Line 1
# Line 2
# Line 3
"#;
        let file = create_temp_file(source, ".py");
        let result = analyze_file_loc(file.path(), 50);

        assert!(result.is_ok());
        let analysis = result.unwrap();
        // Most lines should be comments
        assert!(analysis.stats.total_comment >= 2);
    }

    #[test]
    fn test_loc_metrics_multiline_string() {
        let source = r#"
def foo():
    x = """
    This is a
    multi-line
    string
    """
    return x
"#;
        let file = create_temp_file(source, ".py");
        let result = analyze_file_loc(file.path(), 50);

        assert!(result.is_ok());
        let analysis = result.unwrap();
        // The multiline string interior lines should not inflate SLOC excessively
        assert!(analysis.stats.total_sloc > 0);
    }

    #[test]
    fn test_function_size_detection() {
        let source = r#"
def small_function():
    pass

def larger_function():
    x = 1
    y = 2
    z = 3
    return x + y + z
"#;
        let file = create_temp_file(source, ".py");
        let result = analyze_file_loc(file.path(), 50);

        assert!(result.is_ok());
        let analysis = result.unwrap();
        assert!(!analysis.files.is_empty());

        let file_loc = &analysis.files[0];
        assert!(!file_loc.functions.is_empty(), "Should detect functions");
    }

    #[test]
    fn test_typescript_loc() {
        let source = r#"
// Single line comment
function hello(): void {
    console.log("Hello");
}

/*
 * Block comment
 */
const world = () => {
    return "world";
};
"#;
        let file = create_temp_file(source, ".ts");
        let result = analyze_file_loc(file.path(), 50);

        assert!(result.is_ok());
        let analysis = result.unwrap();
        assert!(analysis.stats.total_sloc > 0);
        assert!(analysis.stats.total_comment > 0);
    }

    #[test]
    fn test_rust_loc() {
        let source = r#"
// Comment
fn main() {
    let x = 42;
    println!("{}", x);
}

/* Block comment */
fn helper() -> i32 {
    0
}
"#;
        let file = create_temp_file(source, ".rs");
        let result = analyze_file_loc(file.path(), 50);

        assert!(result.is_ok());
        let analysis = result.unwrap();
        assert!(analysis.stats.total_sloc > 0);
    }

    #[test]
    fn test_code_to_comment_ratio() {
        let metrics = LOCMetrics::from_counts(100, 80, 60, 20, 10);

        // 80 SLOC / 20 CLOC = 4.0
        assert!((metrics.code_to_comment_ratio - 4.0).abs() < 0.001);

        // Comment density: 20/80 * 100 = 25%
        assert!((metrics.comment_density() - 25.0).abs() < 0.001);
    }

    #[test]
    fn test_loc_metrics_go() {
        let source = r#"
package main

// Comment
func main() {
    x := 42
    fmt.Println(x)
}
"#;
        let file = create_temp_file(source, ".go");
        let result = analyze_file_loc(file.path(), 50);

        assert!(result.is_ok());
        let analysis = result.unwrap();
        assert!(analysis.stats.total_sloc > 0);
    }

    #[test]
    fn test_oversized_function_detection() {
        // Create a function with many lines
        let mut lines = vec!["def big_function():".to_string()];
        for i in 0..60 {
            lines.push(format!("    x{} = {}", i, i));
        }
        lines.push("    pass".to_string());
        let source = lines.join("\n");

        let file = create_temp_file(&source, ".py");
        let result = analyze_file_loc(file.path(), 50);

        assert!(result.is_ok());
        let analysis = result.unwrap();

        // Should detect the function as oversized
        assert!(analysis.stats.oversized_function_count > 0 ||
                !analysis.oversized_functions.is_empty() ||
                analysis.files.iter().any(|f| f.oversized_functions > 0));
    }

    #[test]
    fn test_simd_sum_u32_correctness() {
        // Empty slice
        assert_eq!(super::simd_sum_u32(&[]), 0);

        // Single element
        assert_eq!(super::simd_sum_u32(&[42]), 42);

        // Less than 8 elements (scalar path only)
        assert_eq!(super::simd_sum_u32(&[1, 2, 3, 4, 5]), 15);

        // Exactly 8 elements (one SIMD iteration, no remainder)
        assert_eq!(super::simd_sum_u32(&[1, 2, 3, 4, 5, 6, 7, 8]), 36);

        // 16 elements (two SIMD iterations, no remainder)
        let arr16: Vec<u32> = (1..=16).collect();
        assert_eq!(super::simd_sum_u32(&arr16), 136); // sum of 1..=16

        // 17 elements (two SIMD iterations + 1 scalar remainder)
        let arr17: Vec<u32> = (1..=17).collect();
        assert_eq!(super::simd_sum_u32(&arr17), 153); // sum of 1..=17

        // 100 elements (12 SIMD iterations + 4 scalar remainder)
        let arr100: Vec<u32> = (1..=100).collect();
        assert_eq!(super::simd_sum_u32(&arr100), 5050); // sum of 1..=100

        // Large values (verify no overflow issues with wrapping_add)
        let large_arr = vec![u32::MAX / 4; 8];
        let expected = (u32::MAX / 4).wrapping_mul(8);
        assert_eq!(super::simd_sum_u32(&large_arr), expected);
    }

    #[test]
    fn test_aggregate_metrics_collector() {
        // Create mock FileLOC entries
        let files: Vec<super::FileLOC> = (0..20).map(|i| {
            super::FileLOC {
                file: std::path::PathBuf::from(format!("test_{}.py", i)),
                language: Some("python".to_string()),
                metrics: super::LOCMetrics::from_counts(
                    100 + i,    // physical
                    80 + i,     // source
                    60 + i,     // logical
                    10 + i,     // comment
                    10,         // blank (constant)
                ),
                functions: Vec::new(),
                oversized_functions: 0,
            }
        }).collect();

        let collector = super::AggregateMetricsCollector::collect_from_file_locs(&files);
        let (physical, source, logical, comment, blank) = collector.sum_all();

        // Verify sums: sum(100+i for i in 0..20) = 20*100 + sum(0..20) = 2000 + 190 = 2190
        assert_eq!(physical, 2190);
        assert_eq!(source, 1790); // 20*80 + 190
        assert_eq!(logical, 1390); // 20*60 + 190
        assert_eq!(comment, 390);  // 20*10 + 190
        assert_eq!(blank, 200);    // 20*10
    }

    #[test]
    fn test_find_first_nonws_empty() {
        assert_eq!(super::find_first_nonws(b""), None);
    }

    #[test]
    fn test_find_first_nonws_all_whitespace() {
        // Various whitespace combinations
        assert_eq!(super::find_first_nonws(b"   "), None);
        assert_eq!(super::find_first_nonws(b"\t\t\t"), None);
        assert_eq!(super::find_first_nonws(b" \t \n \r "), None);
        assert_eq!(super::find_first_nonws(b"                                "), None); // 32 spaces
    }

    #[test]
    fn test_find_first_nonws_immediate() {
        // First byte is non-whitespace
        assert_eq!(super::find_first_nonws(b"hello"), Some(0));
        assert_eq!(super::find_first_nonws(b"x"), Some(0));
        assert_eq!(super::find_first_nonws(b"#comment"), Some(0));
    }

    #[test]
    fn test_find_first_nonws_with_leading_spaces() {
        assert_eq!(super::find_first_nonws(b"  hello"), Some(2));
        assert_eq!(super::find_first_nonws(b"\t\thello"), Some(2));
        assert_eq!(super::find_first_nonws(b"    code"), Some(4));
    }

    #[test]
    fn test_find_first_nonws_simd_boundary() {
        // Test around 32-byte SIMD boundary
        let mut data = vec![b' '; 31];
        data.push(b'x');
        assert_eq!(super::find_first_nonws(&data), Some(31));

        let mut data = vec![b' '; 32];
        data.push(b'x');
        assert_eq!(super::find_first_nonws(&data), Some(32));

        let mut data = vec![b' '; 33];
        data.push(b'x');
        assert_eq!(super::find_first_nonws(&data), Some(33));

        // First non-ws in second 32-byte chunk
        let mut data = vec![b' '; 40];
        data.push(b'x');
        assert_eq!(super::find_first_nonws(&data), Some(40));
    }

    #[test]
    fn test_find_first_nonws_large_input() {
        // 100 spaces then content (tests multiple SIMD iterations)
        let mut data = vec![b' '; 100];
        data.extend_from_slice(b"content");
        assert_eq!(super::find_first_nonws(&data), Some(100));
    }

    #[test]
    fn test_find_last_nonws_empty() {
        assert_eq!(super::find_last_nonws(b""), None);
    }

    #[test]
    fn test_find_last_nonws_all_whitespace() {
        assert_eq!(super::find_last_nonws(b"   "), None);
        assert_eq!(super::find_last_nonws(b"\t\n\r "), None);
        assert_eq!(super::find_last_nonws(b"                                "), None); // 32 spaces
    }

    #[test]
    fn test_find_last_nonws_immediate() {
        // Last byte is non-whitespace
        assert_eq!(super::find_last_nonws(b"hello"), Some(4));
        assert_eq!(super::find_last_nonws(b"x"), Some(0));
    }

    #[test]
    fn test_find_last_nonws_with_trailing_spaces() {
        assert_eq!(super::find_last_nonws(b"hello  "), Some(4));
        assert_eq!(super::find_last_nonws(b"code\t\t"), Some(3));
        assert_eq!(super::find_last_nonws(b"x   "), Some(0));
    }

    #[test]
    fn test_find_last_nonws_simd_boundary() {
        // Content ends right at various positions relative to 32-byte chunks
        let mut data = b"hello".to_vec();
        data.extend(vec![b' '; 27]); // Total: 32 bytes, last non-ws at 4
        assert_eq!(super::find_last_nonws(&data), Some(4));

        let mut data = b"hello".to_vec();
        data.extend(vec![b' '; 28]); // Total: 33 bytes
        assert_eq!(super::find_last_nonws(&data), Some(4));

        let mut data = b"hello".to_vec();
        data.extend(vec![b' '; 59]); // Total: 64 bytes
        assert_eq!(super::find_last_nonws(&data), Some(4));
    }

    #[test]
    fn test_find_last_nonws_large_input() {
        // Content at start, lots of trailing whitespace
        let mut data = b"content".to_vec();
        data.extend(vec![b' '; 100]);
        assert_eq!(super::find_last_nonws(&data), Some(6)); // 'content' ends at index 6
    }

    #[test]
    fn test_find_nonws_consistency() {
        // Verify first and last are consistent for various inputs
        let test_cases = [
            b"  hello world  ".as_slice(),
            b"\thello\t".as_slice(),
            b"   x   ".as_slice(),
            b"no_whitespace".as_slice(),
        ];

        for input in test_cases {
            let first = super::find_first_nonws(input);
            let last = super::find_last_nonws(input);

            // Both should find something
            assert!(first.is_some(), "first should find non-ws in {:?}", input);
            assert!(last.is_some(), "last should find non-ws in {:?}", input);

            // First should be <= last
            assert!(first.unwrap() <= last.unwrap(),
                "first {} should be <= last {} for {:?}",
                first.unwrap(), last.unwrap(), input);
        }
    }

    #[test]
    fn test_is_ascii_ws() {
        // Whitespace bytes
        assert!(super::is_ascii_ws(b' '));
        assert!(super::is_ascii_ws(b'\t'));
        assert!(super::is_ascii_ws(b'\n'));
        assert!(super::is_ascii_ws(b'\r'));

        // Non-whitespace bytes
        assert!(!super::is_ascii_ws(b'a'));
        assert!(!super::is_ascii_ws(b'0'));
        assert!(!super::is_ascii_ws(b'#'));
        assert!(!super::is_ascii_ws(0));
        assert!(!super::is_ascii_ws(255));
    }
}