oneyaml 1.0.0

oneyaml — one core, one interpretation: serde + lossless editing for YAML 1.2.
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
//! Scalar presentation (Q2.3): the **one** place that decides how a
//! scalar's text is written — plain, single-quoted, double-quoted, or a
//! literal block — and writes it.
//!
//! The interface is deliberately narrow and independent of serde, so the
//! `Document` edit API can format new nodes with it too:
//!
//! * [`Rules::new`] from the output settings ([`Emit`]) and the settings the
//!   output is read back with;
//! * [`Rules::string_style`] picks a [`Style`] for a string at a
//!   [`BlockIndent`] (where a literal block's lines would go);
//! * [`Rules::write_string`] writes it in that style;
//! * [`Rules::write_implicit_key`] writes the one-line form of a string
//!   (with an optional tag) when it can be an implicit mapping key;
//! * [`Rules::write_number`], [`write_bool`], [`write_null`], and
//!   [`write_tag`] write the other scalars and tags.
//!
//! # The every-reader-safe rule (Q2.3)
//!
//! A string is written plain only if no reader could take it for anything
//! else: the **one sealed resolver** (NN-3) must resolve the plain text to
//! a string under the Core schema, under the JSON schema (where "no match"
//! is not a non-string), and under Core with every YAML 1.1 behaviour on;
//! it must not be a YAML 1.1 type our resolver does not model, nor a
//! non-string for the other readers the Q2.3 ruling names — Ruby Psych
//! and Go `yaml.v2`/`v3` ([`foreign_yaml11`]: timestamps, `<<`, `=`, 1.1
//! float spellings, comma-grouped numbers, base 60 led by any digit,
//! `.iNf`, `_` anywhere in a number); and it must have no syntax hazard
//! ([`plain_syntax_ok`]). When in doubt, the string is quoted.
//!
//! Every layout (`Layout::SerdeYaml` included) also has a floor: the text
//! must read back as a string under the settings the output is read with
//! (the caller's YAML 1.1 behaviours and merge keys; unit 36c).

use std::fmt::Write as _;

use oneyaml_core::{
    Compat, Diagnostic, Emit, Label, Layout, QuoteStyle, Schema, Settings, Span, code,
};

use crate::error::Error;
use crate::resolve::{ResolveError, Resolved, Resolver, Style as ResolveStyle, TagInput};
use crate::value::{MAX_CONVERTED_DIGITS, Number, TagStr};

/// Longest implicit key, in bytes of its written form (YAML 1.2.2 §7.4.2:
/// 1024 characters; bytes are never fewer than characters).
const MAX_IMPLICIT_KEY: usize = 1024;
/// `serde_yaml`'s (libyaml's) longest simple key.
const SERDE_YAML_MAX_SIMPLE_KEY: usize = 128;

/// How a string is written.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Style {
    /// Unquoted.
    Plain,
    /// `'…'`, with `'` doubled.
    Single,
    /// `"…"`, with escapes.
    Double,
    /// A `|` literal block (multi-line strings only).
    Literal,
}

/// Where a literal block's content lines would go.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct BlockIndent {
    /// Column of the content lines.
    pub(crate) content: usize,
    /// The indentation indicator to print when one is needed: the content
    /// indentation minus the parent node's. `None` where readers disagree
    /// on it, so a text needing one is not written as a block: at the
    /// root, the spec puts the parent at -1 (YAML 1.2.2 §8.1.1.1) and
    /// libyaml-based readers at 0.
    pub(crate) indicator: Option<usize>,
}

/// Which output layout the rules follow.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Flavor {
    /// Q2.3 defaults.
    Default,
    /// `Layout::SerdeYaml`: `serde_yaml`'s choices, best effort.
    SerdeYaml,
}

/// The scalar presentation rules for one set of output settings.
#[derive(Clone, Debug)]
pub(crate) struct Rules {
    flavor: Flavor,
    double: bool,
    indent: usize,
    indent_sequences: bool,
    /// Core schema, no 1.1 behaviours: how oneyaml reads its output.
    core: Resolver,
    /// JSON schema.
    json: Resolver,
    /// Core with every YAML 1.1 behaviour on.
    yaml11: Resolver,
    /// Core plus the 1.1 integer forms `serde_yaml` parses (`0b…`, signed
    /// hex): what `serde_yaml` quotes as "would read as a number" (with
    /// [`serde_yaml_signed_octal`], which no setting models).
    serde_yaml: Resolver,
    /// The settings the output will be read back with (Q2.3 round trip):
    /// the caller's own YAML 1.1 behaviours. A string that resolves to
    /// another type under them is never written plain, in any layout.
    /// `None` when they resolve plain scalars exactly as [`Rules::core`]
    /// does (Q4.4: no second Core check per string, unit 37).
    active: Option<Resolver>,
    /// `honor_version_directive`: a `%YAML 1.1` document is read with every
    /// 1.1 behaviour, so the floor is [`Rules::yaml11`] too.
    honor_yaml11: bool,
    /// `merge_keys`: a plain `<<` key would merge instead of being the
    /// string key `<<`.
    merge_keys: bool,
    /// `Layout::SerdeYaml` read back with
    /// `Settings::resolve_tagged_variant_content`: a newtype variant around
    /// a null, bool, or number is written `!Name value` (unit 41).
    tagged_scalar_variants: bool,
    /// The reading settings' `Limits::max_scalar_len`: the longest tag the
    /// output may carry (unit 42c: a longer one would not read back).
    max_tag_len: usize,
}

impl Rules {
    /// The rules for `emit`. `Err(Internal)` for a layout or quote style
    /// this version does not know (NN-4; `-core` enums are
    /// `#[non_exhaustive]`).
    ///
    /// `Y1012` for an indentation outside `1..=Emit::MAX_INDENT`: zero
    /// cannot express nesting, and a wider one would multiply the output
    /// (every line repeats it once per level) into an allocation blow-up
    /// (NN-4; Q2.3 ruling: any value is accepted by the type, never a
    /// panic).
    ///
    /// `reading`: the settings the output is read back with; its schema is
    /// Core here (the serializer and `Document` refuse others first).
    ///
    /// With `reading.honor_version_directive(true)` the floor applies
    /// **all** of YAML 1.1 ([`Compat::yaml_1_1`]) and treats `<<` as a merge
    /// key, for every document, as if each carried `%YAML 1.1`: the rules
    /// cannot know which document the output is read as. Our serializer
    /// never writes `%YAML 1.1`, so this only quotes more than needed; it
    /// never writes a string that reads back as another type.
    pub(crate) fn new(emit: &Emit, reading: &Settings) -> Result<Self, Error> {
        let indent = emit.get_indent();
        if indent == 0 || indent > Emit::MAX_INDENT {
            let d = Diagnostic::error(
                code::INCORRECT_SETTINGS,
                format!(
                    "Emit::indent({indent}) cannot produce correct output: the indentation must be 1..={}",
                    Emit::MAX_INDENT
                ),
                Label::new(Span::empty(0), ""),
            )
            .with_help("use an indentation of 1 to Emit::MAX_INDENT spaces (the default is 2)");
            return Err(Error::from_diagnostic(d));
        }
        let flavor = match emit.get_layout() {
            Layout::Default => Flavor::Default,
            Layout::SerdeYaml => Flavor::SerdeYaml,
            // A layout added to `-core` without an emitter here (NN-4).
            other => {
                debug_assert!(false, "no emitter for {other:?}");
                return Err(Error::internal(&format!("no emitter for {other:?}"), None));
            }
        };
        let double = match emit.get_quote_style() {
            QuoteStyle::Single => false,
            QuoteStyle::Double => true,
            other => {
                debug_assert!(false, "no emitter for {other:?}");
                return Err(Error::internal(&format!("no emitter for {other:?}"), None));
            }
        };
        Ok(Self {
            flavor,
            double,
            indent,
            indent_sequences: emit.get_indent_sequences(),
            core: Resolver::new(&Settings::new()),
            json: Resolver::new(&Settings::new().schema(Schema::Json)),
            yaml11: Resolver::new(&Compat::yaml_1_1()),
            serde_yaml: Resolver::new(&Settings::new().yaml11_int_forms(true)),
            active: Some(Resolver::new(&reading.clone().schema(Schema::Core)))
                .filter(|r| !r.plain_is_core()),
            honor_yaml11: reading.get_honor_version_directive(),
            merge_keys: reading.get_merge_keys() || reading.get_honor_version_directive(),
            // Q2.3: only when the output reads back that way.
            tagged_scalar_variants: flavor == Flavor::SerdeYaml
                && reading.get_resolve_tagged_variant_content(),
            max_tag_len: reading.get_limits().get_max_scalar_len(),
        })
    }

    /// Unit 42c (Q4.5): whether a tag resolving to `len` bytes reads back
    /// under the reading settings (`max_scalar_len` bounds resolved tags).
    pub(crate) fn tag_fits(&self, len: usize) -> bool {
        len <= self.max_tag_len
    }

