vb6runtime 0.2.0

VB6 runtime library - value system, type conversions, and standard library implementations
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
//! # `Format$` Function
//!
//! Returns a `String` formatted according to instructions contained in a format expression.
//!
//! ## Syntax
//!
//! ```vb6
//! Format$(expression[, format[, firstdayofweek[, firstweekofyear]]])
//! ```
//!
//! ## Parameters
//!
//! - `expression`: Required. Any valid expression.
//! - `format`: Optional. A valid named or user-defined format expression.
//! - `firstdayofweek`: Optional. A constant that specifies the first day of the week.
//! - `firstweekofyear`: Optional. A constant that specifies the first week of the year.
//!
//! ## Return Value
//!
//! Returns a `String` containing the formatted representation of the expression. If `format` is omitted, `Format$` returns a string similar to `Str$`.
//!
//! ## Remarks
//!
//! The `Format$` function is one of the most versatile functions in VB6, allowing you to format numbers, dates, times, and strings according to predefined or custom format expressions.
//!
//! You can use one of the predefined named formats or create user-defined formats with special characters that specify how the value should be displayed.
//!
//! ### Named Numeric Formats
//! - `General Number`: Display number with no thousand separator
//! - `Currency`: Display number with thousand separator and two decimal places
//! - `Fixed`: Display at least one digit to the left and two digits to the right of decimal
//! - `Standard`: Display number with thousand separator
//! - `Percent`: Display number multiplied by 100 with percent sign
//! - `Scientific`: Use standard scientific notation
//! - `Yes/No`: Display No if number is 0; otherwise display Yes
//! - `True/False`: Display False if number is 0; otherwise display True
//! - `On/Off`: Display Off if number is 0; otherwise display On
//!
//! ### Named Date/Time Formats
//! - `General Date`: Display date and/or time
//! - `Long Date`: Display date according to long date format
//! - `Medium Date`: Display date using medium date format
//! - `Short Date`: Display date using short date format
//! - `Long Time`: Display time using long time format (includes hours, minutes, seconds)
//! - `Medium Time`: Display time in 12-hour format using hours and minutes and AM/PM
//! - `Short Time`: Display time using 24-hour format (hh:mm)
//!
//! ### User-Defined Number Format Characters
//! - `0`: Digit placeholder. Display digit or zero
//! - `#`: Digit placeholder. Display digit or nothing
//! - `.`: Decimal placeholder
//! - `%`: Percentage placeholder
//! - `,`: Thousand separator
//! - `E- E+ e- e+`: Scientific notation
//! - `- + $ ( )`: Display literal character
//! - `\`: Display next character as literal
//!
//! ### User-Defined Date/Time Format Characters
//! - `c`: Display date as `ddddd` and time as `ttttt`
//! - `d`: Display day as number without leading zero (1-31)
//! - `dd`: Display day as number with leading zero (01-31)
//! - `ddd`: Display day as abbreviation (Sun-Sat)
//! - `dddd`: Display day as full name (Sunday-Saturday)
//! - `m`: Display month as number without leading zero (1-12)
//! - `mm`: Display month as number with leading zero (01-12)
//! - `mmm`: Display month as abbreviation (Jan-Dec)
//! - `mmmm`: Display month as full name (January-December)
//! - `yy`: Display year as 2-digit number (00-99)
//! - `yyyy`: Display year as 4-digit number (100-9999)
//! - `h`: Display hour as number without leading zero (0-23)
//! - `hh`: Display hour as number with leading zero (00-23)
//! - `n`: Display minute as number without leading zero (0-59)
//! - `nn`: Display minute as number with leading zero (00-59)
//! - `s`: Display second as number without leading zero (0-59)
//! - `ss`: Display second as number with leading zero (00-59)
//! - `AM/PM`: Use 12-hour clock and display uppercase AM/PM
//!
//! ### User-Defined String Format Characters
//! - `@`: Character placeholder. Display character or space
//! - `&`: Character placeholder. Display character or nothing
//! - `<`: Force lowercase
//! - `>`: Force uppercase
//!
//! ## Typical Uses
//!
//! ### Example 1: Formatting Currency
//! ```vb6
//! Dim amount As Double
//! amount = 1234.56
//! Text1.Text = Format$(amount, "Currency")  ' "$1,234.56"
//! ```
//!
//! ### Example 2: Custom Number Format
//! ```vb6
//! Dim value As Double
//! value = 1234.5
//! result = Format$(value, "0000.00")  ' "1234.50"
//! ```
//!
//! ### Example 3: Date Formatting
//! ```vb6
//! Dim today As Date
//! today = Now
//! dateStr = Format$(today, "Long Date")
//! ```
//!
//! ### Example 4: Custom Date Format
//! ```vb6
//! dateStr = Format$(Now, "yyyy-mm-dd")  ' "2024-01-15"
//! ```
//!
//! ## Common Usage Patterns
//!
//! ### Formatting as Percentage
//! ```vb6
//! Dim rate As Double
//! rate = 0.075
//! display = Format$(rate, "0.00%")  ' "7.50%"
//! ```
//!
//! ### Zero-Padded Numbers
//! ```vb6
//! Dim id As Integer
//! id = 42
//! idStr = Format$(id, "000000")  ' "000042"
//! ```
//!
//! ### Phone Number Formatting
//! ```vb6
//! Dim phone As String
//! phone = "5551234567"
//! formatted = Format$(phone, "(@@@) @@@-@@@@")  ' "(555) 123-4567"
//! ```
//!
//! ### Time Formatting
//! ```vb6
//! Dim currentTime As Date
//! currentTime = Now
//! timeStr = Format$(currentTime, "hh:nn:ss AM/PM")
//! ```
//!
//! ### Scientific Notation
//! ```vb6
//! Dim bigNum As Double
//! bigNum = 12345678
//! sciStr = Format$(bigNum, "0.00E+00")  ' "1.23E+07"
//! ```
//!
//! ### File Timestamp
//! ```vb6
//! filename = "backup_" & Format$(Now, "yyyymmdd_hhnnss") & ".dat"
//! ```
//!
//! ### Accounting Format
//! ```vb6
//! balance = Format$(amount, "#,##0.00;(#,##0.00)")
//! ' Positive: "1,234.56"
//! ' Negative: "(1,234.56)"
//! ```
//!
//! ### Leading Zeros for Dates
//! ```vb6
//! monthStr = Format$(Month(Date), "00")  ' "01" to "12"
//! dayStr = Format$(Day(Date), "00")      ' "01" to "31"
//! ```
//!
//! ### Conditional Formatting
//! ```vb6
//! ' Format: positive;negative;zero
//! result = Format$(value, "+0.00;-0.00;Zero")
//! ```
//!
//! ### Uppercase/Lowercase Conversion
//! ```vb6
//! upperName = Format$("john doe", ">")      ' "JOHN DOE"
//! lowerName = Format$("JOHN DOE", "<")      ' "john doe"
//! ```
//!
//! ## Related Functions
//!
//! - `Format`: Variant version of `Format$`
//! - `Str$`: Converts a number to a string
//! - `CStr`: Converts an expression to a string
//! - `FormatNumber`: Formats a number with specific options
//! - `FormatCurrency`: Formats a number as currency
//! - `FormatDateTime`: Formats a date/time value
//! - `FormatPercent`: Formats a number as a percentage
//!
//! ## Best Practices
//!
//! 1. Use named formats for common formatting tasks (clearer intent)
//! 2. Cache format strings if using the same format repeatedly
//! 3. Test custom format strings with edge cases (zero, negative, very large/small)
//! 4. Use `@` instead of `&` in string formats when you want spaces preserved
//! 5. Remember that `m` vs `mm` depends on context (month vs minute)
//! 6. Use four-digit years (`yyyy`) to avoid Y2K-style issues
//! 7. Consider locale settings when using named formats
//! 8. Use semicolons to specify different formats for positive, negative, and zero
//! 9. Escape literal characters with backslash or quotes when needed
//! 10. Be aware that `Format$` returns a string - convert back if needed
//!
//! ## Performance Considerations
//!
//! - Named formats are slightly faster than complex user-defined formats
//! - Avoid calling `Format$` in tight loops if possible (cache results)
//! - For simple zero-padding, `String$` + `Right$` may be faster
//! - `Format$` is slower than simple string concatenation
//! - Consider using `FormatNumber`, `FormatCurrency`, etc. for specific tasks
//!
//! ## Locale Considerations
//!
//! | Aspect | Behavior |
//! |--------|----------|
//! | Currency Symbol | Uses system locale currency symbol |
//! | Decimal Separator | Uses locale decimal separator (. or ,) |
//! | Thousand Separator | Uses locale thousand separator |
//! | Date Format | Named date formats use locale settings |
//! | Day/Month Names | Uses locale language for names |
//! | AM/PM Designators | Uses locale AM/PM strings |
//! | First Day of Week | Can be overridden with parameter |
//! | First Week of Year | Can be overridden with parameter |
//!
//! ## Common Pitfalls
//!
//! - Using `m` for minutes instead of `n` (m means month)
//! - Forgetting that `Format$` always returns a string
//! - Not escaping literal characters in format strings
//! - Assuming `#` and `0` behave the same (they don't)
//! - Using comma as decimal separator in code (always use period)
//! - Not handling empty strings or null values
//! - Forgetting that format strings are case-sensitive
//! - Using named formats that don't exist (causes error)
//!
//! ## Limitations
//!
//! - Cannot create truly custom named formats
//! - Limited control over locale-specific formatting
//! - No built-in format for ISO 8601 dates (must use `yyyy-mm-ddThh:nn:ss`)
//! - Cannot format arrays or objects directly
//! - Some format combinations may produce unexpected results
//! - Maximum string length limitations apply to output
//! - Cannot use for binary or hexadecimal display (use `Hex$` or `Oct$`)

