nish 0.49.1 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/README.md +27 -21
  2. package/docs/AI.md +237 -160
  3. package/docs/INSTALL.md +1 -1
  4. package/package.json +12 -12
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
package/std/yaml.ts CHANGED
@@ -252,6 +252,12 @@ const YAML_BREAKS: string = "\n\r"
252
252
  const YAML_MAX_KEY_LENGTH: i32 = 1024
253
253
  /** The deepest `maxDepth` may be, and its default. */
254
254
  const YAML_MAX_DEPTH: i32 = 1000
255
+ /**
256
+ * One past the deepest column a block scalar's content may start at: its node
257
+ * keeps that column, plus one, in the 28 bits above its chomping and indicator
258
+ * flags.
259
+ */
260
+ const YAML_MAX_BLOCK_INDENT: i32 = 268435455
255
261
  /** Past this many pairs a map's duplicate check hashes instead of comparing every pair with every other. */
256
262
  const YAML_PAIRWISE_KEYS: i32 = 16
257
263
  /** No node, no slot, no offset: what every lookup answers outside its array. */
@@ -406,12 +412,15 @@ export class YamlStream {
406
412
  /** @internal Used by `YamlParser`. */
407
413
  types: u8[]
408
414
 
409
- /** How many nodes the arrays have room for, and how many are in use. */
415
+ /** How many nodes the arrays have room for: the length of each. */
410
416
  private cap: i32 = 0
411
- /** @internal Used by `YamlPathWalker`. */
417
+ /**
418
+ * How many nodes are in use.
419
+ * @internal Used by `YamlPathWalker`.
420
+ */
412
421
  top: i32 = 0
413
422
  /**
414
- * How many slots of `kids` are in use.
423
+ * How many slots of `kids` are in use: its length.
415
424
  * @internal Used by `YamlParser`.
416
425
  */
417
426
  kidTop: i32 = 0
@@ -458,7 +467,7 @@ export class YamlStream {
458
467
  this.counts = new Array<i32>(room)
459
468
  this.extras = new Array<i32>(room)
460
469
  this.props = new Array<i32>(room)
461
- this.kids = new Array<i32>(room)
470
+ this.kids = []
462
471
  this.kinds = new Array<u8>(room)
463
472
  this.styles = new Array<u8>(room)
464
473
  this.types = new Array<u8>(room)
@@ -593,7 +602,19 @@ export class YamlStream {
593
602
  if (i < 0 || i >= this.count(n)) {
594
603
  return -1
595
604
  }
596
- return yamlAt(this.kids, yamlAt(this.firsts, n) + i)
605
+ const first: i32 = yamlAt(this.firsts, n)
606
+ const kids: i32[] = this.kids
607
+ const len: i32 = toI32(kids.length)
608
+ // `first` and `i` are each below the slots in use. Past 2^30 slots the
609
+ // node arrays would hold tens of gigabytes, an allocation that fails first
610
+ // (see `grow`), so the test never fails; it is what proves the sum.
611
+ if (first >= 0 && first < 1073741824 && i < 1073741824) {
612
+ const at: i32 = first + i
613
+ if (at < len) {
614
+ return kids[at]
615
+ }
616
+ }
617
+ return -1
597
618
  }
598
619
 
599
620
  /**
@@ -901,7 +922,9 @@ export class YamlStream {
901
922
  */
902
923
  comment(i: i32): string {
903
924
  const from: i32 = this.commentStart(i)
904
- return from < 0 ? "" : this.source.substring(from + 1, this.commentEnd(i))
925
+ return from < 0 || from >= toI32(this.source.length)
926
+ ? ""
927
+ : this.source.substring(from + 1, this.commentEnd(i))
905
928
  }
906
929
 
907
930
  // ---- Building ----------------------------------------------------------------
@@ -911,11 +934,15 @@ export class YamlStream {
911
934
  * @internal Called by `YamlParser`.
912
935
  */
913
936
  newNode(kind: i32, style: i32, start: i32, end: i32): i32 {
914
- if (this.top >= this.cap) {
937
+ const n: i32 = this.top
938
+ if (n >= this.cap) {
915
939
  this.grow()
916
940
  }
917
- const n: i32 = this.top
918
- this.top = n + 1
941
+ // `grow` left room past `n`, so the test never fails; it is what proves the count.
942
+ const cap: i32 = this.cap
943
+ if (n >= 0 && n < cap) {
944
+ this.top = n + 1
945
+ }
919
946
  yamlPut(this.starts, n, start)
920
947
  yamlPut(this.ends, n, end)
921
948
  yamlPut(this.firsts, n, -1)
@@ -928,9 +955,17 @@ export class YamlStream {
928
955
  return n
929
956
  }
930
957
 
931
- /** Doubles every node array together. */
958
+ /**
959
+ * Doubles every node array together. Past 2^30 nodes the arrays already
960
+ * hold tens of gigabytes, so the allocation fails before the doubling
961
+ * could pass the longest array there is, which is where it stops.
962
+ */
932
963
  private grow(): void {
933
- const room: i32 = this.cap * 2
964
+ const cap: i32 = this.cap
965
+ let room: i32 = 2147483647
966
+ if (cap >= 0 && cap < 1073741824) {
967
+ room = cap + cap
968
+ }
934
969
  this.starts = yamlRoomI32(this.starts, room)
935
970
  this.ends = yamlRoomI32(this.ends, room)
936
971
  this.firsts = yamlRoomI32(this.firsts, room)
@@ -944,14 +979,14 @@ export class YamlStream {
944
979
  }
945
980
 
946
981
  /**
947
- * Appends `n` to `kids`, growing it by doubling; answers its slot.
982
+ * Appends `n` to `kids`; answers its slot.
948
983
  * @internal Called by `YamlParser`.
949
984
  */
950
985
  pushKid(n: i32): i32 {
951
- const slot: i32 = this.kidTop
952
- this.kids = yamlRoomI32(this.kids, slot + 1)
953
- yamlPut(this.kids, slot, n)
954
- this.kidTop = slot + 1
986
+ const kids: i32[] = this.kids
987
+ const slot: i32 = toI32(kids.length)
988
+ kids.push(n)
989
+ this.kidTop = toI32(kids.length)
955
990
  return slot
956
991
  }
957
992
 
@@ -966,10 +1001,19 @@ export class YamlStream {
966
1001
  this.diagStarts.push(start)
967
1002
  this.diagEnds.push(end < start ? start : end)
968
1003
  this.diagDocuments.push(document)
1004
+ // Each count stays below the diagnostics recorded, the one just pushed
1005
+ // included: the tests never fail, and they are what prove the counts.
1006
+ const recorded: i32 = toI32(this.diagCodes.length)
969
1007
  if (warning) {
970
- this.warnings += 1
1008
+ const warnings: i32 = this.warnings
1009
+ if (warnings < recorded) {
1010
+ this.warnings = warnings + 1
1011
+ }
971
1012
  } else {
972
- this.errors += 1
1013
+ const errors: i32 = this.errors
1014
+ if (errors < recorded) {
1015
+ this.errors = errors + 1
1016
+ }
973
1017
  }
974
1018
  }
975
1019
 
@@ -1067,22 +1111,44 @@ const yamlPutU8 = (a: u8[], i: i32, v: i32): void => {
1067
1111
  /** `a`, or a copy of it at least `n` long and at least twice as long. */
1068
1112
  const yamlRoomI32 = (a: i32[], n: i32): i32[] => {
1069
1113
  const have: i32 = toI32(a.length)
1070
- if (n <= have) {
1114
+ // `n < 0` is inside `n <= have`; spelled out, it is what proves `n` a length.
1115
+ if (n < 0 || n <= have) {
1071
1116
  return a
1072
1117
  }
1073
- const grown: i32[] = new Array<i32>(n > have * 2 ? n : have * 2)
1074
- grown.set(a, 0)
1118
+ let size: i32 = n
1119
+ if (have < 1073741824) {
1120
+ const twice: i32 = have + have
1121
+ if (twice > n) {
1122
+ size = twice
1123
+ }
1124
+ }
1125
+ const grown: i32[] = new Array<i32>(size)
1126
+ // `size` is above `have`, so the test never fails; it is what proves the copy.
1127
+ if (toI32(a.length) <= toI32(grown.length)) {
1128
+ grown.set(a, 0)
1129
+ }
1075
1130
  return grown
1076
1131
  }
1077
1132
 
1078
1133
  /** `a`, or a copy of it at least `n` long and at least twice as long. */
1079
1134
  const yamlRoomU8 = (a: u8[], n: i32): u8[] => {
1080
1135
  const have: i32 = toI32(a.length)
1081
- if (n <= have) {
1136
+ // `n < 0` is inside `n <= have`; spelled out, it is what proves `n` a length.
1137
+ if (n < 0 || n <= have) {
1082
1138
  return a
1083
1139
  }
1084
- const grown: u8[] = new Array<u8>(n > have * 2 ? n : have * 2)
1085
- grown.set(a, 0)
1140
+ let size: i32 = n
1141
+ if (have < 1073741824) {
1142
+ const twice: i32 = have + have
1143
+ if (twice > n) {
1144
+ size = twice
1145
+ }
1146
+ }
1147
+ const grown: u8[] = new Array<u8>(size)
1148
+ // `size` is above `have`, so the test never fails; it is what proves the copy.
1149
+ if (toI32(a.length) <= toI32(grown.length)) {
1150
+ grown.set(a, 0)
1151
+ }
1086
1152
  return grown
1087
1153
  }
1088
1154
 
@@ -1092,15 +1158,17 @@ const yamlByte = (text: string, i: i32): i32 =>
1092
1158
 
1093
1159
  /** Whether `text[a0, a1)` and `text[b0, b1)` hold the same bytes. */
1094
1160
  const yamlSameBytes = (text: string, a0: i32, a1: i32, b0: i32, b1: i32): boolean => {
1095
- if (a1 - a0 !== b1 - b0) {
1161
+ if (toI64(a1) - toI64(a0) !== toI64(b1) - toI64(b0)) {
1096
1162
  return false
1097
1163
  }
1098
- let i: i32 = 0
1099
- while (i < a1 - a0) {
1100
- if (yamlByte(text, a0 + i) !== yamlByte(text, b0 + i)) {
1164
+ let i: i32 = a0
1165
+ let j: i32 = b0
1166
+ while (i < a1 && j < b1) {
1167
+ if (yamlByte(text, i) !== yamlByte(text, j)) {
1101
1168
  return false
1102
1169
  }
1103
1170
  i += 1
1171
+ j += 1
1104
1172
  }
1105
1173
  return true
1106
1174
  }
@@ -1108,15 +1176,17 @@ const yamlSameBytes = (text: string, a0: i32, a1: i32, b0: i32, b1: i32): boolea
1108
1176
  /** Whether `text[from, to)` is exactly `word`. */
1109
1177
  const yamlIsWord = (text: string, from: i32, to: i32, word: string): boolean => {
1110
1178
  const n: i32 = toI32(word.length)
1111
- if (to - from !== n) {
1179
+ if (toI64(to) - toI64(from) !== toI64(n)) {
1112
1180
  return false
1113
1181
  }
1114
1182
  let i: i32 = 0
1115
- while (i < n) {
1116
- if (yamlByte(text, from + i) !== toI32(word.charCodeAt(i))) {
1183
+ let j: i32 = from
1184
+ while (i < n && j < to) {
1185
+ if (yamlByte(text, j) !== toI32(word.charCodeAt(i))) {
1117
1186
  return false
1118
1187
  }
1119
1188
  i += 1
1189
+ j += 1
1120
1190
  }
1121
1191
  return true
1122
1192
  }
@@ -1134,8 +1204,9 @@ const yamlIsFlowIndicator = (code: i32): boolean =>
1134
1204
  code === YAML_RBRACE
1135
1205
  const yamlIsDigit = (code: i32): boolean => code >= YAML_ZERO && code <= YAML_NINE
1136
1206
  const yamlHexValue = (code: i32): i32 => {
1137
- if (code >= YAML_ZERO && code <= YAML_NINE) {
1138
- return code - YAML_ZERO
1207
+ const zero: i32 = 48
1208
+ if (code >= zero && code <= 57) {
1209
+ return code - zero
1139
1210
  }
1140
1211
  if (code >= 97 && code <= 102) {
1141
1212
  return code - 87
@@ -1146,9 +1217,14 @@ const yamlHexValue = (code: i32): i32 => {
1146
1217
  return -1
1147
1218
  }
1148
1219
 
1149
- /** Past `text[i]`, a break: one byte, or two for CR LF. */
1150
- const yamlAfterBreak = (text: string, i: i32): i32 =>
1151
- yamlByte(text, i) === YAML_CR && yamlByte(text, i + 1) === YAML_LF ? i + 2 : i + 1
1220
+ /** Past `text[i]`, a break: one byte, or two for CR LF; past the text's last byte, its end. */
1221
+ const yamlAfterBreak = (text: string, i: i32): i32 => {
1222
+ const n: i32 = toI32(text.length)
1223
+ if (i >= n - 1) {
1224
+ return n
1225
+ }
1226
+ return yamlByte(text, i) === YAML_CR && yamlByte(text, i + 1) === YAML_LF ? i + 2 : i + 1
1227
+ }
1152
1228
 
1153
1229
  /**
1154
1230
  * The first break in `text[from, to)`, or `to`. Short of the end of the text
@@ -1171,25 +1247,42 @@ const yamlLineEnd = (text: string, from: i32, to: i32): i32 => {
1171
1247
 
1172
1248
  /** The bytes a scalar decodes to, collected before they become one string. */
1173
1249
  class YamlBuffer {
1250
+ /** Room for the bytes; the first `count` are in use. */
1174
1251
  private bytes: u8[]
1175
- /** @internal Used by `yamlDecodeFlow`. */
1176
- count: i32 = 0
1252
+ private count: i32 = 0
1177
1253
 
1254
+ /** A buffer with room for `room` bytes, or 16 if that is more. */
1178
1255
  constructor(room: i32) {
1179
- const least: i32 = 16
1180
- this.bytes = new Array<u8>(room > least ? room : least)
1256
+ let size: i32 = room
1257
+ if (size < 16) {
1258
+ size = 16
1259
+ }
1260
+ this.bytes = new Array<u8>(size)
1181
1261
  }
1182
1262
 
1183
1263
  /**
1184
- * Appends one byte.
1264
+ * How many bytes the buffer holds.
1265
+ * @internal Called by `yamlDecodeFlow`.
1266
+ */
1267
+ size(): i32 {
1268
+ return this.count
1269
+ }
1270
+
1271
+ /**
1272
+ * Appends one byte: in place while there is room, then by the array's own
1273
+ * `push`, which grows it. Either way `count` never passes the length.
1185
1274
  * @internal Called by `yamlDecodeFlow`, `yamlDecodeEscape` and `yamlDecodeBlock`.
1186
1275
  */
1187
1276
  push(code: i32): void {
1188
- if (this.count >= toI32(this.bytes.length)) {
1189
- this.bytes = yamlRoomU8(this.bytes, this.count + 1)
1277
+ const bytes: u8[] = this.bytes
1278
+ const count: i32 = this.count
1279
+ if (count >= 0 && count < toI32(bytes.length)) {
1280
+ bytes[count] = toU8(code)
1281
+ this.count = count + 1
1282
+ } else {
1283
+ bytes.push(toU8(code))
1284
+ this.count = toI32(bytes.length)
1190
1285
  }
1191
- yamlPutU8(this.bytes, this.count, code)
1192
- this.count += 1
1193
1286
  }
1194
1287
 
1195
1288
  /**
@@ -1232,18 +1325,18 @@ class YamlBuffer {
1232
1325
  const three: i32 = 0x10000
1233
1326
  if (cp < two) {
1234
1327
  const lead: i32 = 0xc0
1235
- this.push(lead + (cp >> six))
1328
+ this.push(lead | (cp >> six))
1236
1329
  } else if (cp < three) {
1237
1330
  const lead: i32 = 0xe0
1238
- this.push(lead + (cp >> (six * 2)))
1239
- this.push(continuation + ((cp >> six) & low))
1331
+ this.push(lead | (cp >> 12))
1332
+ this.push(continuation | ((cp >> six) & low))
1240
1333
  } else {
1241
1334
  const lead: i32 = 0xf0
1242
- this.push(lead + (cp >> (six * 3)))
1243
- this.push(continuation + ((cp >> (six * 2)) & low))
1244
- this.push(continuation + ((cp >> six) & low))
1335
+ this.push(lead | (cp >> 18))
1336
+ this.push(continuation | ((cp >> 12) & low))
1337
+ this.push(continuation | ((cp >> six) & low))
1245
1338
  }
1246
- this.push(continuation + (cp & low))
1339
+ this.push(continuation | (cp & low))
1247
1340
  }
1248
1341
 
1249
1342
  /**
@@ -1252,14 +1345,15 @@ class YamlBuffer {
1252
1345
  */
1253
1346
  trimBlanks(floor: i32): void {
1254
1347
  const bytes: u8[] = this.bytes
1255
- while (this.count > floor) {
1256
- const at: i32 = this.count - 1
1257
- const code: i32 = at >= 0 && at < toI32(bytes.length) ? toI32(bytes[at]) : 0
1258
- if (!yamlIsBlank(code)) {
1259
- return
1348
+ let count: i32 = this.count
1349
+ while (count > 0 && count > floor && count <= toI32(bytes.length)) {
1350
+ const code: i32 = toI32(bytes[count - 1])
1351
+ if (code !== YAML_SPACE && code !== YAML_TAB) {
1352
+ break
1260
1353
  }
1261
- this.count = at
1354
+ count -= 1
1262
1355
  }
1356
+ this.count = count
1263
1357
  }
1264
1358
 
1265
1359
  /**
@@ -1267,7 +1361,14 @@ class YamlBuffer {
1267
1361
  * @internal Called by `yamlDecodeFlow` and `yamlDecodeBlock`.
1268
1362
  */
1269
1363
  text(): string {
1270
- return String.fromBytes(this.bytes, 0, this.count)
1364
+ const bytes: u8[] = this.bytes
1365
+ const count: i32 = this.count
1366
+ // `push` never lets `count` pass the length, so the test never fails; it
1367
+ // is what proves the slice.
1368
+ if (count >= 0 && count <= toI32(bytes.length)) {
1369
+ return String.fromBytes(bytes, 0, count)
1370
+ }
1371
+ return String.fromBytes(bytes, 0, toI32(bytes.length))
1271
1372
  }
1272
1373
  }
1273
1374
 
@@ -1307,7 +1408,7 @@ const yamlDecodeFlow = (text: string, from: i32, to: i32, style: i32): string =>
1307
1408
  if (i >= to) {
1308
1409
  return text.substring(from, to)
1309
1410
  }
1310
- const out: YamlBuffer = new YamlBuffer(to - from)
1411
+ const out: YamlBuffer = new YamlBuffer(from >= 0 && to > from ? to - from : 0)
1311
1412
  out.copy(text, from, i)
1312
1413
  // Blanks the source spelled as escapes are content, and a fold never trims them.
1313
1414
  let floor: i32 = 0
@@ -1316,33 +1417,37 @@ const yamlDecodeFlow = (text: string, from: i32, to: i32, style: i32): string =>
1316
1417
  if (yamlIsBreak(code)) {
1317
1418
  out.trimBlanks(floor)
1318
1419
  i = yamlAfterBreak(text, i)
1319
- let empties: i32 = 0
1420
+ // A break alone folds to a space; each empty line after it, to a newline.
1421
+ let empty: boolean = false
1320
1422
  while (true) {
1321
1423
  let j: i32 = i
1322
1424
  while (j < to && yamlIsBlank(yamlByte(text, j))) {
1323
1425
  j += 1
1324
1426
  }
1325
1427
  if (j < to && yamlIsBreak(yamlByte(text, j))) {
1326
- empties += 1
1428
+ out.push(YAML_LF)
1429
+ empty = true
1327
1430
  i = yamlAfterBreak(text, j)
1328
1431
  } else {
1329
1432
  i = j
1330
1433
  break
1331
1434
  }
1332
1435
  }
1333
- if (empties === 0) {
1436
+ if (!empty) {
1334
1437
  out.push(YAML_SPACE)
1335
- } else {
1336
- out.repeat(YAML_LF, empties)
1337
1438
  }
1338
- floor = out.count
1439
+ floor = out.size()
1339
1440
  } else if (style === YAML_SINGLE_QUOTED && code === YAML_SQUOTE) {
1340
1441
  out.push(YAML_SQUOTE)
1341
- i += yamlByte(text, i + 1) === YAML_SQUOTE ? 2 : 1
1442
+ i += 1
1443
+ if (i < to && yamlByte(text, i) === YAML_SQUOTE) {
1444
+ i += 1
1445
+ }
1342
1446
  } else if (style === YAML_DOUBLE_QUOTED && code === YAML_BACKSLASH) {
1343
- if (i + 1 < to && yamlIsBreak(yamlByte(text, i + 1))) {
1447
+ const next: i32 = i + 1
1448
+ if (next < to && yamlIsBreak(yamlByte(text, next))) {
1344
1449
  // An escaped break joins the lines; the empty lines after it stay.
1345
- i = yamlAfterBreak(text, i + 1)
1450
+ i = yamlAfterBreak(text, next)
1346
1451
  while (true) {
1347
1452
  let j: i32 = i
1348
1453
  while (j < to && yamlIsBlank(yamlByte(text, j))) {
@@ -1359,7 +1464,7 @@ const yamlDecodeFlow = (text: string, from: i32, to: i32, style: i32): string =>
1359
1464
  } else {
1360
1465
  i = yamlDecodeEscape(text, i, to, out)
1361
1466
  }
1362
- floor = out.count
1467
+ floor = out.size()
1363
1468
  } else {
1364
1469
  out.push(code)
1365
1470
  i += 1
@@ -1370,29 +1475,45 @@ const yamlDecodeFlow = (text: string, from: i32, to: i32, style: i32): string =>
1370
1475
 
1371
1476
  /** The low surrogate a `\u` escape at `text[at]` writes, or `-1` when it writes none before `to`. */
1372
1477
  const yamlLowSurrogateAt = (text: string, at: i32, to: i32): i32 => {
1373
- if (at + 6 > to || yamlByte(text, at) !== YAML_BACKSLASH || yamlByte(text, at + 1) !== 117) {
1478
+ if (at < 0 || at >= to || yamlByte(text, at) !== YAML_BACKSLASH) {
1479
+ return -1
1480
+ }
1481
+ let j: i32 = at + 1
1482
+ if (j >= to || yamlByte(text, j) !== 117) {
1374
1483
  return -1
1375
1484
  }
1485
+ j += 1
1376
1486
  let cp: i32 = 0
1377
1487
  let k: i32 = 0
1378
1488
  while (k < 4) {
1379
- const v: i32 = yamlHexValue(yamlByte(text, at + 2 + k))
1489
+ if (j >= to) {
1490
+ return -1
1491
+ }
1492
+ const v: i32 = yamlHexValue(yamlByte(text, j))
1380
1493
  if (v < 0) {
1381
1494
  return -1
1382
1495
  }
1383
- cp = cp * 16 + v
1496
+ cp = (cp << 4) | v
1384
1497
  k += 1
1498
+ j += 1
1385
1499
  }
1386
1500
  return cp >= 0xdc00 && cp < 0xe000 ? cp : -1
1387
1501
  }
1388
1502
 
1389
1503
  /** Appends what the escape at `text[at]`, a `\`, stands for; answers the byte past it. */
1390
1504
  const yamlDecodeEscape = (text: string, at: i32, to: i32, out: YamlBuffer): i32 => {
1391
- if (at + 1 >= to) {
1505
+ if (at >= to) {
1392
1506
  out.push(YAML_BACKSLASH)
1393
- return at + 1
1507
+ return to
1394
1508
  }
1395
- const e: i32 = yamlByte(text, at + 1)
1509
+ // `j` walks the escape: past the `\`, then past each byte it reads.
1510
+ let j: i32 = at + 1
1511
+ if (j >= to) {
1512
+ out.push(YAML_BACKSLASH)
1513
+ return j
1514
+ }
1515
+ const e: i32 = yamlByte(text, j)
1516
+ j += 1
1396
1517
  let digits: i32 = 0
1397
1518
  if (e === 120) {
1398
1519
  digits = 2
@@ -1402,29 +1523,37 @@ const yamlDecodeEscape = (text: string, at: i32, to: i32, out: YamlBuffer): i32
1402
1523
  digits = 8
1403
1524
  }
1404
1525
  if (digits > 0) {
1526
+ const past: i32 = j
1405
1527
  let cp: i32 = 0
1406
1528
  let k: i32 = 0
1407
- while (k < digits && at + 2 + k < to) {
1408
- const v: i32 = yamlHexValue(yamlByte(text, at + 2 + k))
1529
+ while (k < digits && j < to) {
1530
+ const v: i32 = yamlHexValue(yamlByte(text, j))
1409
1531
  if (v < 0 || cp > 0x10ffff) {
1410
1532
  break
1411
1533
  }
1412
- cp = cp * 16 + v
1534
+ cp = (cp << 4) | v
1413
1535
  k += 1
1536
+ j += 1
1414
1537
  }
1415
1538
  if (k === digits && cp <= 0x10ffff) {
1416
1539
  // A `\u` high surrogate and a `\u` low one after it are one code point, written as one.
1417
- const low: i32 = digits === 4 && cp >= 0xd800 && cp < 0xdc00 ? yamlLowSurrogateAt(text, at + 6, to) : -1
1540
+ const low: i32 = digits === 4 && cp >= 0xd800 && cp < 0xdc00 ? yamlLowSurrogateAt(text, j, to) : -1
1418
1541
  if (low >= 0) {
1419
- out.codePoint((cp - 0xd800) * 0x400 + (low - 0xdc00) + 0x10000)
1420
- return at + 12
1542
+ out.codePoint((cp & 0x3ff) * 0x400 + (low & 0x3ff) + 0x10000)
1543
+ // Past the low surrogate's six bytes, which `yamlLowSurrogateAt` found before `to`.
1544
+ let skip: i32 = 0
1545
+ while (skip < 6 && j < to) {
1546
+ j += 1
1547
+ skip += 1
1548
+ }
1549
+ return j
1421
1550
  }
1422
1551
  out.codePoint(cp)
1423
- return at + 2 + digits
1552
+ return j
1424
1553
  }
1425
1554
  out.push(YAML_BACKSLASH)
1426
1555
  out.push(e)
1427
- return at + 2
1556
+ return past
1428
1557
  }
1429
1558
  const mapped: i32 = yamlEscapeValue(e)
1430
1559
  if (mapped < 0) {
@@ -1433,7 +1562,7 @@ const yamlDecodeEscape = (text: string, at: i32, to: i32, out: YamlBuffer): i32
1433
1562
  } else {
1434
1563
  out.codePoint(mapped)
1435
1564
  }
1436
- return at + 2
1565
+ return j
1437
1566
  }
1438
1567
 
1439
1568
  /** The code point `\` and `e` stand for in a double-quoted scalar, or `-1` for no escape. */
@@ -1491,7 +1620,7 @@ const yamlDecodeBlock = (text: string, from: i32, to: i32, extras: i32, folded:
1491
1620
  const indent: i32 = (extras >> 3) - 1
1492
1621
  const explicit: boolean = (extras & 4) !== 0
1493
1622
  const chomp: i32 = extras & 3
1494
- const out: YamlBuffer = new YamlBuffer(to - from)
1623
+ const out: YamlBuffer = new YamlBuffer(from >= 0 && to > from ? to - from : 0)
1495
1624
  let i: i32 = from
1496
1625
  let seenContent: boolean = false
1497
1626
  let previousMore: boolean = false
@@ -1499,13 +1628,15 @@ const yamlDecodeBlock = (text: string, from: i32, to: i32, extras: i32, folded:
1499
1628
  let pending: i32 = 0
1500
1629
  while (i < to) {
1501
1630
  let j: i32 = i
1502
- while (j < to && j - i < indent && yamlByte(text, j) === YAML_SPACE) {
1631
+ let column: i32 = 0
1632
+ while (j < to && column < indent && yamlByte(text, j) === YAML_SPACE) {
1503
1633
  j += 1
1634
+ column += 1
1504
1635
  }
1505
1636
  const e: i32 = yamlLineEnd(text, j, to)
1506
1637
  const hasBreak: boolean = e < to
1507
1638
  let empty: boolean = j === e
1508
- if (!empty && (j - i < indent || (!seenContent && !explicit))) {
1639
+ if (!empty && (column < indent || (!seenContent && !explicit))) {
1509
1640
  // Short of the indent, or before the indent was found: empty when only
1510
1641
  // spaces, as the scanner counts it; a tab makes the first content line.
1511
1642
  let k: i32 = j
@@ -1515,7 +1646,9 @@ const yamlDecodeBlock = (text: string, from: i32, to: i32, extras: i32, folded:
1515
1646
  empty = k === e
1516
1647
  }
1517
1648
  if (empty) {
1518
- if (hasBreak) {
1649
+ // Each break counted is a byte of `[from, to)`, so `pending` stays below
1650
+ // `to`: the test never fails, and it is what proves the count.
1651
+ if (hasBreak && pending < to) {
1519
1652
  pending += 1
1520
1653
  }
1521
1654
  } else {
@@ -1523,7 +1656,7 @@ const yamlDecodeBlock = (text: string, from: i32, to: i32, extras: i32, folded:
1523
1656
  if (!seenContent) {
1524
1657
  out.repeat(YAML_LF, pending)
1525
1658
  } else if (folded && !more && !previousMore) {
1526
- if (pending === 1) {
1659
+ if (pending <= 1) {
1527
1660
  out.push(YAML_SPACE)
1528
1661
  } else {
1529
1662
  out.repeat(YAML_LF, pending - 1)
@@ -1596,7 +1729,7 @@ const yamlPlainType = (text: string, from: i32, to: i32): i32 => {
1596
1729
  return YAML_STRING
1597
1730
  }
1598
1731
  // Each word is told by its first byte, so a text is compared with its own three at most.
1599
- if (c === 126 && to - from === 1) {
1732
+ if (c === 126 && to === from + 1) {
1600
1733
  return YAML_NULL
1601
1734
  }
1602
1735
  if (letter) {
@@ -1642,14 +1775,17 @@ const yamlWordType = (text: string, from: i32, to: i32, c: i32): i32 => {
1642
1775
  const yamlIntBase = (text: string, from: i32, to: i32): i32 => {
1643
1776
  let i: i32 = from
1644
1777
  const sign: i32 = yamlByte(text, i)
1645
- if (sign === YAML_PLUS || sign === YAML_DASH) {
1778
+ if (i < to && (sign === YAML_PLUS || sign === YAML_DASH)) {
1646
1779
  i += 1
1647
1780
  }
1648
1781
  let base: i32 = 10
1649
- const x: i32 = yamlByte(text, i + 1)
1650
- if (yamlByte(text, i) === YAML_ZERO && (x === 120 || x === 111)) {
1651
- base = x === 120 ? 16 : 8
1652
- i += 2
1782
+ if (i < to && yamlByte(text, i) === YAML_ZERO) {
1783
+ const next: i32 = i + 1
1784
+ const x: i32 = yamlByte(text, next)
1785
+ if (next < to && (x === 120 || x === 111)) {
1786
+ base = x === 120 ? 16 : 8
1787
+ i = next + 1
1788
+ }
1653
1789
  }
1654
1790
  if (i >= to) {
1655
1791
  return 0
@@ -1668,30 +1804,30 @@ const yamlIntBase = (text: string, from: i32, to: i32): i32 => {
1668
1804
  const yamlIsFloatForm = (text: string, from: i32, to: i32): boolean => {
1669
1805
  let i: i32 = from
1670
1806
  const sign: i32 = yamlByte(text, i)
1671
- if (sign === YAML_PLUS || sign === YAML_DASH) {
1807
+ if (i < to && (sign === YAML_PLUS || sign === YAML_DASH)) {
1672
1808
  i += 1
1673
1809
  }
1674
1810
  const wholeStart: i32 = i
1675
1811
  while (i < to && yamlIsDigit(yamlByte(text, i))) {
1676
1812
  i += 1
1677
1813
  }
1678
- const whole: i32 = i - wholeStart
1814
+ const whole: boolean = i !== wholeStart
1679
1815
  if (i < to && yamlByte(text, i) === YAML_DOT) {
1680
1816
  i += 1
1681
1817
  const fractionStart: i32 = i
1682
1818
  while (i < to && yamlIsDigit(yamlByte(text, i))) {
1683
1819
  i += 1
1684
1820
  }
1685
- if (whole === 0 && i === fractionStart) {
1821
+ if (!whole && i === fractionStart) {
1686
1822
  return false
1687
1823
  }
1688
- } else if (whole === 0) {
1824
+ } else if (!whole) {
1689
1825
  return false
1690
1826
  }
1691
1827
  if (i < to && (yamlByte(text, i) === 101 || yamlByte(text, i) === 69)) {
1692
1828
  i += 1
1693
1829
  const s: i32 = yamlByte(text, i)
1694
- if (s === YAML_PLUS || s === YAML_DASH) {
1830
+ if (i < to && (s === YAML_PLUS || s === YAML_DASH)) {
1695
1831
  i += 1
1696
1832
  }
1697
1833
  const expStart: i32 = i
@@ -1708,7 +1844,7 @@ const yamlIsFloatForm = (text: string, from: i32, to: i32): boolean => {
1708
1844
  /** `1` for a core-schema positive infinity, `2` for a negative one, `3` for not-a-number, `0` otherwise. */
1709
1845
  const yamlSpecialFloat = (text: string, from: i32, to: i32): i32 => {
1710
1846
  const c: i32 = yamlByte(text, from)
1711
- const at: i32 = c === YAML_PLUS || c === YAML_DASH ? from + 1 : from
1847
+ const at: i32 = from < to && (c === YAML_PLUS || c === YAML_DASH) ? from + 1 : from
1712
1848
  if (
1713
1849
  yamlIsWord(text, at, to, ".inf") ||
1714
1850
  yamlIsWord(text, at, to, ".Inf") ||
@@ -1734,11 +1870,14 @@ const yamlParseInt = (text: string, from: i32, to: i32): i64 => {
1734
1870
  }
1735
1871
  let i: i32 = from
1736
1872
  const sign: i32 = yamlByte(text, i)
1737
- if (sign === YAML_PLUS || sign === YAML_DASH) {
1873
+ if (i < to && (sign === YAML_PLUS || sign === YAML_DASH)) {
1738
1874
  i += 1
1739
1875
  }
1740
- if (base !== 10) {
1741
- i += 2
1876
+ // Past the `0x` or `0o` that `yamlIntBase` found before `to`.
1877
+ let prefix: i32 = base === 10 ? 0 : 2
1878
+ while (prefix > 0 && i < to) {
1879
+ i += 1
1880
+ prefix -= 1
1742
1881
  }
1743
1882
  const wide: i64 = toI64(base)
1744
1883
  while (i < to) {
@@ -1772,10 +1911,10 @@ const yamlParseFloat = (text: string, from: i32, to: i32): f64 => {
1772
1911
  const yamlStackPush = (a: i32[], top: i32, v: i32): i32 => {
1773
1912
  if (top < toI32(a.length)) {
1774
1913
  yamlPut(a, top, v)
1775
- } else {
1776
- a.push(v)
1914
+ return top + 1
1777
1915
  }
1778
- return top + 1
1916
+ a.push(v)
1917
+ return toI32(a.length)
1779
1918
  }
1780
1919
 
1781
1920
  /** A tag with the core schema's `tag:yaml.org,2002:` prefix taken off. */
@@ -1932,9 +2071,19 @@ class YamlParser {
1932
2071
  return yamlByte(this.text, i)
1933
2072
  }
1934
2073
 
2074
+ /** The byte after `i`, or `-1` outside the text: `at(i + 1)`, with the sum proven. */
2075
+ private after(i: i32): i32 {
2076
+ return i < this.len ? yamlByte(this.text, i + 1) : -1
2077
+ }
2078
+
2079
+ /** The byte before `i`, or `-1` outside the text: `at(i - 1)`, with the difference proven. */
2080
+ private before(i: i32): i32 {
2081
+ return i > 0 ? yamlByte(this.text, i - 1) : -1
2082
+ }
2083
+
1935
2084
  /** Whether `i` holds a `-` that starts a block sequence entry. */
1936
2085
  private isSeqEntryDash(i: i32): boolean {
1937
- return this.at(i) === YAML_DASH && yamlIsWs(this.at(i + 1))
2086
+ return this.at(i) === YAML_DASH && yamlIsWs(this.after(i))
1938
2087
  }
1939
2088
 
1940
2089
  /** Whether `[from, to)` holds a line break. */
@@ -1951,12 +2100,24 @@ class YamlParser {
1951
2100
  /** Whether `i` starts a `---` or `...` marker. */
1952
2101
  private isDocMarker(i: i32): boolean {
1953
2102
  const c: i32 = this.at(i)
1954
- return (
1955
- (c === YAML_DASH || c === YAML_DOT) &&
1956
- this.at(i + 1) === c &&
1957
- this.at(i + 2) === c &&
1958
- yamlIsWs(this.at(i + 3))
1959
- )
2103
+ if ((c !== YAML_DASH && c !== YAML_DOT) || i >= this.len) {
2104
+ return false
2105
+ }
2106
+ // Each byte matched is inside the text, so each step is below its length.
2107
+ const j: i32 = i + 1
2108
+ if (this.at(j) !== c || j >= this.len) {
2109
+ return false
2110
+ }
2111
+ const k: i32 = j + 1
2112
+ return this.at(k) === c && yamlIsWs(this.after(k))
2113
+ }
2114
+
2115
+ /** The end of the `---` or `...` marker `isDocMarker` found at `i`: `i + 3`, each step proven. */
2116
+ private markerEnd(i: i32): i32 {
2117
+ // The marker's three bytes are inside the text, so no step stops short.
2118
+ const j: i32 = i < this.len ? i + 1 : i
2119
+ const k: i32 = j < this.len ? j + 1 : j
2120
+ return k < this.len ? k + 1 : k
1960
2121
  }
1961
2122
 
1962
2123
  /** The end of `[from, …)` before any trailing blanks and a comment, on its line. */
@@ -1968,7 +2129,7 @@ class YamlParser {
1968
2129
  if (yamlIsBreak(c)) {
1969
2130
  break
1970
2131
  }
1971
- if (c === YAML_HASH && i > from && yamlIsBlank(this.at(i - 1))) {
2132
+ if (c === YAML_HASH && i > from && yamlIsBlank(this.before(i))) {
1972
2133
  break
1973
2134
  }
1974
2135
  i += 1
@@ -1994,10 +2155,11 @@ class YamlParser {
1994
2155
  */
1995
2156
  private propTokenEnd(from: i32, flow: boolean): i32 {
1996
2157
  let i: i32 = from
1997
- if (this.at(from - 1) === YAML_BANG && this.at(from) === YAML_LT) {
2158
+ if (this.before(from) === YAML_BANG && this.at(from) === YAML_LT) {
1998
2159
  while (i < this.len && !yamlIsWs(this.at(i))) {
2160
+ const c: i32 = this.at(i)
1999
2161
  i += 1
2000
- if (this.at(i - 1) === YAML_GT) {
2162
+ if (c === YAML_GT) {
2001
2163
  return i
2002
2164
  }
2003
2165
  }
@@ -2021,9 +2183,10 @@ class YamlParser {
2021
2183
  */
2022
2184
  private peekLine(minIndent: i32): void {
2023
2185
  let p: i32 = this.pos
2024
- while (p < this.len) {
2186
+ // A negative `p` reads -1, which ends the scan as the end of the text does.
2187
+ while (p >= 0 && p < this.len) {
2025
2188
  let i: i32 = p
2026
- while (this.at(i) === YAML_SPACE) {
2189
+ while (i < this.len && this.at(i) === YAML_SPACE) {
2027
2190
  i += 1
2028
2191
  }
2029
2192
  const c: i32 = this.at(i)
@@ -2039,7 +2202,7 @@ class YamlParser {
2039
2202
  }
2040
2203
  if (c === YAML_TAB) {
2041
2204
  let j: i32 = i
2042
- while (yamlIsBlank(this.at(j))) {
2205
+ while (j < this.len && yamlIsBlank(this.at(j))) {
2043
2206
  j += 1
2044
2207
  }
2045
2208
  const d: i32 = this.at(j)
@@ -2076,17 +2239,21 @@ class YamlParser {
2076
2239
 
2077
2240
  /** Steps past spaces and tabs. */
2078
2241
  private skipInlineSpaces(): void {
2079
- while (yamlIsBlank(this.at(this.pos))) {
2080
- this.pos += 1
2242
+ let p: i32 = this.pos
2243
+ while (p < this.len && yamlIsBlank(this.at(p))) {
2244
+ p += 1
2081
2245
  }
2246
+ this.pos = p
2082
2247
  }
2083
2248
 
2084
2249
  /** Steps past the spaces after an indicator, reporting a tab that indents a compact node. */
2085
2250
  private skipIndicatorSeparation(): void {
2086
- while (this.at(this.pos) === YAML_SPACE) {
2087
- this.pos += 1
2251
+ let p: i32 = this.pos
2252
+ while (p < this.len && this.at(p) === YAML_SPACE) {
2253
+ p += 1
2088
2254
  }
2089
- if (this.at(this.pos) !== YAML_TAB) {
2255
+ this.pos = p
2256
+ if (this.at(p) !== YAML_TAB) {
2090
2257
  return
2091
2258
  }
2092
2259
  const tab: i32 = this.pos
@@ -2097,7 +2264,7 @@ class YamlParser {
2097
2264
  }
2098
2265
  if (
2099
2266
  this.isSeqEntryDash(this.pos) ||
2100
- (c === YAML_QUESTION && yamlIsWs(this.at(this.pos + 1))) ||
2267
+ (c === YAML_QUESTION && yamlIsWs(this.after(this.pos))) ||
2101
2268
  this.findKeyColon(this.pos) >= 0
2102
2269
  ) {
2103
2270
  this.error(YAML_ERR_TAB_INDENT, "Tabs cannot be used for indentation", tab, this.pos)
@@ -2121,13 +2288,13 @@ class YamlParser {
2121
2288
  return
2122
2289
  }
2123
2290
  let i: i32 = this.pos
2124
- while (yamlIsBlank(this.at(i))) {
2291
+ while (i < this.len && yamlIsBlank(this.at(i))) {
2125
2292
  i += 1
2126
2293
  }
2127
2294
  if (this.at(i) !== YAML_HASH) {
2128
2295
  return
2129
2296
  }
2130
- if (i === this.pos && !yamlIsWs(this.at(i - 1))) {
2297
+ if (i === this.pos && !yamlIsWs(this.before(i))) {
2131
2298
  return
2132
2299
  }
2133
2300
  this.recordComment(i)
@@ -2144,7 +2311,7 @@ class YamlParser {
2144
2311
  if (yamlIsBreak(c)) {
2145
2312
  return
2146
2313
  }
2147
- if (c === YAML_HASH && yamlIsWs(this.at(i - 1))) {
2314
+ if (c === YAML_HASH && yamlIsWs(this.before(i))) {
2148
2315
  this.recordComment(i)
2149
2316
  return
2150
2317
  }
@@ -2166,7 +2333,8 @@ class YamlParser {
2166
2333
 
2167
2334
  /** `finishLine`, unless the cursor is already at the start of a line. */
2168
2335
  private finishLineIfMidLine(): void {
2169
- if (this.pos > 0 && this.pos < this.len && !yamlIsBreak(this.at(this.pos - 1))) {
2336
+ const p: i32 = this.pos
2337
+ if (p > 0 && p < this.len && !yamlIsBreak(this.at(p - 1))) {
2170
2338
  this.finishLine()
2171
2339
  }
2172
2340
  }
@@ -2209,10 +2377,15 @@ class YamlParser {
2209
2377
  /** The tag written at `[start, end)`, resolved against this document's handles. */
2210
2378
  private resolveTag(start: i32, end: i32): string {
2211
2379
  const written: string = this.text.substring(start, end)
2212
- if (end - start === 1) {
2380
+ // A tag is a span of the text holding at least its `!`, so this never returns.
2381
+ if (start < 0 || end > this.len || end < 1 || end <= start) {
2382
+ return written
2383
+ }
2384
+ const open: i32 = start + 1
2385
+ if (open >= end) {
2213
2386
  return "!"
2214
2387
  }
2215
- if (this.at(start + 1) === YAML_LT) {
2388
+ if (this.at(open) === YAML_LT) {
2216
2389
  if (this.at(end - 1) !== YAML_GT) {
2217
2390
  this.error(YAML_ERR_BAD_TAG, 'Verbatim tag is missing its closing ">"', start, end)
2218
2391
  return written
@@ -2221,17 +2394,18 @@ class YamlParser {
2221
2394
  this.error(YAML_ERR_BAD_TAG, "Verbatim tag is empty", start, end)
2222
2395
  return written
2223
2396
  }
2224
- return yamlShortenTag(this.text.substring(start + 2, end - 1))
2397
+ return yamlShortenTag(this.text.substring(open + 1, end - 1))
2225
2398
  }
2226
- let handleEnd: i32 = start + 1
2227
- if (this.at(start + 1) === YAML_BANG) {
2228
- handleEnd = start + 2
2399
+ let handleEnd: i32 = open
2400
+ if (this.at(open) === YAML_BANG) {
2401
+ handleEnd = open + 1
2229
2402
  } else {
2230
- let i: i32 = start + 1
2403
+ let i: i32 = open
2231
2404
  while (i < end && yamlIsHandleChar(this.at(i))) {
2232
2405
  i += 1
2233
2406
  }
2234
- if (i > start + 1 && this.at(i) === YAML_BANG) {
2407
+ // `!` is not a handle character, so the loop stops on it inside the tag.
2408
+ if (i > open && i < end && this.at(i) === YAML_BANG) {
2235
2409
  handleEnd = i + 1
2236
2410
  }
2237
2411
  }
@@ -2245,10 +2419,12 @@ class YamlParser {
2245
2419
  }
2246
2420
  const handle: string = this.text.substring(start, handleEnd)
2247
2421
  const suffix: string = this.text.substring(handleEnd, end)
2422
+ const prefixes: string[] = this.handlePrefixes
2248
2423
  let i: i32 = toI32(this.handleNames.length) - 1
2249
2424
  while (i >= 0) {
2250
- if (this.handleNames[i] === handle) {
2251
- return yamlShortenTag(this.handlePrefixes[i] + suffix)
2425
+ // The two lists grow together, so the prefix is always there.
2426
+ if (this.handleNames[i] === handle && i < toI32(prefixes.length)) {
2427
+ return yamlShortenTag(prefixes[i] + suffix)
2252
2428
  }
2253
2429
  i -= 1
2254
2430
  }
@@ -2269,26 +2445,37 @@ class YamlParser {
2269
2445
 
2270
2446
  /** Reports an anchor or alias name that is empty or runs into a flow indicator. */
2271
2447
  private reportBadAnchorName(what: string, start: i32, end: i32): void {
2272
- if (end === start + 1) {
2448
+ // A name follows its `&` or `*` inside the text, so this never returns.
2449
+ if (start < 0 || end > this.len || end <= start) {
2450
+ return
2451
+ }
2452
+ const open: i32 = start + 1
2453
+ if (end === open) {
2273
2454
  this.error(YAML_ERR_BAD_ANCHOR, `${what} name cannot be empty`, start, end)
2274
2455
  return
2275
2456
  }
2276
- const name: string = this.text.substring(start + 1, end)
2277
- const next: string = this.text.substring(end, end + 1)
2457
+ const name: string = this.text.substring(open, end)
2458
+ // The flow indicator at `end` is inside the text, so `stop` is always `end + 1`.
2459
+ const stop: i32 = end < this.len ? end + 1 : end
2460
+ const next: string = this.text.substring(end, stop)
2278
2461
  this.error(
2279
2462
  YAML_ERR_BAD_ANCHOR,
2280
2463
  `${what} name "${name}" cannot run on into "${next}": a flow indicator is not part of a name, so it needs a space before it`,
2281
2464
  start,
2282
- end + 1
2465
+ stop
2283
2466
  )
2284
2467
  }
2285
2468
 
2286
2469
  /** Warns about an anchor or alias name `[start + 1, end)` that ends in `:`. */
2287
2470
  private checkAmbiguousName(what: string, start: i32, end: i32): void {
2288
- if (end <= start + 1 || this.at(end - 1) !== YAML_COLON) {
2471
+ if (start >= end) {
2472
+ return
2473
+ }
2474
+ const open: i32 = start + 1
2475
+ if (end <= open || this.before(end) !== YAML_COLON) {
2289
2476
  return
2290
2477
  }
2291
- const name: string = this.text.substring(start + 1, end)
2478
+ const name: string = this.text.substring(open, end)
2292
2479
  this.warn(
2293
2480
  YAML_ERR_AMBIGUOUS_ANCHOR_NAME,
2294
2481
  `${what} name "${name}" ends in ":", which YAML reads as part of the name rather than as a mapping separator`,
@@ -2306,20 +2493,22 @@ class YamlParser {
2306
2493
  let slot: i32 = -1
2307
2494
  while (true) {
2308
2495
  this.skipInlineSpaces()
2309
- const c: i32 = this.at(this.pos)
2310
- if (c !== YAML_AMP && c !== YAML_BANG) {
2496
+ const start: i32 = this.pos
2497
+ const c: i32 = this.at(start)
2498
+ // A byte matched is inside the text.
2499
+ if ((c !== YAML_AMP && c !== YAML_BANG) || start >= this.len) {
2311
2500
  break
2312
2501
  }
2313
- const start: i32 = this.pos
2314
- const end: i32 = this.propTokenEnd(start + 1, c === YAML_AMP || flow)
2502
+ const open: i32 = start + 1
2503
+ const end: i32 = this.propTokenEnd(open, c === YAML_AMP || flow)
2315
2504
  if (slot < 0) {
2316
2505
  slot = this.stream.newProps()
2317
2506
  yamlPut(this.stream.propStarts, slot, start)
2318
2507
  }
2319
2508
  if (c === YAML_AMP) {
2320
- if (end === start + 1 || (!flow && yamlIsFlowIndicator(this.at(end)))) {
2509
+ if (end === open || (!flow && yamlIsFlowIndicator(this.at(end)))) {
2321
2510
  this.reportBadAnchorName("Anchor", start, end)
2322
- if (end === start + 1) {
2511
+ if (end === open) {
2323
2512
  this.pos = end
2324
2513
  continue
2325
2514
  }
@@ -2327,7 +2516,7 @@ class YamlParser {
2327
2516
  if (yamlAt(this.stream.propAnchorStarts, slot) >= 0) {
2328
2517
  this.error(YAML_ERR_BAD_PROPERTY, "A node cannot carry more than one anchor", start, end)
2329
2518
  }
2330
- yamlPut(this.stream.propAnchorStarts, slot, start + 1)
2519
+ yamlPut(this.stream.propAnchorStarts, slot, open)
2331
2520
  yamlPut(this.stream.propAnchorEnds, slot, end)
2332
2521
  this.checkAmbiguousName("Anchor", start, end)
2333
2522
  } else {
@@ -2361,11 +2550,12 @@ class YamlParser {
2361
2550
  let i: i32 = from
2362
2551
  while (true) {
2363
2552
  const c: i32 = this.at(i)
2364
- if (c !== YAML_AMP && c !== YAML_BANG) {
2553
+ // A byte matched is inside the text.
2554
+ if ((c !== YAML_AMP && c !== YAML_BANG) || i >= this.len) {
2365
2555
  return i
2366
2556
  }
2367
2557
  i = this.propTokenEnd(i + 1, c === YAML_AMP)
2368
- while (yamlIsBlank(this.at(i))) {
2558
+ while (i < this.len && yamlIsBlank(this.at(i))) {
2369
2559
  i += 1
2370
2560
  }
2371
2561
  }
@@ -2373,12 +2563,12 @@ class YamlParser {
2373
2563
 
2374
2564
  /** The entry of the anchor named `[from, to)` among those defined so far, or `-1`. */
2375
2565
  private findAnchor(from: i32, to: i32): i32 {
2376
- let i: i32 = this.anchorTop - 1
2377
- while (i >= 0) {
2566
+ let i: i32 = this.anchorTop
2567
+ while (i > 0) {
2568
+ i -= 1
2378
2569
  if (yamlSameBytes(this.text, yamlAt(this.anchorStarts, i), yamlAt(this.anchorEnds, i), from, to)) {
2379
2570
  return i
2380
2571
  }
2381
- i -= 1
2382
2572
  }
2383
2573
  return -1
2384
2574
  }
@@ -2389,20 +2579,25 @@ class YamlParser {
2389
2579
  yamlStackPush(this.anchorEnds, this.anchorTop, to)
2390
2580
  yamlPut(this.stream.propEntries, slot, this.anchorTop)
2391
2581
  this.anchorTop = yamlStackPush(this.anchorNodes, this.anchorTop, node)
2392
- let i: i32 = this.pendingTop - 1
2393
- while (i >= 0) {
2582
+ const top: i32 = this.pendingTop
2583
+ let i: i32 = top
2584
+ while (i > 0) {
2585
+ i -= 1
2394
2586
  if (yamlSameBytes(this.text, yamlAt(this.pendingStarts, i), yamlAt(this.pendingEnds, i), from, to)) {
2395
2587
  // Close the gap: what was pending after it moves down one.
2396
- let j: i32 = i + 1
2397
- while (j < this.pendingTop) {
2398
- yamlPut(this.pendingStarts, j - 1, yamlAt(this.pendingStarts, j))
2399
- yamlPut(this.pendingEnds, j - 1, yamlAt(this.pendingEnds, j))
2400
- j += 1
2588
+ let gap: i32 = i
2589
+ while (gap < top) {
2590
+ const j: i32 = gap + 1
2591
+ if (j >= top) {
2592
+ break
2593
+ }
2594
+ yamlPut(this.pendingStarts, gap, yamlAt(this.pendingStarts, j))
2595
+ yamlPut(this.pendingEnds, gap, yamlAt(this.pendingEnds, j))
2596
+ gap = j
2401
2597
  }
2402
- this.pendingTop -= 1
2598
+ this.pendingTop = gap
2403
2599
  return
2404
2600
  }
2405
- i -= 1
2406
2601
  }
2407
2602
  }
2408
2603
 
@@ -2470,7 +2665,12 @@ class YamlParser {
2470
2665
 
2471
2666
  /** Reports the bad escape whose `\` is at `at`. */
2472
2667
  private reportBadEscape(at: i32): void {
2473
- const e: i32 = this.at(at + 1)
2668
+ // The caller found its `\` inside the text, so this never returns.
2669
+ if (at < 0 || at >= this.len) {
2670
+ return
2671
+ }
2672
+ const open: i32 = at + 1
2673
+ const e: i32 = this.at(open)
2474
2674
  let digits: i32 = 0
2475
2675
  if (e === 120) {
2476
2676
  digits = 2
@@ -2479,40 +2679,58 @@ class YamlParser {
2479
2679
  } else if (e === 85) {
2480
2680
  digits = 8
2481
2681
  }
2482
- if (digits > 0) {
2483
- let limit: i32 = at + 2 + digits
2484
- if (limit > this.len) {
2485
- limit = this.len
2486
- }
2487
- let end: i32 = at + 2
2682
+ // A letter matched at `open` is inside the text.
2683
+ if (digits > 0 && open < this.len) {
2684
+ let end: i32 = open + 1
2685
+ let count: i32 = 0
2488
2686
  let value: i32 = 0
2489
2687
  let tooBig: boolean = false
2490
- while (end < limit && yamlHexValue(this.at(end)) >= 0) {
2491
- // 0x10FFFF fits in 21 bits: past it, stop growing and remember.
2492
- if (value > 1114111) {
2688
+ while (count < digits && end < this.len) {
2689
+ // A hex digit is worth 0 to 15, so the upper test never fires.
2690
+ const h: i32 = yamlHexValue(this.at(end))
2691
+ if (h < 0 || h > 15) {
2692
+ break
2693
+ }
2694
+ // 0x10FFFF fits in 21 bits: past it, stop growing and remember. The
2695
+ // value starts at 0 and only grows, so it is never negative.
2696
+ if (value > 1114111 || value < 0) {
2493
2697
  tooBig = true
2494
2698
  } else {
2495
- value = value * 16 + yamlHexValue(this.at(end))
2699
+ value = value * 16 + h
2496
2700
  }
2497
2701
  end += 1
2702
+ count += 1
2498
2703
  }
2499
2704
  const written: string = this.text.substring(at, end)
2500
- if (end - at - 2 < digits) {
2705
+ if (count < digits) {
2501
2706
  this.error(YAML_ERR_BAD_ESCAPE, `"${written}" needs exactly ${digits} hex digits`, at, end)
2502
2707
  } else if (digits === 8 && (tooBig || value > 1114111)) {
2503
2708
  this.error(YAML_ERR_BAD_ESCAPE, `"${written}" is past the last Unicode code point`, at, end)
2504
2709
  }
2505
2710
  return
2506
2711
  }
2712
+ // The escaped character is one to four UTF-8 bytes; step over them one at
2713
+ // a time so a character cut short by the end of the text stops there.
2507
2714
  const width: i32 = yamlCharWidth(e)
2508
- const escaped: string = this.text.substring(at + 1, at + 1 + width)
2509
- this.error(YAML_ERR_BAD_ESCAPE, `"\\${escaped}" is not a valid escape sequence`, at, at + 1 + width)
2715
+ const len: i32 = this.len
2716
+ let stop: i32 = open
2717
+ let k: i32 = 0
2718
+ while (k < width && stop < len) {
2719
+ stop += 1
2720
+ k += 1
2721
+ }
2722
+ const escaped: string = this.text.substring(open, stop)
2723
+ this.error(YAML_ERR_BAD_ESCAPE, `"\\${escaped}" is not a valid escape sequence`, at, stop)
2510
2724
  }
2511
2725
 
2512
2726
  /** Reports a quoted scalar's continuation line at `lineStart` that is not indented past its parent. */
2513
2727
  private checkQuotedIndent(lineStart: i32, parentIndent: i32): boolean {
2728
+ // Outside the text the byte reads -1, which answers `false` below as well.
2729
+ if (lineStart < 0) {
2730
+ return false
2731
+ }
2514
2732
  let i: i32 = lineStart
2515
- while (this.at(i) === YAML_SPACE) {
2733
+ while (i < this.len && this.at(i) === YAML_SPACE) {
2516
2734
  i += 1
2517
2735
  }
2518
2736
  const c: i32 = this.at(i)
@@ -2533,7 +2751,9 @@ class YamlParser {
2533
2751
  private scanQuoted(quote: i32, parentIndent: i32): i32 {
2534
2752
  const start: i32 = this.pos
2535
2753
  const single: boolean = quote === YAML_SQUOTE
2536
- let i: i32 = start + 1
2754
+ // The quote at the cursor is inside the text, so this is always `start + 1`.
2755
+ const open: i32 = start < this.len ? start + 1 : start
2756
+ let i: i32 = open
2537
2757
  let indentReported: boolean = parentIndent < 0
2538
2758
  let closed: boolean = false
2539
2759
  let contentEnd: i32 = this.len
@@ -2541,16 +2761,18 @@ class YamlParser {
2541
2761
  let verbatim: boolean = true
2542
2762
  while (i < this.len) {
2543
2763
  const at: i32 = toI32(indexOfAny(this.text, single ? YAML_SINGLE_STOPS : YAML_DOUBLE_STOPS, i))
2544
- if (at < 0) {
2764
+ // A byte found is inside the text, so the second test never fires.
2765
+ if (at < 0 || at >= this.len) {
2545
2766
  i = this.len
2546
2767
  break
2547
2768
  }
2548
2769
  i = at
2549
2770
  const c: i32 = this.at(i)
2550
2771
  if (c === YAML_SQUOTE) {
2551
- if (this.at(i + 1) === YAML_SQUOTE) {
2772
+ const next: i32 = i + 1
2773
+ if (next < this.len && this.at(next) === YAML_SQUOTE) {
2552
2774
  verbatim = false
2553
- i += 2
2775
+ i = next + 1
2554
2776
  continue
2555
2777
  }
2556
2778
  contentEnd = i
@@ -2566,8 +2788,9 @@ class YamlParser {
2566
2788
  }
2567
2789
  if (c === YAML_BACKSLASH) {
2568
2790
  verbatim = false
2569
- const e: i32 = this.at(i + 1)
2570
- if (e < 0) {
2791
+ const next: i32 = i + 1
2792
+ const e: i32 = this.at(next)
2793
+ if (e < 0 || next >= this.len) {
2571
2794
  i = this.len
2572
2795
  break
2573
2796
  }
@@ -2578,7 +2801,7 @@ class YamlParser {
2578
2801
  if (yamlEscapeKind(e) !== 1) {
2579
2802
  this.reportBadEscape(i)
2580
2803
  }
2581
- i += 2
2804
+ i = next + 1
2582
2805
  continue
2583
2806
  }
2584
2807
  // A line break.
@@ -2603,7 +2826,7 @@ class YamlParser {
2603
2826
  start,
2604
2827
  i
2605
2828
  )
2606
- yamlPut(this.stream.firsts, node, start + 1)
2829
+ yamlPut(this.stream.firsts, node, open)
2607
2830
  yamlPut(this.stream.counts, node, contentEnd)
2608
2831
  if (verbatim && closed) {
2609
2832
  yamlPut(this.stream.extras, node, YAML_VERBATIM)
@@ -2615,7 +2838,9 @@ class YamlParser {
2615
2838
  /** Reads an alias at the cursor and resolves it against the anchors defined so far. */
2616
2839
  private scanAlias(): i32 {
2617
2840
  const start: i32 = this.pos
2618
- let i: i32 = start + 1
2841
+ // The `*` at the cursor is inside the text, so this is always `start + 1`.
2842
+ const open: i32 = start < this.len ? start + 1 : start
2843
+ let i: i32 = open
2619
2844
  while (i < this.len) {
2620
2845
  const c: i32 = this.at(i)
2621
2846
  if (yamlIsWs(c) || yamlIsFlowIndicator(c)) {
@@ -2626,21 +2851,20 @@ class YamlParser {
2626
2851
  this.checkAmbiguousName("Alias", start, i)
2627
2852
  this.pos = i
2628
2853
  const node: i32 = this.stream.newNode(YAML_ALIAS, YAML_PLAIN, start, i)
2629
- if (i === start + 1) {
2854
+ if (i === open) {
2630
2855
  this.reportBadAnchorName("Alias", start, i)
2631
2856
  return node
2632
2857
  }
2633
- const entry: i32 = this.findAnchor(start + 1, i)
2858
+ const entry: i32 = this.findAnchor(open, i)
2634
2859
  if (entry >= 0) {
2635
2860
  yamlPut(this.stream.firsts, node, yamlAt(this.anchorNodes, entry))
2636
2861
  return node
2637
2862
  }
2638
- const name: string = this.text.substring(start + 1, i)
2639
- let p: i32 = this.pendingTop - 1
2640
- while (p >= 0) {
2641
- if (
2642
- yamlSameBytes(this.text, yamlAt(this.pendingStarts, p), yamlAt(this.pendingEnds, p), start + 1, i)
2643
- ) {
2863
+ const name: string = this.text.substring(open, i)
2864
+ let p: i32 = this.pendingTop
2865
+ while (p > 0) {
2866
+ p -= 1
2867
+ if (yamlSameBytes(this.text, yamlAt(this.pendingStarts, p), yamlAt(this.pendingEnds, p), open, i)) {
2644
2868
  this.error(
2645
2869
  YAML_ERR_RECURSIVE_ALIAS,
2646
2870
  `Alias "*${name}" refers to the node that contains it; recursive structures are not supported`,
@@ -2649,7 +2873,6 @@ class YamlParser {
2649
2873
  )
2650
2874
  return node
2651
2875
  }
2652
- p -= 1
2653
2876
  }
2654
2877
  this.error(YAML_ERR_UNRESOLVED_ALIAS, `Alias "*${name}" has no matching anchor`, start, i)
2655
2878
  return node
@@ -2695,12 +2918,17 @@ class YamlParser {
2695
2918
  if (this.lastErrStart <= pos && pos < this.lastErrEnd) {
2696
2919
  return
2697
2920
  }
2921
+ // Callers found an indicator at `pos`, so it is inside the text and this never returns.
2922
+ if (pos < 0 || pos >= this.len) {
2923
+ return
2924
+ }
2925
+ const next: i32 = pos + 1
2698
2926
  const c: i32 = this.at(pos)
2699
2927
  if (this.plainStartClass(c) === 2) {
2700
2928
  if (c === YAML_COLON) {
2701
2929
  return
2702
2930
  }
2703
- if (flow ? !this.flowIndicatorBoundary(pos + 1) : !yamlIsWs(this.at(pos + 1))) {
2931
+ if (flow ? !this.flowIndicatorBoundary(next) : !yamlIsWs(this.at(next))) {
2704
2932
  return
2705
2933
  }
2706
2934
  if (c === YAML_DASH) {
@@ -2709,14 +2937,14 @@ class YamlParser {
2709
2937
  YAML_ERR_BAD_SCALAR_START,
2710
2938
  'A "-" sequence indicator cannot start a flow collection entry',
2711
2939
  pos,
2712
- pos + 1
2940
+ next
2713
2941
  )
2714
2942
  } else {
2715
2943
  this.error(
2716
2944
  YAML_ERR_UNEXPECTED_CONTENT,
2717
2945
  "A block sequence cannot start on the line of the key it belongs to",
2718
2946
  pos,
2719
- pos + 1
2947
+ next
2720
2948
  )
2721
2949
  }
2722
2950
  return
@@ -2725,19 +2953,19 @@ class YamlParser {
2725
2953
  YAML_ERR_BAD_SCALAR_START,
2726
2954
  '"?" is an explicit-key indicator here and cannot start a plain scalar — quote the value',
2727
2955
  pos,
2728
- pos + 1
2956
+ next
2729
2957
  )
2730
2958
  return
2731
2959
  }
2732
2960
  if (flow && yamlIsFlowIndicator(c)) {
2733
2961
  return
2734
2962
  }
2735
- const written: string = this.text.substring(pos, pos + 1)
2963
+ const written: string = this.text.substring(pos, next)
2736
2964
  this.error(
2737
2965
  YAML_ERR_BAD_SCALAR_START,
2738
2966
  `Indicator "${written}" cannot start a plain scalar — quote the value`,
2739
2967
  pos,
2740
- pos + 1
2968
+ next
2741
2969
  )
2742
2970
  }
2743
2971
 
@@ -2755,6 +2983,10 @@ class YamlParser {
2755
2983
 
2756
2984
  /** Reports the `: ` at `at` inside a plain scalar. */
2757
2985
  private reportPlainColon(at: i32): void {
2986
+ // Callers found the `:` at `at`, so it is inside the text and this never returns.
2987
+ if (at < 0 || at >= this.len) {
2988
+ return
2989
+ }
2758
2990
  this.error(
2759
2991
  YAML_ERR_BAD_SCALAR_CONTENT,
2760
2992
  'A plain scalar cannot contain ": " — quote the value, or indent it as a nested mapping',
@@ -2766,7 +2998,7 @@ class YamlParser {
2766
2998
  /** Whether only blanks and a comment follow `from` on its line. */
2767
2999
  private endsAtComment(from: i32): boolean {
2768
3000
  let i: i32 = from
2769
- while (yamlIsBlank(this.at(i))) {
3001
+ while (i < this.len && yamlIsBlank(this.at(i))) {
2770
3002
  i += 1
2771
3003
  }
2772
3004
  return this.at(i) === YAML_HASH
@@ -2785,15 +3017,16 @@ class YamlParser {
2785
3017
  colonReported = true
2786
3018
  }
2787
3019
  let scan: i32 = this.nextLineStart(valueEnd)
2788
- while (scan < this.len) {
3020
+ // A negative `scan` reads -1, which ends the scalar as the end of the text does.
3021
+ while (scan >= 0 && scan < this.len) {
2789
3022
  let i: i32 = scan
2790
- while (this.at(i) === YAML_SPACE) {
3023
+ while (i < this.len && this.at(i) === YAML_SPACE) {
2791
3024
  i += 1
2792
3025
  }
2793
3026
  const c: i32 = this.at(i)
2794
3027
  // A line of blanks is an empty line, tabs included, though a tab cannot indent.
2795
3028
  let blank: i32 = i
2796
- while (yamlIsBlank(this.at(blank))) {
3029
+ while (blank < this.len && yamlIsBlank(this.at(blank))) {
2797
3030
  blank += 1
2798
3031
  }
2799
3032
  if (yamlIsBreak(this.at(blank))) {
@@ -2837,7 +3070,7 @@ class YamlParser {
2837
3070
  /** Reports anything but a comment after a block scalar's header. */
2838
3071
  private checkBlockHeaderEnd(): void {
2839
3072
  let i: i32 = this.pos
2840
- while (yamlIsBlank(this.at(i))) {
3073
+ while (i < this.len && yamlIsBlank(this.at(i))) {
2841
3074
  i += 1
2842
3075
  }
2843
3076
  const c: i32 = this.at(i)
@@ -2858,45 +3091,67 @@ class YamlParser {
2858
3091
  private scanBlockScalar(parentIndent: i32): i32 {
2859
3092
  const start: i32 = this.pos
2860
3093
  const folded: boolean = this.at(start) === YAML_GT
2861
- this.pos += 1
3094
+ // The `|` or `>` at the cursor is inside the text, so this is always `start + 1`.
3095
+ let p: i32 = start < this.len ? start + 1 : start
2862
3096
  let chomp: i32 = YAML_CLIP
2863
3097
  let explicitIndent: i32 = 0
2864
3098
  let sawChomp: boolean = false
2865
- while (true) {
2866
- const c: i32 = this.at(this.pos)
3099
+ while (p < this.len) {
3100
+ const c: i32 = this.at(p)
2867
3101
  if (c === YAML_DASH || c === YAML_PLUS) {
2868
3102
  if (sawChomp) {
2869
- this.error(YAML_ERR_BAD_BLOCK_HEADER, "Repeated chomping indicator", this.pos, this.pos + 1)
3103
+ this.error(YAML_ERR_BAD_BLOCK_HEADER, "Repeated chomping indicator", p, p + 1)
2870
3104
  }
2871
3105
  sawChomp = true
2872
3106
  chomp = c === YAML_DASH ? YAML_STRIP : YAML_KEEP
2873
3107
  } else if (c >= YAML_ONE && c <= YAML_NINE) {
2874
3108
  if (explicitIndent !== 0) {
2875
- this.error(YAML_ERR_BAD_BLOCK_HEADER, "Repeated indentation indicator", this.pos, this.pos + 1)
3109
+ this.error(YAML_ERR_BAD_BLOCK_HEADER, "Repeated indentation indicator", p, p + 1)
2876
3110
  }
2877
- explicitIndent = c - YAML_ZERO
3111
+ // "1" to "9" are 0x31 to 0x39, so the low four bits are the digit.
3112
+ explicitIndent = c & 15
2878
3113
  } else {
2879
3114
  break
2880
3115
  }
2881
- this.pos += 1
3116
+ p += 1
2882
3117
  }
3118
+ this.pos = p
2883
3119
  // An empty scalar ends at its header, not at the line after it.
2884
3120
  let valueEnd: i32 = this.pos
2885
3121
  this.checkBlockHeaderEnd()
2886
3122
  this.finishLine()
2887
3123
  const bodyStart: i32 = this.pos
2888
- let contentIndent: i32 =
2889
- explicitIndent > 0 ? (parentIndent > 0 ? parentIndent : toI32(0)) + explicitIndent : -1
3124
+ let contentIndent: i32 = -1
3125
+ // An indicator is one digit, 1 to 9, so the upper test never fails.
3126
+ if (explicitIndent > 0 && explicitIndent < 16) {
3127
+ if (parentIndent <= 0) {
3128
+ contentIndent = explicitIndent
3129
+ } else if (parentIndent < YAML_MAX_BLOCK_INDENT) {
3130
+ contentIndent = parentIndent + explicitIndent
3131
+ } else {
3132
+ // Too deep for the node to keep: the packing below reports it.
3133
+ contentIndent = YAML_MAX_BLOCK_INDENT
3134
+ }
3135
+ }
2890
3136
  let leadingBlank: i32 = -1
2891
3137
  while (this.pos < this.len) {
2892
3138
  const lineStart: i32 = this.pos
3139
+ // The cursor never moves before the text, so this never fires.
3140
+ if (lineStart < 0) {
3141
+ break
3142
+ }
2893
3143
  let i: i32 = lineStart
2894
- while (this.at(i) === YAML_SPACE) {
3144
+ while (i < this.len && this.at(i) === YAML_SPACE) {
2895
3145
  i += 1
2896
3146
  }
2897
3147
  const c: i32 = this.at(i)
2898
3148
  const indent: i32 = i - lineStart
2899
- if (c === YAML_TAB && indent < (contentIndent === -1 ? parentIndent + 1 : contentIndent)) {
3149
+ // A tab read is inside the text.
3150
+ if (
3151
+ c === YAML_TAB &&
3152
+ i < this.len &&
3153
+ (contentIndent === -1 ? indent <= parentIndent : indent < contentIndent)
3154
+ ) {
2900
3155
  this.error(YAML_ERR_TAB_INDENT, "Tabs cannot be used for indentation", i, i + 1)
2901
3156
  }
2902
3157
  if (c < 0 || yamlIsBreak(c)) {
@@ -2926,16 +3181,29 @@ class YamlParser {
2926
3181
  if (indent < contentIndent) {
2927
3182
  break
2928
3183
  }
2929
- const lineEnd: i32 = yamlLineEnd(this.text, lineStart + contentIndent, this.len)
3184
+ // The bytes from the content's indent to `i` are spaces, so the line ends where it would from there.
3185
+ const lineEnd: i32 = yamlLineEnd(this.text, i, this.len)
2930
3186
  valueEnd = lineEnd
2931
3187
  this.pos = this.nextLineStart(lineEnd)
2932
3188
  }
2933
3189
  const node: i32 = this.stream.newNode(YAML_SCALAR, folded ? YAML_FOLDED : YAML_LITERAL, start, valueEnd)
2934
3190
  yamlPut(this.stream.firsts, node, bodyStart)
2935
3191
  yamlPut(this.stream.counts, node, this.pos)
2936
- const eight: i32 = 8
2937
- const four: i32 = 4
2938
- yamlPut(this.stream.extras, node, (contentIndent + 1) * eight + (explicitIndent > 0 ? four : 0) + chomp)
3192
+ let packed: i32 = chomp
3193
+ if (contentIndent >= -1 && contentIndent < YAML_MAX_BLOCK_INDENT) {
3194
+ // Bit fields: the column above bit 3, the indicator flag at bit 2 and the
3195
+ // chomping mode (0 to 2) in bits 0 and 1.
3196
+ const flag: i32 = explicitIndent > 0 ? 4 : 0
3197
+ packed = ((contentIndent + 1) << 3) | flag | chomp
3198
+ } else {
3199
+ this.error(
3200
+ YAML_ERR_BAD_INDENT,
3201
+ `A block scalar's content is indented past the ${YAML_MAX_BLOCK_INDENT - 1} columns the parser can hold`,
3202
+ start,
3203
+ valueEnd
3204
+ )
3205
+ }
3206
+ yamlPut(this.stream.extras, node, packed)
2939
3207
  return node
2940
3208
  }
2941
3209
 
@@ -2943,17 +3211,22 @@ class YamlParser {
2943
3211
 
2944
3212
  /** Reads the `%` directive at `at` into the document's handles and version. */
2945
3213
  private readDirective(at: i32): void {
2946
- const nameEnd: i32 = this.tokenEnd(at + 1)
2947
- const isTag: boolean = yamlIsWord(this.text, at + 1, nameEnd, "TAG")
2948
- if (isTag || yamlIsWord(this.text, at + 1, nameEnd, "YAML")) {
3214
+ // The one caller found the `%` at `at`, so this never returns.
3215
+ if (at < 0 || at >= this.len) {
3216
+ return
3217
+ }
3218
+ const open: i32 = at + 1
3219
+ const nameEnd: i32 = this.tokenEnd(open)
3220
+ const isTag: boolean = yamlIsWord(this.text, open, nameEnd, "TAG")
3221
+ if (isTag || yamlIsWord(this.text, open, nameEnd, "YAML")) {
2949
3222
  let i: i32 = nameEnd
2950
- while (yamlIsBlank(this.at(i))) {
3223
+ while (i < this.len && yamlIsBlank(this.at(i))) {
2951
3224
  i += 1
2952
3225
  }
2953
3226
  const firstEnd: i32 = this.tokenEnd(i)
2954
3227
  if (isTag) {
2955
3228
  let j: i32 = firstEnd
2956
- while (yamlIsBlank(this.at(j))) {
3229
+ while (j < this.len && yamlIsBlank(this.at(j))) {
2957
3230
  j += 1
2958
3231
  }
2959
3232
  const prefixEnd: i32 = this.tokenEnd(j)
@@ -2962,9 +3235,10 @@ class YamlParser {
2962
3235
  return
2963
3236
  }
2964
3237
  const handle: string = this.text.substring(i, firstEnd)
2965
- let k: i32 = toI32(this.handleNames.length) - 1
2966
- while (k >= 0) {
2967
- if (this.handleNames[k] === handle) {
3238
+ const names: string[] = this.handleNames
3239
+ let k: i32 = 0
3240
+ while (k < toI32(names.length)) {
3241
+ if (names[k] === handle) {
2968
3242
  this.warn(
2969
3243
  YAML_ERR_DUPLICATE_DIRECTIVE,
2970
3244
  `Tag handle "${handle}" is declared more than once`,
@@ -2973,7 +3247,7 @@ class YamlParser {
2973
3247
  )
2974
3248
  break
2975
3249
  }
2976
- k -= 1
3250
+ k += 1
2977
3251
  }
2978
3252
  this.handleNames.push(handle)
2979
3253
  this.handlePrefixes.push(this.text.substring(j, prefixEnd))
@@ -2984,7 +3258,7 @@ class YamlParser {
2984
3258
  }
2985
3259
  this.warn(
2986
3260
  YAML_ERR_UNKNOWN_DIRECTIVE,
2987
- `Unknown directive "%${this.text.substring(at + 1, nameEnd)}"`,
3261
+ `Unknown directive "%${this.text.substring(open, nameEnd)}"`,
2988
3262
  at,
2989
3263
  nameEnd
2990
3264
  )
@@ -3002,8 +3276,8 @@ class YamlParser {
3002
3276
  i += 1
3003
3277
  }
3004
3278
  const major: i32 = i
3005
- let valid: boolean = i > from && this.at(i) === YAML_DOT
3006
- if (valid) {
3279
+ let valid: boolean = false
3280
+ if (i > from && i < this.len && this.at(i) === YAML_DOT) {
3007
3281
  i += 1
3008
3282
  const minor: i32 = i
3009
3283
  while (i < to && yamlIsDigit(this.at(i))) {
@@ -3017,7 +3291,7 @@ class YamlParser {
3017
3291
  }
3018
3292
  if (!this.lineRestIsBlank(to)) {
3019
3293
  let after: i32 = to
3020
- while (yamlIsBlank(this.at(after))) {
3294
+ while (after < this.len && yamlIsBlank(this.at(after))) {
3021
3295
  after += 1
3022
3296
  }
3023
3297
  this.error(
@@ -3029,33 +3303,38 @@ class YamlParser {
3029
3303
  }
3030
3304
  if (version !== "1.2") {
3031
3305
  const message: string =
3032
- major === from + 1 && this.at(from) === YAML_ONE
3306
+ major > from && major === from + 1 && this.at(from) === YAML_ONE
3033
3307
  ? `Document declares YAML ${version}; it is parsed with the 1.2 core schema`
3034
3308
  : `Unsupported YAML version "${version}"`
3035
3309
  this.warn(YAML_ERR_UNSUPPORTED_YAML_VERSION, message, at, to)
3036
3310
  }
3037
3311
  }
3038
3312
 
3039
- /** The end of the quoted scalar opening at `from`, past its closing quote, or `-1` at a break. */
3313
+ /**
3314
+ * The end of the quoted scalar opening at `from`, past its closing quote; `-1` at a break;
3315
+ * the text's end when it never closes.
3316
+ */
3040
3317
  private quotedKeyEnd(from: i32, stopAtBreak: boolean): i32 {
3041
3318
  const quote: i32 = this.at(from)
3042
- let i: i32 = from + 1
3319
+ // The callers found the quote at `from`, so `open` is always `from + 1`.
3320
+ let i: i32 = from < this.len ? from + 1 : from
3043
3321
  while (i < this.len) {
3044
3322
  const q: i32 = this.at(i)
3045
3323
  if (stopAtBreak && yamlIsBreak(q)) {
3046
3324
  return -1
3047
3325
  }
3326
+ const next: i32 = i + 1
3048
3327
  if (q === quote) {
3049
- if (quote === YAML_SQUOTE && this.at(i + 1) === YAML_SQUOTE) {
3050
- i += 2
3328
+ if (quote === YAML_SQUOTE && next < this.len && this.at(next) === YAML_SQUOTE) {
3329
+ i = next + 1
3051
3330
  } else {
3052
- return i + 1
3331
+ return next
3053
3332
  }
3054
3333
  } else {
3055
- i += quote === YAML_DQUOTE && q === YAML_BACKSLASH ? 2 : 1
3334
+ i = quote === YAML_DQUOTE && q === YAML_BACKSLASH && next < this.len ? next + 1 : next
3056
3335
  }
3057
3336
  }
3058
- return i + 1
3337
+ return this.len
3059
3338
  }
3060
3339
 
3061
3340
  /** The end of the flow collection opening at `from`, when it closes on its line; else `-1`. */
@@ -3075,12 +3354,17 @@ class YamlParser {
3075
3354
  continue
3076
3355
  }
3077
3356
  if (c === YAML_LBRACKET || c === YAML_LBRACE) {
3357
+ // `depth` counts the opens in `[from, i)`, so it is below `i` and this never returns.
3358
+ if (depth >= this.len) {
3359
+ return -1
3360
+ }
3078
3361
  depth += 1
3079
3362
  } else if (c === YAML_RBRACKET || c === YAML_RBRACE) {
3080
- depth -= 1
3081
- if (depth === 0) {
3363
+ // `from` opens the collection and a close back to 0 returns, so `depth` is at least 1 here.
3364
+ if (depth <= 1) {
3082
3365
  return i + 1
3083
3366
  }
3367
+ depth -= 1
3084
3368
  }
3085
3369
  i += 1
3086
3370
  }
@@ -3104,7 +3388,7 @@ class YamlParser {
3104
3388
  if (yamlIsBreak(c)) {
3105
3389
  return -1
3106
3390
  }
3107
- if (c === YAML_HASH && i > from && yamlIsBlank(this.at(i - 1))) {
3391
+ if (c === YAML_HASH && i > from && yamlIsBlank(this.before(i))) {
3108
3392
  return -1
3109
3393
  }
3110
3394
  if (c === YAML_COLON && yamlIsWs(this.at(i + 1))) {
@@ -3118,7 +3402,7 @@ class YamlParser {
3118
3402
  /** Whether the rest of the line from `from` is blank or a comment. */
3119
3403
  private lineRestIsBlank(from: i32): boolean {
3120
3404
  let i: i32 = from
3121
- while (yamlIsBlank(this.at(i))) {
3405
+ while (i < this.len && yamlIsBlank(this.at(i))) {
3122
3406
  i += 1
3123
3407
  }
3124
3408
  const c: i32 = this.at(i)
@@ -3128,7 +3412,7 @@ class YamlParser {
3128
3412
  /** `to` moved back over the blanks that end `[from, to)`. */
3129
3413
  private trimEnd(from: i32, to: i32): i32 {
3130
3414
  let e: i32 = to
3131
- while (e > from && yamlIsBlank(this.at(e - 1))) {
3415
+ while (e > from && e > 0 && yamlIsBlank(this.at(e - 1))) {
3132
3416
  e -= 1
3133
3417
  }
3134
3418
  return e
@@ -3136,7 +3420,7 @@ class YamlParser {
3136
3420
 
3137
3421
  /** Whether `at` holds a `?` explicit-key indicator. */
3138
3422
  private isExplicitKey(at: i32): boolean {
3139
- return this.at(at) === YAML_QUESTION && yamlIsWs(this.at(at + 1))
3423
+ return this.at(at) === YAML_QUESTION && yamlIsWs(this.after(at))
3140
3424
  }
3141
3425
 
3142
3426
  // ---- Flow collections ----------------------------------------------------------
@@ -3149,14 +3433,18 @@ class YamlParser {
3149
3433
 
3150
3434
  /** Checks that the flow line starting at `lineStart` is indented deeper than its block parent. */
3151
3435
  private checkFlowIndent(lineStart: i32): void {
3436
+ // Both callers pass the start of a line inside the text, so this never returns.
3437
+ if (lineStart < 0) {
3438
+ return
3439
+ }
3152
3440
  let i: i32 = lineStart
3153
- while (this.at(i) === YAML_SPACE) {
3441
+ while (i < this.len && this.at(i) === YAML_SPACE) {
3154
3442
  i += 1
3155
3443
  }
3156
3444
  let c: i32 = this.at(i)
3157
3445
  if (c === YAML_TAB) {
3158
3446
  let j: i32 = i
3159
- while (yamlIsBlank(this.at(j))) {
3447
+ while (j < this.len && yamlIsBlank(this.at(j))) {
3160
3448
  j += 1
3161
3449
  }
3162
3450
  if (j >= this.len) {
@@ -3198,7 +3486,7 @@ class YamlParser {
3198
3486
  YAML_ERR_UNEXPECTED_CONTENT,
3199
3487
  "A document marker cannot appear inside a flow collection",
3200
3488
  p,
3201
- p + 3
3489
+ this.markerEnd(p)
3202
3490
  )
3203
3491
  } else if (!this.flowIndentReported) {
3204
3492
  this.checkFlowIndent(p)
@@ -3233,7 +3521,8 @@ class YamlParser {
3233
3521
  let i: i32 = from
3234
3522
  while (i < this.len) {
3235
3523
  const hit: i32 = toI32(indexOfAny(this.text, YAML_PLAIN_FLOW_STOPS, i))
3236
- if (hit < 0) {
3524
+ // `indexOfAny` answers a byte inside the text or -1, so `hit >= this.len` never holds.
3525
+ if (hit < 0 || hit >= this.len) {
3237
3526
  return this.len
3238
3527
  }
3239
3528
  const c: i32 = this.at(hit)
@@ -3267,7 +3556,7 @@ class YamlParser {
3267
3556
  let scan: i32 = yamlAfterBreak(this.text, i)
3268
3557
  while (true) {
3269
3558
  let j: i32 = scan
3270
- while (yamlIsBlank(this.at(j))) {
3559
+ while (j < this.len && yamlIsBlank(this.at(j))) {
3271
3560
  j += 1
3272
3561
  }
3273
3562
  const c: i32 = this.at(j)
@@ -3284,7 +3573,7 @@ class YamlParser {
3284
3573
  if (j === scan && this.isDocMarker(j)) {
3285
3574
  break
3286
3575
  }
3287
- if (c === YAML_COLON && this.flowColonEnds(j + 1)) {
3576
+ if (c === YAML_COLON && j < this.len && this.flowColonEnds(j + 1)) {
3288
3577
  break
3289
3578
  }
3290
3579
  if (!this.flowIndentReported) {
@@ -3346,22 +3635,25 @@ class YamlParser {
3346
3635
 
3347
3636
  /** Steps over a `?` explicit-key indicator in flow; whether there was one. */
3348
3637
  private skipFlowExplicitKey(): boolean {
3349
- if (!this.isExplicitKey(this.pos)) {
3638
+ const at: i32 = this.pos
3639
+ if (at >= this.len || !this.isExplicitKey(at)) {
3350
3640
  return false
3351
3641
  }
3352
- this.pos += 1
3642
+ this.pos = at + 1
3353
3643
  this.skipFlowWs()
3354
3644
  return true
3355
3645
  }
3356
3646
 
3357
3647
  /** Reports an implicit key whose `:` is not on the key's line. */
3358
3648
  private checkImplicitKeyLine(afterKey: i32): void {
3359
- if (this.crossesLine(afterKey, this.pos)) {
3649
+ const colon: i32 = this.pos
3650
+ if (this.crossesLine(afterKey, colon)) {
3651
+ // The caller found the `:` at the cursor, so the span always ends at `colon + 1`.
3360
3652
  this.error(
3361
3653
  YAML_ERR_BAD_IMPLICIT_KEY,
3362
3654
  'An implicit key must be followed by its ":" on the same line',
3363
3655
  afterKey,
3364
- this.pos + 1
3656
+ colon < this.len ? colon + 1 : colon
3365
3657
  )
3366
3658
  }
3367
3659
  }
@@ -3372,14 +3664,15 @@ class YamlParser {
3372
3664
  YAML_ERR_BAD_IMPLICIT_KEY,
3373
3665
  `An implicit key may not run more than ${YAML_MAX_KEY_LENGTH} characters before its ":"`,
3374
3666
  keyStart,
3375
- colon + 1
3667
+ // The caller found the `:` at `colon`, so the span always ends at `colon + 1`.
3668
+ colon < this.len ? colon + 1 : colon
3376
3669
  )
3377
3670
  }
3378
3671
 
3379
3672
  /** Reports content between a key node and its `:`. */
3380
3673
  private checkKeyGap(colon: i32): void {
3381
3674
  let from: i32 = this.pos
3382
- while (yamlIsBlank(this.at(from))) {
3675
+ while (from < this.len && yamlIsBlank(this.at(from))) {
3383
3676
  from += 1
3384
3677
  }
3385
3678
  if (from >= colon) {
@@ -3400,23 +3693,28 @@ class YamlParser {
3400
3693
  YAML_ERR_BAD_PROPERTY,
3401
3694
  'Node properties cannot come before a "?" explicit key indicator on its line; write them after the "?", or on a line of their own above',
3402
3695
  at,
3403
- at + 1
3696
+ // The callers found the `?` at `at`, so the span always ends at `at + 1`.
3697
+ at >= 0 && at < this.len ? at + 1 : at
3404
3698
  )
3405
3699
  }
3406
3700
 
3407
3701
  /** Steps over a `,` with no entry before it, reporting it; whether there was one. */
3408
3702
  private skipEmptyFlowEntry(): boolean {
3409
- if (this.at(this.pos) !== YAML_COMMA) {
3703
+ const at: i32 = this.pos
3704
+ if (at >= this.len || this.at(at) !== YAML_COMMA) {
3410
3705
  return false
3411
3706
  }
3412
- this.error(YAML_ERR_UNEXPECTED_COMMA, 'Missing value before ","', this.pos, this.pos + 1)
3413
- this.pos += 1
3707
+ const next: i32 = at + 1
3708
+ this.error(YAML_ERR_UNEXPECTED_COMMA, 'Missing value before ","', at, next)
3709
+ this.pos = next
3414
3710
  return true
3415
3711
  }
3416
3712
 
3417
3713
  /** Reads the flow value after a `:` in flow, or `-1` when the entry ends first. */
3418
3714
  private parseFlowValue(close: i32): i32 {
3419
- this.pos += 1
3715
+ // Both callers found the `:` at the cursor, so this always steps one byte.
3716
+ const at: i32 = this.pos
3717
+ this.pos = at < this.len ? at + 1 : at
3420
3718
  this.skipFlowWs()
3421
3719
  const c: i32 = this.at(this.pos)
3422
3720
  if (c < 0 || c === YAML_COMMA || c === close) {
@@ -3427,13 +3725,14 @@ class YamlParser {
3427
3725
 
3428
3726
  /** Reads the separator after a flow entry; whether the collection goes on. */
3429
3727
  private flowSeparator(close: i32, start: i32): boolean {
3430
- const c: i32 = this.at(this.pos)
3431
- if (c === YAML_COMMA) {
3432
- this.pos += 1
3728
+ const at: i32 = this.pos
3729
+ const c: i32 = this.at(at)
3730
+ if (c === YAML_COMMA && at < this.len) {
3731
+ this.pos = at + 1
3433
3732
  return true
3434
3733
  }
3435
- if (c === close) {
3436
- this.pos += 1
3734
+ if (c === close && at < this.len) {
3735
+ this.pos = at + 1
3437
3736
  return false
3438
3737
  }
3439
3738
  this.reportUnterminatedFlow(close, start)
@@ -3457,12 +3756,13 @@ class YamlParser {
3457
3756
 
3458
3757
  /** Reads the flow sequence opening at the cursor. */
3459
3758
  private parseFlowSeq(): i32 {
3460
- if (this.depth >= this.maxDepth) {
3759
+ const depth: i32 = this.depth
3760
+ if (depth >= this.maxDepth) {
3461
3761
  return this.depthLimitNode()
3462
3762
  }
3463
- this.depth += 1
3763
+ this.depth = depth + 1
3464
3764
  const node: i32 = this.parseFlowSeqBody()
3465
- this.depth -= 1
3765
+ this.depth = depth
3466
3766
  return node
3467
3767
  }
3468
3768
 
@@ -3470,12 +3770,14 @@ class YamlParser {
3470
3770
  private parseFlowSeqBody(): i32 {
3471
3771
  const start: i32 = this.pos
3472
3772
  const base: i32 = this.scratchTop
3473
- this.pos += 1
3773
+ // The caller found the `[` at the cursor, so this always steps one byte.
3774
+ this.pos = start < this.len ? start + 1 : start
3474
3775
  while (true) {
3475
3776
  this.skipFlowWs()
3476
- const c: i32 = this.at(this.pos)
3477
- if (c === YAML_RBRACKET) {
3478
- this.pos += 1
3777
+ const at: i32 = this.pos
3778
+ const c: i32 = this.at(at)
3779
+ if (c === YAML_RBRACKET && at < this.len) {
3780
+ this.pos = at + 1
3479
3781
  break
3480
3782
  }
3481
3783
  if (c < 0) {
@@ -3520,12 +3822,13 @@ class YamlParser {
3520
3822
 
3521
3823
  /** Reads the flow mapping opening at the cursor. */
3522
3824
  private parseFlowMap(): i32 {
3523
- if (this.depth >= this.maxDepth) {
3825
+ const depth: i32 = this.depth
3826
+ if (depth >= this.maxDepth) {
3524
3827
  return this.depthLimitNode()
3525
3828
  }
3526
- this.depth += 1
3829
+ this.depth = depth + 1
3527
3830
  const node: i32 = this.parseFlowMapBody()
3528
- this.depth -= 1
3831
+ this.depth = depth
3529
3832
  return node
3530
3833
  }
3531
3834
 
@@ -3534,12 +3837,14 @@ class YamlParser {
3534
3837
  const start: i32 = this.pos
3535
3838
  const base: i32 = this.scratchTop
3536
3839
  let keySet: i32 = -1
3537
- this.pos += 1
3840
+ // The caller found the `{` at the cursor, so this always steps one byte.
3841
+ this.pos = start < this.len ? start + 1 : start
3538
3842
  while (true) {
3539
3843
  this.skipFlowWs()
3540
- const c: i32 = this.at(this.pos)
3541
- if (c === YAML_RBRACE) {
3542
- this.pos += 1
3844
+ const at: i32 = this.pos
3845
+ const c: i32 = this.at(at)
3846
+ if (c === YAML_RBRACE && at < this.len) {
3847
+ this.pos = at + 1
3543
3848
  break
3544
3849
  }
3545
3850
  if (c < 0) {
@@ -3573,6 +3878,10 @@ class YamlParser {
3573
3878
 
3574
3879
  /** Moves the scratch slots from `base` up into `node`'s children. */
3575
3880
  private finishCollection(node: i32, base: i32): void {
3881
+ // `base` is a `scratchTop` mark, never negative, so this never returns.
3882
+ if (base < 0) {
3883
+ return
3884
+ }
3576
3885
  const stream: YamlStream = this.stream
3577
3886
  let first: i32 = stream.kidTop
3578
3887
  let i: i32 = base
@@ -3584,7 +3893,8 @@ class YamlParser {
3584
3893
  i += 1
3585
3894
  }
3586
3895
  yamlPut(stream.firsts, node, first)
3587
- yamlPut(stream.counts, node, this.scratchTop - base)
3896
+ // `base` is a mark below `scratchTop`, so `i` stopped at `scratchTop`.
3897
+ yamlPut(stream.counts, node, i - base)
3588
3898
  this.scratchTop = base
3589
3899
  }
3590
3900
 
@@ -3627,7 +3937,12 @@ class YamlParser {
3627
3937
  let i: i32 = 0
3628
3938
  while (i < count) {
3629
3939
  const item: i32 = stream.child(seq, i)
3630
- stream.mergeItemsChecked += 1
3940
+ // Each item is read at most once, and a stream holds fewer than 2^31
3941
+ // nodes, so the test never fails; it is what proves the step.
3942
+ const checked: i32 = stream.mergeItemsChecked
3943
+ if (checked < 2147483647) {
3944
+ stream.mergeItemsChecked = checked + 1
3945
+ }
3631
3946
  if (!this.mergesMap(item)) {
3632
3947
  this.badMerge(stream.start(item), stream.end(item))
3633
3948
  }
@@ -3645,6 +3960,10 @@ class YamlParser {
3645
3960
  */
3646
3961
  private aliasedSeqMerges(seq: i32): boolean {
3647
3962
  const stream: YamlStream = this.stream
3963
+ // `seq` is a node the parser made, so this never returns.
3964
+ if (seq < 0 || seq >= stream.top) {
3965
+ return false
3966
+ }
3648
3967
  this.mergeVerdicts = yamlRoomU8(this.mergeVerdicts, seq + 1)
3649
3968
  const verdicts: u8[] = this.mergeVerdicts
3650
3969
  if (seq < 0 || seq >= toI32(verdicts.length)) {
@@ -3658,7 +3977,11 @@ class YamlParser {
3658
3977
  let good: boolean = true
3659
3978
  let i: i32 = 0
3660
3979
  while (good && i < count) {
3661
- stream.mergeItemsChecked += 1
3980
+ // As in `checkMerge`: the test never fails and proves the step.
3981
+ const checked: i32 = stream.mergeItemsChecked
3982
+ if (checked < 2147483647) {
3983
+ stream.mergeItemsChecked = checked + 1
3984
+ }
3662
3985
  good = this.mergesMap(stream.child(seq, i))
3663
3986
  i += 1
3664
3987
  }
@@ -3720,7 +4043,12 @@ class YamlParser {
3720
4043
  return set
3721
4044
  }
3722
4045
  const stream: YamlStream = this.stream
3723
- if (set < 0 && this.scratchTop - base < YAML_PAIRWISE_KEYS) {
4046
+ const top: i32 = this.scratchTop
4047
+ // `base` is a `scratchTop` mark at or below the top, so this never returns.
4048
+ if (base < 0 || base > top) {
4049
+ return set
4050
+ }
4051
+ if (set < 0 && top - base < YAML_PAIRWISE_KEYS) {
3724
4052
  let text: string = ""
3725
4053
  let decoded: boolean = false
3726
4054
  let i: i32 = base
@@ -3748,17 +4076,25 @@ class YamlParser {
3748
4076
  if (held >= toI32(this.keySets.length)) {
3749
4077
  this.keySets.push(new YamlKeySet())
3750
4078
  }
4079
+ }
4080
+ const sets: YamlKeySet[] = this.keySets
4081
+ // `keySetTop` never passes the pool's length and a held set came from the pool, so this never returns.
4082
+ if (held < 0 || held >= toI32(sets.length)) {
4083
+ return set
4084
+ }
4085
+ const keys: YamlKeySet = sets[held]
4086
+ if (set < 0) {
3751
4087
  this.keySetTop = held + 1
3752
4088
  let i: i32 = base
3753
4089
  while (i < this.scratchTop) {
3754
4090
  const other: i32 = stream.key(yamlAt(this.scratch, i))
3755
4091
  if (!this.isMergeKey(other)) {
3756
- this.keySets[held].add(yamlKeyText(stream, other))
4092
+ keys.add(yamlKeyText(stream, other))
3757
4093
  }
3758
4094
  i += 1
3759
4095
  }
3760
4096
  }
3761
- if (!this.keySets[held].add(yamlKeyText(stream, key))) {
4097
+ if (!keys.add(yamlKeyText(stream, key))) {
3762
4098
  this.reportDuplicateKey(key)
3763
4099
  }
3764
4100
  return held
@@ -3766,8 +4102,10 @@ class YamlParser {
3766
4102
 
3767
4103
  /** Hands the key set a finished map held back to the pool. */
3768
4104
  private releaseKeySet(set: i32): void {
3769
- if (set >= 0) {
3770
- this.keySets[set].clear()
4105
+ const sets: YamlKeySet[] = this.keySets
4106
+ // `trackKey` hands out only sets inside the pool, so `set` is below its length.
4107
+ if (set >= 0 && set < toI32(sets.length)) {
4108
+ sets[set].clear()
3771
4109
  this.keySetTop = set
3772
4110
  }
3773
4111
  }
@@ -3787,8 +4125,8 @@ class YamlParser {
3787
4125
 
3788
4126
  /** The start of a node written on the `---` line at `marker`, or `-1` when the line ends first. */
3789
4127
  private docMarkerInlineNode(marker: i32): i32 {
3790
- let i: i32 = marker + 3
3791
- while (yamlIsBlank(this.at(i))) {
4128
+ let i: i32 = this.markerEnd(marker)
4129
+ while (i < this.len && yamlIsBlank(this.at(i))) {
3792
4130
  i += 1
3793
4131
  }
3794
4132
  const c: i32 = this.at(i)
@@ -3811,11 +4149,11 @@ class YamlParser {
3811
4149
 
3812
4150
  /** Reports content after the `...` marker at `marker` on its line. */
3813
4151
  private checkDocEndTail(marker: i32): void {
3814
- if (this.lineRestIsBlank(marker + 3)) {
4152
+ let after: i32 = this.markerEnd(marker)
4153
+ if (this.lineRestIsBlank(after)) {
3815
4154
  return
3816
4155
  }
3817
- let after: i32 = marker + 3
3818
- while (yamlIsBlank(this.at(after))) {
4156
+ while (after < this.len && yamlIsBlank(this.at(after))) {
3819
4157
  after += 1
3820
4158
  }
3821
4159
  this.error(
@@ -3831,7 +4169,7 @@ class YamlParser {
3831
4169
  let i: i32 = from
3832
4170
  while (i < this.len) {
3833
4171
  if (!this.lineRestIsBlank(i)) {
3834
- if (!this.isDocMarker(i) || !this.lineRestIsBlank(i + 3)) {
4172
+ if (!this.isDocMarker(i) || !this.lineRestIsBlank(this.markerEnd(i))) {
3835
4173
  return false
3836
4174
  }
3837
4175
  }
@@ -3962,16 +4300,17 @@ class YamlParser {
3962
4300
  * marker, which is the first document's footer, or `-1`.
3963
4301
  */
3964
4302
  private warnIfMoreDocuments(marker: i32): i32 {
3965
- this.recordLineComment(marker + 3)
4303
+ const end: i32 = this.markerEnd(marker)
4304
+ this.recordLineComment(end)
3966
4305
  const footer: boolean = this.at(marker) === YAML_DOT
3967
4306
  let hidden: boolean = false
3968
4307
  if (footer) {
3969
4308
  this.checkDocEndTail(marker)
3970
4309
  } else {
3971
- hidden = !this.lineRestIsBlank(marker + 3)
4310
+ hidden = !this.lineRestIsBlank(end)
3972
4311
  }
3973
- this.pos = this.nextLineStart(marker + 3)
3974
- const footerEnd: i32 = footer ? marker + 3 : -1
4312
+ this.pos = this.nextLineStart(end)
4313
+ const footerEnd: i32 = footer ? end : -1
3975
4314
  if (!hidden) {
3976
4315
  this.peekLine(toI32(0))
3977
4316
  if (this.lineEof || this.onlyEmptyDocumentsFollow(this.pos)) {
@@ -3982,7 +4321,7 @@ class YamlParser {
3982
4321
  YAML_ERR_MULTIPLE_DOCUMENTS,
3983
4322
  "Only the first document of this stream was parsed — turn off firstDocumentOnly to read the rest",
3984
4323
  marker,
3985
- marker + 3
4324
+ end
3986
4325
  )
3987
4326
  return footerEnd
3988
4327
  }
@@ -4022,8 +4361,9 @@ class YamlParser {
4022
4361
  }
4023
4362
  if (c === YAML_DOT) {
4024
4363
  this.checkDocEndTail(p)
4025
- this.recordLineComment(p + 3)
4026
- this.pos = this.nextLineStart(p + 3)
4364
+ const end: i32 = this.markerEnd(p)
4365
+ this.recordLineComment(end)
4366
+ this.pos = this.nextLineStart(end)
4027
4367
  seen = seen | YAML_SAW_DOC_END
4028
4368
  continue
4029
4369
  }
@@ -4116,9 +4456,9 @@ class YamlParser {
4116
4456
  if (this.atMarkerLine() && this.at(this.lineContent) === YAML_DOT) {
4117
4457
  const p: i32 = this.lineContent
4118
4458
  this.checkDocEndTail(p)
4119
- this.recordLineComment(p + 3)
4120
- this.pos = this.nextLineStart(p + 3)
4121
- footerEnd = p + 3
4459
+ footerEnd = this.markerEnd(p)
4460
+ this.recordLineComment(footerEnd)
4461
+ this.pos = this.nextLineStart(footerEnd)
4122
4462
  }
4123
4463
  this.pushDocument(root, footerEnd)
4124
4464
  closed = footerEnd >= 0
@@ -4127,7 +4467,8 @@ class YamlParser {
4127
4467
  this.handleNames = []
4128
4468
  this.handlePrefixes = []
4129
4469
  this.yamlDirective = false
4130
- this.doc += 1
4470
+ // Each pass pushes one document, so the count is `this.doc + 1` with no sum to bound.
4471
+ this.doc = toI32(this.stream.docRoots.length)
4131
4472
  }
4132
4473
  const stream: YamlStream = this.stream
4133
4474
  const count: i32 = toI32(stream.docRoots.length)
@@ -4150,14 +4491,16 @@ class YamlParser {
4150
4491
  private pushDocument(root: i32, footerEnd: i32): void {
4151
4492
  const stream: YamlStream = this.stream
4152
4493
  let start: i32 = root >= 0 ? stream.start(root) : this.pos
4153
- if (this.markerAt >= 0) {
4154
- start = this.markerAt
4494
+ const marker: i32 = this.markerAt
4495
+ if (marker >= 0) {
4496
+ start = marker
4155
4497
  }
4156
4498
  let end: i32 = footerEnd
4157
4499
  if (end < 0) {
4158
4500
  end = root >= 0 ? stream.end(root) : start
4159
- if (this.markerAt + 3 > end) {
4160
- end = this.markerAt + 3
4501
+ const markerEnd: i32 = marker >= 0 ? this.markerEnd(marker) : marker + 3
4502
+ if (markerEnd > end) {
4503
+ end = markerEnd
4161
4504
  }
4162
4505
  if (start > end) {
4163
4506
  end = start
@@ -4180,7 +4523,8 @@ class YamlParser {
4180
4523
  private parsePropsOnlyValue(props: i32, parentIndent: i32, seqAtParentIndent: boolean): i32 {
4181
4524
  const propsEnd: i32 = this.pos
4182
4525
  this.finishLine()
4183
- this.peekLine(parentIndent + 1)
4526
+ // An indent is a column inside the text, so this is always `parentIndent + 1`.
4527
+ this.peekLine(parentIndent < this.len ? parentIndent + 1 : parentIndent)
4184
4528
  if (!this.lineEof && this.lineIndent > parentIndent) {
4185
4529
  this.pos = this.lineContent
4186
4530
  return this.attachProps(this.parseNode(this.lineIndent, parentIndent, seqAtParentIndent), props)
@@ -4229,11 +4573,15 @@ class YamlParser {
4229
4573
 
4230
4574
  /** The column of `at`: its distance from the start of its line. */
4231
4575
  private columnOf(at: i32): i32 {
4576
+ if (at <= 0) {
4577
+ return 0
4578
+ }
4232
4579
  let i: i32 = at
4233
4580
  while (i > 0 && !yamlIsBreak(this.at(i - 1))) {
4234
4581
  i -= 1
4235
4582
  }
4236
- return at - i
4583
+ // The loop only walks `i` down from `at` to `0`, so the guard always holds.
4584
+ return i >= 0 && i <= at ? at - i : 0
4237
4585
  }
4238
4586
 
4239
4587
  /**
@@ -4284,7 +4632,8 @@ class YamlParser {
4284
4632
  }
4285
4633
  if (this.atLineEnd()) {
4286
4634
  this.finishLine()
4287
- this.peekLine(indent + 1)
4635
+ // An indent is a column inside the text, so this is always `indent + 1`.
4636
+ this.peekLine(indent < this.len ? indent + 1 : indent)
4288
4637
  if (!this.lineEof && this.lineIndent > indent) {
4289
4638
  this.pos = this.lineContent
4290
4639
  return this.parseNode(this.lineIndent, indent, true)
@@ -4313,12 +4662,13 @@ class YamlParser {
4313
4662
  * `-1`, an explicit `?` key at the cursor.
4314
4663
  */
4315
4664
  private parseBlockMap(indent: i32, firstColon: i32, firstKey: i32, firstProps: i32): i32 {
4316
- if (this.depth >= this.maxDepth) {
4665
+ const depth: i32 = this.depth
4666
+ if (depth >= this.maxDepth) {
4317
4667
  return this.depthLimitNode()
4318
4668
  }
4319
- this.depth += 1
4669
+ this.depth = depth + 1
4320
4670
  const node: i32 = this.parseBlockMapBody(indent, firstColon, firstKey, firstProps)
4321
- this.depth -= 1
4671
+ this.depth = depth
4322
4672
  return node
4323
4673
  }
4324
4674
 
@@ -4374,26 +4724,31 @@ class YamlParser {
4374
4724
  let key: i32 = -1
4375
4725
  let value: i32 = -1
4376
4726
  if (explicit) {
4377
- this.pos = contentPos + 1
4727
+ // `isExplicitKey` found the `?` at `contentPos`, so this is always `contentPos + 1`.
4728
+ const keyAt: i32 = contentPos < this.len ? contentPos + 1 : contentPos
4729
+ this.pos = keyAt
4378
4730
  key = this.parseValueOrChild(indent, true)
4379
4731
  if (key < 0) {
4380
- key = this.emptyScalar(contentPos + 1)
4732
+ key = this.emptyScalar(keyAt)
4381
4733
  }
4382
4734
  key = this.attachProps(key, keyProps)
4383
4735
  this.peekLine(indent)
4736
+ const lineContent: i32 = this.lineContent
4384
4737
  if (
4385
4738
  !this.lineEof &&
4386
4739
  this.lineIndent === indent &&
4387
- this.at(this.lineContent) === YAML_COLON &&
4388
- yamlIsWs(this.at(this.lineContent + 1))
4740
+ lineContent < this.len &&
4741
+ this.at(lineContent) === YAML_COLON &&
4742
+ yamlIsWs(this.after(lineContent))
4389
4743
  ) {
4390
- this.pos = this.lineContent + 1
4744
+ this.pos = lineContent + 1
4391
4745
  value = this.parseValueOrChild(indent, true)
4392
4746
  }
4393
4747
  } else {
4394
4748
  key = this.parseImplicitKey(indent, contentPos, colon)
4395
4749
  key = this.attachProps(key, keyProps)
4396
- this.pos = colon + 1
4750
+ // `findKeyColon` found the `:` at `colon`, so this is always `colon + 1`.
4751
+ this.pos = colon < this.len ? colon + 1 : colon
4397
4752
  value = this.parseValueOrChild(indent, false)
4398
4753
  }
4399
4754
  keySet = this.trackKey(base, key, keySet)
@@ -4402,9 +4757,11 @@ class YamlParser {
4402
4757
  this.releaseKeySet(keySet)
4403
4758
  let start: i32 = this.pos
4404
4759
  let end: i32 = this.pos
4405
- if (this.scratchTop > base) {
4760
+ const top: i32 = this.scratchTop
4761
+ // `base` is a `scratchTop` mark, never negative, so `top > 0` adds nothing.
4762
+ if (top > base && top > 0) {
4406
4763
  start = stream.start(yamlAt(this.scratch, base))
4407
- end = stream.end(yamlAt(this.scratch, this.scratchTop - 1))
4764
+ end = stream.end(yamlAt(this.scratch, top - 1))
4408
4765
  }
4409
4766
  const node: i32 = stream.newNode(YAML_MAP, YAML_BLOCK, start, end)
4410
4767
  this.finishCollection(node, base)
@@ -4413,7 +4770,8 @@ class YamlParser {
4413
4770
 
4414
4771
  /** Reads the implicit key at `from` of a block mapping at `indent`, whose `:` is at `colon`. */
4415
4772
  private parseImplicitKey(indent: i32, from: i32, colon: i32): i32 {
4416
- if (colon - from > YAML_MAX_KEY_LENGTH) {
4773
+ // `findKeyColon` found `colon` at or after `from`, inside the text.
4774
+ if (from >= 0 && colon > from && colon - from > YAML_MAX_KEY_LENGTH) {
4417
4775
  this.reportLongImplicitKey(from, colon)
4418
4776
  }
4419
4777
  this.pos = from
@@ -4449,12 +4807,13 @@ class YamlParser {
4449
4807
 
4450
4808
  /** Reads a block sequence at `indent`, whose first `-` is at the cursor. */
4451
4809
  private parseBlockSeq(indent: i32): i32 {
4452
- if (this.depth >= this.maxDepth) {
4810
+ const depth: i32 = this.depth
4811
+ if (depth >= this.maxDepth) {
4453
4812
  return this.depthLimitNode()
4454
4813
  }
4455
- this.depth += 1
4814
+ this.depth = depth + 1
4456
4815
  const node: i32 = this.parseBlockSeqBody(indent)
4457
- this.depth -= 1
4816
+ this.depth = depth
4458
4817
  return node
4459
4818
  }
4460
4819
 
@@ -4474,7 +4833,7 @@ class YamlParser {
4474
4833
  contentPos = this.lineContent
4475
4834
  }
4476
4835
  firstEntry = false
4477
- if (!this.isSeqEntryDash(contentPos)) {
4836
+ if (contentPos >= this.len || !this.isSeqEntryDash(contentPos)) {
4478
4837
  break
4479
4838
  }
4480
4839
  if (start < 0) {
@@ -4486,7 +4845,8 @@ class YamlParser {
4486
4845
  let item: i32 = -1
4487
4846
  if (this.atLineEnd()) {
4488
4847
  this.finishLine()
4489
- this.peekLine(indent + 1)
4848
+ // An indent is a column inside the text, so this is always `indent + 1`.
4849
+ this.peekLine(indent < this.len ? indent + 1 : indent)
4490
4850
  if (!this.lineEof && this.lineIndent > indent) {
4491
4851
  this.pos = this.lineContent
4492
4852
  item = this.parseNode(this.lineIndent, indent, false)
@@ -4498,7 +4858,21 @@ class YamlParser {
4498
4858
  if (c === YAML_PIPE || c === YAML_GT) {
4499
4859
  item = this.scanBlockScalar(indent)
4500
4860
  } else {
4501
- item = this.parseNode(this.pos - contentPos + indent, indent, false)
4861
+ // The item's column is the dash's, `indent`, plus its distance from
4862
+ // the dash. Both are below the text's length, so the walk back to the
4863
+ // line start never runs; the test is what proves the sum.
4864
+ const pos: i32 = this.pos
4865
+ let column: i32 = -1
4866
+ if (contentPos >= 0 && pos >= contentPos) {
4867
+ const off: i32 = pos - contentPos
4868
+ if (off >= 0 && off < 1073741824 && indent >= -1 && indent < 1073741824) {
4869
+ column = off + indent
4870
+ }
4871
+ }
4872
+ if (column < 0) {
4873
+ column = this.columnOf(pos)
4874
+ }
4875
+ item = this.parseNode(column, indent, false)
4502
4876
  }
4503
4877
  this.finishLineIfMidLine()
4504
4878
  }
@@ -4507,7 +4881,9 @@ class YamlParser {
4507
4881
  if (start < 0) {
4508
4882
  start = this.pos
4509
4883
  }
4510
- const end: i32 = this.scratchTop > base ? stream.end(yamlAt(this.scratch, this.scratchTop - 1)) : this.pos
4884
+ const top: i32 = this.scratchTop
4885
+ // `base` is a `scratchTop` mark, never negative, so `top > 0` adds nothing.
4886
+ const end: i32 = top > base && top > 0 ? stream.end(yamlAt(this.scratch, top - 1)) : this.pos
4511
4887
  const node: i32 = stream.newNode(YAML_SEQ, YAML_BLOCK, start, end)
4512
4888
  this.finishCollection(node, base)
4513
4889
  return node
@@ -4525,12 +4901,13 @@ class YamlParser {
4525
4901
  const props: i32 = this.scanProps(false)
4526
4902
  if (props >= 0) {
4527
4903
  this.skipInlineSpaces()
4528
- if (this.isSeqEntryDash(this.pos)) {
4904
+ const at: i32 = this.pos
4905
+ if (at < this.len && this.isSeqEntryDash(at)) {
4529
4906
  this.error(
4530
4907
  YAML_ERR_UNEXPECTED_CONTENT,
4531
4908
  "A block sequence cannot start on the line its node properties are written on",
4532
- this.pos,
4533
- this.pos + 1
4909
+ at,
4910
+ at + 1
4534
4911
  )
4535
4912
  }
4536
4913
  if (this.atLineEnd()) {
@@ -4548,7 +4925,8 @@ class YamlParser {
4548
4925
  }
4549
4926
  node = this.attachProps(node, props)
4550
4927
  this.skipInlineSpaces()
4551
- if (this.at(this.pos) === YAML_COLON && yamlIsWs(this.at(this.pos + 1))) {
4928
+ const at: i32 = this.pos
4929
+ if (at < this.len && this.at(at) === YAML_COLON && yamlIsWs(this.after(at))) {
4552
4930
  const stream: YamlStream = this.stream
4553
4931
  if (this.crossesLine(stream.start(node), stream.end(node))) {
4554
4932
  this.error(
@@ -4558,7 +4936,7 @@ class YamlParser {
4558
4936
  stream.end(node)
4559
4937
  )
4560
4938
  }
4561
- this.pos += 1
4939
+ this.pos = at + 1
4562
4940
  return this.parseBlockMap(indent, YAML_NONE, node, YAML_NONE)
4563
4941
  }
4564
4942
  this.checkTrailingContent()
@@ -4616,7 +4994,6 @@ class YamlKeySet {
4616
4994
  private texts: string[]
4617
4995
  private used: boolean[]
4618
4996
  private filled: i32[]
4619
- private mask: i32 = 0
4620
4997
 
4621
4998
  constructor() {
4622
4999
  this.texts = []
@@ -4630,21 +5007,30 @@ class YamlKeySet {
4630
5007
  * @internal Called by `YamlParser`.
4631
5008
  */
4632
5009
  add(text: string): boolean {
4633
- if ((toI32(this.filled.length) + 1) * 2 > this.mask + 1) {
5010
+ const slots: i32 = toI32(this.used.length)
5011
+ if (slots < 1073741824 && toI32(this.filled.length) >= slots / 2) {
4634
5012
  this.grow()
4635
5013
  }
4636
- let slot: i32 = yamlKeyHash(text) & this.mask
4637
- while (slot < toI32(this.used.length) && this.used[slot]) {
4638
- if (this.texts[slot] === text) {
5014
+ const used: boolean[] = this.used
5015
+ const texts: string[] = this.texts
5016
+ const size: i32 = toI32(used.length)
5017
+ // `size` is a power of two, so the mask keeps every probe inside the table.
5018
+ const mask: i32 = size - 1
5019
+ let slot: i32 = yamlKeyHash(text) & mask
5020
+ // A probe visits each slot at most once: past that, `text` is in none of them.
5021
+ let probes: i32 = 0
5022
+ while (probes < size && slot >= 0 && slot < size && slot < toI32(texts.length) && used[slot]) {
5023
+ if (texts[slot] === text) {
4639
5024
  return false
4640
5025
  }
4641
- slot = (slot + 1) & this.mask
5026
+ slot = (slot + 1) & mask
5027
+ probes += 1
4642
5028
  }
4643
- if (slot < toI32(this.used.length)) {
4644
- this.used[slot] = true
4645
- this.texts[slot] = text
5029
+ if (probes < size && slot >= 0 && slot < size && slot < toI32(texts.length)) {
5030
+ used[slot] = true
5031
+ texts[slot] = text
5032
+ this.filled.push(slot)
4646
5033
  }
4647
- this.filled.push(slot)
4648
5034
  return true
4649
5035
  }
4650
5036
 
@@ -4653,28 +5039,38 @@ class YamlKeySet {
4653
5039
  * @internal Called by `YamlParser`.
4654
5040
  */
4655
5041
  clear(): void {
5042
+ const filled: i32[] = this.filled
5043
+ const used: boolean[] = this.used
5044
+ const texts: string[] = this.texts
4656
5045
  let i: i32 = 0
4657
- while (i < toI32(this.filled.length)) {
4658
- const slot: i32 = this.filled[i]
4659
- if (slot >= 0 && slot < toI32(this.used.length)) {
4660
- this.used[slot] = false
4661
- this.texts[slot] = ""
5046
+ while (i < toI32(filled.length)) {
5047
+ const slot: i32 = filled[i]
5048
+ if (slot >= 0 && slot < toI32(used.length) && slot < toI32(texts.length)) {
5049
+ used[slot] = false
5050
+ texts[slot] = ""
4662
5051
  }
4663
5052
  i += 1
4664
5053
  }
4665
5054
  this.filled = []
4666
5055
  }
4667
5056
 
4668
- /** Doubles the table and puts every key back. */
5057
+ /**
5058
+ * Doubles the table and puts every key back. `add` stops growing it at
5059
+ * 2^30 slots, which is more keys than a text can spell: each takes a byte
5060
+ * and a separator, and a text is shorter than 2^31 bytes.
5061
+ */
4669
5062
  private grow(): void {
4670
5063
  const texts: string[] = []
5064
+ const filled: i32[] = this.filled
5065
+ const old: string[] = this.texts
4671
5066
  let i: i32 = 0
4672
- while (i < toI32(this.filled.length)) {
4673
- const slot: i32 = this.filled[i]
4674
- texts.push(slot >= 0 && slot < toI32(this.texts.length) ? this.texts[slot] : "")
5067
+ while (i < toI32(filled.length)) {
5068
+ const slot: i32 = filled[i]
5069
+ texts.push(slot >= 0 && slot < toI32(old.length) ? old[slot] : "")
4675
5070
  i += 1
4676
5071
  }
4677
- this.resize((this.mask + 1) * 2)
5072
+ const slots: i32 = toI32(this.used.length)
5073
+ this.resize(slots >= 0 && slots < 1073741824 ? slots + slots : slots)
4678
5074
  let j: i32 = 0
4679
5075
  while (j < toI32(texts.length)) {
4680
5076
  this.add(texts[j])
@@ -4684,16 +5080,17 @@ class YamlKeySet {
4684
5080
 
4685
5081
  /** An empty table of `slots` slots. */
4686
5082
  private resize(slots: i32): void {
4687
- this.texts = []
4688
- this.used = []
4689
- this.filled = []
5083
+ const texts: string[] = []
5084
+ const used: boolean[] = []
4690
5085
  let i: i32 = 0
4691
5086
  while (i < slots) {
4692
- this.texts.push("")
4693
- this.used.push(false)
5087
+ texts.push("")
5088
+ used.push(false)
4694
5089
  i += 1
4695
5090
  }
4696
- this.mask = slots - 1
5091
+ this.texts = texts
5092
+ this.used = used
5093
+ this.filled = []
4697
5094
  }
4698
5095
  }
4699
5096
 
@@ -4704,9 +5101,7 @@ const yamlIsMergeKey = (stream: YamlStream, key: i32): boolean => {
4704
5101
  }
4705
5102
  const start: i32 = stream.start(key)
4706
5103
  if (
4707
- stream.end(key) !== start + 2 ||
4708
- yamlByte(stream.source, start) !== YAML_LT ||
4709
- yamlByte(stream.source, start + 1) !== YAML_LT ||
5104
+ !yamlIsWord(stream.source, start, stream.end(key), "<<") ||
4710
5105
  stream.kind(key) !== YAML_SCALAR ||
4711
5106
  stream.style(key) !== YAML_PLAIN
4712
5107
  ) {
@@ -4745,9 +5140,10 @@ class YamlFlowWriter {
4745
5140
  return
4746
5141
  }
4747
5142
  const length: i32 = toI32(s.length)
4748
- if (length > this.left) {
5143
+ const left: i32 = this.left
5144
+ if (length > left) {
4749
5145
  // Back up to the start of a character, so the cut never splits one's bytes.
4750
- let keep: i32 = this.left
5146
+ let keep: i32 = left
4751
5147
  while (keep > 0 && (yamlByte(s, keep) & 0xc0) === 0x80) {
4752
5148
  keep -= 1
4753
5149
  }
@@ -4756,7 +5152,7 @@ class YamlFlowWriter {
4756
5152
  return
4757
5153
  }
4758
5154
  this.out = this.out + s
4759
- this.left -= length
5155
+ this.left = left - length
4760
5156
  }
4761
5157
 
4762
5158
  /**
@@ -4770,6 +5166,8 @@ class YamlFlowWriter {
4770
5166
  this.cut = true
4771
5167
  return
4772
5168
  }
5169
+ // `depth` is at most the limit, so the step never passes the limit and one.
5170
+ const deeper: i32 = depth < YAML_KEY_TEXT_DEPTH ? depth + 1 : YAML_KEY_TEXT_DEPTH + 1
4773
5171
  const at: i32 = n >= 0 ? stream.resolved(n) : -1
4774
5172
  if (at < 0) {
4775
5173
  this.append(n >= 0 ? yamlKeyText(stream, n) : "null")
@@ -4794,11 +5192,11 @@ class YamlFlowWriter {
4794
5192
  }
4795
5193
  const item: i32 = stream.child(at, i)
4796
5194
  if (map) {
4797
- this.write(stream.key(item), depth + 1)
5195
+ this.write(stream.key(item), deeper)
4798
5196
  this.append(": ")
4799
- this.write(stream.value(item), depth + 1)
5197
+ this.write(stream.value(item), deeper)
4800
5198
  } else {
4801
- this.write(item, depth + 1)
5199
+ this.write(item, deeper)
4802
5200
  }
4803
5201
  i += 1
4804
5202
  }
@@ -4870,7 +5268,7 @@ class YamlPathWalker {
4870
5268
  let at: i32 = n
4871
5269
  let hops: i32 = 0
4872
5270
  while (at >= 0 && stream.kind(at) === YAML_ALIAS) {
4873
- if (hops === YAML_ALIAS_HOPS) {
5271
+ if (hops >= YAML_ALIAS_HOPS) {
4874
5272
  return -1
4875
5273
  }
4876
5274
  at = stream.target(at)
@@ -4903,7 +5301,7 @@ class YamlPathWalker {
4903
5301
  /** The index `step` writes, or `-1` when it is not 1 to 9 decimal digits without a leading zero. */
4904
5302
  private indexOf(step: string): i32 {
4905
5303
  const length: i32 = toI32(step.length)
4906
- if (length === 0 || length > 9 || (length > 1 && toI32(step.charCodeAt(0)) === 48)) {
5304
+ if (length === 0 || length > 9 || (length > 1 && step.startsWith("0"))) {
4907
5305
  return -1
4908
5306
  }
4909
5307
  let index: i32 = 0
@@ -4913,7 +5311,10 @@ class YamlPathWalker {
4913
5311
  if (c < 48 || c > 57) {
4914
5312
  return -1
4915
5313
  }
4916
- index = index * 10 + (c - 48)
5314
+ // Nine digits at most stay below 10^9, so the test never fails; it is what proves the step.
5315
+ if (index >= 0 && index < 100000000) {
5316
+ index = index * 10 + (c - 48)
5317
+ }
4917
5318
  i += 1
4918
5319
  }
4919
5320
  return index
@@ -4922,13 +5323,13 @@ class YamlPathWalker {
4922
5323
  /** The last pair of map `map` whose key `step` matches, as when a duplicate is let through; `-1` for none. */
4923
5324
  private ownPair(map: i32, step: string): i32 {
4924
5325
  const stream: YamlStream = this.stream
4925
- let i: i32 = stream.count(map) - 1
4926
- while (i >= 0) {
5326
+ let i: i32 = stream.count(map)
5327
+ while (i > 0) {
5328
+ i -= 1
4927
5329
  const pair: i32 = stream.child(map, i)
4928
5330
  if (this.keyIs(stream.key(pair), step)) {
4929
5331
  return pair
4930
5332
  }
4931
- i -= 1
4932
5333
  }
4933
5334
  return -1
4934
5335
  }
@@ -4998,15 +5399,17 @@ class YamlPathWalker {
4998
5399
  while (toI32(stamps.length) < stream.top) {
4999
5400
  stamps.push(0)
5000
5401
  }
5001
- if (stream.mergeStep === 2147483647) {
5402
+ const step: i32 = stream.mergeStep
5403
+ if (step < 2147483647) {
5404
+ stream.mergeStep = step + 1
5405
+ } else {
5002
5406
  let k: i32 = 0
5003
5407
  while (k < toI32(stamps.length)) {
5004
5408
  stamps[k] = 0
5005
5409
  k += 1
5006
5410
  }
5007
- stream.mergeStep = 0
5411
+ stream.mergeStep = 1
5008
5412
  }
5009
- stream.mergeStep += 1
5010
5413
  }
5011
5414
 
5012
5415
  /** Marks map `map` searched by this step; `false` when it already was. */
@@ -5030,12 +5433,13 @@ class YamlPathWalker {
5030
5433
  this.pair = -1
5031
5434
  this.matched = 0
5032
5435
  const steps: i32 = toI32(path.length)
5033
- while (this.matched < steps) {
5436
+ let matched: i32 = 0
5437
+ while (matched < steps && matched < toI32(path.length)) {
5034
5438
  const at: i32 = this.follow(this.node)
5035
5439
  if (at < 0) {
5036
5440
  return
5037
5441
  }
5038
- const step: string = path[this.matched]
5442
+ const step: string = path[matched]
5039
5443
  const kind: i32 = stream.kind(at)
5040
5444
  if (kind === YAML_MAP) {
5041
5445
  let pair: i32 = this.ownPair(at, step)
@@ -5050,7 +5454,8 @@ class YamlPathWalker {
5050
5454
  // An empty value (`a:`) matches the step and reaches no node, which ends the walk.
5051
5455
  this.pair = pair
5052
5456
  this.node = stream.value(pair)
5053
- this.matched += 1
5457
+ matched += 1
5458
+ this.matched = matched
5054
5459
  if (this.node < 0) {
5055
5460
  return
5056
5461
  }
@@ -5060,7 +5465,8 @@ class YamlPathWalker {
5060
5465
  return
5061
5466
  }
5062
5467
  this.node = stream.child(at, index)
5063
- this.matched += 1
5468
+ matched += 1
5469
+ this.matched = matched
5064
5470
  } else {
5065
5471
  return
5066
5472
  }
@@ -5131,6 +5537,11 @@ export const parseYamlWith = (text: string, options: YamlOptions): YamlStream =>
5131
5537
  if (options.maxDepth < 1 || options.maxDepth > YAML_MAX_DEPTH) {
5132
5538
  panic("nish/yaml: maxDepth must be between 1 and 1000")
5133
5539
  }
5540
+ return yamlParseValid(text, options)
5541
+ }
5542
+
5543
+ /** Parses `text` with `options`, whose `maxDepth` the caller has checked is 1 to 1000. */
5544
+ const yamlParseValid = (text: string, options: YamlOptions): YamlStream => {
5134
5545
  const parser: YamlParser = new YamlParser(text, options)
5135
5546
  if (options.firstDocumentOnly) {
5136
5547
  parser.parseFirst()
@@ -5147,4 +5558,6 @@ export const parseYamlWith = (text: string, options: YamlOptions): YamlStream =>
5147
5558
  * @param text The YAML source.
5148
5559
  * @returns The documents, nodes and diagnostics the parse found.
5149
5560
  */
5150
- export const parseYaml = (text: string): YamlStream => parseYamlWith(text, new YamlOptions())
5561
+ export const parseYaml = (text: string): YamlStream =>
5562
+ // The default `maxDepth` is 1000, inside the range `parseYamlWith` checks.
5563
+ yamlParseValid(text, new YamlOptions())