    /// The `Y2001` error for a tag of `len` bytes that does not fit.
    pub(crate) fn tag_too_long(&self, len: usize) -> Error {
        Error::new(
            code::SCALAR_TOO_LONG,
            format!(
                "a tag of {len} bytes is longer than Limits::max_scalar_len ({}), which the output \
                 is read back with",
                self.max_tag_len
            ),
            None,
        )
        .with_help("raise Limits::max_scalar_len, or write a shorter tag")
    }

    /// Spaces per indentation level.
    pub(crate) fn indent(&self) -> usize {
        self.indent
    }

    /// Whether block sequences are indented under their key.
    pub(crate) fn indent_sequences(&self) -> bool {
        self.indent_sequences
    }

    /// Whether the rules follow `serde_yaml`'s layout.
    pub(crate) fn serde_yaml_layout(&self) -> bool {
        self.flavor == Flavor::SerdeYaml
    }

    /// Whether a newtype variant around a null, bool, or number is written
    /// as a tagged scalar (`!Square 2.0`, as `serde_yaml` writes it): the
    /// `serde_yaml` layout, read back with
    /// `Settings::resolve_tagged_variant_content` (unit 41).
    pub(crate) fn tagged_scalar_variants(&self) -> bool {
        self.tagged_scalar_variants
    }

    /// The style for `text` written as a node whose literal block would sit
    /// at `block`.
    pub(crate) fn string_style(&self, text: &str, block: BlockIndent) -> Style {
        match self.flavor {
            Flavor::Default => self.default_style(text, block),
            Flavor::SerdeYaml => self.serde_yaml_style(text, block),
        }
    }

    fn quoted(&self) -> Style {
        if self.double {
            Style::Double
        } else {
            Style::Single
        }
    }

    // Q2.3 default layout.
    fn default_style(&self, text: &str, block: BlockIndent) -> Style {
        if text.contains('\n') {
            return if literal_ok(text, block, false) {
                Style::Literal
            } else {
                Style::Double
            };
        }
        if has_escape(text) {
            return Style::Double;
        }
        if self.plain_ok(text) {
            Style::Plain
        } else {
            self.quoted()
        }
    }

    /// Q2.3 every-reader-safe: plain only when every reader reads a string.
    fn plain_ok(&self, text: &str) -> bool {
        // Conservative: leading or trailing Unicode white space (e.g.
        // U+00A0) is text to YAML but invisible, and tools trim it.
        let edge = |c: Option<char>| c.is_some_and(char::is_whitespace);
        !edge(text.chars().next())
            && !edge(text.chars().next_back())
            && plain_syntax_ok(text, true)
            && reads_as_string(&self.core, text)
            && reads_as_string(&self.json, text)
            && reads_as_string(&self.yaml11, text)
            // The floor ([`Rules::reads_back_as_string`]) minus what was
            // just checked: Core, and all of 1.1 (Q4.4, unit 37).
            && self.reading_extras_ok(text)
            && !foreign_yaml11(text)
    }

    /// Q2.3: the floor of every layout. The text reads back as a string
    /// under the settings it is written for (Core plus the caller's 1.1
    /// behaviours; all of them for `honor_version_directive`).
    fn reads_back_as_string(&self, text: &str) -> bool {
        reads_as_string(&self.core, text)
            && self.reading_extras_ok(text)
            && (!self.honor_yaml11 || reads_as_string(&self.yaml11, text))
    }

    /// The part of the floor beyond Core and `honor_version_directive`: the
    /// caller's own 1.1 behaviours (skipped when they add nothing to Core)
    /// and merge keys.
    fn reading_extras_ok(&self, text: &str) -> bool {
        self.active
            .as_ref()
            .is_none_or(|r| reads_as_string(r, text))
            && !(self.merge_keys && text == "<<")
    }

    // Layout::SerdeYaml: `serde_yaml` requests a literal block for strings
    // with `\n`, single quotes for strings it would read as another type,
    // and otherwise lets libyaml pick plain / single / double. Floor: the
    // result must read back as the same string under our Core reader.
    fn serde_yaml_style(&self, text: &str, block: BlockIndent) -> Style {
        let a = Analysis::of(text);
        if text.contains('\n') {
            return if a.block && literal_ok(text, block, true) {
                Style::Literal
            } else {
                Style::Double
            };
        }
        // Unit 42b (injection): a plain `<<` is the merge key and `=` the
        // YAML 1.1 value key to every merge-key reader (PyYAML, Go, Psych,
        // ours with `merge_keys`), so a string `{"<<": {..}}` would merge
        // into its parent. Always quoted, in every layout (serde_yaml
        // writes them plain: a recorded divergence).
        let typed = !reads_as_string(&self.serde_yaml, text)
            || serde_yaml_signed_octal(text)
            || matches!(text, "<<" | "=");
        let quoted = if a.single && !self.double {
            Style::Single
        } else {
            Style::Double
        };
        if typed || !a.plain || !plain_syntax_ok(text, false) || !self.reads_back_as_string(text) {
            quoted
        } else {
            Style::Plain
        }
    }

    /// Writes `text` in `style` (from [`Rules::string_style`] at the same
    /// `block`). Literal blocks end at their last content line, without a
    /// line break: the caller ends the line.
    pub(crate) fn write_string(
        &self,
        out: &mut String,
        text: &str,
        style: Style,
        block: BlockIndent,
    ) {
        match style {
            Style::Plain => out.push_str(text),
            Style::Single => write_single(out, text),
            Style::Double => self.write_double(out, text),
            Style::Literal => self.write_literal(out, text, block),
        }
    }

    /// Writes the one-line form of a string key (after `tag`, if any) to
    /// `out` and returns `true`; or writes nothing and returns `false` when
    /// the key must use the explicit `? ` form (a multi-line string, one
    /// too long for an implicit key, or an unwritable tag). Written in
    /// place, then measured (Q4.4: no per-key buffer).
    pub(crate) fn write_implicit_key(
        &self,
        out: &mut String,
        text: &str,
        tag: Option<TagStr<'_>>,
    ) -> bool {
        // A key never becomes a literal block: `block` is irrelevant.
        let block = BlockIndent {
            content: self.indent,
            indicator: None,
        };
        let style = self.string_style(text, block);
        if style == Style::Literal {
            return false;
        }
        let start = out.len();
        let mut tag_len = 0usize;
        if let Some(t) = tag {
            // (A tag too long to read back: the node form reports it.)
            if !self.tag_fits(t.len()) || write_tag(out, t).is_err() {
                out.truncate(start);
                return false;
            }
            out.push(' ');
            tag_len = out.len().saturating_sub(start);
        }
        self.write_string(out, text, style, block);
        let fits = match self.flavor {
            Flavor::Default => out.len().saturating_sub(start) <= MAX_IMPLICIT_KEY,
            // libyaml: anchor + tag + value length, and never a key with a
            // line break (`\r`, NEL, LS, PS included) in it.
            Flavor::SerdeYaml => {
                text.len().saturating_add(tag_len.saturating_sub(1)) <= SERDE_YAML_MAX_SIMPLE_KEY
                    && !text.contains(['\n', '\r', '\u{85}', '\u{2028}', '\u{2029}'])
            }
        };
        if !fits {
            out.truncate(start);
        }
        fits
    }

    /// Whether a one-line scalar of `len` bytes (other than a string) fits
    /// an implicit key.
    pub(crate) fn short_key(&self, len: usize) -> bool {
        match self.flavor {
            Flavor::Default => len <= MAX_IMPLICIT_KEY,
            Flavor::SerdeYaml => len <= SERDE_YAML_MAX_SIMPLE_KEY,
        }
    }

    /// Writes a number: integers in decimal or `0x` hex (Q2.3 ruling: never
    /// `0o`; see `Number::to_core_string`); floats per
    /// [`Rules::write_float`].
    ///
    /// `Y3015` (Q2.3 ruling, extended 2026-09-28) for a wide integer with
    /// no linear Core form — a **negative** octal or hexadecimal, or a
    /// sexagesimal integer of **either sign** — past the 4,096-digit bound
    /// (its stored digits, octal counted as the hexadecimal it is stored
    /// as, or the digits of its decimal form): converting it to decimal
    /// would cost O(d²) (Q4.4), and neither `-0x…` nor base 60 is Core.
    pub(crate) fn write_number(&self, out: &mut String, n: &Number) -> Result<(), Error> {
        match n.as_float() {
            Some(f) => {
                self.write_float(out, f);
                Ok(())
            }
            None => n.write_core(out, MAX_CONVERTED_DIGITS).map_err(|()| {
                Error::new(
                    code::UNREPRESENTABLE_VALUE,
                    format!(
                        "a negative octal or hexadecimal integer, or a sexagesimal (base-60) \
                         integer of either sign, wider than {MAX_CONVERTED_DIGITS} digits has \
                         no linear YAML 1.2 Core form"
                    ),
                    None,
                )
                .with_help(
                    "Core reads `-0x…` and base 60 as strings, and converting to decimal is \
                     quadratic past this bound; write the integer in decimal, or as a string",
                )
            }),
        }
    }