use crate::{
    error::{VBError, VBResult},
    value::{VBLong, VBString, VBVariant},
};

/// Named numeric format identifiers recognized by `Format$`.
///
/// These correspond to the VB6 built-in named formats that can be passed as
/// the format string (e.g. `"Currency"`, `"Fixed"`, ...).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum NamedNumericFormat {
    /// General Number: no thousand separator.
    GeneralNumber,
    /// Currency: thousand separator, two decimals, currency symbol.
    Currency,
    /// Fixed: at least one digit left, two right of decimal.
    Fixed,
    /// Standard: thousand separator, two decimals.
    Standard,
    /// Percent: multiplied by 100, percent sign, two decimals.
    Percent,
    /// Scientific: standard scientific notation.
    Scientific,
    /// Yes/No: "Yes" for non-zero, "No" for zero.
    YesNo,
    /// True/False: "True" for non-zero, "False" for zero.
    TrueFalse,
    /// On/Off: "On" for non-zero, "Off" for zero.
    OnOff,
}

/// Named date/time format identifiers recognized by `Format$`.
///
/// These correspond to the VB6 built-in named formats that can be passed as
/// the format string (e.g. `"Long Date"`, `"Short Time"`, ...).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum NamedDateFormat {
    /// General Date: date and time (time omitted when midnight).
    GeneralDate,
    /// Long Date: full day name, full month name, day, year.
    LongDate,
    /// Medium Date: dd-Mon-yy.
    MediumDate,
    /// Short Date: m/d/yyyy.
    ShortDate,
    /// Long Time: h:mm:ss AM/PM.
    LongTime,
    /// Medium Time: hh:mm AM/PM (no seconds).
    MediumTime,
    /// Short Time: hh:mm (24-hour).
    ShortTime,
}

/// Returns a `String` containing the formatted representation of `expression`
/// according to the instructions in `format`.
///
/// `format` may be a named format (`"Currency"`, `"Short Date"`, ...) or a
/// user-defined format expression. When `format` is `None` the value is
/// converted to its natural string representation (like `Str$`, but without
/// the leading space for positive numbers). A `Null` expression always yields
/// the empty string, matching `Format$`.
///
/// `firstdayofweek` and `firstweekofyear` select the week start and first-week
/// rule used by the `w`/`ww` date tokens; when omitted, `vbSunday` (1) and
/// `vbFirstJan1` (1) are used.
///
/// # Errors
///
/// Returns error 13 (`Type mismatch`) when the expression cannot be coerced to
/// the type implied by the format string (e.g. a date format applied to a
/// non-numeric string).
pub fn format_dollar(
    expression: &VBVariant,
    format: Option<&VBString>,
    firstdayofweek: Option<&VBLong>,
    firstweekofyear: Option<&VBLong>,
) -> VBResult<VBString> {
    if expression.is_null() {
        return Ok(VBString::from(""));
    }
    let first_day_of_week = firstdayofweek.map_or(1, |v| {
        let n = v.as_i32();
        if (1..=7).contains(&n) {
            n
        } else {
            1
        }
    });
    let first_week_of_year = firstweekofyear.map_or(1, |v| v.as_i32());

    let Some(fmt) = format else {
        let result = if expression.is_date() {
            match date_parts(expression.as_date_serial()?) {
                Some(parts) => general_date_string(&parts),
                None => return Err(VBError::type_mismatch()),
            }
        } else {
            expression.as_string()?
        };
        return Ok(VBString::from(result));
    };

    let fmt_str = fmt.as_str();
    if fmt_str.trim().is_empty() {
        return Ok(VBString::from(expression.as_string()?));
    }

    if let Some(name) = named_numeric_format_name(fmt_str) {
        let value = expression.as_f64()?;
        return Ok(VBString::from(format_named_numeric(name, value)?));
    }

    if let Some(name) = named_date_format_name(fmt_str) {
        let serial = expression.as_date_serial()?;
        let parts = date_parts(serial).ok_or_else(VBError::type_mismatch)?;
        return Ok(VBString::from(format_named_date(name, &parts)));
    }

    if has_string_placeholder(fmt_str) {
        let input = expression.as_string()?;
        return Ok(VBString::from(format_string_custom(&input, fmt_str)));
    }

    if has_date_letters(fmt_str) {
        let serial = expression.as_date_serial()?;
        return Ok(VBString::from(format_date_custom(
            serial,
            fmt_str,
            first_day_of_week,
            first_week_of_year,
        )?));
    }

    let value = expression.as_f64()?;
    Ok(VBString::from(format_number_custom(value, fmt_str)?))
}

