@grain/stdlib 0.4.4 → 0.4.5

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.
package/string.gr CHANGED
@@ -9,7 +9,13 @@
9
9
  import WasmI32 from "runtime/unsafe/wasmi32"
10
10
  import Memory from "runtime/unsafe/memory"
11
11
  import Exception from "runtime/exception"
12
- import { tagSimpleNumber, allocateArray, allocateChar, allocateString, allocateBytes } from "runtime/dataStructures"
12
+ import {
13
+ tagSimpleNumber,
14
+ allocateArray,
15
+ allocateChar,
16
+ allocateString,
17
+ allocateBytes,
18
+ } from "runtime/dataStructures"
13
19
 
14
20
  /**
15
21
  * @section Types: Type declarations included in the String module.
@@ -196,10 +202,16 @@ export let rec indexOf = (search: String, string: String) => {
196
202
  @disableGC
197
203
  export let rec charAt = (position, string: String) => {
198
204
  Memory.incRef(WasmI32.fromGrain((<=)))
199
- if (wasmSafeLength(string) <= position || {Memory.incRef(WasmI32.fromGrain((<))); position < 0}) {
205
+ if (
206
+ wasmSafeLength(string) <= position ||
207
+ {
208
+ Memory.incRef(WasmI32.fromGrain((<)))
209
+ position < 0
210
+ }
211
+ ) {
200
212
  Memory.incRef(WasmI32.fromGrain((++)))
201
213
  Memory.incRef(WasmI32.fromGrain(toString))
202
- fail ("Invalid offset: " ++ toString(position))
214
+ fail "Invalid offset: " ++ toString(position)
203
215
  }
204
216
  // Implementation is similar to explodeHelp, but doesn't perform unneeded memory allocations
205
217
  let (>>>) = WasmI32.shrU
@@ -211,9 +223,9 @@ export let rec charAt = (position, string: String) => {
211
223
  let len = WasmI32.fromGrain(wasmSafeLength(string)) >>> 1n
212
224
  let position = WasmI32.fromGrain(position) >>> 1n
213
225
  let string = WasmI32.fromGrain(string)
214
- let mut ptr = string + 8n;
215
- let end = ptr + size;
216
- let mut counter = 0n;
226
+ let mut ptr = string + 8n
227
+ let end = ptr + size
228
+ let mut counter = 0n
217
229
  let mut result = 0n
218
230
  while (ptr < end) {
219
231
  let byte = WasmI32.load8U(ptr, 0n)
@@ -304,10 +316,10 @@ let explodeHelp = (s: String, chars) => {
304
316
  * @since v0.3.0
305
317
  */
306
318
  @disableGC