    /// Writes a float (Q2.3): `.inf`, `-.inf`, `.nan`, or the shortest
    /// digits that read back exactly, always with a `.` or an exponent.
    ///
    /// Default layout: exponents are written as `1.0e+16` (a `.` and a
    /// signed exponent), the form YAML 1.1 readers also read as a float
    /// (their float pattern needs both), so the value is safe for every
    /// reader; written in place (Q4.4). `Layout::SerdeYaml`: `serde_yaml`'s
    /// (ryu's) layout, e.g. `1e16`, `0.00001`.
    pub(crate) fn write_float(&self, out: &mut String, f: f64) {
        if f.is_nan() {
            out.push_str(".nan");
        } else if f.is_infinite() {
            out.push_str(if f.is_sign_negative() {
                "-.inf"
            } else {
                ".inf"
            });
        } else {
            match self.flavor {
                Flavor::Default => every_reader_float(out, f),
                Flavor::SerdeYaml => out.push_str(&ryu_layout(f)),
            }
        }
    }

    /// [`Rules::write_float`] into a new string (tests).
    #[cfg(test)]
    pub(crate) fn float_text(&self, f: f64) -> String {
        let mut out = String::new();
        self.write_float(&mut out, f);
        out
    }

    fn write_double(&self, out: &mut String, text: &str) {
        out.push('"');
        for c in text.chars() {
            let escape = match self.flavor {
                Flavor::Default => needs_escape(c),
                // libyaml escapes every non-printable character, including
                // everything outside the Basic Multilingual Plane.
                Flavor::SerdeYaml => needs_escape(c) || !libyaml_printable(c),
            };
            if c == '"' || c == '\\' || escape {
                push_escape(out, c);
            } else {
                out.push(c);
            }
        }
        out.push('"');
    }

    fn write_literal(&self, out: &mut String, text: &str, block: BlockIndent) {
        out.push('|');
        let indicator = match self.flavor {
            Flavor::Default => first_content_line_indented(text),
            // libyaml: when the text starts with a space or a line break.
            Flavor::SerdeYaml => text.starts_with([' ', '\n']),
        };
        if let (true, Some(n)) = (indicator, block.indicator) {
            let _ = write!(out, "{n}");
        }
        let content = text.trim_end_matches('\n');
        let trailing = text.len().saturating_sub(content.len());
        // Clip keeps the final break only after content: a text of only
        // breaks needs keep (as libyaml writes it).
        out.push_str(match trailing {
            0 => "-",
            1 if !content.is_empty() => "",
            _ => "+",
        });
        let body = text.strip_suffix('\n').unwrap_or(text);
        for line in body.split('\n') {
            out.push('\n');
            if !line.is_empty() {
                push_spaces(out, block.content);
                out.push_str(line);
            }
        }
    }
}

/// Writes `null`.
pub(crate) fn write_null(out: &mut String) {
    out.push_str("null");
}

/// Writes `true` or `false`.
pub(crate) fn write_bool(out: &mut String, b: bool) {
    out.push_str(if b { "true" } else { "false" });
}

/// The core tags (`tag:yaml.org,2002:…`) oneyaml's resolver interprets; a
/// `Value::Tagged` with one of them would not read back as a tagged value.
const CORE_TAGS: [&str; 7] = ["null", "bool", "int", "float", "str", "seq", "map"];
const YAML_PREFIX: &str = "tag:yaml.org,2002:";

/// Writes the tag property for a resolved tag (Q2.3): `!local` and
/// `!!name` (for `tag:yaml.org,2002:name`) as shorthands, any other tag
/// verbatim as `!<…>`. Characters a tag cannot hold literally are
/// `%`-escaped (UTF-8), which the reader decodes back.
///
/// # Errors
///
/// `Err(())` for a tag no output reads back as the same custom tag: the
/// empty tag, the core tags the resolver interprets, and every tag with no
/// spec-valid form (YAML 1.2.2 §6.8.2.1, Example 6.25): `!` alone (the
/// non-specific tag, `!<!>` is invalid) and a global tag that is not a URI
/// with a scheme (`!<$:?>` is invalid).
#[allow(clippy::result_unit_err, reason = "the caller builds the diagnostic")]
///
/// Unit 42d: `tag` may be in two pieces (a shared `%TAG` prefix and its
/// suffix); it is written without joining them.
pub(crate) fn write_tag<'t>(out: &mut String, tag: impl Into<TagStr<'t>>) -> Result<(), ()> {
    let tag = tag.into();
    if tag.len() == 0 || tag.is("!") {
        return Err(());
    }
    if let Some(name) = tag.strip_prefix(YAML_PREFIX) {
        if CORE_TAGS.iter().any(|c| name.is(c)) {
            return Err(());
        }
        if name.len() > 0 {
            out.push_str("!!");
            percent_encode(out, name.chars(), tag_char);
            return Ok(());
        }
    }
    match tag.strip_prefix("!") {
        Some(local) if local.len() > 0 => write_local_tag(out, local)?,
        Some(_) => return Err(()),
        None if !has_uri_scheme(tag) => return Err(()),
        None => {
            out.push_str("!<");
            percent_encode(out, tag.chars(), uri_char);
            out.push('>');
        }
    }
    Ok(())
}

/// Writes the local tag `!name` for a non-empty `name`: what [`write_tag`]
/// writes for `!name`, without building that string (an enum variant's
/// tag, unit 38).
///
/// # Errors
///
/// `Err(())` for an empty `name` (`!` alone is the non-specific tag, not
/// a local tag; NN-4: the caller reports it as `Internal`), writing
/// nothing.
#[allow(clippy::result_unit_err, reason = "the caller builds the diagnostic")]
pub(crate) fn write_local_tag<'t>(out: &mut String, name: impl Into<TagStr<'t>>) -> Result<(), ()> {
    let name = name.into();
    if name.len() == 0 {
        return Err(());
    }
    out.push('!');
    percent_encode(out, name.chars(), tag_char);
    Ok(())
}

/// RFC 3986 `scheme ":"`: `ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )`
/// then `:`. A verbatim global tag must be a URI (§6.8.2.1).
fn has_uri_scheme(tag: TagStr<'_>) -> bool {
    let mut b = tag.bytes();
    if !b.next().is_some_and(|c| c.is_ascii_alphabetic()) {
        return false;
    }
    for c in b {
        match c {
            b':' => return true,
            c if c.is_ascii_alphanumeric() || matches!(c, b'+' | b'-' | b'.') => {}
            _ => return false,
        }
    }
    false
}

/// `ns-uri-char` without `%` (production 39): a `%` is always escaped.
fn uri_char(c: char) -> bool {
    c.is_ascii_alphanumeric() || "-#;/?:@&=+$,_.!~*'()[]".contains(c)
}

/// `ns-tag-char` without `%` (production 40): no `!` or flow indicators.
pub(crate) fn tag_char(c: char) -> bool {
    uri_char(c) && !"!,[]{}".contains(c)
}

fn percent_encode(out: &mut String, text: impl Iterator<Item = char>, keep: fn(char) -> bool) {
    let mut buf = [0u8; 4];
    for c in text {
        if keep(c) {
            out.push(c);
        } else {
            for b in c.encode_utf8(&mut buf).bytes() {
                let _ = write!(out, "%{b:02X}");
            }
        }
    }
}

/// Writes `n` spaces, a slice at a time (Q4.4: not a `char` at a time).
pub(crate) fn push_spaces(out: &mut String, n: usize) {
    const SPACES: &str = "                                                                ";
    let mut left = n;
    while left > 0 {
        let take = left.min(SPACES.len());
        out.push_str(SPACES.get(..take).unwrap_or(""));
        left = left.saturating_sub(take);
    }
}

/// `text.chars().any(needs_escape)`, over the UTF-8 bytes (Q4.4): C0
/// controls and DEL are single bytes; C1 controls (U+0080–U+009F) are
/// `C2 80..9F`; U+2028/U+2029 are `E2 80 A8/A9`; U+FEFF is `EF BB BF`;
/// U+FFFE/U+FFFF are `EF BF BE/BF`. Continuation bytes (`80..BF`) never
/// equal the lead bytes matched here, so a match starts at a character.
fn has_escape(text: &str) -> bool {
    let mut s = text.as_bytes();
    loop {
        match s {
            [] => return false,
            [0x00..=0x1f | 0x7f, ..]
            | [0xc2, 0x80..=0x9f, ..]
            | [0xe2, 0x80, 0xa8 | 0xa9, ..]
            | [0xef, 0xbb, 0xbf, ..]
            | [0xef, 0xbf, 0xbe | 0xbf, ..] => return true,
            [_, rest @ ..] => s = rest,
        }
    }
}