// ---------------------------------------------------------------------------
// Named formats
// ---------------------------------------------------------------------------

/// Maps a case-insensitive named numeric format string to its enum variant.
fn named_numeric_format_name(fmt: &str) -> Option<NamedNumericFormat> {
    match fmt.trim().to_ascii_lowercase().as_str() {
        "general number" => Some(NamedNumericFormat::GeneralNumber),
        "currency" => Some(NamedNumericFormat::Currency),
        "fixed" => Some(NamedNumericFormat::Fixed),
        "standard" => Some(NamedNumericFormat::Standard),
        "percent" => Some(NamedNumericFormat::Percent),
        "scientific" => Some(NamedNumericFormat::Scientific),
        "yes/no" => Some(NamedNumericFormat::YesNo),
        "true/false" => Some(NamedNumericFormat::TrueFalse),
        "on/off" => Some(NamedNumericFormat::OnOff),
        _ => None,
    }
}

/// Formats `value` using one of the named numeric formats.
fn format_named_numeric(name: NamedNumericFormat, value: f64) -> VBResult<String> {
    match name {
        NamedNumericFormat::GeneralNumber => Ok(general_number(value)),
        NamedNumericFormat::Currency => format_number_custom(value, "$#,##0.00;($#,##0.00)"),
        NamedNumericFormat::Fixed => format_number_custom(value, "0.00"),
        NamedNumericFormat::Standard => format_number_custom(value, "#,##0.00"),
        NamedNumericFormat::Percent => format_number_custom(value, "0.00%"),
        NamedNumericFormat::Scientific => format_number_custom(value, "0.00E+00"),
        NamedNumericFormat::YesNo => Ok(if value == 0.0 { "No" } else { "Yes" }.to_string()),
        NamedNumericFormat::TrueFalse => {
            Ok(if value == 0.0 { "False" } else { "True" }.to_string())
        }
        NamedNumericFormat::OnOff => Ok(if value == 0.0 { "Off" } else { "On" }.to_string()),
    }
}

/// Maps a case-insensitive named date format string to its enum variant.
fn named_date_format_name(fmt: &str) -> Option<NamedDateFormat> {
    match fmt.trim().to_ascii_lowercase().as_str() {
        "general date" => Some(NamedDateFormat::GeneralDate),
        "long date" => Some(NamedDateFormat::LongDate),
        "medium date" => Some(NamedDateFormat::MediumDate),
        "short date" => Some(NamedDateFormat::ShortDate),
        "long time" => Some(NamedDateFormat::LongTime),
        "medium time" => Some(NamedDateFormat::MediumTime),
        "short time" => Some(NamedDateFormat::ShortTime),
        _ => None,
    }
}

/// Formats `parts` using one of the named date/time formats.
pub(crate) fn format_named_date(name: NamedDateFormat, parts: &DateParts) -> String {
    match name {
        NamedDateFormat::GeneralDate => general_date_string(parts),
        NamedDateFormat::LongDate => format!(
            "{}, {} {}, {}",
            DAY_NAMES[(parts.weekday - 1) as usize],
            MONTH_NAMES[(parts.month - 1) as usize],
            parts.day,
            parts.year
        ),
        NamedDateFormat::MediumDate => format!(
            "{:02}-{}-{:02}",
            parts.day,
            &MONTH_NAMES[(parts.month - 1) as usize][..3],
            parts.year % 100
        ),
        NamedDateFormat::ShortDate => format!("{}/{}/{}", parts.month, parts.day, parts.year),
        NamedDateFormat::LongTime => format!(
            "{}:{:02}:{:02} {}",
            hour12(parts.hour),
            parts.minute,
            parts.second,
            ampm(parts.hour)
        ),
        NamedDateFormat::MediumTime => format!(
            "{:02}:{:02} {}",
            hour12(parts.hour),
            parts.minute,
            ampm(parts.hour)
        ),
        NamedDateFormat::ShortTime => format!("{:02}:{:02}", parts.hour, parts.minute),
    }
}

// ---------------------------------------------------------------------------
// String formatting (`@`, `&`, `<`, `>`, `!`)
// ---------------------------------------------------------------------------

/// Whether `format` uses string-formatting placeholders/modifiers.
fn has_string_placeholder(fmt: &str) -> bool {
    let mut in_quote = false;
    let mut chars = fmt.chars();
    while let Some(c) = chars.next() {
        match c {
            '"' => in_quote = !in_quote,
            '\\' => {
                chars.next();
            }
            c if !in_quote && matches!(c, '@' | '&' | '<' | '>' | '!') => return true,
            _ => {}
        }
    }
    false
}

