iridium-units 0.1.0

A high-performance runtime unit-of-measure library for Rust
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
//! String parsing for units and quantities.
//!
//! This module provides flexible parsing of unit strings from various formats,
//! including technical documents, academic papers, and user input. It supports
//! Unicode symbols, LaTeX notation, natural language ("per"), and parenthesized
//! expressions.
//!
//! # Quick Start
//!
//! ```
//! use iridium_units::prelude::*;
//! use std::str::FromStr;
//!
//! // Parse simple units
//! let meter = Unit::from_str("m").unwrap();
//! let velocity = Unit::from_str("m/s").unwrap();
//!
//! // Parse quantities (value + unit)
//! let distance = Quantity::from_str("100 km").unwrap();
//! let speed = Quantity::from_str("9.8 m/s^2").unwrap();
//!
//! // Unicode and technical formats work too
//! let area = parse_unit("m²").unwrap();           // Unicode superscript
//! let wavelength = parse_unit("µm").unwrap();     // Unicode micro
//! ```
//!
//! # Supported Syntax
//!
//! ## Basic Unit Strings
//!
//! | Format | Examples |
//! |--------|----------|
//! | Simple units | `"m"`, `"meter"`, `"meters"`, `"km"`, `"kg"` |
//! | Powers | `"m^2"`, `"s^-1"`, `"m^1/2"` (fractional) |
//! | Multiplication | `"kg m"`, `"kg*m"`, `"kg.m"` |
//! | Division | `"m/s"`, `"kg/m^3"`, `"erg/cm^2/s"` |
//! | Combined | `"kg m^2 / s^2"`, `"m/s^2"` |
//!
//! ## Unicode Support
//!
//! The parser automatically normalizes Unicode characters:
//!
//! | Unicode | Normalized | Description |
//! |---------|------------|-------------|
//! | `µ`, `μ` | `u` | Micro sign, Greek mu |
//! | `Ω` | `ohm` | Ohm sign, Greek Omega |
//! | `Å` | `angstrom` | Angstrom sign |
//! | `°` | `deg` | Degree symbol |
//! | `′` | `arcmin` | Prime (arc minute) |
//! | `″` | `arcsec` | Double prime (arc second) |
//! | `²`, `³`, etc. | `^2`, `^3` | Superscript digits |
//! | `⁻¹` | `^-1` | Superscript negative |
//! | `·`, `×` | `*` | Multiplication signs |
//! | `÷` | `/` | Division sign |
//!
//! ```
//! # use iridium_units::prelude::*;
//! // All of these work:
//! let _ = parse_unit("m²").unwrap();      // Unicode superscript
//! let _ = parse_unit("s⁻¹").unwrap();     // Negative superscript
//! let _ = parse_unit("µm").unwrap();      // Micro symbol
//! let _ = parse_unit("kg·m").unwrap();    // Middle dot multiplication
//! let _ = parse_unit("Ω").unwrap();       // Ohm symbol
//! ```
//!
//! ## LaTeX Notation
//!
//! Common LaTeX patterns are recognized:
//!
//! | LaTeX | Normalized | Description |
//! |-------|------------|-------------|
//! | `m^{2}` | `m^2` | Braced exponents |
//! | `\cdot` | `*` | Multiplication |
//! | `\times` | `*` | Multiplication |
//! | `\mu` | `u` | Micro prefix |
//! | `\Omega` | `ohm` | Ohm |
//!
//! ```
//! # use iridium_units::prelude::*;
//! let energy = parse_unit("kg m^{2} / s^{2}").unwrap();
//! let product = parse_unit(r"kg \cdot m").unwrap();
//! ```
//!
//! ## Natural Language ("per" notation)
//!
//! The word "per" is converted to division:
//!
//! ```
//! # use iridium_units::prelude::*;
//! let speed = parse_unit("km per hour").unwrap();
//! let rate = parse_unit("m per s").unwrap();
//! ```
//!
//! ## Astrophysical Subscript Notation
//!
//! Common astrophysical subscript patterns are recognized:
//!
//! | Input | Recognized as |
//! |-------|---------------|
//! | `M_sun`, `M_⊙` | Solar mass |
//! | `R_sun`, `R_⊙` | Solar radius |
//! | `L_sun`, `L_⊙` | Solar luminosity |
//! | `M_jup` | Jupiter mass |
//! | `R_jup` | Jupiter radius |
//! | `M_earth`, `M_⊕` | Earth mass |
//! | `R_earth`, `R_⊕` | Earth radius |
//!
//! ```no_run
//! # #[cfg(feature = "astrophysics")]
//! # fn main() {
//! # use iridium_units::prelude::*;
//! let stellar_mass = parse_unit("M_sun").unwrap();
//! let planet_radius = parse_unit("R_jup").unwrap();
//! # }
//! # #[cfg(not(feature = "astrophysics"))]
//! # fn main() {}
//! ```
//!
//! ## Parentheses
//!
//! Parentheses can be used for grouping:
//!
//! ```
//! # use iridium_units::prelude::*;
//! let force = parse_unit("(kg m)/s^2").unwrap();
//! let accel = parse_unit("m/(s^2)").unwrap();
//! let squared_vel = parse_unit("(m/s)^2").unwrap();
//! ```
//!
//! ## Quantity Strings
//!
//! Quantities combine a numeric value with a unit:
//!
//! ```
//! # use iridium_units::prelude::*;
//! let distance = parse_quantity("100 km").unwrap();
//! let speed = parse_quantity("9.8 m/s^2").unwrap();
//! let wavelength = parse_quantity("500 nm").unwrap();
//! let scientific = parse_quantity("1.5e8 m").unwrap();
//! let negative = parse_quantity("-3.14 rad").unwrap();
//! ```
//!
//! # Error Handling and Suggestions
//!
//! When a unit name is not recognized, the parser suggests similar unit names:
//!
//! ```
//! # use iridium_units::prelude::*;
//! let result = parse_unit("metrs");
//! match result {
//!     Err(UnitError::UnknownUnit { name, suggestions }) => {
//!         println!("Unknown unit '{}', did you mean: {:?}?", name, suggestions);
//!         // Output: Unknown unit 'metrs', did you mean: ["meters", "meter"]?
//!     }
//!     _ => {}
//! }
//! ```
//!
//! # Custom Unit Registry
//!
//! For applications that need custom units (e.g., from a database), use [`UnitRegistry`]:
//!
//! ```
//! use iridium_units::prelude::*;
//!
//! // Create a registry with all built-in units
//! let registry = UnitRegistry::with_builtins();
//!
//! // Parse using the registry
//! let unit = registry.parse_unit("m/s").unwrap();
//! let quantity = registry.parse_quantity("100 km").unwrap();
//!
//! // Create a custom registry with additional units
//! let custom_registry = UnitRegistry::with_builtins()
//!     .with_unit(&["custom_length", "cl"], Unit::from(M));
//!
//! let custom = custom_registry.parse_unit("custom_length").unwrap();
//! ```
//!
//! ## Registry API
//!
//! | Method | Description |
//! |--------|-------------|
//! | `new()` | Create empty registry |
//! | `with_builtins()` | Create with all standard units |
//! | `register(&mut self, names, unit)` | Add a unit with aliases |
//! | `with_unit(self, names, unit)` | Builder: add unit |
//! | `lookup(name)` | Look up unit by name |
//! | `parse_unit(s)` | Parse unit string |
//! | `parse_quantity(s)` | Parse quantity string |
//! | `merge(&mut self, other)` | Merge another registry |
//!
//! # Available Unit Names
//!
//! ## SI Units
//!
//! - **Base**: `m`, `s`, `kg`, `a` (ampere), `k` (kelvin), `mol`, `cd`, `rad`, `sr`
//! - **Length**: `km`, `cm`, `mm`, `um`/`µm`, `nm`, `pm`, `fm`
//! - **Time**: `ms`, `us`/`µs`, `ns`, `ps`, `min`, `h`/`hr`, `d`/`day`, `yr`
//! - **Mass**: `g`, `mg`, `ug`/`µg`, `t`/`tonne`
//! - **Frequency**: `hz`, `khz`, `mhz`, `ghz`, `thz`
//! - **Derived**: `n` (newton), `j` (joule), `w` (watt), `pa`, `c` (coulomb), `v`, `f` (farad), `ohm`/`Ω`
//! - **Energy**: `ev`, `kev`, `mev`, `gev`
//! - **Angle**: `deg`/`°`, `arcmin`/`′`, `arcsec`/`″`, `mas`, `uas`
//!
//! ## Astrophysical Units
//!
//! - **Distance**: `au`, `pc`, `kpc`, `mpc`, `gpc`, `ly`/`lightyear`
//! - **Solar**: `msun`/`M_sun`/`solar_mass`, `rsun`/`R_sun`, `lsun`/`L_sun`
//! - **Planetary**: `mjup`/`M_jup`, `rjup`/`R_jup`, `mearth`/`M_earth`, `rearth`/`R_earth`
//! - **Spectroscopic**: `angstrom`/`Å`, `jy` (jansky), `mjy`, `ujy`, `barn`
//! - **CGS**: `erg`, `dyn`, `gauss`
//!
//! ## Imperial Units
//!
//! - **Length**: `in`, `ft`, `yd`, `mi`, `nmi`
//! - **Mass**: `lb`, `oz`, `ton`
//! - **Other**: `psi`, `mph`, `hp`, `btu`, `gal`, `pt`, `qt`