/// Whether the plain `text` reads as a string (or, under JSON, as nothing:
/// "no match" is not another type). A resolver failure counts as "not a
/// string", so the text gets quoted.
/// `serde_yaml`'s reader also takes a signed `0o` as an integer
/// (`[-+]0o[0-7]+`: `-0o17` is -15), which is neither YAML 1.1 nor 1.2 and
/// so is in no resolver setting (RULES Q2.1). Quoted like its other
/// numbers (unit 36c).
fn serde_yaml_signed_octal(text: &str) -> bool {
    text.strip_prefix(['-', '+'])
        .and_then(|t| t.strip_prefix("0o"))
        .is_some_and(|d| !d.is_empty() && d.bytes().all(|c| matches!(c, b'0'..=b'7')))
}

fn reads_as_string(r: &Resolver, text: &str) -> bool {
    matches!(
        r.resolve_scalar(TagInput::Absent, ResolveStyle::Plain, text),
        Ok(Resolved::Str) | Err(ResolveError::JsonUnmatched)
    )
}

/// Characters written escaped in double quotes and never plain or in single
/// quotes: C0 controls (tab and line breaks included), DEL, C1 controls
/// (NEL is a line break in YAML 1.1), the Unicode line and paragraph
/// separators (line breaks in YAML 1.1), the byte-order mark, and the
/// non-characters U+FFFE/U+FFFF (outside `c-printable`, production 1).
fn needs_escape(c: char) -> bool {
    matches!(c, '\0'..='\x1f' | '\x7f'..='\u{9f}' | '\u{2028}' | '\u{2029}' | '\u{feff}' | '\u{fffe}' | '\u{ffff}')
}

fn push_escape(out: &mut String, c: char) {
    let short = match c {
        '\0' => "\\0",
        '\x07' => "\\a",
        '\x08' => "\\b",
        '\t' => "\\t",
        '\n' => "\\n",
        '\x0b' => "\\v",
        '\x0c' => "\\f",
        '\r' => "\\r",
        '\x1b' => "\\e",
        '"' => "\\\"",
        '\\' => "\\\\",
        '\u{85}' => "\\N",
        '\u{2028}' => "\\L",
        '\u{2029}' => "\\P",
        _ => "",
    };
    if !short.is_empty() {
        out.push_str(short);
        return;
    }
    let n = u32::from(c);
    let _ = if n <= 0xff {
        write!(out, "\\x{n:02X}")
    } else if n <= 0xffff {
        write!(out, "\\u{n:04X}")
    } else {
        write!(out, "\\U{n:08X}")
    };
}

fn write_single(out: &mut String, text: &str) {
    out.push('\'');
    for c in text.chars() {
        if c == '\'' {
            out.push('\'');
        }
        out.push(c);
    }
    out.push('\'');
}

/// Syntax hazards of a plain scalar in block context (Q2.3, YAML 1.2.2
/// §7.3.3): empty, leading or trailing white space, a leading indicator,
/// `: ` / ` #` / a final `:`, a document marker, or a character that needs
/// escaping. `conservative` (the default layout) also refuses a leading `-`,
/// `?`, or `:` where the spec allows it (before a non-blank, production
/// 126), as `serde_yaml`'s libyaml does not.
///
/// Q4.4 (unit 38b): one pass over the bytes. Every hazard is ASCII, and
/// the bytes of a multi-byte character are all `>= 0x80`, so a byte test
/// decides exactly what the character test did (the reference form is
/// kept in the tests, `plain_syntax_reference`).
fn plain_syntax_ok(text: &str, conservative: bool) -> bool {
    let b = text.as_bytes();
    let Some(&first) = b.first() else {
        return false;
    };
    let spec_plain_first =
        matches!(first, b'-' | b'?' | b':') && b.get(1).is_some_and(|c| !matches!(c, b' ' | b'\t'));
    let indicator = matches!(
        first,
        b'-' | b'?'
            | b':'
            | b','
            | b'['
            | b']'
            | b'{'
            | b'}'
            | b'#'
            | b'&'
            | b'*'
            | b'!'
            | b'|'
            | b'>'
            | b'\''
            | b'"'
            | b'%'
            | b'@'
            | b'`'
    ) && (conservative || !spec_plain_first);
    if indicator
        || matches!(first, b' ' | b'\t')
        || matches!(b.last(), Some(b' ' | b'\t' | b':'))
        || b.starts_with(b"---")
        || b.starts_with(b"...")
    {
        return false;
    }
    let mut prev = 0u8;
    for &c in b {
        if (prev == b':' && c == b' ') || (prev == b' ' && c == b'#') {
            return false;
        }
        prev = c;
    }
    !has_escape(text)
}

/// YAML 1.1 types our resolver does not model, which 1.1 readers (PyYAML,
/// Go `yaml.v2`, …) would not read as a string (yaml.org/type): the merge
/// key `<<`, the value key `=`, timestamps (`2001-12-14`, with or without
/// a time), the full 1.1 float pattern (`1.2.3`, `.`), and 1.1 integers of
/// only `_` digits (`0x_`, `0b_`).
///
/// Also the non-string readings of the other readers the Q2.3 ruling names
/// (RULES Q2.3, 2026-09-27): Ruby Psych ([`psych_number`]) and Go
/// `yaml.v2`/`v3` ([`go_number`]).
fn foreign_yaml11(text: &str) -> bool {
    // Q4.4 (unit 38b): text led by an ASCII letter other than `e`/`E`
    // can only be one of Psych's words. Every other form needs a digit,
    // sign, `.`, `<`, or `=` first, and Psych's comma-grouped numbers
    // allow no letter but `e`/`E` (the reference is kept in the tests).
    // Q4.6: MSRV 1.85 has no let-chains.
    if let Some(&c) = text.as_bytes().first() {
        if c.is_ascii_alphabetic() && !matches!(c, b'e' | b'E') {
            return psych_word(text);
        }
    }
    matches!(text, "<<" | "=")
        || timestamp_like(text)
        || yaml11_float(text)
        || underscore_int(text)
        || prefixed_int_any_case(text)
        || psych_number(text)
        || go_number(text)
}

/// Ruby Psych's scalar scanner (Q2.3 every-reader ruling), conservatively:
///
/// * `null`, `yes`/`true`/`on`, and `no`/`false`/`off` in **any** letter
///   case (`tRUE`, `nULL`, `yEs`, `oFF`): Psych matches them with `/i`;
/// * (in [`prefixed_int_any_case`]) binary and hexadecimal with `,`
///   anywhere after the prefix (`0b1,0`, `0x1,F`, `0x,1F`): Psych's legacy
///   integer pattern allows `,` there and deletes `,`/`_` before parsing;
/// * comma-grouped numbers (`1,000`, `1,2`, `+1,000`, `0,7`, `1,000.5`,
///   `1,000e+3`): Psych deletes `,` and `_` before parsing, so any text of
///   only digits, `,`, `_`, `.`, and an exponent, with a `,` and a digit,
///   is quoted;
/// * base 60 led by **any** digit (`09:15`, `00:30`, `0:30`, `1:2.5`):
///   `[-+]?[0-9][0-9_]*(:[0-9]{1,2})+(\.[0-9_]*)?`;
/// * `.inf`, `+.inf`, `-.inf`, `.nan` in any letter case (`.iNf`).
fn psych_number(text: &str) -> bool {
    if psych_word(text) {
        return true;
    }
    let s = text.strip_prefix(['-', '+']).unwrap_or(text);
    if s.eq_ignore_ascii_case(".inf") || s.eq_ignore_ascii_case(".nan") {
        return true;
    }
    let numeric =
        |c: u8| c.is_ascii_digit() || matches!(c, b',' | b'_' | b'.' | b'e' | b'E' | b'+' | b'-');
    if s.contains(',') && s.bytes().any(|c| c.is_ascii_digit()) && s.bytes().all(numeric) {
        return true;
    }
    psych_base60(s)
}

/// Psych's null and boolean words, in any letter case.
fn psych_word(text: &str) -> bool {
    const WORDS: [&str; 7] = ["null", "yes", "true", "on", "no", "false", "off"];
    (2..=5).contains(&text.len()) && WORDS.iter().any(|w| text.eq_ignore_ascii_case(w))
}

/// `[0-9][0-9_]*(:[0-9]{1,2})+(\.[0-9_]*)?` (after the sign).
fn psych_base60(s: &str) -> bool {
    let b = s.as_bytes();
    if !b.first().is_some_and(u8::is_ascii_digit) {
        return false;
    }
    let mut i = b
        .iter()
        .take_while(|c| c.is_ascii_digit() || **c == b'_')
        .count();
    let mut groups = 0usize;
    while b.get(i) == Some(&b':') {
        i = i.saturating_add(1);
        let n = b
            .iter()
            .skip(i)
            .take(2)
            .take_while(|c| c.is_ascii_digit())
            .count();
        if n == 0 {
            return false;
        }
        i = i.saturating_add(n);
        groups = groups.saturating_add(1);
    }
    if groups == 0 {
        return false;
    }
    if b.get(i) == Some(&b'.') {
        i = i.saturating_add(1);
        i = i.saturating_add(
            b.iter()
                .skip(i)
                .take_while(|c| c.is_ascii_digit() || **c == b'_')
                .count(),
        );
    }
    i == b.len()
}