307
- export let rec explode = (string) => {
319
+ export let rec explode = string => {
308
320
  // `explodeHelp` and `string` do not need to be incRef'd as they are not
309
321
  // decRef'd in `explodeHelp`
310
- let ret = WasmI32.toGrain(explodeHelp(string, true)) : (Array<Char>)
322
+ let ret = WasmI32.toGrain(explodeHelp(string, true)): (Array<Char>)
311
323
 
312
324
  Memory.decRef(WasmI32.fromGrain(string))
313
325
  Memory.decRef(WasmI32.fromGrain(explode))
@@ -383,7 +395,7 @@ export let rec implode = (arr: Array<Char>) => {
383
395
  }
384
396
 
385
397
  // Helper to get the length in constant time without depending on Array
386
- primitive arrayLength : Array<a> -> Number = "@array.length"
398
+ primitive arrayLength: Array<a> -> Number = "@array.length"
387
399
 
388
400
  /**
389
401
  * Create a string that is the given string reversed.
@@ -395,7 +407,7 @@ primitive arrayLength : Array<a> -> Number = "@array.length"
395
407
  *
396
408
  * @since v0.4.5
397
409
  */
398
- export let reverse = (string) => {
410
+ export let reverse = string => {
399
411
  let mut arr = explode(string)
400
412
  let len = arrayLength(arr)
401
413
  let halfLen = len / 2
@@ -433,12 +445,12 @@ export let rec split = (separator: String, string: String) => {
433
445
  let psize = WasmI32.fromGrain(wasmSafeByteLength(separator)) >> 1n
434
446
 
435
447
  let ret = if (psize == 0n) {
436
- WasmI32.toGrain(explodeHelp(string, false)): Array<String>
448
+ WasmI32.toGrain(explodeHelp(string, false)): (Array<String>)
437
449
  } else if (psize > size) {
438
450
  let string = WasmI32.fromGrain(string)
439
451
  let ptr = allocateArray(1n)
440
452
  WasmI32.store(ptr, Memory.incRef(string), 8n)
441
- WasmI32.toGrain(ptr): Array<String>
453
+ WasmI32.toGrain(ptr): (Array<String>)
442
454
  } else {
443
455
  let string = WasmI32.fromGrain(string)
444
456
  let separator = WasmI32.fromGrain(separator)
@@ -499,7 +511,7 @@ export let rec split = (separator: String, string: String) => {
499
511
  Memory.copy(lastStr + 8n, last, strSize)
500
512
  WasmI32.store(arr + arrIdx, lastStr, 8n)
501
513
 
502
- WasmI32.toGrain(arr): Array<String>
514
+ WasmI32.toGrain(arr): (Array<String>)
503
515
  }
504
516
  Memory.decRef(WasmI32.fromGrain(separator))
505
517
  Memory.decRef(WasmI32.fromGrain(string))
@@ -681,7 +693,10 @@ export let rec contains = (search: String, string: String) => {
681
693
  if (WasmI32.load8U(search, 1n) != WasmI32.load8U(string + j, 1n)) {
682
694
  j += k
683
695
  } else {
684
- if (Memory.compare(search + 2n, string + j + 2n, m - 2n) == 0n && WasmI32.load8U(search, 0n) == WasmI32.load8U(string + j, 0n)) {
696
+ if (
697
+ Memory.compare(search + 2n, string + j + 2n, m - 2n) == 0n &&
698
+ WasmI32.load8U(search, 0n) == WasmI32.load8U(string + j, 0n)
699
+ ) {
685
700
  result = true
686
701
  break
687
702
  }
@@ -849,7 +864,7 @@ let utf16Length = (s: String) => {
849
864
 
850
865
  @disableGC
851
866
  let encodedLength = (s: String, encoding) => {
852
- match(encoding) {
867
+ match (encoding) {
853
868
  UTF32_BE => {
854
869
  Memory.incRef(WasmI32.fromGrain((*)))
855
870
  wasmSafeLength(s) * 4
@@ -917,7 +932,7 @@ let getCodePoint = (ptr: WasmI32) => {
917
932
  }
918
933
  lowerBoundary = 0x80n
919
934
  upperBoundary = 0xBFn
920
- codePoint = (codePoint << 6n) | (byte & 0x3Fn)
935
+ codePoint = codePoint << 6n | byte & 0x3Fn
921
936
  bytesSeen += 1n
922
937
  if (bytesSeen == bytesNeeded) {
923
938
  result = codePoint
@@ -929,19 +944,26 @@ let getCodePoint = (ptr: WasmI32) => {
929
944
 
930
945
  // hack to avoid incRef on this pointer
931
946
  @disableGC
932
- let mut _BYTES_SIZE_OFFSET = 1n;
947
+ let mut _BYTES_SIZE_OFFSET = 1n
933
948
  @disableGC
934
- let mut _BYTES_OFFSET = 1n;
949
+ let mut _BYTES_OFFSET = 1n
935
950
 
936
951
  @disableGC
937
952
  let initPtr = () => {
938
953
  _BYTES_SIZE_OFFSET = 4n
939
954
  _BYTES_OFFSET = 8n
940
955
  }
941
- initPtr();
956
+ initPtr()
942
957
 
943
958
  @disableGC
944
- let rec encodeAtHelp = (string: String, encoding: Encoding, includeBom: Bool, dest: Bytes, destPos: Number) => {
959
+ let rec encodeAtHelp =
960
+ (
961
+ string: String,
962
+ encoding: Encoding,
963
+ includeBom: Bool,
964
+ dest: Bytes,
965
+ destPos: Number,
966
+ ) => {
945
967
  let (>>>) = WasmI32.shrU
946
968
  let (-) = WasmI32.sub
947
969
  let (&) = WasmI32.and
@@ -964,9 +986,9 @@ let rec encodeAtHelp = (string: String, encoding: Encoding, includeBom: Bool, de
964
986
  let destSize = WasmI32.load(bytes, _BYTES_SIZE_OFFSET)
965
987
 
966
988
  if (includeBom) {
967
- match(encoding) {
989
+ match (encoding) {
968
990
  UTF8 => {
969
- if ((bytesIdx + 3n) > destSize) {
991
+ if (bytesIdx + 3n > destSize) {
970
992
  throw Exception.IndexOutOfBounds
971
993
  }
972
994
  WasmI32.store8(bytes + bytesIdx, 0xEFn, _BYTES_OFFSET)
@@ -975,7 +997,7 @@ let rec encodeAtHelp = (string: String, encoding: Encoding, includeBom: Bool, de
975
997
  bytesIdx += 3n
976
998
  },
977
999
  UTF16_BE => {
978
- if ((bytesIdx + 2n) > destSize) {
1000
+ if (bytesIdx + 2n > destSize) {
979
1001
  throw Exception.IndexOutOfBounds
980
1002
  }
981
1003
  WasmI32.store8(bytes + bytesIdx, 0xFEn, _BYTES_OFFSET)
@@ -983,7 +1005,7 @@ let rec encodeAtHelp = (string: String, encoding: Encoding, includeBom: Bool, de
983
1005
  bytesIdx += 2n
984
1006
  },
985
1007
  UTF16_LE => {
986
- if ((bytesIdx + 2n) > destSize) {
1008
+ if (bytesIdx + 2n > destSize) {
987
1009
  throw Exception.IndexOutOfBounds
988
1010
  }
989
1011
  WasmI32.store8(bytes + bytesIdx, 0xFFn, _BYTES_OFFSET)
@@ -991,7 +1013,7 @@ let rec encodeAtHelp = (string: String, encoding: Encoding, includeBom: Bool, de
991
1013
  bytesIdx += 2n
992
1014
  },
993
1015
  UTF32_BE => {
994
- if ((bytesIdx + 4n) > destSize) {
1016
+ if (bytesIdx + 4n > destSize) {
995
1017
  throw Exception.IndexOutOfBounds
996
1018
  }
997
1019
  WasmI32.store8(bytes + bytesIdx, 0n, _BYTES_OFFSET)
@@ -1001,7 +1023,7 @@ let rec encodeAtHelp = (string: String, encoding: Encoding, includeBom: Bool, de
1001
1023
  bytesIdx += 4n
1002
1024
  },
1003
1025
  UTF32_LE => {
1004
- if ((bytesIdx + 4n) > destSize) {
1026
+ if (bytesIdx + 4n > destSize) {
1005
1027
  throw Exception.IndexOutOfBounds
1006
1028
  }
1007
1029
  WasmI32.store8(bytes + bytesIdx, 0xFFn, _BYTES_OFFSET)
@@ -1009,16 +1031,16 @@ let rec encodeAtHelp = (string: String, encoding: Encoding, includeBom: Bool, de
1009
1031
  WasmI32.store8(bytes + bytesIdx + 2n, 0n, _BYTES_OFFSET)
1010
1032
  WasmI32.store8(bytes + bytesIdx + 3n, 0n, _BYTES_OFFSET)
1011
1033
  bytesIdx += 4n
1012
- }
1034
+ },
1013
1035
  }
1014
1036
  }
1015
1037
 
1016
- match(encoding) {
1038
+ match (encoding) {
1017
1039
  UTF8 => {
1018
1040
  // Optimization: since internally strings in Grain are UTF8 encoded, when
1019
1041
  // the target encoding is UTF8 as well, then copy the entire memory range
1020
1042
  // in bulk. No need to iterate individual characters.
1021
- if ((bytesIdx + byteSize) > destSize) {
1043
+ if (bytesIdx + byteSize > destSize) {
1022
1044
  throw Exception.IndexOutOfBounds
1023
1045
  }
1024
1046
  Memory.copy(bytes + bytesIdx + _BYTES_OFFSET, ptr, byteSize)
@@ -1036,37 +1058,55 @@ let rec encodeAtHelp = (string: String, encoding: Encoding, includeBom: Bool, de
1036
1058
  } else {
1037
1059
  2n
1038
1060
  }
1039
- match(encoding) {
1061
+ match (encoding) {
1040
1062
  UTF8 => {
1041
1063
  // With the optimization above for bulk memory copy, this match
1042
1064
  // should never occur for the UTF8 case.
1043
- if ((bytesIdx + n) > destSize) {
1065
+ if (bytesIdx + n > destSize) {
1044
1066
  throw Exception.IndexOutOfBounds
1045
1067
  }
1046
- Memory.copy(bytes + bytesIdx + _BYTES_OFFSET, ptr, n);
1068
+ Memory.copy(bytes + bytesIdx + _BYTES_OFFSET, ptr, n)
1047
1069
  bytesIdx += n
1048
1070
  },
1049
1071
  UTF16_BE => {
1050
1072
  let codePoint = getCodePoint(ptr)
1051
1073
  if (codePoint <= 0xFFFFn) {
1052
1074
  // <hi><lo>
1053
- if ((bytesIdx + 2n) > destSize) {
1075
+ if (bytesIdx + 2n > destSize) {
1054
1076
  throw Exception.IndexOutOfBounds
1055
1077
  }
1056
- WasmI32.store8(bytes + bytesIdx, (codePoint & 0xff00n) >>> 8n, _BYTES_OFFSET)
1057
- WasmI32.store8(bytes + bytesIdx + 1n, codePoint & 0xffn, _BYTES_OFFSET)
1078
+ WasmI32.store8(
1079
+ bytes + bytesIdx,
1080
+ (codePoint & 0xff00n) >>> 8n,
1081
+ _BYTES_OFFSET
1082
+ )
1083
+ WasmI32.store8(
1084
+ bytes + bytesIdx + 1n,
1085
+ codePoint & 0xffn,
1086
+ _BYTES_OFFSET
1087
+ )
1058
1088
  bytesIdx += 2n
1059
1089
  } else {
1060
1090
  // https://en.wikipedia.org/wiki/UTF-16#Code_points_from_U+010000_to_U+10FFFF
1061
- if ((bytesIdx + 4n) > destSize) {
1091
+ if (bytesIdx + 4n > destSize) {
1062
1092
  throw Exception.IndexOutOfBounds
1063
1093
  }
1064
1094
  let uPrime = codePoint - 0x10000n
1065
- let w1 = ((uPrime & 0b11111111110000000000n) >>> 10n) + 0xD800n // High surrogate
1066
- let w2 = (uPrime & 0b00000000001111111111n) + 0xDC00n // Low surrogate
1067
- WasmI32.store8(bytes + bytesIdx, (w1 & 0xff00n) >>> 8n, _BYTES_OFFSET)
1095
+ let w1 = ((uPrime & 0b11111111110000000000n) >>> 10n) +
1096
+ 0xD800n // High surrogate
1097
+ let w2 = (uPrime & 0b00000000001111111111n) +
1098
+ 0xDC00n // Low surrogate
1099
+ WasmI32.store8(
1100
+ bytes + bytesIdx,
1101
+ (w1 & 0xff00n) >>> 8n,
1102
+ _BYTES_OFFSET
1103
+ )
1068
1104
  WasmI32.store8(bytes + bytesIdx + 1n, w1 & 0xffn, _BYTES_OFFSET)
1069
- WasmI32.store8(bytes + bytesIdx + 2n, (w2 & 0xff00n) >>> 8n, _BYTES_OFFSET)
1105
+ WasmI32.store8(
1106
+ bytes + bytesIdx + 2n,
1107
+ (w2 & 0xff00n) >>> 8n,
1108
+ _BYTES_OFFSET
1109
+ )
1070
1110
  WasmI32.store8(bytes + bytesIdx + 3n, w2 & 0xffn, _BYTES_OFFSET)
1071
1111
  bytesIdx += 4n
1072
1112
  }
@@ -1074,54 +1114,96 @@ let rec encodeAtHelp = (string: String, encoding: Encoding, includeBom: Bool, de
1074
1114
  UTF16_LE => {
1075
1115
  let codePoint = getCodePoint(ptr)
1076
1116
  if (codePoint <= 0xFFFFn) {
1077
- if ((bytesIdx + 2n) > destSize) {
1117
+ if (bytesIdx + 2n > destSize) {
1078
1118
  throw Exception.IndexOutOfBounds
1079
1119
  }
1080
1120
  // <lo><hi>
1081
1121
  WasmI32.store8(bytes + bytesIdx, codePoint & 0xffn, _BYTES_OFFSET)
1082
- WasmI32.store8(bytes + bytesIdx + 1n, (codePoint & 0xff00n) >>> 8n, _BYTES_OFFSET)
1122
+ WasmI32.store8(
1123
+ bytes + bytesIdx + 1n,
1124
+ (codePoint & 0xff00n) >>> 8n,
1125
+ _BYTES_OFFSET
1126
+ )
1083
1127
  bytesIdx += 2n
1084
1128
  } else {
1085
1129
  // https://en.wikipedia.org/wiki/UTF-16#Code_points_from_U+010000_to_U+10FFFF
1086
- if ((bytesIdx + 4n) > destSize) {
1130
+ if (bytesIdx + 4n > destSize) {
1087
1131
  throw Exception.IndexOutOfBounds
1088
1132
  }
1089
1133
  let uPrime = codePoint - 0x10000n
1090
- let w1 = ((uPrime & 0b11111111110000000000n) >>> 10n) + 0xD800n // High surrogate
1091
- let w2 = (uPrime & 0b00000000001111111111n) + 0xDC00n // Low surrogate
1134
+ let w1 = ((uPrime & 0b11111111110000000000n) >>> 10n) +
1135
+ 0xD800n // High surrogate
1136
+ let w2 = (uPrime & 0b00000000001111111111n) +
1137
+ 0xDC00n // Low surrogate
1092
1138
  WasmI32.store8(bytes + bytesIdx, w1 & 0xffn, _BYTES_OFFSET)
1093
- WasmI32.store8(bytes + bytesIdx + 1n, (w1 & 0xff00n) >>> 8n, _BYTES_OFFSET)
1139
+ WasmI32.store8(
1140
+ bytes + bytesIdx + 1n,
1141
+ (w1 & 0xff00n) >>> 8n,
1142
+ _BYTES_OFFSET
1143
+ )
1094
1144
  WasmI32.store8(bytes + bytesIdx + 2n, w2 & 0xffn, _BYTES_OFFSET)
1095
- WasmI32.store8(bytes + bytesIdx + 3n, (w2 & 0xff00n) >>> 8n, _BYTES_OFFSET)
1145
+ WasmI32.store8(
1146
+ bytes + bytesIdx + 3n,
1147
+ (w2 & 0xff00n) >>> 8n,
1148
+ _BYTES_OFFSET
1149
+ )
1096
1150
  bytesIdx += 4n
1097
1151
  }
1098
1152
  },
1099
1153
  UTF32_BE => {
1100
- if ((bytesIdx + 4n) > destSize) {
1154
+ if (bytesIdx + 4n > destSize) {
1101
1155
  throw Exception.IndexOutOfBounds
1102
1156
  }
1103
1157
  let codePoint = getCodePoint(ptr)
1104
- WasmI32.store8(bytes + bytesIdx, (codePoint & 0xff000000n) >>> 24n, _BYTES_OFFSET)
1105
- WasmI32.store8(bytes + bytesIdx + 1n, (codePoint & 0xff0000n) >>> 16n, _BYTES_OFFSET)
1106
- WasmI32.store8(bytes + bytesIdx + 2n, (codePoint & 0xff00n) >>> 8n, _BYTES_OFFSET)
1107
- WasmI32.store8(bytes + bytesIdx + 3n, codePoint & 0xffn, _BYTES_OFFSET)
1158
+ WasmI32.store8(
1159
+ bytes + bytesIdx,
1160
+ (codePoint & 0xff000000n) >>> 24n,
1161
+ _BYTES_OFFSET
1162
+ )
1163
+ WasmI32.store8(
1164
+ bytes + bytesIdx + 1n,
1165
+ (codePoint & 0xff0000n) >>> 16n,
1166
+ _BYTES_OFFSET
1167
+ )
1168
+ WasmI32.store8(
1169
+ bytes + bytesIdx + 2n,
1170
+ (codePoint & 0xff00n) >>> 8n,
1171
+ _BYTES_OFFSET
1172
+ )
1173
+ WasmI32.store8(
1174
+ bytes + bytesIdx + 3n,
1175
+ codePoint & 0xffn,
1176
+ _BYTES_OFFSET
1177
+ )
1108
1178
  bytesIdx += 4n
1109
1179
  },
1110
1180
  UTF32_LE => {
1111
- if ((bytesIdx + 4n) > destSize) {
1181
+ if (bytesIdx + 4n > destSize) {
1112
1182
  throw Exception.IndexOutOfBounds
1113
1183
  }
1114
1184
  let codePoint = getCodePoint(ptr)
1115
1185
  WasmI32.store8(bytes + bytesIdx, codePoint & 0xffn, _BYTES_OFFSET)
1116
- WasmI32.store8(bytes + bytesIdx + 1n, (codePoint & 0xff00n) >>> 8n, _BYTES_OFFSET)
1117
- WasmI32.store8(bytes + bytesIdx + 2n, (codePoint & 0xff0000n) >>> 16n, _BYTES_OFFSET)
1118
- WasmI32.store8(bytes + bytesIdx + 3n, (codePoint & 0xff000000n) >>> 24n, _BYTES_OFFSET)
1186
+ WasmI32.store8(
1187
+ bytes + bytesIdx + 1n,
1188
+ (codePoint & 0xff00n) >>> 8n,
1189
+ _BYTES_OFFSET
1190
+ )
1191
+ WasmI32.store8(
1192
+ bytes + bytesIdx + 2n,
1193
+ (codePoint & 0xff0000n) >>> 16n,
1194
+ _BYTES_OFFSET
1195
+ )
1196
+ WasmI32.store8(
1197
+ bytes + bytesIdx + 3n,
1198
+ (codePoint & 0xff000000n) >>> 24n,
1199
+ _BYTES_OFFSET
1200
+ )
1119
1201
  bytesIdx += 4n
1120
1202
  },
1121
- };
1203
+ }
1122
1204
  ptr += n
1123
1205
  }
1124
- }
1206
+ },
1125
1207
  }
1126
1208
 
1127
1209
  Memory.decRef(WasmI32.fromGrain(string))
@@ -1167,15 +1249,18 @@ export let encodeAtWithBom = (string, encoding, dest, destPos) => {
1167
1249
  @disableGC
1168
1250
  let rec encodeHelp = (string: String, encoding: Encoding, includeBom: Bool) => {
1169
1251
  Memory.incRef(WasmI32.fromGrain((+)))
1170
- let size = encodedLength(string, encoding) + if (includeBom) {
1171
- match(encoding) {
1172
- UTF8 => 3,
1173
- UTF16_LE => 2,
1174
- UTF16_BE => 2,
1175
- UTF32_LE => 4,
1176
- UTF32_BE => 4,
1177
- }
1178
- } else 0
1252
+ let size = encodedLength(string, encoding) +
1253
+ (if (includeBom) {
1254
+ match (encoding) {
1255
+ UTF8 => 3,
1256
+ UTF16_LE => 2,
1257
+ UTF16_BE => 2,
1258
+ UTF32_LE => 4,
1259
+ UTF32_BE => 4,
1260
+ }
1261
+ } else {
1262
+ 0
1263
+ })
1179
1264
  let (>>>) = WasmI32.shrU
1180
1265
  let bytes = WasmI32.toGrain(allocateBytes(WasmI32.fromGrain(size) >>> 1n))
1181
1266
  Memory.incRef(WasmI32.fromGrain(encodeAtHelp))
@@ -1242,8 +1327,8 @@ let writeUtf8CodePoint = (ptr, codePoint) => {
1242
1327
  // The first byte has a three bit prefix of 110, followed by 5 bits of the
1243
1328
  // codepoint. The second byte has a two bit prefix of 10, followed by 6 bits
1244
1329
  // of the codepoint.
1245
- let high = ((codePoint >>> 6n) & 0b000_11111n) | 0b110_00000n
1246
- let low = ( codePoint & 0b00_111111n) | 0b10_000000n
1330
+ let high = codePoint >>> 6n & 0b000_11111n | 0b110_00000n
1331
+ let low = codePoint & 0b00_111111n | 0b10_000000n
1247
1332
  WasmI32.store8(ptr, high, 0n)
1248
1333
  WasmI32.store8(ptr + 1n, low, 0n)
1249
1334
  2n
@@ -1252,9 +1337,9 @@ let writeUtf8CodePoint = (ptr, codePoint) => {
1252
1337
  // The first byte has a four bit prefix of 1110, followed by 4 bits of the
1253
1338
  // codepoint. Remaining bytes each have a two bit prefix of 10, followed by
1254
1339
  // 6 bits of the codepoint.
1255
- let high = ((codePoint >>> 12n) & 0b0000_1111n) | 0b1110_0000n
1256
- let mid = ((codePoint >>> 6n) & 0b00_111111n) | 0b10_000000n
1257
- let low = ( codePoint & 0b00_111111n) | 0b10_000000n
1340
+ let high = codePoint >>> 12n & 0b0000_1111n | 0b1110_0000n
1341
+ let mid = codePoint >>> 6n & 0b00_111111n | 0b10_000000n
1342
+ let low = codePoint & 0b00_111111n | 0b10_000000n
1258
1343
  WasmI32.store8(ptr, high, 0n)
1259
1344
  WasmI32.store8(ptr + 1n, mid, 0n)
1260
1345
  WasmI32.store8(ptr + 2n, low, 0n)
@@ -1264,10 +1349,10 @@ let writeUtf8CodePoint = (ptr, codePoint) => {
1264
1349
  // The first byte has a five bit prefix of 11110, followed by 3 bits of the
1265
1350
  // codepoint. Remaining bytes each have a two bit prefix of 10, followed by
1266
1351
  // 6 bits of the codepoint.
1267
- let high = ((codePoint >>> 18n) & 0b00000_111n) | 0b11110_000n
1268
- let mid1 = ((codePoint >>> 12n) & 0b00_111111n) | 0b10_000000n
1269
- let mid2 = ((codePoint >>> 6n) & 0b00_111111n) | 0b10_000000n
1270
- let low = ( codePoint & 0b00_111111n) | 0b10_000000n
1352
+ let high = codePoint >>> 18n & 0b00000_111n | 0b11110_000n
1353
+ let mid1 = codePoint >>> 12n & 0b00_111111n | 0b10_000000n
1354
+ let mid2 = codePoint >>> 6n & 0b00_111111n | 0b10_000000n
1355
+ let low = codePoint & 0b00_111111n | 0b10_000000n
1271
1356
  WasmI32.store8(ptr, high, 0n)
1272
1357
  WasmI32.store8(ptr + 1n, mid1, 0n)
1273
1358
  WasmI32.store8(ptr + 2n, mid2, 0n)
@@ -1286,25 +1371,46 @@ let bytesHaveBom = (bytes: Bytes, encoding: Encoding, start: WasmI32) => {
1286
1371
  let ptr = ptr + start
1287
1372
  match (encoding) {
1288
1373
  UTF8 => {
1289
- bytesSize >= 3n && WasmI32.load8U(ptr, _BYTES_OFFSET) == 0xEFn && WasmI32.load8U(ptr + 1n, _BYTES_OFFSET) == 0xBBn && WasmI32.load8U(ptr + 2n, _BYTES_OFFSET) == 0xBFn
1374
+ bytesSize >= 3n &&
1375
+ WasmI32.load8U(ptr, _BYTES_OFFSET) == 0xEFn &&
1376
+ WasmI32.load8U(ptr + 1n, _BYTES_OFFSET) == 0xBBn &&
1377
+ WasmI32.load8U(ptr + 2n, _BYTES_OFFSET) == 0xBFn
1290
1378
  },
1291
1379
  UTF16_BE => {
1292
- bytesSize >= 2n && WasmI32.load8U(ptr, _BYTES_OFFSET) == 0xFEn && WasmI32.load8U(ptr + 1n, _BYTES_OFFSET) == 0xFFn
1380
+ bytesSize >= 2n &&
1381
+ WasmI32.load8U(ptr, _BYTES_OFFSET) == 0xFEn &&
1382
+ WasmI32.load8U(ptr + 1n, _BYTES_OFFSET) == 0xFFn
1293
1383
  },
1294
1384
  UTF16_LE => {
1295
- bytesSize >= 2n && WasmI32.load8U(ptr, _BYTES_OFFSET) == 0xFFn && WasmI32.load8U(ptr + 1n, _BYTES_OFFSET) == 0xFEn
1385
+ bytesSize >= 2n &&
1386
+ WasmI32.load8U(ptr, _BYTES_OFFSET) == 0xFFn &&
1387
+ WasmI32.load8U(ptr + 1n, _BYTES_OFFSET) == 0xFEn
1296
1388
  },
1297
1389
  UTF32_BE => {
1298
- bytesSize >= 4n && WasmI32.load8U(ptr, _BYTES_OFFSET) == 0x00n && WasmI32.load8U(ptr + 1n, _BYTES_OFFSET) == 0x00n && WasmI32.load8U(ptr + 2n, _BYTES_OFFSET) == 0xFEn && WasmI32.load8U(ptr + 3n, _BYTES_OFFSET) == 0xFFn
1390
+ bytesSize >= 4n &&
1391
+ WasmI32.load8U(ptr, _BYTES_OFFSET) == 0x00n &&
1392
+ WasmI32.load8U(ptr + 1n, _BYTES_OFFSET) == 0x00n &&
1393
+ WasmI32.load8U(ptr + 2n, _BYTES_OFFSET) == 0xFEn &&
1394
+ WasmI32.load8U(ptr + 3n, _BYTES_OFFSET) == 0xFFn
1299
1395
  },
1300
1396
  UTF32_LE => {
1301
- bytesSize >= 4n && WasmI32.load8U(ptr, _BYTES_OFFSET) == 0xFFn && WasmI32.load8U(ptr + 1n, _BYTES_OFFSET) == 0xFEn && WasmI32.load8U(ptr + 2n, _BYTES_OFFSET) == 0x00n && WasmI32.load8U(ptr + 3n, _BYTES_OFFSET) == 0x00n
1302
- }
1397
+ bytesSize >= 4n &&
1398
+ WasmI32.load8U(ptr, _BYTES_OFFSET) == 0xFFn &&
1399
+ WasmI32.load8U(ptr + 1n, _BYTES_OFFSET) == 0xFEn &&
1400
+ WasmI32.load8U(ptr + 2n, _BYTES_OFFSET) == 0x00n &&
1401
+ WasmI32.load8U(ptr + 3n, _BYTES_OFFSET) == 0x00n
1402
+ },
1303
1403
  }
1304
1404
  }
1305
1405
 
1306
1406
  @disableGC
1307
- let decodedLength = (bytes: Bytes, encoding: Encoding, start: WasmI32, size: WasmI32) => {
1407
+ let decodedLength =
1408
+ (
1409
+ bytes: Bytes,
1410
+ encoding: Encoding,
1411
+ start: WasmI32,
1412
+ size: WasmI32,
1413
+ ) => {
1308
1414
  let (+) = WasmI32.add
1309
1415
  let (-) = WasmI32.sub
1310
1416
  let (==) = WasmI32.eq
@@ -1327,20 +1433,21 @@ let decodedLength = (bytes: Bytes, encoding: Encoding, start: WasmI32, size: Was
1327
1433
  }
1328
1434
  }
1329
1435
  let start = ptr + _BYTES_OFFSET + start
1330
- match(encoding) {
1436
+ match (encoding) {
1331
1437
  UTF8 => bytesSize,
1332
1438
  UTF16_BE => {
1333
1439
  let end = start + bytesSize
1334
1440
  let mut ptr = start
1335
1441
  let mut count = 0n
1336
1442
  while (ptr < end) {
1337
- let codeWord = (WasmI32.load8U(ptr, 0n) << 8n) | (WasmI32.load8U(ptr, 1n))
1443
+ let codeWord = WasmI32.load8U(ptr, 0n) << 8n | WasmI32.load8U(ptr, 1n)
1338
1444
  let codeWord = if (codeWord >= 0xD800n && codeWord <= 0xDBFFn) {
1339
1445
  // high surrogate. need to check that next character is low srurrogate
1340
1446
  let ret = if (ptr + 2n >= end) {
1341
1447
  throw MalformedUnicode
1342
1448
  } else {
1343
- let nextCodeWord = (WasmI32.load8U(ptr, 2n) << 8n) | (WasmI32.load8U(ptr, 3n))
1449
+ let nextCodeWord = WasmI32.load8U(ptr, 2n) << 8n |
1450
+ WasmI32.load8U(ptr, 3n)
1344
1451
  if (nextCodeWord < 0xDC00n || nextCodeWord > 0xDFFFn) {
1345
1452
  // high surrogate without low surrogate
1346
1453
  throw MalformedUnicode
@@ -1374,13 +1481,14 @@ let decodedLength = (bytes: Bytes, encoding: Encoding, start: WasmI32, size: Was
1374
1481
  let mut ptr = start
1375
1482
  let mut count = 0n
1376
1483
  while (ptr < end) {
1377
- let codeWord = (WasmI32.load8U(ptr, 1n) << 8n) | (WasmI32.load8U(ptr, 0n))
1484
+ let codeWord = WasmI32.load8U(ptr, 1n) << 8n | WasmI32.load8U(ptr, 0n)
1378
1485
  let codeWord = if (codeWord >= 0xD800n && codeWord <= 0xDBFFn) {
1379
1486
  // high surrogate. need to check that next character is low srurrogate
1380
1487
  let ret = if (ptr + 2n >= end) {
1381
1488
  throw MalformedUnicode
1382
1489
  } else {
1383
- let nextCodeWord = (WasmI32.load8U(ptr, 3n) << 8n) | (WasmI32.load8U(ptr, 2n))
1490
+ let nextCodeWord = WasmI32.load8U(ptr, 3n) << 8n |
1491
+ WasmI32.load8U(ptr, 2n)
1384
1492
  if (nextCodeWord < 0xDC00n || nextCodeWord > 0xDFFFn) {
1385
1493
  // high surrogate without low surrogate
1386
1494
  throw MalformedUnicode
@@ -1418,7 +1526,10 @@ let decodedLength = (bytes: Bytes, encoding: Encoding, start: WasmI32, size: Was
1418
1526
  let mut ptr = start
1419
1527
  let mut count = 0n
1420
1528
  while (ptr < end) {
1421
- let codeWord = (WasmI32.load8U(ptr, 0n) << 24n) | (WasmI32.load8U(ptr, 1n) << 16n) | (WasmI32.load8U(ptr, 2n) << 8n) | (WasmI32.load8U(ptr, 3n))
1529
+ let codeWord = WasmI32.load8U(ptr, 0n) << 24n |
1530
+ WasmI32.load8U(ptr, 1n) << 16n |
1531
+ WasmI32.load8U(ptr, 2n) << 8n |
1532
+ WasmI32.load8U(ptr, 3n)
1422
1533
  ptr += 4n
1423
1534
  if (codeWord <= 0x007Fn) {
1424
1535
  count += 1n
@@ -1441,7 +1552,10 @@ let decodedLength = (bytes: Bytes, encoding: Encoding, start: WasmI32, size: Was
1441
1552
  let mut ptr = start
1442
1553
  let mut count = 0n
1443
1554
  while (ptr < end) {
1444
- let codeWord = (WasmI32.load8U(ptr, 3n) << 24n) | (WasmI32.load8U(ptr, 2n) << 16n) | (WasmI32.load8U(ptr, 1n) << 8n) | (WasmI32.load8U(ptr, 0n))
1555
+ let codeWord = WasmI32.load8U(ptr, 3n) << 24n |
1556
+ WasmI32.load8U(ptr, 2n) << 16n |
1557
+ WasmI32.load8U(ptr, 1n) << 8n |
1558
+ WasmI32.load8U(ptr, 0n)
1445
1559
  ptr += 4n
1446
1560
  if (codeWord <= 0x007Fn) {
1447
1561
  count += 1n
@@ -1454,12 +1568,19 @@ let decodedLength = (bytes: Bytes, encoding: Encoding, start: WasmI32, size: Was
1454
1568
  }
1455
1569
  }
1456
1570
  count
1457
- }
1571
+ },
1458
1572
  }
1459
1573
  }
1460
1574
 
1461
1575
  @disableGC
1462
- let rec decodeRangeHelp = (bytes: Bytes, encoding: Encoding, skipBom: Bool, start: Number, size: Number) => {
1576
+ let rec decodeRangeHelp =
1577
+ (
1578
+ bytes: Bytes,
1579
+ encoding: Encoding,
1580
+ skipBom: Bool,
1581
+ start: Number,
1582
+ size: Number,
1583
+ ) => {
1463
1584
  let (+) = WasmI32.add
1464
1585
  let (-) = WasmI32.sub
1465
1586
  let (<) = WasmI32.ltU
@@ -1474,7 +1595,7 @@ let rec decodeRangeHelp = (bytes: Bytes, encoding: Encoding, skipBom: Bool, star
1474
1595
  let size = grainToWasmNumber(size, _SIZE_NAME)
1475
1596
  let hasBom = bytesHaveBom(bytes, encoding, start)
1476
1597
  let stringSize = decodedLength(bytes, encoding, start, size)
1477
- let stringSize = if (skipBom && hasBom) (stringSize - 3n) else stringSize
1598
+ let stringSize = if (skipBom && hasBom) stringSize - 3n else stringSize
1478
1599
  let str = allocateString(stringSize)
1479
1600
  let mut bytesPtr = WasmI32.fromGrain(bytes)
1480
1601
  let bytesSize = {
@@ -1489,7 +1610,7 @@ let rec decodeRangeHelp = (bytes: Bytes, encoding: Encoding, skipBom: Bool, star
1489
1610
  let mut strPtr = str + 8n
1490
1611
  let mut bomRead = false
1491
1612
  if (skipBom && hasBom) {
1492
- bytesPtr += match(encoding) {
1613
+ bytesPtr += match (encoding) {
1493
1614
  UTF8 => 3n,
1494
1615
  UTF16_LE => 2n,
1495
1616
  UTF16_BE => 2n,
@@ -1498,9 +1619,9 @@ let rec decodeRangeHelp = (bytes: Bytes, encoding: Encoding, skipBom: Bool, star
1498
1619
  }
1499
1620
  }
1500
1621
  let ret = if (stringSize == 0n) {
1501
- WasmI32.toGrain(str) : String
1622
+ WasmI32.toGrain(str): String
1502
1623
  } else {
1503
- match(encoding) {
1624
+ match (encoding) {
1504
1625
  UTF8 => {
1505
1626
  Memory.copy(strPtr, bytesPtr, stringSize)
1506
1627
  },
@@ -1508,11 +1629,14 @@ let rec decodeRangeHelp = (bytes: Bytes, encoding: Encoding, skipBom: Bool, star
1508
1629
  // NOTE: Because the size check passed, we know the string is well-formed
1509
1630
  let end = bytesPtr + bytesSize
1510
1631
  while (bytesPtr < end) {
1511
- let w1 = (WasmI32.load8U(bytesPtr, 0n) << 8n) | (WasmI32.load8U(bytesPtr, 1n))
1632
+ let w1 = WasmI32.load8U(bytesPtr, 0n) << 8n |
1633
+ WasmI32.load8U(bytesPtr, 1n)
1512
1634
  let codeWord = if (w1 >= 0xD800n && w1 <= 0xDBFFn) {
1513
1635
  // high surrogate. next character is low srurrogate
1514
1636
  let w1 = (w1 & 0x03FFn) << 10n
1515
- let w2 = ((WasmI32.load8U(bytesPtr, 2n) << 8n) | (WasmI32.load8U(bytesPtr, 3n))) & 0x03FFn
1637
+ let w2 = (WasmI32.load8U(bytesPtr, 2n) << 8n |
1638
+ WasmI32.load8U(bytesPtr, 3n)) &
1639
+ 0x03FFn
1516
1640
  let codeWord = w1 + w2 + 0x10000n
1517
1641
  // no problems, so go past both code words
1518
1642
  bytesPtr += 4n
@@ -1528,11 +1652,14 @@ let rec decodeRangeHelp = (bytes: Bytes, encoding: Encoding, skipBom: Bool, star
1528
1652
  // NOTE: Because the size check passed, we know the string is well-formed
1529
1653
  let end = bytesPtr + bytesSize
1530
1654
  while (bytesPtr < end) {
1531
- let w1 = (WasmI32.load8U(bytesPtr, 1n) << 8n) | (WasmI32.load8U(bytesPtr, 0n))
1655
+ let w1 = WasmI32.load8U(bytesPtr, 1n) << 8n |
1656
+ WasmI32.load8U(bytesPtr, 0n)
1532
1657
  let codeWord = if (w1 >= 0xD800n && w1 <= 0xDBFFn) {
1533
1658
  // high surrogate. next character is low srurrogate
1534
1659
  let w1 = (w1 & 0x03FFn) << 10n
1535
- let w2 = ((WasmI32.load8U(bytesPtr, 3n) << 8n) | (WasmI32.load8U(bytesPtr, 2n))) & 0x03FFn
1660
+ let w2 = (WasmI32.load8U(bytesPtr, 3n) << 8n |
1661
+ WasmI32.load8U(bytesPtr, 2n)) &
1662
+ 0x03FFn
1536
1663
  //let uPrime = codePoint - 0x10000n
1537
1664
  //let w1 = ((uPrime & 0b11111111110000000000n) >>> 10n) + 0xD800n // High surrogate
1538
1665
  //let w2 = (uPrime & 0b00000000001111111111n) + 0xDC00n // Low surrogate
@@ -1550,7 +1677,10 @@ let rec decodeRangeHelp = (bytes: Bytes, encoding: Encoding, skipBom: Bool, star
1550
1677
  UTF32_BE => {
1551
1678
  let end = bytesPtr + bytesSize
1552
1679
  while (bytesPtr < end) {
1553
- let codeWord = (WasmI32.load8U(bytesPtr, 0n) << 24n) | (WasmI32.load8U(bytesPtr, 1n) << 16n) | (WasmI32.load8U(bytesPtr, 2n) << 8n) | (WasmI32.load8U(bytesPtr, 3n))
1680
+ let codeWord = WasmI32.load8U(bytesPtr, 0n) << 24n |
1681
+ WasmI32.load8U(bytesPtr, 1n) << 16n |
1682
+ WasmI32.load8U(bytesPtr, 2n) << 8n |
1683
+ WasmI32.load8U(bytesPtr, 3n)
1554
1684
  bytesPtr += 4n
1555
1685
  strPtr += writeUtf8CodePoint(strPtr, codeWord)
1556
1686
  }
@@ -1558,13 +1688,16 @@ let rec decodeRangeHelp = (bytes: Bytes, encoding: Encoding, skipBom: Bool, star
1558
1688
  UTF32_LE => {
1559
1689
  let end = bytesPtr + bytesSize
1560
1690
  while (bytesPtr < end) {
1561
- let codeWord = (WasmI32.load8U(bytesPtr, 3n) << 24n) | (WasmI32.load8U(bytesPtr, 2n) << 16n) | (WasmI32.load8U(bytesPtr, 1n) << 8n) | (WasmI32.load8U(bytesPtr, 0n))
1691
+ let codeWord = WasmI32.load8U(bytesPtr, 3n) << 24n |
1692
+ WasmI32.load8U(bytesPtr, 2n) << 16n |
1693
+ WasmI32.load8U(bytesPtr, 1n) << 8n |
1694
+ WasmI32.load8U(bytesPtr, 0n)
1562
1695
  bytesPtr += 4n
1563
1696
  strPtr += writeUtf8CodePoint(strPtr, codeWord)
1564
1697
  }
1565
- }
1698
+ },
1566
1699
  }
1567
- WasmI32.toGrain(str) : String
1700
+ WasmI32.toGrain(str): String
1568
1701
  }
1569
1702
  // bytes: Bytes, encoding: Encoding, skipBom: Bool, start: Number, size: Number
1570
1703
  Memory.decRef(WasmI32.fromGrain(bytes))
@@ -1585,7 +1718,13 @@ let rec decodeRangeHelp = (bytes: Bytes, encoding: Encoding, skipBom: Bool, star
1585
1718
  *
1586
1719
  * @since v0.4.0
1587
1720
  */
1588
- export let decodeRange = (bytes: Bytes, encoding: Encoding, start: Number, size: Number) => {
1721
+ export let decodeRange =
1722
+ (
1723
+ bytes: Bytes,
1724
+ encoding: Encoding,
1725
+ start: Number,
1726
+ size: Number,
1727
+ ) => {
1589
1728
  decodeRangeHelp(bytes, encoding, true, start, size)
1590
1729
  }
1591
1730
 
@@ -1600,7 +1739,13 @@ export let decodeRange = (bytes: Bytes, encoding: Encoding, start: Number, size:
1600
1739
  *
1601
1740
  * @since v0.4.0
1602
1741
  */
1603
- export let decodeRangeKeepBom = (bytes: Bytes, encoding: Encoding, start: Number, size: Number) => {
1742
+ export let decodeRangeKeepBom =
1743
+ (
1744
+ bytes: Bytes,
1745
+ encoding: Encoding,
1746
+ start: Number,
1747
+ size: Number,
1748
+ ) => {
1604
1749
  decodeRangeHelp(bytes, encoding, false, start, size)
1605
1750
  }
1606
1751
 
@@ -1611,7 +1756,13 @@ let rec decodeHelp = (bytes: Bytes, encoding: Encoding, skipBom: Bool) => {
1611
1756
  Memory.incRef(WasmI32.fromGrain(decodeRangeHelp))
1612
1757
  Memory.incRef(WasmI32.fromGrain(bytes))
1613
1758
  Memory.incRef(WasmI32.fromGrain(encoding))
1614
- let ret = decodeRangeHelp(bytes, encoding, skipBom, 0, tagSimpleNumber(bytesSize))
1759
+ let ret = decodeRangeHelp(
1760
+ bytes,
1761
+ encoding,
1762
+ skipBom,
1763
+ 0,
1764
+ tagSimpleNumber(bytesSize)
1765
+ )
1615
1766
  Memory.incRef(WasmI32.fromGrain(bytes))
1616
1767
  Memory.incRef(WasmI32.fromGrain(encoding))
1617
1768
  Memory.incRef(WasmI32.fromGrain(decodeHelp))
@@ -1655,7 +1806,7 @@ export let decodeKeepBom = (bytes: Bytes, encoding: Encoding) => {
1655
1806
  * @since v0.4.0
1656
1807
  */
1657
1808
  @disableGC
1658
- export let rec forEachCodePoint = (fn: (Number) -> Void, str: String) => {
1809
+ export let rec forEachCodePoint = (fn: Number -> Void, str: String) => {
1659
1810
  let (>>>) = WasmI32.shrU
1660
1811
  let (-) = WasmI32.sub
1661
1812
  let (&) = WasmI32.and
@@ -1717,7 +1868,11 @@ export let rec forEachCodePoint = (fn: (Number) -> Void, str: String) => {
1717
1868
  * @since v0.4.0
1718
1869
  */
1719
1870
  @disableGC
1720
- export let rec forEachCodePointi = (fn: (Number, Number) -> Void, str: String) => {
1871
+ export let rec forEachCodePointi =
1872
+ (
1873
+ fn: (Number, Number) -> Void,
1874
+ str: String,
1875
+ ) => {
1721
1876
  let (>>>) = WasmI32.shrU
1722
1877
  let (-) = WasmI32.sub
1723
1878
  let (&) = WasmI32.and
@@ -1769,27 +1924,26 @@ let trimString = (str: String, end: Bool) => {
1769
1924
  let chars = explode(str), charsLength = length(str)
1770
1925
  let mut i = 0, offset = 1
1771
1926
  if (end) {
1772
- i = charsLength-1
1927
+ i = charsLength - 1
1773
1928
  offset = -1
1774
1929
  }
1775
1930
  for (; i < charsLength && i > -1; i += offset) {
1776
- let currentChar = chars[i];
1931
+ let currentChar = chars[i]
1777
1932
  // TODO: Use unicode whitespace property and unicode line terminator once github issue #661 is completed
1778
1933
  if (
1779
1934
  // Spacing
1780
- currentChar != '\u{0009}' && // Tab
1781
- currentChar != '\u{000B}' && // LINE TABULATION
1782
- currentChar != '\u{000C}' && // FORM FEED (FF)
1783
- currentChar != '\u{0020}' && // Space
1784
- currentChar != '\u{00A0}' && // No Break Space
1785
- currentChar != '\u{FEFF}' && // ZERO WIDTH NO-BREAK SPACE
1935
+ currentChar != '\u{0009}' && // Tab
1936
+ currentChar != '\u{000B}' && // LINE TABULATION
1937
+ currentChar != '\u{000C}' && // FORM FEED (FF)
1938
+ currentChar != '\u{0020}' && // Space
1939
+ currentChar != '\u{00A0}' && // No Break Space
1940
+ currentChar != '\u{FEFF}' && // ZERO WIDTH NO-BREAK SPACE
1786
1941
  // Line Terminators
1787
1942
  currentChar != '\n' && // LF
1788
1943
  currentChar != '\r' // CR
1789
1944
  ) break
1790
1945
  }
1791
- if (end) slice(0, i+1, str)
1792
- else slice(i, charsLength, str)
1946
+ if (end) slice(0, i + 1, str) else slice(i, charsLength, str)
1793
1947
  }
1794
1948
  /**
1795
1949
  * Trims the beginning of a string—removing any leading whitespace characters.