use crate::dimension::Rational16;
use crate::error::{UnitError, UnitResult};
use crate::quantity::Quantity;
use crate::unit::Unit;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::LazyLock;
use std::sync::RwLock;

// ============================================================================
// Levenshtein Distance and Suggestions
// ============================================================================

/// Calculate the Levenshtein distance between two strings.
fn levenshtein_distance(a: &str, b: &str) -> usize {
    let a_chars: Vec<char> = a.chars().collect();
    let b_chars: Vec<char> = b.chars().collect();
    let a_len = a_chars.len();
    let b_len = b_chars.len();

    if a_len == 0 {
        return b_len;
    }
    if b_len == 0 {
        return a_len;
    }

    let mut prev_row: Vec<usize> = (0..=b_len).collect();
    let mut curr_row: Vec<usize> = vec![0; b_len + 1];

    for i in 1..=a_len {
        curr_row[0] = i;
        for j in 1..=b_len {
            let cost = if a_chars[i - 1] == b_chars[j - 1] {
                0
            } else {
                1
            };
            curr_row[j] = (prev_row[j] + 1)
                .min(curr_row[j - 1] + 1)
                .min(prev_row[j - 1] + cost);
        }
        std::mem::swap(&mut prev_row, &mut curr_row);
    }

    prev_row[b_len]
}

/// Find similar unit names for suggestions.
fn find_similar_units(
    name: &str,
    registry: &HashMap<String, UnitEntry>,
    max_suggestions: usize,
) -> Vec<String> {
    let name_lower = name.to_lowercase();
    let threshold = (name_lower.len() / 2).clamp(2, 3);

    let mut candidates: Vec<(String, usize)> = registry
        .keys()
        .filter_map(|key| {
            let dist = levenshtein_distance(&name_lower, key);
            if dist <= threshold {
                Some((key.clone(), dist))
            } else {
                None
            }
        })
        .collect();

    candidates.sort_by_key(|(_, dist)| *dist);
    candidates.truncate(max_suggestions);
    candidates.into_iter().map(|(name, _)| name).collect()
}

// ============================================================================
// Unicode Normalization
// ============================================================================

/// Normalize Unicode characters to ASCII equivalents for parsing.
fn normalize_unicode(s: &str) -> String {
    let mut result = String::with_capacity(s.len() * 2);
    let chars: Vec<char> = s.chars().collect();
    let mut i = 0;

    while i < chars.len() {
        let c = chars[i];
        match c {
            // Greek letters
            'µ' | 'μ' => result.push('u'), // micro sign (U+00B5) and Greek mu (U+03BC)
            '\u{2126}' | '\u{03A9}' => result.push_str("ohm"), // Ohm sign (U+2126) and Greek Omega (U+03A9)
            'α' => result.push_str("alpha"),
            'β' => result.push_str("beta"),
            'γ' => result.push_str("gamma"),
            'δ' => result.push_str("delta"),
            'λ' => result.push_str("lambda"),
            'π' => result.push_str("pi"),

            // Symbols
            '\u{212B}' | '\u{00C5}' => result.push_str("angstrom"), // Angstrom sign (U+212B) and A with ring (U+00C5)
            '°' => result.push_str("deg"),
            '' => result.push_str("arcmin"),
            '' => result.push_str("arcsec"),
            '' => result.push_str("degC"),
            '' => result.push_str("degF"),

            // Superscripts for powers
            '' => result.push_str("^0"),
            '¹' => result.push_str("^1"),
            '²' => result.push_str("^2"),
            '³' => result.push_str("^3"),
            '' => result.push_str("^4"),
            '' => result.push_str("^5"),
            '' => result.push_str("^6"),
            '' => result.push_str("^7"),
            '' => result.push_str("^8"),
            '' => result.push_str("^9"),
            '' => result.push_str("^-"),
            '' => result.push_str("^+"),

            // Fractions
            '½' => result.push_str("^1/2"),
            '' => result.push_str("^1/3"),
            '¼' => result.push_str("^1/4"),
            '' => result.push_str("^2/3"),
            '¾' => result.push_str("^3/4"),

            // Multiplication signs
            '·' | '×' | '' | '' => result.push('*'),

            // Division
            '÷' => result.push('/'),

            _ => result.push(c),
        }
        i += 1;
    }

    // Post-process to merge consecutive power operators
    // e.g., "m^-^2" -> "m^-2" and "m^^2" -> "m^2"
    result
        .replace("^^", "^")
        .replace("^-^", "^-")
        .replace("^+^", "^")
}

// ============================================================================
// LaTeX/Technical Format Support
// ============================================================================

/// Normalize LaTeX-style notation to standard format.
fn normalize_latex(s: &str) -> String {
    let mut result = s.to_string();

    // Handle LaTeX braces in exponents: m^{2} -> m^2
    while let Some(start) = result.find("^{") {
        if let Some(end) = result[start..].find('}') {
            let inner = &result[start + 2..start + end];
            result = format!(
                "{}{}{}",
                &result[..start + 1],
                inner,
                &result[start + end + 1..]
            );
        } else {
            break;
        }
    }

    // Handle LaTeX commands
    result = result.replace(r"\cdot", "*");
    result = result.replace(r"\times", "*");
    result = result.replace(r"\mu", "u");
    result = result.replace(r"\alpha", "alpha");
    result = result.replace(r"\beta", "beta");
    result = result.replace(r"\gamma", "gamma");
    result = result.replace(r"\Omega", "ohm");
    result = result.replace(r"\AA", "angstrom");
    result = result.replace(r"\deg", "deg");
    result = result.replace(r"\prime", "arcmin");
    result = result.replace(r"\arcsec", "arcsec");
    result = result.replace(r"\arcmin", "arcmin");
    result = result.replace(r"\frac{1}{2}", "^1/2");

    result
}

/// Normalize "per" notation to division.
fn normalize_per_notation(s: &str) -> String {
    // Handle various "per" patterns (case insensitive)
    let result = s.to_string();

    // Use regex-like replacement for "per" patterns
    let words: Vec<&str> = result.split_whitespace().collect();
    let mut new_words = Vec::new();
    let mut i = 0;

    while i < words.len() {
        if words[i].eq_ignore_ascii_case("per") && i + 1 < words.len() {
            // Replace "per X" with "/ X"
            new_words.push("/");
            new_words.push(words[i + 1]);
            i += 2;
        } else {
            new_words.push(words[i]);
            i += 1;
        }
    }

    new_words.join(" ")
}