/// Go `yaml.v2`/`v3` (Q2.3 every-reader ruling): a plain scalar starting
/// with a digit, a sign, or `.` has **every** `_` removed, then is parsed
/// with `strconv.ParseInt`/`ParseUint` (base prefix `0x`/`0o`/`0b`, any
/// case, or a leading `0` for octal) or, when it matches the YAML float
/// pattern, `strconv.ParseFloat` (`+_1`, `1e_5`, `0_x1F`, `1_e_5`).
fn go_number(text: &str) -> bool {
    if !text.starts_with(|c: char| c.is_ascii_digit() || matches!(c, '-' | '+' | '.')) {
        return false;
    }
    if !text.contains('_') {
        return go_int(text.as_bytes()) || go_float(text.as_bytes());
    }
    // Only number-like text with `_` gets here: the copy is rare (Q4.4).
    let plain: Vec<u8> = text.bytes().filter(|c| *c != b'_').collect();
    go_int(&plain) || go_float(&plain)
}

/// `strconv.ParseInt(s, 0, _)` syntax (any width).
fn go_int(s: &[u8]) -> bool {
    let s = s
        .strip_prefix(b"-")
        .or_else(|| s.strip_prefix(b"+"))
        .unwrap_or(s);
    let digits = |d: &[u8], ok: fn(&u8) -> bool| !d.is_empty() && d.iter().all(ok);
    match s {
        [b'0', b'x' | b'X', rest @ ..] => digits(rest, u8::is_ascii_hexdigit),
        [b'0', b'b' | b'B', rest @ ..] => digits(rest, |c| matches!(c, b'0' | b'1')),
        [b'0', b'o' | b'O', rest @ ..] => digits(rest, |c| matches!(c, b'0'..=b'7')),
        _ => digits(s, u8::is_ascii_digit),
    }
}

/// yaml.v2/v3 `yamlStyleFloat`:
/// `^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$`.
fn go_float(s: &[u8]) -> bool {
    let s = s
        .strip_prefix(b"-")
        .or_else(|| s.strip_prefix(b"+"))
        .unwrap_or(s);
    let run = |s: &[u8], at: usize| s.iter().skip(at).take_while(|c| c.is_ascii_digit()).count();
    let mut i;
    if s.first() == Some(&b'.') {
        let n = run(s, 1);
        if n == 0 {
            return false;
        }
        i = n.saturating_add(1);
    } else {
        i = run(s, 0);
        if i == 0 {
            return false;
        }
        if s.get(i) == Some(&b'.') {
            i = i.saturating_add(1);
            i = i.saturating_add(run(s, i));
        }
    }
    if matches!(s.get(i), Some(b'e' | b'E')) {
        i = i.saturating_add(1);
        if matches!(s.get(i), Some(b'-' | b'+')) {
            i = i.saturating_add(1);
        }
        let n = run(s, i);
        if n == 0 {
            return false;
        }
        i = i.saturating_add(n);
    }
    i == s.len()
}

/// `[-+]?0[xXoObB][0-9a-fA-F_,]+`: radix prefixes in any case, which some
/// readers (serde-saphyr: `0X1F`) take for numbers though no schema does,
/// and with `,` among the digits, which Ruby Psych's legacy integer
/// pattern accepts for binary and hexadecimal (`0b1,0` is 2, `0x,1F` is
/// 31: it deletes `,` and `_` before parsing).
fn prefixed_int_any_case(text: &str) -> bool {
    let s = text.strip_prefix(['-', '+']).unwrap_or(text);
    let mut b = s.bytes();
    b.next() == Some(b'0')
        && matches!(b.next(), Some(b'x' | b'X' | b'o' | b'O' | b'b' | b'B'))
        && s.len() > 2
        && b.all(|c| c.is_ascii_hexdigit() || matches!(c, b'_' | b','))
}

/// `[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}` followed by the end, `T`/`t`, or
/// white space: conservative for yaml.org/type/timestamp.
fn timestamp_like(text: &str) -> bool {
    let b = text.as_bytes();
    let digits = |from: usize, min: usize, max: usize| -> Option<usize> {
        let n = b
            .iter()
            .skip(from)
            .take(max)
            .take_while(|c| c.is_ascii_digit())
            .count();
        (n >= min).then(|| from.saturating_add(n))
    };
    let dash = |at: usize| (b.get(at) == Some(&b'-')).then(|| at.saturating_add(1));
    let end = digits(0, 4, 4)
        .and_then(dash)
        .and_then(|i| digits(i, 1, 2))
        .and_then(dash)
        .and_then(|i| digits(i, 1, 2));
    match end {
        Some(i) => matches!(b.get(i), None | Some(b'T' | b't' | b' ' | b'\t')),
        None => false,
    }
}

/// The YAML 1.1 float pattern (yaml.org/type/float):
/// `[-+]?([0-9][0-9_]*)?\.[0-9.]*([eE][-+][0-9]+)?`.
fn yaml11_float(text: &str) -> bool {
    let s = text.strip_prefix(['-', '+']).unwrap_or(text);
    let b = s.as_bytes();
    let mut i = 0usize;
    if b.first().is_some_and(u8::is_ascii_digit) {
        i = b
            .iter()
            .take_while(|c| c.is_ascii_digit() || **c == b'_')
            .count();
    }
    if b.get(i) != Some(&b'.') {
        return false;
    }
    i = i.saturating_add(1);
    i = i.saturating_add(
        b.iter()
            .skip(i)
            .take_while(|c| c.is_ascii_digit() || **c == b'.')
            .count(),
    );
    if matches!(b.get(i), Some(b'e' | b'E')) {
        i = i.saturating_add(1);
        if !matches!(b.get(i), Some(b'-' | b'+')) {
            return false;
        }
        i = i.saturating_add(1);
        let n = b.iter().skip(i).take_while(|c| c.is_ascii_digit()).count();
        if n == 0 {
            return false;
        }
        i = i.saturating_add(n);
    }
    i == b.len()
}

/// `[-+]?0[xb]_+`: 1.1 integer patterns allow digit runs of only `_`.
fn underscore_int(text: &str) -> bool {
    let s = text.strip_prefix(['-', '+']).unwrap_or(text);
    s.strip_prefix("0x")
        .or_else(|| s.strip_prefix("0b"))
        .is_some_and(|d| !d.is_empty() && d.bytes().all(|c| c == b'_'))
}

/// Whether the first non-empty line of `text` starts with a space, so a
/// literal block needs an indentation indicator (auto-detection would
/// misread it, §8.1.1.1).
fn first_content_line_indented(text: &str) -> bool {
    text.split('\n')
        .find(|l| !l.is_empty())
        .is_some_and(|l| l.starts_with(' '))
}

/// Whether a literal block represents `text` exactly at `block`.
///
/// Default: some line has content (strings of only line breaks are
/// double-quoted), no character needs escaping other than `\n` and tab,
/// and an indentation indicator, if needed, is a single digit.
/// `serde_yaml` layout (`libyaml`): libyaml's own conditions were checked
/// by the caller; only the indicator limit applies here.
fn literal_ok(text: &str, block: BlockIndent, serde_yaml: bool) -> bool {
    // libyaml-based readers reject a tab at the start of the first content
    // line (they take it for indentation), so that text is double-quoted.
    let chars_ok = serde_yaml
        || (text.chars().any(|c| c != '\n')
            && !text
                .chars()
                .any(|c| c != '\n' && c != '\t' && needs_escape(c))
            && !text
                .split('\n')
                .find(|l| !l.is_empty())
                .is_some_and(|l| l.starts_with('\t')));
    let indicator = if serde_yaml {
        text.starts_with([' ', '\n'])
    } else {
        first_content_line_indented(text)
    };
    chars_ok && (!indicator || block.indicator.is_some_and(|n| (1..=9).contains(&n)))
}

/// `IS_PRINTABLE` of the libyaml `serde_yaml_ng` uses (`unsafe-libyaml`):
/// `\n`, `0x20–0x7E`, `U+00A0–U+D7FF`, `U+E000–U+FFFD` except U+FEFF, and
/// everything outside the Basic Multilingual Plane.
fn libyaml_printable(c: char) -> bool {
    matches!(c, '\n' | ' '..='~' | '\u{a0}'..='\u{d7ff}' | '\u{e000}'..='\u{fffd}' | '\u{10000}'..)
        && c != '\u{feff}'
}