/// Formats `input` using a user-defined string format expression.
///
/// `@` displays a character or a space, `&` a character or nothing, `<` forces
/// lowercase, `>` forces uppercase, and `!` left-aligns the input.
fn format_string_custom(input: &str, format: &str) -> String {
    let chars: Vec<char> = format.chars().collect();
    let mut has_gt = false;
    let mut has_lt = false;
    let mut left_align = false;
    let mut placeholders = 0usize;
    let mut in_quote = false;
    let mut i = 0;
    while i < chars.len() {
        let c = chars[i];
        match c {
            '"' => in_quote = !in_quote,
            '\\' => i += 1,
            c if !in_quote => match c {
                '@' | '&' => placeholders += 1,
                '>' => has_gt = true,
                '<' => has_lt = true,
                '!' => left_align = true,
                _ => {}
            },
            _ => {}
        }
        i += 1;
    }

    let apply_case = |s: String| -> String {
        if has_lt {
            s.to_lowercase()
        } else if has_gt {
            s.to_uppercase()
        } else {
            s
        }
    };
    if placeholders == 0 {
        return apply_case(input.to_string());
    }

    let input_chars: Vec<char> = input.chars().collect();
    let used = placeholders.min(input_chars.len());
    let leading_pad = if left_align { 0 } else { placeholders - used };

    let mut out = String::new();
    let mut consumed = 0usize;
    let mut ph_index = 0usize;
    i = 0;
    while i < chars.len() {
        let c = chars[i];
        match c {
            '@' | '&' => {
                if ph_index < leading_pad {
                    if c == '@' {
                        out.push(' ');
                    }
                } else if consumed < used {
                    out.push(input_chars[consumed]);
                    consumed += 1;
                } else if c == '@' {
                    out.push(' ');
                }
                ph_index += 1;
                i += 1;
            }
            '<' | '>' | '!' => i += 1,
            '"' => {
                i += 1;
                while i < chars.len() && chars[i] != '"' {
                    out.push(chars[i]);
                    i += 1;
                }
                if i < chars.len() {
                    i += 1;
                }
            }
            '\\' => {
                i += 1;
                if i < chars.len() {
                    out.push(chars[i]);
                    i += 1;
                }
            }
            _ => {
                out.push(c);
                i += 1;
            }
        }
    }
    apply_case(out)
}

// ---------------------------------------------------------------------------
// Numeric formatting
// ---------------------------------------------------------------------------

/// A parsed single-section numeric format expression.
struct NumericSection {
    prefix: String,
    int_placeholders: Vec<char>,
    frac_placeholders: Vec<char>,
    has_thousands: bool,
    scaling: u32,
    percent: u32,
    exponent: Option<(char, bool, usize)>,
    suffix: String,
}

/// Splits a format expression into its `;`-separated sections, honoring
/// quoted literals and backslash escapes.
fn split_sections(format: &str) -> Vec<String> {
    let mut sections = vec![String::new()];
    let mut in_quote = false;
    let mut chars = format.chars();
    while let Some(c) = chars.next() {
        match c {
            '"' => {
                in_quote = !in_quote;
                sections.last_mut().unwrap().push(c);
            }
            '\\' => {
                sections.last_mut().unwrap().push(c);
                if let Some(next) = chars.next() {
                    sections.last_mut().unwrap().push(next);
                }
            }
            ';' if !in_quote => sections.push(String::new()),
            _ => sections.last_mut().unwrap().push(c),
        }
    }
    sections
}

/// Whether `c` starts the numeric body of a section.
fn is_num_body_start(c: char) -> bool {
    matches!(c, '0' | '#' | '.' | ',' | '%' | 'E' | 'e')
}

/// Reads a quoted literal (`"..."`) or backslash escape (`\x`) starting at `*i`
/// and returns the literal text, advancing `*i` past it.
fn read_literal(chars: &[char], i: &mut usize) -> String {
    let mut out = String::new();
    while *i < chars.len() {
        let c = chars[*i];
        if c == '"' {
            *i += 1;
            while *i < chars.len() && chars[*i] != '"' {
                out.push(chars[*i]);
                *i += 1;
            }
            if *i < chars.len() {
                *i += 1;
            }
        } else if c == '\\' {
            *i += 1;
            if *i < chars.len() {
                out.push(chars[*i]);
                *i += 1;
            }
        } else {
            break;
        }
    }
    out
}

/// Parses a scientific-notation exponent spec starting at index `i` (where
/// `chars[i]` is `E` or `e`), returning the letter, whether the sign is forced
/// positive, the digit count, and the index just past the spec.
fn parse_exponent(chars: &[char], i: usize) -> Option<(char, bool, usize, usize)> {
    let letter = chars[i];
    let mut j = i + 1;
    if j >= chars.len() {
        return None;
    }
    let forced_plus = match chars[j] {
        '+' => true,
        '-' => false,
        _ => return None,
    };
    j += 1;
    let mut digits = 0usize;
    while j < chars.len() && (chars[j] == '0' || chars[j] == '#') {
        digits += 1;
        j += 1;
    }
    if digits == 0 {
        return None;
    }
    Some((letter, forced_plus, digits, j))
}

/// Parses a single numeric format section into its components.
fn parse_numeric_section(section: &str) -> NumericSection {
    let chars: Vec<char> = section.chars().collect();
    let n = chars.len();
    let mut parsed = NumericSection {
        prefix: String::new(),
        int_placeholders: Vec::new(),
        frac_placeholders: Vec::new(),
        has_thousands: false,
        scaling: 0,
        percent: 0,
        exponent: None,
        suffix: String::new(),
    };
    let mut i = 0;
    while i < n && !is_num_body_start(chars[i]) {
        let mut idx = i;
        let literal = read_literal(&chars, &mut idx);
        if idx > i {
            parsed.prefix.push_str(&literal);
            i = idx;
        } else {
            parsed.prefix.push(chars[i]);
            i += 1;
        }
    }

    let mut is_frac = false;
    let mut pending_comma = false;
    while i < n {
        let c = chars[i];
        match c {
            '0' | '#' => {
                if pending_comma {
                    parsed.has_thousands = true;
                    pending_comma = false;
                }
                if is_frac {
                    parsed.frac_placeholders.push(c);
                } else {
                    parsed.int_placeholders.push(c);
                }
                i += 1;
            }
            '.' => {
                if pending_comma {
                    parsed.scaling += 1;
                    pending_comma = false;
                }
                if is_frac {
                    parsed.suffix.push(c);
                } else {
                    is_frac = true;
                }
                i += 1;
            }
            ',' => {
                if parsed.int_placeholders.is_empty() && !is_frac {
                    parsed.suffix.push(c);
                } else {
                    pending_comma = true;
                }
                i += 1;
            }
            '%' => {
                if pending_comma {
                    parsed.scaling += 1;
                    pending_comma = false;
                }
                parsed.percent += 1;
                i += 1;
            }
            'E' | 'e' => {
                if let Some((letter, forced_plus, digits, next)) = parse_exponent(&chars, i) {
                    parsed.exponent = Some((letter, forced_plus, digits));
                    i = next;
                } else {
                    if pending_comma {
                        parsed.scaling += 1;
                        pending_comma = false;
                    }
                    parsed.suffix.push(c);
                    i += 1;
                }
            }
            '"' | '\\' => {
                if pending_comma {
                    parsed.scaling += 1;
                    pending_comma = false;
                }
                let mut idx = i;
                parsed.suffix.push_str(&read_literal(&chars, &mut idx));
                i = idx;
            }
            _ => {
                if pending_comma {
                    parsed.scaling += 1;
                    pending_comma = false;
                }
                parsed.suffix.push(c);
                i += 1;
            }
        }
    }
    if pending_comma {
        parsed.scaling += 1;
    }
    parsed
}