/// Normalize subscript notation for astrophysical units.
fn normalize_subscripts(s: &str) -> String {
    let mut result = s.to_string();

    // Common astrophysical subscript patterns
    let subscript_mappings = [
        ("M_sun", "msun"),
        ("m_sun", "msun"),
        ("M_⊙", "msun"),
        ("R_sun", "rsun"),
        ("r_sun", "rsun"),
        ("R_⊙", "rsun"),
        ("L_sun", "lsun"),
        ("l_sun", "lsun"),
        ("L_⊙", "lsun"),
        ("M_jup", "mjup"),
        ("m_jup", "mjup"),
        ("R_jup", "rjup"),
        ("r_jup", "rjup"),
        ("M_earth", "mearth"),
        ("m_earth", "mearth"),
        ("M_⊕", "mearth"),
        ("R_earth", "rearth"),
        ("r_earth", "rearth"),
        ("R_⊕", "rearth"),
        ("sol_mass", "msun"),
        ("solar_mass", "msun"),
        ("sol_rad", "rsun"),
        ("solar_rad", "rsun"),
        ("sol_lum", "lsun"),
        ("solar_lum", "lsun"),
        ("jup_mass", "mjup"),
        ("jupiter_mass", "mjup"),
        ("jup_rad", "rjup"),
        ("jupiter_rad", "rjup"),
        ("earth_mass", "mearth"),
        ("earth_rad", "rearth"),
    ];

    for (pattern, replacement) in &subscript_mappings {
        // Case-insensitive replacement
        let pattern_lower = pattern.to_lowercase();
        let result_lower = result.to_lowercase();
        if let Some(pos) = result_lower.find(&pattern_lower) {
            let before = &result[..pos];
            let after = &result[pos + pattern.len()..];
            result = format!("{}{}{}", before, replacement, after);
        }
    }

    result
}

// ============================================================================
// Parentheses Support
// ============================================================================

/// Check if parentheses in a string are balanced.
fn check_balanced_parens(s: &str) -> UnitResult<()> {
    let mut depth = 0i32;
    for c in s.chars() {
        match c {
            '(' => depth += 1,
            ')' => {
                depth -= 1;
                if depth < 0 {
                    return Err(UnitError::ParseError(
                        "unbalanced parentheses: unexpected ')'".into(),
                    ));
                }
            }
            _ => {}
        }
    }
    if depth != 0 {
        return Err(UnitError::ParseError(
            "unbalanced parentheses: missing ')'".into(),
        ));
    }
    Ok(())
}

/// Find the position of a top-level division operator (respecting parentheses).
fn find_top_level_division(s: &str) -> Option<usize> {
    let mut depth = 0;
    let mut in_exponent = false;

    for (i, c) in s.char_indices() {
        match c {
            '(' => depth += 1,
            ')' => depth -= 1,
            '^' => in_exponent = true,
            '/' if depth == 0 && !in_exponent => return Some(i),
            _ if c.is_whitespace() && in_exponent => in_exponent = false,
            _ if !c.is_ascii_digit() && c != '-' && c != '+' && c != '/' && in_exponent => {
                in_exponent = false;
            }
            _ => {}
        }
    }
    None
}

/// Parse an expression that may contain parentheses.
fn parse_with_parens(s: &str, registry: &HashMap<String, UnitEntry>) -> UnitResult<Unit> {
    let s = s.trim();
    if s.is_empty() {
        return Ok(Unit::dimensionless());
    }

    check_balanced_parens(s)?;

    // Check for top-level division first
    if let Some(div_pos) = find_top_level_division(s) {
        let numerator = parse_with_parens(&s[..div_pos], registry)?;
        let denominator = parse_with_parens(&s[div_pos + 1..], registry)?;
        return Ok(&numerator / &denominator);
    }

    // Check for parenthesized expression with optional power
    if s.starts_with('(') {
        if let Some(close_pos) = find_matching_paren(s, 0) {
            let inner = &s[1..close_pos];
            let after = &s[close_pos + 1..];

            let inner_unit = parse_with_parens(inner, registry)?;

            // Check for power after closing paren
            if let Some(power_str) = after.strip_prefix('^') {
                let power = parse_power(power_str)?;
                let result = inner_unit.pow(power);
                return Ok(result);
            } else if after.is_empty() {
                return Ok(inner_unit);
            } else {
                // There's more to parse - multiply
                let rest = parse_with_parens(after.trim_start_matches(['*', ' ']), registry)?;
                return Ok(&inner_unit * &rest);
            }
        }
    }

    // No parentheses at this level - parse as product
    parse_unit_product_with_registry(s, registry)
}

/// Find the position of the matching closing parenthesis.
fn find_matching_paren(s: &str, open_pos: usize) -> Option<usize> {
    let chars: Vec<char> = s.chars().collect();
    if chars.get(open_pos) != Some(&'(') {
        return None;
    }

    let mut depth = 1;
    for (i, &c) in chars.iter().enumerate().skip(open_pos + 1) {
        match c {
            '(' => depth += 1,
            ')' => {
                depth -= 1;
                if depth == 0 {
                    return Some(i);
                }
            }
            _ => {}
        }
    }
    None
}

// ============================================================================
// UnitRegistry Struct
// ============================================================================

/// A registry mapping unit names to Unit values.
///
/// The registry provides a way to look up units by name, with support for
/// custom units beyond the built-in set. This is useful for applications
/// that need to populate unit definitions from external sources (e.g., databases).
///
/// # Examples
///
/// ```
/// use iridium_units::prelude::*;
///
/// // Create a registry with built-in units
/// let registry = UnitRegistry::with_builtins();
/// let meter = registry.lookup("m").unwrap();
///
/// // Parse units using the registry
/// let velocity = registry.parse_unit("m/s").unwrap();
/// ```
#[derive(Clone)]
pub struct UnitRegistry {
    entries: HashMap<String, UnitEntry>,
}

impl Default for UnitRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl UnitRegistry {
    /// Create a new empty registry.
    pub fn new() -> Self {
        UnitRegistry {
            entries: HashMap::new(),
        }
    }

    /// Create a new registry with all built-in units registered.
    pub fn with_builtins() -> Self {
        let mut registry = Self::new();
        register_builtin_units(&mut registry.entries);
        register_extended_aliases(&mut registry.entries);
        registry
    }

    /// Register a unit with one or more names.
    pub fn register(&mut self, names: &[&str], unit: Unit) {
        let entry = UnitEntry { unit };
        for name in names {
            self.entries.insert(name.to_lowercase(), entry.clone());
        }
    }

    /// Builder method to register a unit.
    pub fn with_unit(mut self, names: &[&str], unit: Unit) -> Self {
        self.register(names, unit);
        self
    }

    /// Register multiple units at once.
    pub fn register_many(&mut self, units: Vec<(&[&str], Unit)>) {
        for (names, unit) in units {
            self.register(names, unit);
        }
    }

    /// Look up a unit by name.
    pub fn lookup(&self, name: &str) -> Option<Unit> {
        self.entries
            .get(&name.to_lowercase())
            .map(|e| e.unit.clone())
    }

    /// Parse a unit string using this registry.
    pub fn parse_unit(&self, s: &str) -> UnitResult<Unit> {
        parse_unit_with_registry(s, &self.entries)
    }

    /// Parse a quantity string using this registry.
    pub fn parse_quantity(&self, s: &str) -> UnitResult<Quantity> {
        parse_quantity_with_registry(s, &self.entries)
    }

    /// Merge another registry into this one.
    ///
    /// Entries from the other registry will overwrite existing entries
    /// with the same name.
    pub fn merge(&mut self, other: &UnitRegistry) {
        for (name, entry) in &other.entries {
            self.entries.insert(name.clone(), entry.clone());
        }
    }

    /// Get the number of registered unit names.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if the registry is empty.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Get all registered unit names.
    pub fn names(&self) -> Vec<&str> {
        self.entries.keys().map(|s| s.as_str()).collect()
    }
}