/// libyaml's `yaml_emitter_analyze_scalar`, for block context with
/// unicode output on: which styles libyaml allows for a string.
#[derive(Clone, Copy, Debug)]
struct Analysis {
    plain: bool,
    single: bool,
    block: bool,
}

impl Analysis {
    fn of(text: &str) -> Self {
        if text.is_empty() {
            return Self {
                plain: false,
                single: true,
                block: false,
            };
        }
        let is_break = |c: char| matches!(c, '\r' | '\n' | '\u{85}' | '\u{2028}' | '\u{2029}');
        let is_blankz = |c: Option<char>| c.is_none_or(|c| matches!(c, ' ' | '\t') || is_break(c));
        let mut block_indicators = text.starts_with("---") || text.starts_with("...");
        let (mut special, mut line_breaks) = (false, false);
        let (mut leading_space, mut leading_break) = (false, false);
        let (mut trailing_space, mut trailing_break) = (false, false);
        let (mut break_space, mut space_break) = (false, false);
        let (mut previous_space, mut previous_break) = (false, false);
        let mut preceded_by_ws = true;
        // Q4.4: one pass with one character of look-ahead, no buffer.
        let mut chars = text.chars().peekable();
        let mut first = true;
        while let Some(c) = chars.next() {
            let next = chars.peek().copied();
            let is_last = next.is_none();
            let followed_by_ws = is_blankz(next);
            if first {
                if "#,[]{}&*!|>'\"%@`".contains(c) || (c == '-' && followed_by_ws) {
                    block_indicators = true;
                }
                if (c == '?' || c == ':') && followed_by_ws {
                    block_indicators = true;
                }
            } else if (c == ':' && followed_by_ws) || (c == '#' && preceded_by_ws) {
                block_indicators = true;
            }
            if !libyaml_printable(c) {
                special = true;
            }
            if is_break(c) {
                line_breaks = true;
            }
            if c == ' ' {
                leading_space |= first;
                trailing_space |= is_last;
                break_space |= previous_break;
                previous_space = true;
                previous_break = false;
            } else if is_break(c) {
                leading_break |= first;
                trailing_break |= is_last;
                space_break |= previous_space;
                previous_break = true;
                previous_space = false;
            } else {
                previous_space = false;
                previous_break = false;
            }
            preceded_by_ws = is_blankz(Some(c));
            first = false;
        }
        let mut a = Self {
            plain: true,
            single: true,
            block: true,
        };
        if leading_space || leading_break || trailing_space || trailing_break {
            a.plain = false;
        }
        if trailing_space {
            a.block = false;
        }
        if break_space || space_break || special {
            a.plain = false;
            a.single = false;
        }
        if space_break || special {
            a.block = false;
        }
        if line_breaks || block_indicators {
            a.plain = false;
        }
        // Deviation (listed in the compat divergences): libyaml would write
        // U+2028/U+2029 as line breaks inside single quotes, which YAML 1.2
        // reads as other text. Double quotes escape them.
        if text.contains(['\u{2028}', '\u{2029}']) {
            a.single = false;
        }
        a
    }
}

/// `{:e}` split into (digits without `.`, decimal exponent of the first).
fn sci_parts(f: f64) -> (String, i32) {
    let s = format!("{:e}", f.abs());
    let (mantissa, exp) = s.split_once('e').unwrap_or((&s, "0"));
    let digits: String = mantissa.chars().filter(char::is_ascii_digit).collect();
    (digits, exp.parse().unwrap_or(0))
}

/// Q2.3 default float text: Rust's shortest round-trip form, with an
/// exponent always written as `d.ddde±X` (see [`Rules::write_float`]),
/// written and fixed up in place in `out` (Q4.4).
fn every_reader_float(out: &mut String, f: f64) {
    let start = out.len();
    let _ = write!(out, "{f:?}");
    let Some(e) = out
        .get(start..)
        .and_then(|s| s.find('e'))
        .map(|i| start.saturating_add(i))
    else {
        return;
    };
    let after = e.saturating_add(1);
    if !out.get(after..).is_some_and(|x| x.starts_with('-')) {
        out.insert(after, '+');
    }
    if !out.get(start..e).is_some_and(|m| m.contains('.')) {
        out.insert_str(e, ".0");
    }
}