/// Rounds a non-negative `f64` half away from zero (VB6 `Format` rounding).
fn round_half_away(x: f64) -> f64 {
    (x + 0.5).floor()
}

/// Groups an integer digit string into 3-digit clusters from the right.
fn group_thousands(s: &str) -> String {
    let bytes: Vec<char> = s.chars().collect();
    let len = bytes.len();
    let mut out = String::new();
    for (i, c) in bytes.iter().enumerate() {
        if i > 0 && (len - i).is_multiple_of(3) {
            out.push(',');
        }
        out.push(*c);
    }
    out
}

/// Formats the integer and fractional digit body of `value` according to
/// `parsed`, including zero-fill, thousand separators, and rounding.
fn format_digits(value: f64, parsed: &NumericSection) -> String {
    let frac_count = parsed.frac_placeholders.len();
    let factor = 10.0f64.powi(frac_count as i32);
    let scaled = round_half_away(value * factor);
    let scaled_i = scaled as i128;
    let factor_i = 10i128.pow(frac_count as u32);
    let int_val = scaled_i / factor_i;
    let frac_val = (scaled_i % factor_i).unsigned_abs();
    let int_digits: Vec<char> = int_val.to_string().chars().collect();
    let frac_digits: Vec<char> = if frac_count > 0 {
        format!("{:0>width$}", frac_val, width = frac_count)
            .chars()
            .collect()
    } else {
        Vec::new()
    };

    let mut int_out = String::new();
    let ph = parsed.int_placeholders.len();
    if ph == 0 {
        int_out.push('0');
    } else if int_digits.len() >= ph {
        int_out = int_digits.iter().collect();
    } else {
        let pad = ph - int_digits.len();
        for (k, &p) in parsed.int_placeholders.iter().enumerate() {
            if k < pad {
                if p == '0' {
                    int_out.push('0');
                }
            } else {
                int_out.push(int_digits[k - pad]);
            }
        }
    }
    if parsed.has_thousands {
        int_out = group_thousands(&int_out);
    }

    let mut frac_out = String::new();
    for (k, &p) in parsed.frac_placeholders.iter().enumerate() {
        frac_out.push(frac_digits[k]);
        if p == '#' && k + 1 == parsed.frac_placeholders.len() {
            // Trailing `#` placeholders may drop zero digits; handled below.
            let _ = ();
        }
    }
    let mut frac_ph = parsed.frac_placeholders.clone();
    while let Some(&p) = frac_ph.last() {
        if p == '#' && frac_out.ends_with('0') {
            frac_out.pop();
            frac_ph.pop();
        } else {
            break;
        }
    }

    let mut body = int_out;
    if !frac_out.is_empty() {
        body.push('.');
        body.push_str(&frac_out);
    }
    body
}

/// Formats `value` with a scientific-notation format expression.
fn format_exponent_body(
    value: f64,
    parsed: &NumericSection,
    letter: char,
    forced_plus: bool,
    digits: usize,
) -> String {
    let (mantissa, exponent) = if value == 0.0 {
        (0.0, 0)
    } else {
        let e = value.abs().log10().floor() as i32;
        (value / 10.0f64.powi(e), e)
    };
    let mantissa_body = format_digits(mantissa, parsed);
    let mut exp_str = String::new();
    exp_str.push(letter);
    if forced_plus && exponent >= 0 {
        exp_str.push('+');
    } else if exponent < 0 {
        exp_str.push('-');
    }
    let magnitude = exponent.abs().to_string();
    if magnitude.len() < digits {
        exp_str.push_str(&"0".repeat(digits - magnitude.len()));
    }
    exp_str.push_str(&magnitude);
    format!("{mantissa_body}{exp_str}")
}

/// Formats `value` using a user-defined numeric format expression.
fn format_number_custom(value: f64, format: &str) -> VBResult<String> {
    let sections = split_sections(format);
    let negative = value < 0.0;
    let zero = value == 0.0;
    let index = if negative {
        if sections.len() > 1 {
            1
        } else {
            0
        }
    } else if zero && sections.len() > 2 {
        2
    } else {
        0
    };
    let section = &sections[index.min(sections.len() - 1)];
    let parsed = parse_numeric_section(section);

    if parsed.int_placeholders.is_empty()
        && parsed.frac_placeholders.is_empty()
        && parsed.exponent.is_none()
    {
        let mut literal = parsed.prefix;
        literal.push_str(&parsed.suffix);
        return Ok(literal);
    }

    let mut v = value.abs();
    if parsed.percent > 0 {
        v *= 100.0f64.powi(parsed.percent as i32);
    }
    if parsed.scaling > 0 {
        v /= 1000.0f64.powi(parsed.scaling as i32);
    }

    let body = if let Some((letter, forced_plus, digits)) = parsed.exponent {
        format_exponent_body(v, &parsed, letter, forced_plus, digits)
    } else {
        format_digits(v, &parsed)
    };

    let mut out = String::new();
    out.push_str(&parsed.prefix);
    if negative && sections.len() == 1 && !parsed.prefix.contains('-') {
        out.push('-');
    }
    out.push_str(&body);
    for _ in 0..parsed.percent {
        out.push('%');
    }
    out.push_str(&parsed.suffix);
    Ok(out)
}

/// The `FormatNumber`/`FormatCurrency`/`FormatPercent` wrapper family.
///
/// A negative result is prefixed with `-` (or wrapped in parentheses when
/// `parens` is true) before `prefix`. A true `leading` control keeps the `0`
/// for fractional magnitudes in `(0, 1)`; when false it is suppressed.
pub(crate) fn format_style_number(
    value: f64,
    decimals: usize,
    group: bool,
    leading: bool,
    parens: bool,
    prefix: &str,
    suffix: &str,
) -> String {
    let decimals = decimals.min(30);
    let negative = value < 0.0;
    let abs = value.abs();
    let factor = 10.0f64.powi(decimals as i32);
    let scaled = round_half_away(abs * factor);
    let scaled_i = scaled as i128;
    let factor_i = 10i128.pow(decimals as u32);
    let int_val = scaled_i / factor_i;
    let frac_val = (scaled_i % factor_i).unsigned_abs();

    let mut int_str = int_val.to_string();
    if !leading && int_val == 0 {
        int_str.clear();
    }
    if group {
        int_str = group_thousands(&int_str);
    }
    let frac_str = if decimals > 0 {
        format!(".{:0>width$}", frac_val, width = decimals)
    } else {
        String::new()
    };
    let body = format!("{prefix}{int_str}{frac_str}{suffix}");
    if negative && parens {
        format!("({body})")
    } else if negative {
        format!("-{body}")
    } else {
        body
    }
}