/// A registry entry for a unit.
#[derive(Clone)]
struct UnitEntry {
    unit: Unit,
}

/// Global unit registry mapping strings to units.
static UNIT_REGISTRY: LazyLock<RwLock<HashMap<String, UnitEntry>>> = LazyLock::new(|| {
    let mut map = HashMap::new();
    register_builtin_units(&mut map);
    register_extended_aliases(&mut map);
    RwLock::new(map)
});

/// Register all built-in units with the registry.
fn register_builtin_units(map: &mut HashMap<String, UnitEntry>) {
    use crate::systems::imperial::*;
    use crate::systems::si::*;

    // Helper to register a unit with multiple names
    macro_rules! register {
        ($map:expr, $unit:expr, $($name:expr),+) => {
            let entry = UnitEntry { unit: Unit::from($unit) };
            $(
                $map.insert($name.to_lowercase(), entry.clone());
            )+
        };
    }

    // SI Base Units
    register!(map, M, "m", "meter", "meters", "metre", "metres");
    register!(map, S, "s", "sec", "second", "seconds");
    register!(map, KG, "kg", "kilogram", "kilograms");
    register!(map, A, "a", "amp", "ampere", "amperes");
    register!(map, K, "k", "kelvin");
    register!(map, DEG_C, "°c", "degc", "celsius");
    register!(map, DEG_F, "°f", "degf", "fahrenheit");
    register!(map, MOL, "mol", "mole", "moles");
    register!(map, CD, "cd", "candela");
    register!(map, RAD, "rad", "radian", "radians");
    register!(map, SR, "sr", "steradian", "steradians");

    // SI Length
    register!(
        map,
        KM,
        "km",
        "kilometer",
        "kilometers",
        "kilometre",
        "kilometres"
    );
    register!(
        map,
        CM,
        "cm",
        "centimeter",
        "centimeters",
        "centimetre",
        "centimetres"
    );
    register!(
        map,
        MM,
        "mm",
        "millimeter",
        "millimeters",
        "millimetre",
        "millimetres"
    );
    register!(
        map,
        UM,
        "um",
        "micrometer",
        "micrometers",
        "micron",
        "microns"
    );
    register!(map, NM, "nm", "nanometer", "nanometers");
    register!(map, PM, "pm", "picometer", "picometers");
    register!(map, FM, "fm", "femtometer", "femtometers");

    // SI Time
    register!(map, MS, "ms", "millisecond", "milliseconds");
    register!(map, US, "us", "microsecond", "microseconds");
    register!(map, NS, "ns", "nanosecond", "nanoseconds");
    register!(map, PS, "ps", "picosecond", "picoseconds");
    register!(map, MIN, "min", "minute", "minutes");
    register!(map, H, "h", "hr", "hour", "hours");
    register!(map, DAY, "d", "day", "days");
    register!(map, YR, "yr", "year", "years", "julian_year");

    // SI Mass
    register!(map, G, "g", "gram", "grams");
    register!(map, MG, "mg", "milligram", "milligrams");
    register!(map, UG, "ug", "microgram", "micrograms");
    register!(map, TONNE, "t", "tonne", "tonnes", "metric_ton");

    // SI Derived - Frequency
    register!(map, HZ, "hz", "hertz");
    register!(map, KHZ, "khz", "kilohertz");
    register!(map, MHZ, "mhz", "megahertz");
    register!(map, GHZ, "ghz", "gigahertz");
    register!(map, THZ, "thz", "terahertz");

    // SI Derived - Mechanics
    register!(map, N, "n", "newton", "newtons");
    register!(map, J, "j", "joule", "joules");
    register!(map, W, "w", "watt", "watts");
    register!(map, KW, "kw", "kilowatt", "kilowatts");
    register!(map, MW, "mw", "megawatt", "megawatts");
    register!(map, PA, "pa", "pascal", "pascals");

    // SI Derived - Electrical
    register!(map, C, "c", "coulomb", "coulombs");
    register!(map, V, "v", "volt", "volts");
    register!(map, F, "f", "farad", "farads");
    register!(map, OHM, "ohm", "ohms");

    // SI Derived - Energy
    register!(map, EV, "ev", "electronvolt", "electronvolts");
    register!(map, KEV, "kev", "kiloelectronvolt");
    register!(map, MEV, "mev", "megaelectronvolt");
    register!(map, GEV, "gev", "gigaelectronvolt");

    // SI Angles
    register!(map, DEG, "deg", "degree", "degrees");
    register!(map, ARCMIN, "arcmin", "arcminute", "arcminutes");
    register!(map, ARCSEC, "arcsec", "arcsecond", "arcseconds");
    register!(map, MAS, "mas", "milliarcsecond", "milliarcseconds");
    register!(map, UAS, "uas", "microarcsecond", "microarcseconds");

    // Imperial - Length
    register!(map, INCH, "in", "inch", "inches");
    register!(map, FOOT, "ft", "foot", "feet");
    register!(map, YARD, "yd", "yard", "yards");
    register!(map, MILE, "mi", "mile", "miles");
    register!(map, NAUTICAL_MILE, "nmi", "nautical_mile");

    // Imperial - Mass
    register!(map, POUND, "lb", "lbm", "pound", "pounds");
    register!(map, OUNCE, "oz", "ounce", "ounces");
    register!(map, TON, "ton", "tons", "short_ton");

    // Imperial - Volume
    register!(map, GALLON, "gal", "gallon", "gallons");
    register!(map, PINT, "pt", "pint", "pints");
    register!(map, QUART, "qt", "quart", "quarts");

    // Imperial - Other
    register!(map, PSI, "psi");
    register!(map, MPH, "mph");
    register!(map, KNOT, "kn", "kt", "knot", "knots");
    register!(map, HORSEPOWER, "hp", "horsepower");
    register!(map, BTU, "btu");

    #[cfg(feature = "astrophysics")]
    register_astrophysical_units(map);

    #[cfg(feature = "cgs")]
    register_cgs_units(map);
}

#[cfg(feature = "astrophysics")]
fn register_astrophysical_units(map: &mut HashMap<String, UnitEntry>) {
    use crate::systems::astrophysical::{
        ANGSTROM, AU, BARN, DYN, EARTH_MASS, EARTH_RADIUS, ERG, GAUSS, GPC, JANSKY, JUPITER_MASS,
        JUPITER_RADIUS, KPC, LIGHT_YEAR, MJY, MPC, PARSEC, SOLAR_LUMINOSITY, SOLAR_MASS,
        SOLAR_RADIUS, UJY,
    };

    macro_rules! register {
        ($map:expr, $unit:expr, $($name:expr),+) => {
            let entry = UnitEntry { unit: Unit::from($unit) };
            $(
                $map.insert($name.to_lowercase(), entry.clone());
            )+
        };
    }

    // Distance
    register!(map, AU, "au", "astronomical_unit");
    register!(map, PARSEC, "pc", "parsec", "parsecs");
    register!(map, KPC, "kpc", "kiloparsec", "kiloparsecs");
    register!(map, MPC, "mpc", "megaparsec", "megaparsecs");
    register!(map, GPC, "gpc", "gigaparsec", "gigaparsecs");
    register!(
        map,
        LIGHT_YEAR,
        "ly",
        "lyr",
        "lightyear",
        "lightyears",
        "light_year",
        "light_years"
    );

    // Solar
    register!(map, SOLAR_MASS, "m_sun", "msun", "solmass", "solar_mass");
    register!(map, SOLAR_RADIUS, "r_sun", "rsun", "solrad", "solar_radius");
    register!(
        map,
        SOLAR_LUMINOSITY,
        "l_sun",
        "lsun",
        "sollum",
        "solar_luminosity"
    );

    // Planetary
    register!(map, JUPITER_MASS, "m_jup", "mjup", "jupiter_mass");
    register!(map, JUPITER_RADIUS, "r_jup", "rjup", "jupiter_radius");
    register!(map, EARTH_MASS, "m_earth", "mearth", "earth_mass");
    register!(map, EARTH_RADIUS, "r_earth", "rearth", "earth_radius");

    // Spectroscopic
    register!(map, ANGSTROM, "angstrom", "aa");
    register!(map, JANSKY, "jy", "jansky");
    register!(map, MJY, "mjy", "millijansky");
    register!(map, UJY, "ujy", "microjansky");
    register!(map, BARN, "barn", "barns");

    // CGS commonly used in astrophysics
    register!(map, ERG, "erg", "ergs");
    register!(map, DYN, "dyn", "dyne", "dynes");
    register!(map, GAUSS, "gauss");
}