/// ryu's `format64` layout from the shortest digits (what `serde_yaml`
/// prints for an `f64`).
fn ryu_layout(f: f64) -> String {
    let mut out = String::new();
    if f.is_sign_negative() {
        out.push('-');
    }
    if f == 0.0 {
        out.push_str("0.0");
        return out;
    }
    let (digits, e) = sci_parts(f);
    let length = i64::try_from(digits.len()).unwrap_or(i64::MAX);
    // `kk`: position of the decimal point; `k`: exponent of the last digit.
    let kk = i64::from(e).saturating_add(1);
    let k = kk.saturating_sub(length);
    if (0..=16).contains(&kk) && k >= 0 {
        out.push_str(&digits);
        out.extend(std::iter::repeat_n('0', usize::try_from(k).unwrap_or(0)));
        out.push_str(".0");
    } else if 0 < kk && kk <= 16 {
        let at = usize::try_from(kk).unwrap_or(0);
        let (a, b) = digits.split_at(at.min(digits.len()));
        out.push_str(a);
        out.push('.');
        out.push_str(b);
    } else if -5 < kk && kk <= 0 {
        out.push_str("0.");
        out.extend(std::iter::repeat_n(
            '0',
            usize::try_from(kk.saturating_neg()).unwrap_or(0),
        ));
        out.push_str(&digits);
    } else {
        let mut chars = digits.chars();
        if let Some(d) = chars.next() {
            out.push(d);
        }
        let rest: String = chars.collect();
        if !rest.is_empty() {
            out.push('.');
            out.push_str(&rest);
        }
        let _ = write!(out, "e{}", kk.saturating_sub(1));
    }
    out
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]
    use super::*;

    fn rules(emit: Emit) -> Rules {
        Rules::new(&emit, &Settings::new()).unwrap()
    }

    const NODE: BlockIndent = BlockIndent {
        content: 2,
        indicator: Some(2),
    };

    fn written(r: &Rules, s: &str) -> String {
        let mut out = String::new();
        let style = r.string_style(s, NODE);
        r.write_string(&mut out, s, style, NODE);
        out
    }

    #[test]
    fn every_reader_safe_quoting() {
        let r = rules(Emit::new());
        for s in [
            "",
            " a",
            "a ",
            "#a",
            "a #b",
            "a: b",
            "a:",
            "-",
            "-a",
            "?a",
            ":a",
            "no",
            "yes",
            "on",
            "off",
            "y",
            "n",
            "Y",
            "~",
            "0777",
            "1_000",
            "12:30",
            ".inf",
            "null",
            "123",
            "0x1F",
            "0b1",
            "1e3",
            "1.2.3",
            ".",
            "2001-12-14",
            "2001-1-2t",
            "<<",
            "=",
            "---",
            "...",
            "true",
            "NULL",
            "+1",
            "0o7",
            "0x_",
            "-0b_",
            "@a",
            "`a",
            "%a",
            "!a",
            "&a",
            "*a",
            "|a",
            ">a",
            "[a",
            "]a",
            "{a",
            "}a",
            ",a",
            "'a",
            "\"a",
        ] {
            let w = written(&r, s);
            assert!(w.starts_with('\''), "{s:?} => {w:?}");
        }
        for s in ["\u{a0}", "a\u{2003}", "\u{3000}a"] {
            assert_eq!(written(&r, s), format!("'{s}'"), "{s:?}");
        }
        for s in [
            "a", "a b", "a,b", "a[b", "é", "a'b", "a\"b", "2001-12", "x.y", "1.2.x", "a\u{a0}b",
        ] {
            assert_eq!(written(&r, s), s, "{s:?}");
        }
        assert_eq!(written(&r, "a'b "), "'a''b '");
        assert_eq!(written(&r, "a\tb"), "\"a\\tb\"");
        assert_eq!(written(&r, "a\rb"), "\"a\\rb\"");
        assert_eq!(
            written(&r, "\u{1}\u{7f}\u{85}\u{feff}\u{2028}\u{ffff}\u{a0}"),
            "\"\\x01\\x7F\\N\\uFEFF\\L\\uFFFF\u{a0}\""
        );
        assert_eq!(written(&r, "\\ \""), "\\ \"");
        let d = rules(Emit::new().quote_style(QuoteStyle::Double));
        assert_eq!(written(&d, "yes"), "\"yes\"");
        assert_eq!(written(&d, "a"), "a");
    }

    #[test]
    fn literal_blocks_and_chomping() {
        let r = rules(Emit::new());
        assert_eq!(written(&r, "a\nb"), "|-\n  a\n  b");
        assert_eq!(written(&r, "a\nb\n"), "|\n  a\n  b");
        assert_eq!(written(&r, "a\nb\n\n"), "|+\n  a\n  b\n");
        assert_eq!(written(&r, "\na"), "|-\n\n  a");
        assert_eq!(written(&r, " a\nb"), "|2-\n   a\n  b");
        assert_eq!(written(&r, "\n a"), "|2-\n\n   a");
        assert_eq!(written(&r, "a\n\tb"), "|-\n  a\n  \tb");
        assert_eq!(written(&r, "\ta\nb"), "\"\\ta\\nb\"");
        assert_eq!(written(&r, "\n\ta"), "\"\\n\\ta\"");
        // Only line breaks, or characters a block cannot hold: double.
        assert_eq!(written(&r, "\n"), "\"\\n\"");
        assert_eq!(written(&r, "\n\n"), "\"\\n\\n\"");
        assert_eq!(written(&r, "a\r\nb"), "\"a\\r\\nb\"");
        // An indicator must be one digit.
        let wide = BlockIndent {
            content: 10,
            indicator: Some(10),
        };
        assert_eq!(r.string_style(" a\nb", wide), Style::Double);
        assert_eq!(r.string_style("a\nb", wide), Style::Literal);
        // No indicator possible (the root): double quotes.
        let root = BlockIndent {
            content: 2,
            indicator: None,
        };
        assert_eq!(r.string_style(" a\nb", root), Style::Double);
        assert_eq!(r.string_style("a\nb", root), Style::Literal);
    }

    #[test]
    fn floats() {
        let r = rules(Emit::new());
        let s = rules(Emit::new().layout(Layout::SerdeYaml));
        for (f, ours, theirs) in [
            (1.0, "1.0", "1.0"),
            (0.1, "0.1", "0.1"),
            (1e300, "1.0e+300", "1e300"),
            (1e16, "1.0e+16", "1e16"),
            (1e15, "1000000000000000.0", "1000000000000000.0"),
            (1e-5, "1.0e-5", "0.00001"),
            (1e-7, "1.0e-7", "1e-7"),
            (1.5e-7, "1.5e-7", "1.5e-7"),
            (-0.0, "-0.0", "-0.0"),
            (123_456_789.125, "123456789.125", "123456789.125"),
            (1.25e21, "1.25e+21", "1.25e21"),
            (-2.5e-300, "-2.5e-300", "-2.5e-300"),
            (f64::INFINITY, ".inf", ".inf"),
            (f64::NEG_INFINITY, "-.inf", "-.inf"),
            (f64::NAN, ".nan", ".nan"),
        ] {
            assert_eq!(r.float_text(f), ours, "{f:?}");
            assert_eq!(s.float_text(f), theirs, "{f:?}");
            if f.is_finite() {
                assert_eq!(ours.parse::<f64>().unwrap().to_bits(), f.to_bits());
            }
        }
    }

    #[test]
    fn tags() {
        let t = |tag: &str| {
            let mut out = String::new();
            write_tag(&mut out, tag).map(|()| out)
        };
        assert_eq!(t("!color").unwrap(), "!color");
        assert_eq!(t("!a!b c").unwrap(), "!a%21b%20c");
        assert_eq!(t("!é").unwrap(), "!%C3%A9");
        // Q2.1 §6.8.2.1 Example 6.25: `!<!>` and `!<$:?>` are invalid, so
        // those tags have no written form.
        assert!(t("!").is_err());
        assert!(t("$:?").is_err());
        for bad in ["x", "a b", ":x", "1a:b", "a_b:c", "é:x"] {
            assert!(t(bad).is_err(), "{bad}");
        }
        assert_eq!(t("urn:x").unwrap(), "!<urn:x>");
        assert_eq!(t("a+b.c-d:x y").unwrap(), "!<a+b.c-d:x%20y>");
        assert_eq!(t("!!").unwrap(), "!%21");
        assert_eq!(t("tag:yaml.org,2002:binary").unwrap(), "!!binary");
        assert_eq!(
            t("tag:example.com,2000:a/b").unwrap(),
            "!<tag:example.com,2000:a/b>"
        );
        assert_eq!(t("tag:yaml.org,2002:").unwrap(), "!<tag:yaml.org,2002:>");
        assert!(t("").is_err());
        assert!(t("tag:yaml.org,2002:str").is_err());
    }

    /// Q2.3 every-reader ruling (RULES Q2.3, 2026-09-27): Psych and Go.
    #[test]
    fn psych_and_go_readings_are_quoted() {
        let r = rules(Emit::new());
        for s in [
            // Psych: comma-grouped integers and floats.
            "1,000", "1,2", "+1,000", "-1,000", "0,7", "1,000.5", "1,000e+3", ",1", "1,",
            // Psych: base 60 led by any digit, one or two groups.
            "09:15", "00:30", "0:30", "1:2", "-0:30", "0:30.5", "1_0:30",
            // Psych: `.inf`/`.nan` in any case.
            ".iNf", "+.INf", "-.InF", ".nAn", ".NAn",
            // Psych: null and booleans in any case (Unit 20c finding 5).
            "tRUE", "nULL", "yEs", "oFF", "FaLsE", "nO", "oN", "NuLl", "TrUe",
            // Psych: `,` in binary and hexadecimal digits.
            "0b1,0", "0x1,F", "0x,1F", "-0b,1", "+0x1,f",
            // Go: every `_` stripped before ParseInt/ParseFloat.
            "+_1", "1e_5", "0_x1F", "1_e_5", "-_1", "1__0", "0_b1", "0_o7", ".5_", "1._5",
        ] {
            assert!(foreign_yaml11(s), "{s} is foreign");
            let w = written(&r, s);
            assert_eq!(w, format!("'{s}'"), "{s:?}");
        }
        // Non-numbers stay plain.
        for s in ["a,b", "x_1", "1:a", "infinity_x", "1.2.x"] {
            assert_eq!(written(&r, s), s, "{s:?}");
        }
        // The `serde_yaml` layout keeps serde_yaml's choices (Q2.3).
        let c = rules(Emit::new().layout(Layout::SerdeYaml));
        assert_eq!(written(&c, "1,000"), "1,000");
    }

    #[test]
    fn implicit_keys() {
        // Written after existing text, which a refused key leaves intact.
        let key = |r: &Rules, text: &str, tag: Option<&str>| {
            let mut out = String::from("- ");
            let ok = r.write_implicit_key(&mut out, text, tag.map(TagStr::from));
            assert_eq!(ok, out.len() > 2, "{text:?}");
            ok.then(|| out.split_off(2))
        };
        let r = rules(Emit::new());
        assert_eq!(key(&r, "a", None).unwrap(), "a");
        assert_eq!(key(&r, "yes", Some("!t")).unwrap(), "!t 'yes'");
        assert_eq!(key(&r, "a\nb", None), None);
        assert_eq!(key(&r, "a", Some("!")), None);
        assert!(key(&r, &"x".repeat(1024), None).is_some());
        assert_eq!(key(&r, &"x".repeat(1025), None), None);
        assert_eq!(key(&r, &"x".repeat(1020), Some("!tag")), None);
        let s = rules(Emit::new().layout(Layout::SerdeYaml));
        assert!(key(&s, &"x".repeat(128), None).is_some());
        assert_eq!(key(&s, &"x".repeat(129), None), None);
        assert!(r.short_key(1024) && !r.short_key(1025));
        assert!(s.short_key(128) && !s.short_key(129));
    }

    /// Unit 37 (Q4.4): skipping the reading resolver when it adds nothing to
    /// Core, and not re-checking Core, decide exactly what the unit-36
    /// formula (Core, JSON, 1.1, then Core, active, 1.1 under
    /// `honor_version_directive`, merge `<<`) decides.
    #[test]
    fn reading_floor_shortcut_is_exact() {
        let old_floor = |reading: &Settings, text: &str| {
            let core = Resolver::new(&Settings::new());
            let active = Resolver::new(&reading.clone().schema(Schema::Core));
            let yaml11 = Resolver::new(&Compat::yaml_1_1());
            let honor = reading.get_honor_version_directive();
            let merge = reading.get_merge_keys() || honor;
            reads_as_string(&core, text)
                && reads_as_string(&active, text)
                && (!honor || reads_as_string(&yaml11, text))
                && !(merge && text == "<<")
        };
        let readings = [
            Settings::new(),
            Settings::new().merge_keys(true),
            Settings::new().honor_version_directive(true),
            Settings::new().yaml11_booleans(true),
            Settings::new().legacy_octal(true),
            Settings::new().sexagesimal(true),
            Settings::new().digit_separators(true),
            Settings::new().yaml11_int_forms(true),
            Compat::serde_yaml(),
            Compat::yaml_1_1(),
            Settings::new().schema(Schema::Json),
        ];
        let texts = [
            "a", "yes", "No", "on", "y", "0777", "08", "1_000", "12:30", "0b101", "-0x1F", "+0x1",
            "-0o17", "<<", "=", "null", "~", "1.5", ".inf", "true", "x_1", "a b",
        ];
        for reading in &readings {
            let plain_core = Resolver::new(&reading.clone().schema(Schema::Core)).plain_is_core();
            let expect_core = !(reading.get_yaml11_booleans()
                || reading.get_legacy_octal()
                || reading.get_sexagesimal()
                || reading.get_digit_separators()
                || reading.get_yaml11_int_forms());
            assert_eq!(plain_core, expect_core, "{reading:?}");
            for layout in [Layout::Default, Layout::SerdeYaml] {
                let r = Rules::new(&Emit::new().layout(layout), reading).unwrap();
                assert_eq!(r.active.is_none(), plain_core);
                for t in texts {
                    assert_eq!(r.reads_back_as_string(t), old_floor(reading, t), "{t:?}");
                    let old_plain = !t.starts_with(char::is_whitespace)
                        && !t.ends_with(char::is_whitespace)
                        && plain_syntax_ok(t, true)
                        && reads_as_string(&r.core, t)
                        && reads_as_string(&r.json, t)
                        && reads_as_string(&r.yaml11, t)
                        && old_floor(reading, t)
                        && !foreign_yaml11(t);
                    assert_eq!(r.plain_ok(t), old_plain, "{t:?} {reading:?}");
                }
            }
        }
        // Failsafe and JSON reading schemas are read as Core here (the
        // caller refuses them first); an unknown schema never arises.
        assert!(Resolver::new(&Settings::new()).plain_is_core());
        assert!(!Resolver::new(&Settings::new().schema(Schema::Json)).plain_is_core());
        assert!(!Resolver::new(&Settings::new().schema(Schema::Failsafe)).plain_is_core());
    }

    /// The character-based forms the byte-based fast paths replaced
    /// (unit 38b, Q4.4); they must decide exactly the same.
    fn plain_syntax_reference(text: &str, conservative: bool) -> bool {
        let mut chars = text.chars();
        let Some(first) = chars.next() else {
            return false;
        };
        let spec_plain_first =
            "-?:".contains(first) && chars.next().is_some_and(|c| !matches!(c, ' ' | '\t'));
        let indicator =
            "-?:,[]{}#&*!|>'\"%@`".contains(first) && (conservative || !spec_plain_first);
        !(text.starts_with([' ', '\t'])
            || text.ends_with([' ', '\t'])
            || indicator
            || text.contains(": ")
            || text.contains(" #")
            || text.ends_with(':')
            || text.starts_with("---")
            || text.starts_with("...")
            || text.chars().any(needs_escape))
    }

    fn foreign_reference(text: &str) -> bool {
        const WORDS: [&str; 7] = ["null", "yes", "true", "on", "no", "false", "off"];
        matches!(text, "<<" | "=")
            || timestamp_like(text)
            || yaml11_float(text)
            || underscore_int(text)
            || prefixed_int_any_case(text)
            || WORDS.iter().any(|w| text.eq_ignore_ascii_case(w))
            || psych_number(text)
            || go_number(text)
    }

    /// Every string of up to three characters over an alphabet of the
    /// characters the rules look at, plus longer samples.
    fn samples() -> Vec<String> {
        let alphabet = [
            "a",
            "e",
            "E",
            "n",
            "N",
            "o",
            "O",
            "f",
            "t",
            "T",
            "y",
            "x",
            "b",
            "0",
            "1",
            "9",
            ".",
            ",",
            "_",
            ":",
            "-",
            "+",
            "?",
            " ",
            "#",
            "<",
            "=",
            "\t",
            "'",
            "é",
            "\u{85}",
            "\u{a0}",
            "\u{2028}",
            "\u{feff}",
            "\u{ffff}",
            "\u{fffd}",
            "\u{1f600}",
            "\u{7f}",
        ];
        let mut out = vec![String::new()];
        for a in alphabet {
            out.push(a.to_owned());
            for b in alphabet {
                out.push(format!("{a}{b}"));
                for c in alphabet {
                    out.push(format!("{a}{b}{c}"));
                }
            }
        }
        for s in [
            "null",
            "NuLl",
            "false",
            "FALSE",
            "off",
            "On",
            "yes",
            "true",
            "e,1",
            "E1,0",
            "e",
            "e_1",
            "eE,+",
            "nulls",
            "no-",
            "1,000",
            "2001-12-14",
            "0x1F",
            "1:30",
            ".inf",
            "a: b",
            "a #b",
            "---a",
            "...a",
            "a:",
            "-a",
            "?a",
            ":a",
            "- a",
            "é: x",
        ] {
            out.push(s.to_owned());
        }
        out
    }

    #[test]
    fn byte_fast_paths_decide_as_before() {
        for s in samples() {
            for conservative in [true, false] {
                assert_eq!(
                    plain_syntax_ok(&s, conservative),
                    plain_syntax_reference(&s, conservative),
                    "{s:?} {conservative}"
                );
            }
            assert_eq!(foreign_yaml11(&s), foreign_reference(&s), "{s:?}");
            assert_eq!(has_escape(&s), s.chars().any(needs_escape), "{s:?}");
        }
        // `has_escape` for every character, alone and between others.
        let mut buf = [0u8; 4];
        for c in (0..=0x10_ffffu32).filter_map(char::from_u32) {
            let one = c.encode_utf8(&mut buf);
            assert_eq!(has_escape(one), needs_escape(c), "{c:?}");
            let mid = format!("a{one}é");
            assert_eq!(has_escape(&mid), needs_escape(c), "{c:?}");
        }
        let mut out = String::new();
        for n in [0, 1, 63, 64, 65, 200] {
            out.clear();
            push_spaces(&mut out, n);
            assert_eq!(out, " ".repeat(n));
        }
        assert!(write_local_tag(&mut out, "").is_err());
    }

    /// Unit 38c: a seeded random property over longer strings (up to 12
    /// characters) made of every letter of the Psych/YAML 1.1 words in
    /// both cases and the characters the number patterns look at.
    #[test]
    fn byte_fast_paths_decide_as_before_random() {
        // Unit 39 (unit-38 review): `i`, `x`, `b` (`inf`, `0x`, `0b`) and
        // non-ASCII (a multi-byte character next to a byte-level check;
        // NEL, NBSP, BOM and LS are YAML-significant or near it).
        let alphabet: Vec<char> =
            "nulyestrofaNULYESTROFAixbIXB0123456789.,_:+-eE~<= #éüß\u{85}\u{a0}\u{feff}\u{2028}☃"
                .chars()
                .collect();
        // xorshift64*, fixed seed: the same strings on every run.
        let mut state: u64 = 0x9e37_79b9_7f4a_7c15;
        let mut next = move || {
            state ^= state >> 12;
            state ^= state << 25;
            state ^= state >> 27;
            state.wrapping_mul(0x2545_f491_4f6c_dd1d)
        };
        let words = [
            "null", "yes", "true", "on", "no", "false", "off", "inf", "nan",
        ];
        let mut text = String::new();
        let mut hits = 0usize;
        // 400k rounds (300k before unit 39): the wider alphabet makes the
        // foreign forms rarer, and both outcomes must stay well covered.
        for round in 0..400_000u32 {
            text.clear();
            // One round in four starts with a word in random letter case,
            // so the words (rare by chance) are well covered.
            if round % 4 == 0 {
                let w = words[(next() % words.len() as u64) as usize];
                for c in w.chars() {
                    let upper = next() % 2 == 0;
                    text.push(if upper { c.to_ascii_uppercase() } else { c });
                }
            }
            let len = (next() % 13) as usize;
            for _ in 0..len {
                text.push(alphabet[(next() % alphabet.len() as u64) as usize]);
            }
            for conservative in [true, false] {
                assert_eq!(
                    plain_syntax_ok(&text, conservative),
                    plain_syntax_reference(&text, conservative),
                    "{text:?} {conservative}"
                );
            }
            let foreign = foreign_yaml11(&text);
            assert_eq!(foreign, foreign_reference(&text), "{text:?}");
            hits += usize::from(foreign);
        }
        // Both outcomes are exercised.
        assert!(hits > 10_000, "{hits}");
    }

    #[test]
    fn foreign_yaml11_forms() {
        for s in [
            "0X1F",
            "-0B1",
            "+0O7_7",
            "<<",
            "=",
            "2001-12-14",
            "2001-12-14T21:59:43.10-05:00",
            "2001-1-1 1:2:3",
            "1.2.3",
            ".",
            "-.5.",
            "1.e+3",
            "0x_",
            "+0b__",
            // Go reads `1.e3` (yamlStyleFloat) as 1000.
            "1.e3",
        ] {
            assert!(foreign_yaml11(s), "{s}");
        }
        for s in [
            "2001-12",
            "20011-12-14",
            "2001-12-14x",
            "a.b",
            "0x",
            "0XZ",
            "0y1",
            "a,b",
            "1,a",
            ",",
            "1:a",
            "1:",
            "2001:db8::1",
            "_",
            "a_1",
            "1_a",
            ".in",
            "inf",
        ] {
            assert!(!foreign_yaml11(s), "{s}");
        }
    }
}