/// The style used by the `FormatNumber`/`FormatCurrency`/`FormatPercent` family.
#[derive(Clone, Copy)]
pub(crate) enum NumericStyle {
    Number,
    Currency,
    Percent,
}

impl NumericStyle {
    fn prefix(self) -> &'static str {
        match self {
            NumericStyle::Currency => "$",
            _ => "",
        }
    }

    fn suffix(self) -> &'static str {
        match self {
            NumericStyle::Percent => "%",
            _ => "",
        }
    }

    /// The multiplier applied before formatting.
    fn scale(self) -> f64 {
        match self {
            NumericStyle::Percent => 100.0,
            _ => 1.0,
        }
    }
}

/// Resolves a tristate argument (`vbTrue` -1, `vbFalse` 0, `vbUseDefault` -2)
/// against the locale default `default`.
fn tristate(v: Option<&VBLong>, default: bool) -> bool {
    match v.map(|x| x.as_i32()) {
        None | Some(-2) => default,
        Some(-1) => true,
        _ => false,
    }
}

/// Backs the `FormatNumber`, `FormatCurrency`, and `FormatPercent` wrappers.
///
/// `numdigitsafterdecimal` of -1 (the default) selects the locale's two-digit
/// grouping; negative values below -1 are treated as an invalid argument.
/// Tristate arguments default to the locale behavior (grouping and leading
/// zero on, parentheses off). A `Null` `expression` propagates as `Null`.
pub(crate) fn format_number_family(
    expression: &VBVariant,
    numdigitsafterdecimal: Option<&VBLong>,
    includeleadingdigit: Option<&VBLong>,
    useparensfornegativenumbers: Option<&VBLong>,
    groupdigits: Option<&VBLong>,
    style: NumericStyle,
) -> VBResult<VBVariant> {
    if expression.is_null() {
        return Ok(VBVariant::Null);
    }
    let value = expression.as_f64()?;
    let digits = match numdigitsafterdecimal {
        None => 2,
        Some(v) => {
            let n = v.as_i32();
            if n == -1 {
                2
            } else if n < 0 {
                return Err(VBError::invalid_procedure_call());
            } else {
                n
            }
        }
    };
    let leading = tristate(includeleadingdigit, true);
    let parens = tristate(useparensfornegativenumbers, false);
    let grouped = tristate(groupdigits, true);
    let result = format_style_number(
        value * style.scale(),
        digits as usize,
        grouped,
        leading,
        parens,
        style.prefix(),
        style.suffix(),
    );
    Ok(VBVariant::from_string(result))
}

/// The `General Number` named format.
fn general_number(value: f64) -> String {
    if value.is_finite() && value == value.trunc() && value.abs() < 1.0e15 {
        format!("{}", value as i64)
    } else {
        format!("{}", value)
    }
}

// ---------------------------------------------------------------------------
// Date/time formatting
// ---------------------------------------------------------------------------

const DAY_NAMES: [&str; 7] = [
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
];

const MONTH_NAMES: [&str; 12] = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
];

/// The decomposed civil date/time components of an OLE automation serial.
pub(crate) struct DateParts {
    year: i16,
    month: u8,
    day: u8,
    hour: u8,
    minute: u8,
    second: u8,
    /// Day of the week, 1 = Sunday .. 7 = Saturday.
    weekday: i8,
    /// Day of the year, 1-based.
    day_of_year: i16,
}

pub(crate) fn date_parts(serial: f64) -> Option<DateParts> {
    let dt = crate::value::date_serial_to_datetime(serial)?;
    Some(DateParts {
        year: dt.year(),
        month: dt.month() as u8,
        day: dt.day() as u8,
        hour: dt.hour() as u8,
        minute: dt.minute() as u8,
        second: dt.second() as u8,
        weekday: dt.date().weekday().to_sunday_one_offset(),
        day_of_year: dt.date().day_of_year(),
    })
}

/// The 12-hour clock hour for a 0-23 hour.
fn hour12(hour: u8) -> u8 {
    let r = hour % 12;
    if r == 0 {
        12
    } else {
        r
    }
}

fn ampm(hour: u8) -> &'static str {
    if hour < 12 {
        "AM"
    } else {
        "PM"
    }
}

fn pad2(v: i32) -> String {
    format!("{v:02}")
}

/// The `General Date` representation: short date, plus time when not midnight.
fn general_date_string(parts: &DateParts) -> String {
    let date = format!("{}/{}/{}", parts.month, parts.day, parts.year);
    if parts.hour == 0 && parts.minute == 0 && parts.second == 0 {
        date
    } else {
        format!(
            "{date} {}:{:02}:{:02} {}",
            hour12(parts.hour),
            parts.minute,
            parts.second,
            ampm(parts.hour)
        )
    }
}

/// VB6 `Weekday(date, first_day_of_week)`: 1-based index relative to `fdow`.
fn vb_weekday(sunday_based: i32, fdow: i32) -> i32 {
    ((sunday_based - fdow).rem_euclid(7)) + 1
}

/// The weekday of January 1 of the year containing `parts` (1 = Sunday).
fn jan1_weekday(parts: &DateParts) -> i32 {
    let offset = (parts.day_of_year as i32 - 1) % 7;
    (((parts.weekday as i32 - 1 - offset) % 7) + 7) % 7 + 1
}

/// VB6 `DatePart("ww", ...)` week-of-year (assuming the first week starts on
/// January 1, i.e. `vbFirstJan1`).
fn week_of_year(parts: &DateParts, fdow: i32) -> i32 {
    let jan1_wd = vb_weekday(jan1_weekday(parts), fdow);
    ((parts.day_of_year as i32 - 1 + jan1_wd - 1) / 7) + 1
}