#[cfg(feature = "cgs")]
fn register_cgs_units(map: &mut HashMap<String, UnitEntry>) {
    use crate::systems::cgs::{CENTIMETER, GRAM};

    macro_rules! register {
        ($map:expr, $unit:expr, $($name:expr),+) => {
            let entry = UnitEntry { unit: Unit::from($unit) };
            $(
                $map.insert($name.to_lowercase(), entry.clone());
            )+
        };
    }

    register!(map, CENTIMETER, "centimeter_cgs");
    register!(map, GRAM, "gram_cgs");
}

/// Register extended Unicode and academic aliases.
fn register_extended_aliases(map: &mut HashMap<String, UnitEntry>) {
    use crate::systems::si::*;

    macro_rules! register {
        ($map:expr, $unit:expr, $($name:expr),+) => {
            let entry = UnitEntry { unit: Unit::from($unit) };
            $(
                $map.insert($name.to_lowercase(), entry.clone());
            )+
        };
    }

    // Unicode micro symbol aliases
    register!(map, UM, "µm");
    register!(map, US, "µs");
    register!(map, UG, "µg");

    // Ohm with Unicode
    register!(map, OHM, "ω");

    // Degree symbol
    register!(map, DEG, "°");

    // Arc minute/second with Unicode
    register!(map, ARCMIN, "");
    register!(map, ARCSEC, "");

    #[cfg(feature = "astrophysics")]
    register_astrophysical_aliases(map);
}

#[cfg(feature = "astrophysics")]
fn register_astrophysical_aliases(map: &mut HashMap<String, UnitEntry>) {
    use crate::systems::astrophysical::{
        ANGSTROM, EARTH_MASS, EARTH_RADIUS, JUPITER_MASS, JUPITER_RADIUS, SOLAR_LUMINOSITY,
        SOLAR_MASS, SOLAR_RADIUS,
    };

    macro_rules! register {
        ($map:expr, $unit:expr, $($name:expr),+) => {
            let entry = UnitEntry { unit: Unit::from($unit) };
            $(
                $map.insert($name.to_lowercase(), entry.clone());
            )+
        };
    }

    // Angstrom with Unicode
    register!(map, ANGSTROM, "å");

    // Extended astrophysical aliases
    register!(map, SOLAR_MASS, "m⊙", "solmass", "sol_mass");
    register!(map, SOLAR_RADIUS, "r⊙", "solrad", "sol_rad", "solarradius");
    register!(
        map,
        SOLAR_LUMINOSITY,
        "l⊙",
        "sollum",
        "sol_lum",
        "solarluminosity"
    );
    register!(map, JUPITER_MASS, "m_jupiter", "jupitermass");
    register!(map, JUPITER_RADIUS, "r_jupiter", "jupiterradius");
    register!(map, EARTH_MASS, "m⊕", "earthmass");
    register!(map, EARTH_RADIUS, "r⊕", "earthradius");
}

/// Look up a simple unit by name.
pub fn lookup_unit(name: &str) -> Option<Unit> {
    let registry = UNIT_REGISTRY.read().ok()?;
    registry.get(&name.to_lowercase()).map(|e| e.unit.clone())
}

/// Register a custom unit with the registry.
///
/// This allows adding user-defined units that can be parsed from strings.
pub fn register_unit(names: &[&str], unit: Unit) {
    if let Ok(mut registry) = UNIT_REGISTRY.write() {
        let entry = UnitEntry { unit };
        for name in names {
            registry.insert(name.to_lowercase(), entry.clone());
        }
    }
}

/// Parse a unit string into a Unit.
///
/// Supports:
/// - Simple units: "m", "kg", "s"
/// - Powers: "m^2", "s^-1", "m^1/2"
/// - Multiplication: "kg m", "kg*m"
/// - Division: "m/s", "kg/m^3"
/// - Combined: "kg m^2 / s^2"
/// - Unicode: "m²", "µm", "Ω", "°", "′", "″"
/// - LaTeX: "m^{2}", "\cdot", "\mu"
/// - Per notation: "km per hour"
/// - Subscripts: "M_sun", "R_jup"
/// - Parentheses: "(kg m)/s^2"
pub fn parse_unit(s: &str) -> UnitResult<Unit> {
    let registry = UNIT_REGISTRY
        .read()
        .map_err(|_| UnitError::ParseError("failed to acquire registry lock".into()))?;
    parse_unit_with_registry(s, &registry)
}

/// Parse a unit string using a specific registry.
fn parse_unit_with_registry(s: &str, registry: &HashMap<String, UnitEntry>) -> UnitResult<Unit> {
    let s = s.trim();
    if s.is_empty() {
        return Ok(Unit::dimensionless());
    }

    // Apply normalizations in order
    let normalized = normalize_unicode(s);
    let normalized = normalize_latex(&normalized);
    let normalized = normalize_per_notation(&normalized);
    let normalized = normalize_subscripts(&normalized);

    // Check for parentheses - use the parentheses-aware parser
    if normalized.contains('(') || normalized.contains(')') {
        return parse_with_parens(&normalized, registry);
    }

    // Split by division, but be careful not to split inside exponents
    // Exponents like "m^1/2" should not be split at the "/"
    let parts = split_unit_by_division(&normalized);

    match parts.len() {
        1 => parse_unit_product_with_registry(&parts[0], registry),
        2 => {
            let numerator = parse_unit_product_with_registry(&parts[0], registry)?;
            let denominator = parse_unit_product_with_registry(&parts[1], registry)?;
            Ok(&numerator / &denominator)
        }
        _ => {
            // Multiple divisions: a/b/c = a / (b * c)
            let numerator = parse_unit_product_with_registry(&parts[0], registry)?;
            let mut denominator = parse_unit_product_with_registry(&parts[1], registry)?;
            for part in &parts[2..] {
                let next = parse_unit_product_with_registry(part, registry)?;
                denominator = &denominator * &next;
            }
            Ok(&numerator / &denominator)
        }
    }
}

/// Split a unit string by division, respecting exponent notation.
///
/// This handles cases like "m^1/2" where the "/" is part of the exponent,
/// not a division between units.
fn split_unit_by_division(s: &str) -> Vec<String> {
    let mut parts = Vec::new();
    let mut current = String::new();
    let mut in_exponent = false;

    let chars: Vec<char> = s.chars().collect();
    let mut i = 0;

    while i < chars.len() {
        let c = chars[i];

        if c == '^' {
            in_exponent = true;
            current.push(c);
        } else if c == '/' && !in_exponent {
            // This is a unit division
            parts.push(current.trim().to_string());
            current = String::new();
        } else if c == '/' && in_exponent {
            // Check if this is a fractional power or a unit division
            // It's a fractional power only if followed by a digit (possibly with sign)
            let next_idx = i + 1;
            let is_fraction = if next_idx < chars.len() {
                let next = chars[next_idx];
                next.is_ascii_digit()
                    || (next == '-'
                        && next_idx + 1 < chars.len()
                        && chars[next_idx + 1].is_ascii_digit())
            } else {
                false
            };

            if is_fraction {
                // This is part of a fractional exponent like ^1/2
                current.push(c);
            } else {
                // Exponent has ended, this is a unit division
                in_exponent = false;
                parts.push(current.trim().to_string());
                current = String::new();
            }
        } else if c.is_whitespace() && in_exponent {
            // Exponent ends at whitespace
            in_exponent = false;
            current.push(c);
        } else if !c.is_ascii_digit() && c != '-' && c != '+' && in_exponent {
            // Exponent ends at non-numeric character
            in_exponent = false;
            current.push(c);
        } else {
            current.push(c);
        }

        i += 1;
    }

    if !current.is_empty() {
        parts.push(current.trim().to_string());
    }

    if parts.is_empty() {
        parts.push(String::new());
    }

    parts
}

/// Parse a product of units using a specific registry.
fn parse_unit_product_with_registry(
    s: &str,
    registry: &HashMap<String, UnitEntry>,
) -> UnitResult<Unit> {
    let s = s.trim();
    if s.is_empty() {
        return Ok(Unit::dimensionless());
    }

    // Split by whitespace, *, or . (multiplication separators)
    let tokens: Vec<&str> = s
        .split(|c: char| c.is_whitespace() || c == '*' || c == '.')
        .filter(|t| !t.is_empty())
        .collect();

    if tokens.is_empty() {
        return Ok(Unit::dimensionless());
    }

    let mut result = parse_unit_with_power_registry(tokens[0], registry)?;
    for token in &tokens[1..] {
        let next = parse_unit_with_power_registry(token, registry)?;
        result = &result * &next;
    }

    Ok(result)
}

/// Parse a single unit with optional power using a specific registry.
fn parse_unit_with_power_registry(
    s: &str,
    registry: &HashMap<String, UnitEntry>,
) -> UnitResult<Unit> {
    let s = s.trim();

    // Check for power notation
    if let Some(idx) = s.find('^') {
        let (name, power_str) = s.split_at(idx);
        let power_str = &power_str[1..]; // Skip the '^'

        let power = parse_power(power_str)?;
        let base_unit = lookup_simple_unit_with_registry(name, registry)?;
        Ok(base_unit.pow(power))
    } else if let Some(idx) = s.find("**") {
        // Python-style power notation
        let (name, power_str) = s.split_at(idx);
        let power_str = &power_str[2..]; // Skip the '**'

        let power = parse_power(power_str)?;
        let base_unit = lookup_simple_unit_with_registry(name, registry)?;
        Ok(base_unit.pow(power))
    } else {
        lookup_simple_unit_with_registry(s, registry)
    }
}

/// Parse a power exponent (integer or fraction)
fn parse_power(s: &str) -> UnitResult<Rational16> {
    let s = s.trim();

    // Check for fraction notation (e.g., "1/2")
    if let Some(idx) = s.find('/') {
        let (num_str, den_str) = s.split_at(idx);
        let den_str = &den_str[1..];

        let num: i16 = num_str
            .trim()
            .parse()
            .map_err(|_| UnitError::ParseError(format!("invalid power numerator: {}", num_str)))?;
        let den: i16 = den_str.trim().parse().map_err(|_| {
            UnitError::ParseError(format!("invalid power denominator: {}", den_str))
        })?;

        if den == 0 {
            return Err(UnitError::ParseError(
                "power denominator cannot be zero".into(),
            ));
        }

        Ok(Rational16::new(num, den))
    } else {
        // Simple integer power
        let exp: i16 = s
            .parse()
            .map_err(|_| UnitError::ParseError(format!("invalid power: {}", s)))?;
        Ok(Rational16::new(exp, 1))
    }
}

/// Look up a simple unit name using a specific registry.
fn lookup_simple_unit_with_registry(
    name: &str,
    registry: &HashMap<String, UnitEntry>,
) -> UnitResult<Unit> {
    let name = name.trim();
    let name_lower = name.to_lowercase();

    if let Some(entry) = registry.get(&name_lower) {
        return Ok(entry.unit.clone());
    }

    // Unit not found - provide helpful suggestions
    let suggestions = find_similar_units(name, registry, 3);
    Err(UnitError::UnknownUnit {
        name: name.to_string(),
        suggestions,
    })
}

/// Parse a quantity string (value + unit).
///
/// Format: `<value> <unit>`
///
/// Examples:
/// - "5.0 km"
/// - "100 m/s"
/// - "9.8 m/s^2"
/// - "1.5e8 m"
/// - "-3.14 rad"
pub fn parse_quantity(s: &str) -> UnitResult<Quantity> {
    let registry = UNIT_REGISTRY
        .read()
        .map_err(|_| UnitError::ParseError("failed to acquire registry lock".into()))?;
    parse_quantity_with_registry(s, &registry)
}

/// Parse a quantity string using a specific registry.
fn parse_quantity_with_registry(
    s: &str,
    registry: &HashMap<String, UnitEntry>,
) -> UnitResult<Quantity> {
    let s = s.trim();

    // Find where the number ends and the unit begins
    // Numbers can contain: digits, '.', 'e', 'E', '+', '-'
    let mut unit_start = 0;
    let mut in_exponent = false;

    for (i, c) in s.char_indices() {
        if c == 'e' || c == 'E' {
            in_exponent = true;
            continue;
        }

        if in_exponent && (c == '+' || c == '-') {
            in_exponent = false;
            continue;
        }

        if c.is_ascii_digit() || c == '.' || c == '-' || c == '+' {
            continue;
        }

        // Found a non-number character
        if c.is_whitespace() {
            unit_start = i;
            break;
        } else {
            // Unit starts immediately after number (e.g., "5km")
            unit_start = i;
            break;
        }
    }

    if unit_start == 0 {
        // No unit found, try parsing whole string as number
        return Err(UnitError::ParseError(format!(
            "cannot parse quantity: no unit found in '{}'",
            s
        )));
    }

    let (value_str, unit_str) = s.split_at(unit_start);
    let value_str = value_str.trim();
    let unit_str = unit_str.trim();

    let value: f64 = value_str
        .parse()
        .map_err(|_| UnitError::ParseError(format!("invalid number: '{}'", value_str)))?;

    let unit = parse_unit_with_registry(unit_str, registry)?;

    Ok(Quantity::new(value, unit))
}

// Implement FromStr for Unit
impl FromStr for Unit {
    type Err = UnitError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        parse_unit(s)
    }
}

// Implement FromStr for Quantity
impl FromStr for Quantity {
    type Err = UnitError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        parse_quantity(s)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::systems::si::{H, KG, KM, M, S};

    #[test]
    fn test_lookup_simple_unit() {
        let m = lookup_unit("m").unwrap();
        assert_eq!(m.symbol(), "m");

        let meter = lookup_unit("meter").unwrap();
        assert_eq!(meter.symbol(), "m");

        let meters = lookup_unit("meters").unwrap();
        assert_eq!(meters.symbol(), "m");
    }

    #[test]
    fn test_lookup_case_insensitive() {
        let m1 = lookup_unit("M").unwrap();
        let m2 = lookup_unit("m").unwrap();
        let m3 = lookup_unit("METER").unwrap();

        assert_eq!(m1.dimension(), m2.dimension());
        assert_eq!(m2.dimension(), m3.dimension());
    }

    #[test]
    fn test_parse_simple_unit() {
        let m = parse_unit("m").unwrap();
        assert_eq!(m.dimension(), M.dimension());

        let km = parse_unit("km").unwrap();
        assert_eq!(km.dimension(), KM.dimension());
    }

    #[test]
    fn test_parse_unit_with_power() {
        let m2 = parse_unit("m^2").unwrap();
        let dim = m2.dimension();
        assert_eq!(dim.length, Rational16::new(2, 1));

        let s_inv = parse_unit("s^-1").unwrap();
        let dim = s_inv.dimension();
        assert_eq!(dim.time, Rational16::new(-1, 1));
    }

    #[test]
    fn test_parse_unit_division() {
        let velocity = parse_unit("m/s").unwrap();
        let dim = velocity.dimension();
        assert_eq!(dim.length, Rational16::ONE);
        assert_eq!(dim.time, Rational16::new(-1, 1));
    }