/// Whether `format` uses any date/time token letters outside quotes/escapes.
fn has_date_letters(fmt: &str) -> bool {
    let mut in_quote = false;
    let mut chars = fmt.chars();
    while let Some(c) = chars.next() {
        match c {
            '"' => in_quote = !in_quote,
            '\\' => {
                chars.next();
            }
            c if !in_quote && "dmyhnsqwc".contains(c.to_ascii_lowercase()) => return true,
            _ => {}
        }
    }
    false
}

/// Whether the text at `i` starts with `token`, ignoring case.
fn starts_ci(chars: &[char], i: usize, token: &str) -> bool {
    let t: Vec<char> = token.chars().collect();
    i + t.len() <= chars.len()
        && t.iter()
            .zip(&chars[i..i + t.len()])
            .all(|(a, b)| a.eq_ignore_ascii_case(b))
}

/// Formats `serial` using a user-defined date/time format expression.
fn format_date_custom(serial: f64, format: &str, fdow: i32, _fwoy: i32) -> VBResult<String> {
    let parts = date_parts(serial).ok_or_else(VBError::type_mismatch)?;
    let chars: Vec<char> = format.chars().collect();
    let lower_format = format.to_ascii_lowercase();
    let has_ampm = lower_format.contains("am/pm") || lower_format.contains("a/p");

    let mut out = String::new();
    let mut i = 0;
    let mut last_was_hour = false;
    while i < chars.len() {
        let c = chars[i];
        if c == '"' || c == '\\' {
            let mut idx = i;
            out.push_str(&read_literal(&chars, &mut idx));
            i = idx;
            continue;
        }
        let c_lower = c.to_ascii_lowercase();
        if (c_lower == 'a' || c_lower == 'p') && starts_ci(&chars, i, "am/pm") {
            out.push_str(ampm(parts.hour));
            i += 5;
            last_was_hour = false;
            continue;
        }
        if (c_lower == 'a' || c_lower == 'p') && starts_ci(&chars, i, "a/p") {
            out.push_str(if parts.hour < 12 { "A" } else { "P" });
            i += 3;
            last_was_hour = false;
            continue;
        }

        let mut j = i;
        while j < chars.len() && chars[j].eq_ignore_ascii_case(&c) {
            j += 1;
        }
        let run_len = j - i;
        let lower = c.to_ascii_lowercase();
        let mut handled = false;
        let mut sets_hour = false;
        match lower {
            'c' if run_len == 1 => {
                out.push_str(&general_date_string(&parts));
                handled = true;
            }
            'd' => {
                let text = match run_len {
                    4 => DAY_NAMES[(parts.weekday - 1) as usize].to_string(),
                    3 => DAY_NAMES[(parts.weekday - 1) as usize][..3].to_string(),
                    2 => pad2(parts.day as i32),
                    _ => parts.day.to_string(),
                };
                out.push_str(&text);
                handled = true;
            }
            'm' => {
                let text = if last_was_hour {
                    match run_len {
                        2 => pad2(parts.minute as i32),
                        _ => parts.minute.to_string(),
                    }
                } else {
                    match run_len {
                        4 => MONTH_NAMES[(parts.month - 1) as usize].to_string(),
                        3 => MONTH_NAMES[(parts.month - 1) as usize][..3].to_string(),
                        2 => pad2(parts.month as i32),
                        _ => parts.month.to_string(),
                    }
                };
                out.push_str(&text);
                handled = true;
            }
            'y' => {
                let text = match run_len {
                    2 => pad2((parts.year % 100) as i32),
                    _ => parts.year.to_string(),
                };
                out.push_str(&text);
                handled = true;
            }
            'h' => {
                let hour = if has_ampm {
                    hour12(parts.hour)
                } else {
                    parts.hour
                };
                let text = if run_len >= 2 {
                    pad2(hour as i32)
                } else {
                    hour.to_string()
                };
                out.push_str(&text);
                handled = true;
                sets_hour = true;
            }
            'n' => {
                let text = if run_len >= 2 {
                    pad2(parts.minute as i32)
                } else {
                    parts.minute.to_string()
                };
                out.push_str(&text);
                handled = true;
            }
            's' => {
                let text = if run_len >= 2 {
                    pad2(parts.second as i32)
                } else {
                    parts.second.to_string()
                };
                out.push_str(&text);
                handled = true;
            }
            'q' => {
                out.push_str(&((parts.month as i32 - 1) / 3 + 1).to_string());
                handled = true;
            }
            'w' => {
                let text = if run_len >= 2 {
                    week_of_year(&parts, fdow).to_string()
                } else {
                    vb_weekday(parts.weekday as i32, fdow).to_string()
                };
                out.push_str(&text);
                handled = true;
            }
            _ => {}
        }
        if !handled {
            out.extend(chars[i..j].iter());
        } else {
            last_was_hour = sets_hour;
        }
        i = j;
    }
    Ok(out)
}

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

    fn fmt(expr: VBVariant, format: &str) -> String {
        format_dollar(&expr, Some(&VBString::from(format)), None, None)
            .unwrap()
            .into_inner()
    }

    fn fmt_opt(expr: VBVariant, format: Option<&str>) -> String {
        format_dollar(&expr, format.map(VBString::from).as_ref(), None, None)
            .unwrap()
            .into_inner()
    }

    #[test]
    fn no_format_converts_like_str() {
        assert_eq!(fmt_opt(VBVariant::Long(1234), None), "1234");
        assert_eq!(fmt_opt(VBVariant::Long(-1234), None), "-1234");
        assert_eq!(fmt_opt(VBVariant::Double(1234.5), None), "1234.5");
        assert_eq!(fmt_opt(VBVariant::from_string("hello"), None), "hello");
        assert_eq!(fmt_opt(VBVariant::Boolean(true), None), "True");
        assert_eq!(
            fmt_opt(VBVariant::from_date_serial(45_662.0), None),
            "1/5/2025"
        );
    }

    #[test]
    fn null_returns_empty_string() {
        assert_eq!(fmt_opt(VBVariant::Null, None), "");
        assert_eq!(fmt(VBVariant::Null, "0.00"), "");
    }

    #[test]
    fn named_numeric_formats() {
        assert_eq!(fmt(VBVariant::Double(1234.5), "General Number"), "1234.5");
        assert_eq!(fmt(VBVariant::Double(1234.5), "Currency"), "$1,234.50");
        assert_eq!(fmt(VBVariant::Double(-1234.5), "Currency"), "($1,234.50)");
        assert_eq!(fmt(VBVariant::Double(1234.5), "Fixed"), "1234.50");
        assert_eq!(fmt(VBVariant::Double(1234.5), "Standard"), "1,234.50");
        assert_eq!(fmt(VBVariant::Double(0.075), "Percent"), "7.50%");
        assert_eq!(
            fmt(VBVariant::Double(12_345_678.0), "Scientific"),
            "1.23E+07"
        );
        assert_eq!(fmt(VBVariant::Double(5.0), "Yes/No"), "Yes");
        assert_eq!(fmt(VBVariant::Double(0.0), "Yes/No"), "No");
        assert_eq!(fmt(VBVariant::Double(5.0), "True/False"), "True");
        assert_eq!(fmt(VBVariant::Double(0.0), "On/Off"), "Off");
    }

    #[test]
    fn custom_numeric_formats() {
        assert_eq!(fmt(VBVariant::Double(1234.5), "0000.00"), "1234.50");
        assert_eq!(fmt(VBVariant::Long(42), "000000"), "000042");
        assert_eq!(fmt(VBVariant::Long(7), "00"), "07");
        assert_eq!(fmt(VBVariant::Double(0.075), "0.00%"), "7.50%");
        assert_eq!(fmt(VBVariant::Double(12_345_678.0), "0.00E+00"), "1.23E+07");
        assert_eq!(fmt(VBVariant::Double(0.000_012_3), "0.00E-00"), "1.23E-05");
        assert_eq!(
            fmt(VBVariant::Double(1234.56), "#,##0.00;(#,##0.00)"),
            "1,234.56"
        );
        assert_eq!(
            fmt(VBVariant::Double(-1234.56), "#,##0.00;(#,##0.00)"),
            "(1,234.56)"
        );
    }

    #[test]
    fn custom_numeric_sections() {
        assert_eq!(fmt(VBVariant::Double(1.5), "+0.00;-0.00;Zero"), "+1.50");
        assert_eq!(fmt(VBVariant::Double(-1.5), "+0.00;-0.00;Zero"), "-1.50");
        assert_eq!(fmt(VBVariant::Double(0.0), "+0.00;-0.00;Zero"), "Zero");
    }

    #[test]
    fn custom_numeric_rounding() {
        assert_eq!(fmt(VBVariant::Double(2.5), "0"), "3");
        assert_eq!(fmt(VBVariant::Double(2.4), "0"), "2");
        assert_eq!(fmt(VBVariant::Double(-2.5), "0.0"), "-2.5");
    }

    #[test]
    fn named_date_formats() {
        assert_eq!(
            fmt(VBVariant::from_date_serial(45_662.0), "Long Date"),
            "Sunday, January 5, 2025"
        );
        assert_eq!(
            fmt(VBVariant::from_date_serial(45_662.0), "Medium Date"),
            "05-Jan-25"
        );
        assert_eq!(
            fmt(VBVariant::from_date_serial(45_662.0), "Short Date"),
            "1/5/2025"
        );
        assert_eq!(
            fmt(VBVariant::from_date_serial(45_662.656_597_222), "Long Time"),
            "3:45:30 PM"
        );
        assert_eq!(
            fmt(
                VBVariant::from_date_serial(45_662.656_597_222),
                "Medium Time"
            ),
            "03:45 PM"
        );
        assert_eq!(
            fmt(
                VBVariant::from_date_serial(45_662.656_597_222),
                "Short Time"
            ),
            "15:45"
        );
        assert_eq!(
            fmt(
                VBVariant::from_date_serial(45_662.656_597_222),
                "General Date"
            ),
            "1/5/2025 3:45:30 PM"
        );
    }

    #[test]
    fn custom_date_formats() {
        assert_eq!(
            fmt(VBVariant::from_date_serial(45_662.0), "yyyy-mm-dd"),
            "2025-01-05"
        );
        assert_eq!(
            fmt(VBVariant::from_date_serial(45_662.0), "dddd, mmmm d, yyyy"),
            "Sunday, January 5, 2025"
        );
        assert_eq!(
            fmt(VBVariant::from_date_serial(45_662.0), "dd-mmm-yy"),
            "05-Jan-25"
        );
        assert_eq!(
            fmt(
                VBVariant::from_date_serial(45_662.656_597_222),
                "yyyymmdd_hhnnss"
            ),
            "20250105_154530"
        );
        assert_eq!(
            fmt(
                VBVariant::from_date_serial(45_662.656_597_222),
                "hh:nn:ss AM/PM"
            ),
            "03:45:30 PM"
        );
        assert_eq!(
            fmt(VBVariant::from_date_serial(45_662.656_597_222), "hh:mm:ss"),
            "15:45:30"
        );
        assert_eq!(
            fmt(
                VBVariant::from_date_serial(45_662.656_597_222),
                "mm/dd/yyyy"
            ),
            "01/05/2025"
        );
        assert_eq!(
            fmt(
                VBVariant::from_date_serial(45_662.656_597_222),
                "h:mm AM/PM"
            ),
            "3:45 PM"
        );
    }

    #[test]
    fn m_following_h_is_minutes() {
        assert_eq!(
            fmt(VBVariant::from_date_serial(45_662.656_597_222), "hh:mm"),
            "15:45"
        );
        assert_eq!(
            fmt(VBVariant::from_date_serial(45_662.656_597_222), "mm"),
            "01"
        );
    }

    #[test]
    fn string_formats() {
        assert_eq!(fmt(VBVariant::from_string("john doe"), ">"), "JOHN DOE");
        assert_eq!(fmt(VBVariant::from_string("JOHN DOE"), "<"), "john doe");
        assert_eq!(
            fmt(VBVariant::from_string("5551234567"), "(@@@) @@@-@@@@"),
            "(555) 123-4567"
        );
        assert_eq!(
            fmt(VBVariant::from_string("hello"), "@@@@@@@@@@"),
            "     hello"
        );
    }

    #[test]
    fn type_mismatch_for_incompatible_values() {
        assert_eq!(
            format_dollar(
                &VBVariant::from_string("abc"),
                Some(&VBString::from("Currency")),
                None,
                None
            )
            .unwrap_err()
            .number,
            err_number::TYPE_MISMATCH
        );
        assert_eq!(
            format_dollar(
                &VBVariant::from_string("abc"),
                Some(&VBString::from("0.00")),
                None,
                None
            )
            .unwrap_err()
            .number,
            err_number::TYPE_MISMATCH
        );
        assert_eq!(
            format_dollar(
                &VBVariant::from_string("abc"),
                Some(&VBString::from("yyyy-mm-dd")),
                None,
                None
            )
            .unwrap_err()
            .number,
            err_number::TYPE_MISMATCH
        );
    }

    #[test]
    fn empty_format_behaves_like_no_format() {
        assert_eq!(fmt(VBVariant::Double(1234.5), ""), "1234.5");
    }
}