    #[test]
    fn test_parse_unit_product() {
        let momentum = parse_unit("kg m").unwrap();
        let dim = momentum.dimension();
        assert_eq!(dim.mass, Rational16::ONE);
        assert_eq!(dim.length, Rational16::ONE);

        // With asterisk
        let momentum2 = parse_unit("kg*m").unwrap();
        assert_eq!(momentum2.dimension(), momentum.dimension());
    }

    #[test]
    fn test_parse_complex_unit() {
        // Energy: kg m^2 / s^2
        let energy = parse_unit("kg m^2 / s^2").unwrap();
        let dim = energy.dimension();
        assert_eq!(dim.mass, Rational16::ONE);
        assert_eq!(dim.length, Rational16::new(2, 1));
        assert_eq!(dim.time, Rational16::new(-2, 1));
    }

    #[test]
    fn test_parse_acceleration() {
        let accel = parse_unit("m/s^2").unwrap();
        let dim = accel.dimension();
        assert_eq!(dim.length, Rational16::ONE);
        assert_eq!(dim.time, Rational16::new(-2, 1));
    }

    #[test]
    fn test_parse_quantity_simple() {
        let q = parse_quantity("100 km").unwrap();
        assert!((q.value() - 100.0).abs() < 1e-10);
        assert_eq!(q.unit().dimension(), KM.dimension());
    }

    #[test]
    fn test_parse_quantity_velocity() {
        let q = parse_quantity("10 m/s").unwrap();
        assert!((q.value() - 10.0).abs() < 1e-10);
        let dim = q.unit().dimension();
        assert_eq!(dim.length, Rational16::ONE);
        assert_eq!(dim.time, Rational16::new(-1, 1));
    }

    #[test]
    fn test_parse_quantity_scientific() {
        let q = parse_quantity("1.5e8 m").unwrap();
        assert!((q.value() - 1.5e8).abs() < 1.0);
    }

    #[test]
    fn test_parse_quantity_negative() {
        let q = parse_quantity("-3.14 rad").unwrap();
        assert!((q.value() - (-3.14)).abs() < 1e-10);
    }

    #[test]
    fn test_unit_from_str() {
        let m: Unit = "m".parse().unwrap();
        assert_eq!(m.dimension(), M.dimension());

        let velocity: Unit = "km/h".parse().unwrap();
        let expected_dim = (KM / H).dimension();
        assert_eq!(velocity.dimension(), expected_dim);
    }

    #[test]
    fn test_quantity_from_str() {
        let q: Quantity = "100 km".parse().unwrap();
        assert!((q.value() - 100.0).abs() < 1e-10);
    }

    #[test]
    fn test_unknown_unit_error() {
        let result = parse_unit("foo");
        assert!(matches!(result, Err(UnitError::UnknownUnit { .. })));
    }

    #[test]
    fn test_unknown_unit_with_suggestions() {
        let result = parse_unit("metrs");
        match result {
            Err(UnitError::UnknownUnit { name, suggestions }) => {
                assert_eq!(name, "metrs");
                // Should suggest "meters" or similar
                assert!(!suggestions.is_empty());
            }
            _ => panic!("Expected UnknownUnit error"),
        }
    }

    #[cfg(feature = "astrophysics")]
    #[test]
    fn test_astrophysical_units() {
        let pc = parse_unit("pc").unwrap();
        let au = parse_unit("AU").unwrap();
        let ly = parse_unit("ly").unwrap();

        // All are length units
        assert_eq!(pc.dimension(), M.dimension());
        assert_eq!(au.dimension(), M.dimension());
        assert_eq!(ly.dimension(), M.dimension());
    }

    #[test]
    fn test_imperial_units() {
        let ft = parse_unit("ft").unwrap();
        let mi = parse_unit("mi").unwrap();
        let lb = parse_unit("lb").unwrap();

        assert_eq!(ft.dimension(), M.dimension());
        assert_eq!(mi.dimension(), M.dimension());
        assert_eq!(lb.dimension(), KG.dimension());
    }

    #[test]
    fn test_dimensionless() {
        let d = parse_unit("").unwrap();
        assert!(d.is_dimensionless());
    }

    #[test]
    fn test_fractional_power() {
        let sqrt_m = parse_unit("m^1/2").unwrap();
        let dim = sqrt_m.dimension();
        assert_eq!(dim.length, Rational16::new(1, 2));
    }

    // ========================================================================
    // Unicode Parsing Tests
    // ========================================================================

    #[test]
    fn test_unicode_superscript_power() {
        let m2 = parse_unit("").unwrap();
        let dim = m2.dimension();
        assert_eq!(dim.length, Rational16::new(2, 1));

        let m3 = parse_unit("").unwrap();
        let dim = m3.dimension();
        assert_eq!(dim.length, Rational16::new(3, 1));
    }

    #[test]
    fn test_unicode_negative_power() {
        let s_inv = parse_unit("s⁻¹").unwrap();
        let dim = s_inv.dimension();
        assert_eq!(dim.time, Rational16::new(-1, 1));

        let accel = parse_unit("m/s²").unwrap();
        let dim = accel.dimension();
        assert_eq!(dim.length, Rational16::ONE);
        assert_eq!(dim.time, Rational16::new(-2, 1));
    }

    #[test]
    fn test_unicode_micro() {
        let um = parse_unit("µm").unwrap();
        assert_eq!(um.dimension(), M.dimension());
    }

    #[test]
    fn test_unicode_multiplication() {
        let momentum = parse_unit("kg·m").unwrap();
        let dim = momentum.dimension();
        assert_eq!(dim.mass, Rational16::ONE);
        assert_eq!(dim.length, Rational16::ONE);

        let momentum2 = parse_unit("kg×m").unwrap();
        assert_eq!(momentum2.dimension(), momentum.dimension());
    }

    #[test]
    fn test_unicode_division() {
        let velocity = parse_unit("m÷s").unwrap();
        let dim = velocity.dimension();
        assert_eq!(dim.length, Rational16::ONE);
        assert_eq!(dim.time, Rational16::new(-1, 1));
    }

    // ========================================================================
    // LaTeX Format Tests
    // ========================================================================

    #[test]
    fn test_latex_braces() {
        let m2 = parse_unit("m^{2}").unwrap();
        let dim = m2.dimension();
        assert_eq!(dim.length, Rational16::new(2, 1));

        let energy = parse_unit("kg m^{2} / s^{2}").unwrap();
        let dim = energy.dimension();
        assert_eq!(dim.mass, Rational16::ONE);
        assert_eq!(dim.length, Rational16::new(2, 1));
        assert_eq!(dim.time, Rational16::new(-2, 1));
    }

    #[test]
    fn test_latex_cdot() {
        let momentum = parse_unit(r"kg \cdot m").unwrap();
        let dim = momentum.dimension();
        assert_eq!(dim.mass, Rational16::ONE);
        assert_eq!(dim.length, Rational16::ONE);
    }

    #[test]
    fn test_latex_times() {
        let area = parse_unit(r"m \times m").unwrap();
        let dim = area.dimension();
        assert_eq!(dim.length, Rational16::new(2, 1));
    }

    // ========================================================================
    // Per Notation Tests
    // ========================================================================

    #[test]
    fn test_per_notation() {
        let velocity = parse_unit("km per hour").unwrap();
        let dim = velocity.dimension();
        assert_eq!(dim.length, Rational16::ONE);
        assert_eq!(dim.time, Rational16::new(-1, 1));

        let velocity2 = parse_unit("m per s").unwrap();
        assert_eq!(velocity2.dimension(), (M / S).dimension());
    }

    #[test]
    fn test_per_notation_case_insensitive() {
        let v1 = parse_unit("km PER hour").unwrap();
        let v2 = parse_unit("km Per hour").unwrap();
        assert_eq!(v1.dimension(), v2.dimension());
    }

    // ========================================================================
    // Subscript/Astrophysical Notation Tests
    // ========================================================================

    #[cfg(feature = "astrophysics")]
    #[test]
    fn test_subscript_solar() {
        let msun = parse_unit("M_sun").unwrap();
        assert_eq!(msun.dimension(), KG.dimension());

        let rsun = parse_unit("R_sun").unwrap();
        assert_eq!(rsun.dimension(), M.dimension());
    }

    #[cfg(feature = "astrophysics")]
    #[test]
    fn test_subscript_planetary() {
        let mjup = parse_unit("M_jup").unwrap();
        assert_eq!(mjup.dimension(), KG.dimension());

        let mearth = parse_unit("M_earth").unwrap();
        assert_eq!(mearth.dimension(), KG.dimension());
    }

    // ========================================================================
    // Parentheses Support Tests
    // ========================================================================

    #[test]
    fn test_parentheses_simple() {
        let force = parse_unit("(kg m)/s^2").unwrap();
        let dim = force.dimension();
        assert_eq!(dim.mass, Rational16::ONE);
        assert_eq!(dim.length, Rational16::ONE);
        assert_eq!(dim.time, Rational16::new(-2, 1));
    }

    #[test]
    fn test_parentheses_denominator() {
        let unit = parse_unit("m/(s^2)").unwrap();
        let dim = unit.dimension();
        assert_eq!(dim.length, Rational16::ONE);
        assert_eq!(dim.time, Rational16::new(-2, 1));
    }

    #[test]
    fn test_parentheses_with_power() {
        let unit = parse_unit("(m/s)^2").unwrap();
        let dim = unit.dimension();
        assert_eq!(dim.length, Rational16::new(2, 1));
        assert_eq!(dim.time, Rational16::new(-2, 1));
    }

    #[test]
    fn test_parentheses_complex() {
        let unit = parse_unit("(kg m^2)/(s^2)").unwrap();
        let dim = unit.dimension();
        assert_eq!(dim.mass, Rational16::ONE);
        assert_eq!(dim.length, Rational16::new(2, 1));
        assert_eq!(dim.time, Rational16::new(-2, 1));
    }

    #[test]
    fn test_unbalanced_parens_error() {
        let result = parse_unit("(m/s");
        assert!(result.is_err());

        let result = parse_unit("m/s)");
        assert!(result.is_err());
    }

    // ========================================================================
    // UnitRegistry Tests
    // ========================================================================

    #[test]
    fn test_registry_new() {
        let registry = UnitRegistry::new();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn test_registry_with_builtins() {
        let registry = UnitRegistry::with_builtins();
        assert!(!registry.is_empty());

        let m = registry.lookup("m").unwrap();
        assert_eq!(m.dimension(), M.dimension());

        let km = registry.lookup("km").unwrap();
        assert_eq!(km.dimension(), KM.dimension());
    }

    #[test]
    fn test_registry_register() {
        let mut registry = UnitRegistry::new();
        registry.register(&["custom", "cust"], Unit::from(M));

        let custom = registry.lookup("custom").unwrap();
        assert_eq!(custom.dimension(), M.dimension());

        let cust = registry.lookup("cust").unwrap();
        assert_eq!(cust.dimension(), M.dimension());
    }

    #[test]
    fn test_registry_builder_pattern() {
        let registry = UnitRegistry::new()
            .with_unit(&["custom1"], Unit::from(M))
            .with_unit(&["custom2", "c2"], Unit::from(KG));

        assert!(registry.lookup("custom1").is_some());
        assert!(registry.lookup("custom2").is_some());
        assert!(registry.lookup("c2").is_some());
    }

    #[test]
    fn test_registry_parse_unit() {
        let registry = UnitRegistry::with_builtins();

        let velocity = registry.parse_unit("m/s").unwrap();
        assert_eq!(velocity.dimension(), (M / S).dimension());

        let energy = registry.parse_unit("kg m^2 / s^2").unwrap();
        let dim = energy.dimension();
        assert_eq!(dim.mass, Rational16::ONE);
        assert_eq!(dim.length, Rational16::new(2, 1));
        assert_eq!(dim.time, Rational16::new(-2, 1));
    }

    #[test]
    fn test_registry_parse_quantity() {
        let registry = UnitRegistry::with_builtins();

        let q = registry.parse_quantity("100 km").unwrap();
        assert!((q.value() - 100.0).abs() < 1e-10);
        assert_eq!(q.unit().dimension(), KM.dimension());
    }

    #[test]
    fn test_registry_merge() {
        let mut registry1 = UnitRegistry::new();
        registry1.register(&["unit1"], Unit::from(M));

        let mut registry2 = UnitRegistry::new();
        registry2.register(&["unit2"], Unit::from(KG));

        registry1.merge(&registry2);

        assert!(registry1.lookup("unit1").is_some());
        assert!(registry1.lookup("unit2").is_some());
    }

    #[test]
    fn test_registry_names() {
        let mut registry = UnitRegistry::new();
        registry.register(&["a", "b", "c"], Unit::from(M));

        let names = registry.names();
        assert_eq!(names.len(), 3);
        assert!(names.contains(&"a"));
        assert!(names.contains(&"b"));
        assert!(names.contains(&"c"));
    }

    // ========================================================================
    // Levenshtein Distance Tests
    // ========================================================================

    #[test]
    fn test_levenshtein_distance_identical() {
        assert_eq!(levenshtein_distance("meter", "meter"), 0);
    }

    #[test]
    fn test_levenshtein_distance_one_char() {
        assert_eq!(levenshtein_distance("meter", "meters"), 1);
        assert_eq!(levenshtein_distance("metr", "meter"), 1);
    }

    #[test]
    fn test_levenshtein_distance_swap() {
        assert_eq!(levenshtein_distance("metrs", "meters"), 1);
    }

    #[test]
    fn test_levenshtein_distance_empty() {
        assert_eq!(levenshtein_distance("", "meter"), 5);
        assert_eq!(levenshtein_distance("meter", ""), 5);
    }

    // ========================================================================
    // Normalization Function Tests
    // ========================================================================

    #[test]
    fn test_normalize_unicode() {
        assert_eq!(normalize_unicode(""), "m^2");
        assert_eq!(normalize_unicode("s⁻¹"), "s^-1");
        assert_eq!(normalize_unicode("kg·m"), "kg*m");
        assert_eq!(normalize_unicode("µm"), "um");
    }

    #[test]
    fn test_normalize_latex() {
        assert_eq!(normalize_latex("m^{2}"), "m^2");
        assert_eq!(normalize_latex(r"kg \cdot m"), "kg * m");
        assert_eq!(normalize_latex(r"\mu m"), "u m");
    }

    #[test]
    fn test_normalize_per_notation() {
        assert_eq!(normalize_per_notation("km per hour"), "km / hour");
        assert_eq!(normalize_per_notation("m per s"), "m / s");
    }

    #[test]
    fn test_normalize_subscripts() {
        let result = normalize_subscripts("M_sun");
        assert_eq!(result, "msun");
    }

    // ========================================================================
    // Combined/Integration Tests
    // ========================================================================

    #[cfg(feature = "astrophysics")]
    #[test]
    fn test_astrophysical_flux_unit() {
        let flux = parse_unit("erg/cm^2/s").unwrap();
        let dim = flux.dimension();
        // Energy / area / time = mass * length^2 / time^2 / length^2 / time
        // = mass / time^3
        assert_eq!(dim.mass, Rational16::ONE);
        assert_eq!(dim.time, Rational16::new(-3, 1));
    }

    #[cfg(feature = "astrophysics")]
    #[test]
    fn test_unicode_astrophysical() {
        let flux = parse_unit("erg/cm²/s").unwrap();
        let dim = flux.dimension();
        assert_eq!(dim.mass, Rational16::ONE);
        assert_eq!(dim.time, Rational16::new(-3, 1));
    }

    #[test]
    fn test_mixed_formats() {
        // Unicode superscript with division
        let accel = parse_unit("m·s⁻²").unwrap();
        let dim = accel.dimension();
        assert_eq!(dim.length, Rational16::ONE);
        assert_eq!(dim.time, Rational16::new(-2, 1));
    }
}