@grain/stdlib 0.5.13 → 0.6.1

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 (155) hide show
  1. package/CHANGELOG.md +201 -0
  2. package/LICENSE +1 -1
  3. package/README.md +25 -2
  4. package/array.gr +1512 -199
  5. package/array.md +2032 -94
  6. package/bigint.gr +239 -140
  7. package/bigint.md +450 -106
  8. package/buffer.gr +595 -102
  9. package/buffer.md +903 -145
  10. package/bytes.gr +401 -110
  11. package/bytes.md +551 -63
  12. package/char.gr +228 -49
  13. package/char.md +373 -7
  14. package/exception.gr +26 -12
  15. package/exception.md +29 -5
  16. package/float32.gr +130 -109
  17. package/float32.md +185 -57
  18. package/float64.gr +112 -99
  19. package/float64.md +185 -57
  20. package/hash.gr +62 -40
  21. package/hash.md +27 -3
  22. package/int16.gr +430 -0
  23. package/int16.md +618 -0
  24. package/int32.gr +200 -269
  25. package/int32.md +254 -289
  26. package/int64.gr +142 -225
  27. package/int64.md +254 -289
  28. package/int8.gr +511 -0
  29. package/int8.md +786 -0
  30. package/json.gr +2071 -0
  31. package/json.md +646 -0
  32. package/list.gr +120 -68
  33. package/list.md +125 -80
  34. package/map.gr +560 -57
  35. package/map.md +672 -56
  36. package/marshal.gr +239 -227
  37. package/marshal.md +36 -4
  38. package/number.gr +626 -676
  39. package/number.md +738 -153
  40. package/option.gr +33 -35
  41. package/option.md +58 -42
  42. package/package.json +2 -2
  43. package/path.gr +148 -187
  44. package/path.md +47 -96
  45. package/pervasives.gr +75 -416
  46. package/pervasives.md +85 -180
  47. package/priorityqueue.gr +433 -74
  48. package/priorityqueue.md +422 -54
  49. package/queue.gr +362 -80
  50. package/queue.md +433 -38
  51. package/random.gr +67 -75
  52. package/random.md +68 -40
  53. package/range.gr +135 -63
  54. package/range.md +198 -43
  55. package/rational.gr +284 -0
  56. package/rational.md +545 -0
  57. package/regex.gr +933 -1066
  58. package/regex.md +59 -60
  59. package/result.gr +23 -25
  60. package/result.md +54 -39
  61. package/runtime/atof/common.gr +78 -82
  62. package/runtime/atof/common.md +22 -10
  63. package/runtime/atof/decimal.gr +102 -127
  64. package/runtime/atof/decimal.md +28 -7
  65. package/runtime/atof/lemire.gr +56 -71
  66. package/runtime/atof/lemire.md +9 -1
  67. package/runtime/atof/parse.gr +83 -110
  68. package/runtime/atof/parse.md +12 -2
  69. package/runtime/atof/slow.gr +28 -35
  70. package/runtime/atof/slow.md +9 -1
  71. package/runtime/atof/table.gr +19 -18
  72. package/runtime/atof/table.md +10 -2
  73. package/runtime/atoi/parse.gr +153 -136
  74. package/runtime/atoi/parse.md +50 -1
  75. package/runtime/bigint.gr +410 -517
  76. package/runtime/bigint.md +71 -57
  77. package/runtime/compare.gr +176 -85
  78. package/runtime/compare.md +31 -1
  79. package/runtime/dataStructures.gr +144 -32
  80. package/runtime/dataStructures.md +267 -31
  81. package/runtime/debugPrint.gr +34 -15
  82. package/runtime/debugPrint.md +37 -5
  83. package/runtime/equal.gr +53 -52
  84. package/runtime/equal.md +30 -1
  85. package/runtime/exception.gr +38 -47
  86. package/runtime/exception.md +10 -8
  87. package/runtime/gc.gr +23 -152
  88. package/runtime/gc.md +13 -17
  89. package/runtime/malloc.gr +31 -31
  90. package/runtime/malloc.md +11 -3
  91. package/runtime/numberUtils.gr +193 -174
  92. package/runtime/numberUtils.md +29 -9
  93. package/runtime/numbers.gr +1695 -1021
  94. package/runtime/numbers.md +1098 -134
  95. package/runtime/string.gr +543 -245
  96. package/runtime/string.md +76 -6
  97. package/runtime/unsafe/constants.gr +30 -13
  98. package/runtime/unsafe/constants.md +80 -0
  99. package/runtime/unsafe/conv.gr +55 -28
  100. package/runtime/unsafe/conv.md +41 -9
  101. package/runtime/unsafe/memory.gr +10 -30
  102. package/runtime/unsafe/memory.md +15 -19
  103. package/runtime/unsafe/tags.gr +37 -21
  104. package/runtime/unsafe/tags.md +88 -8
  105. package/runtime/unsafe/wasmf32.gr +30 -36
  106. package/runtime/unsafe/wasmf32.md +64 -56
  107. package/runtime/unsafe/wasmf64.gr +30 -36
  108. package/runtime/unsafe/wasmf64.md +64 -56
  109. package/runtime/unsafe/wasmi32.gr +49 -66
  110. package/runtime/unsafe/wasmi32.md +102 -94
  111. package/runtime/unsafe/wasmi64.gr +52 -79
  112. package/runtime/unsafe/wasmi64.md +108 -100
  113. package/runtime/utils/printing.gr +13 -15
  114. package/runtime/utils/printing.md +11 -3
  115. package/runtime/wasi.gr +294 -295
  116. package/runtime/wasi.md +62 -42
  117. package/set.gr +574 -64
  118. package/set.md +634 -54
  119. package/stack.gr +181 -64
  120. package/stack.md +271 -42
  121. package/string.gr +453 -533
  122. package/string.md +241 -151
  123. package/uint16.gr +369 -0
  124. package/uint16.md +585 -0
  125. package/uint32.gr +470 -0
  126. package/uint32.md +737 -0
  127. package/uint64.gr +471 -0
  128. package/uint64.md +737 -0
  129. package/uint8.gr +369 -0
  130. package/uint8.md +585 -0
  131. package/uri.gr +1093 -0
  132. package/uri.md +477 -0
  133. package/{sys → wasi}/file.gr +914 -500
  134. package/{sys → wasi}/file.md +454 -50
  135. package/wasi/process.gr +292 -0
  136. package/{sys → wasi}/process.md +164 -6
  137. package/wasi/random.gr +77 -0
  138. package/wasi/random.md +80 -0
  139. package/{sys → wasi}/time.gr +15 -22
  140. package/{sys → wasi}/time.md +5 -5
  141. package/immutablearray.gr +0 -929
  142. package/immutablearray.md +0 -1038
  143. package/immutablemap.gr +0 -493
  144. package/immutablemap.md +0 -479
  145. package/immutablepriorityqueue.gr +0 -360
  146. package/immutablepriorityqueue.md +0 -291
  147. package/immutableset.gr +0 -498
  148. package/immutableset.md +0 -449
  149. package/runtime/debug.gr +0 -2
  150. package/runtime/debug.md +0 -6
  151. package/runtime/unsafe/errors.gr +0 -36
  152. package/runtime/unsafe/errors.md +0 -204
  153. package/sys/process.gr +0 -254
  154. package/sys/random.gr +0 -79
  155. package/sys/random.md +0 -66
package/uri.md ADDED
@@ -0,0 +1,477 @@
1
+ ---
2
+ title: Uri
3
+ ---
4
+
5
+ Utilities for working with URIs.
6
+
7
+ <details disabled>
8
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
9
+ No other changes yet.
10
+ </details>
11
+
12
+ ```grain
13
+ from "uri" include Uri
14
+ ```
15
+
16
+ ## Types
17
+
18
+ Type declarations included in the Uri module.
19
+
20
+ ### Uri.**Uri**
21
+
22
+ ```grain
23
+ record Uri {
24
+ scheme: Option<String>,
25
+ userinfo: Option<String>,
26
+ host: Option<String>,
27
+ port: Option<Number>,
28
+ path: String,
29
+ query: Option<String>,
30
+ fragment: Option<String>,
31
+ }
32
+ ```
33
+
34
+ Represents a parsed RFC 3986 URI.
35
+
36
+ ### Uri.**ParseError**
37
+
38
+ ```grain
39
+ enum ParseError {
40
+ ParseError,
41
+ }
42
+ ```
43
+
44
+ Represents an error encountered while parsing a URI.
45
+
46
+ ### Uri.**ConstructUriError**
47
+
48
+ ```grain
49
+ enum ConstructUriError {
50
+ UserinfoWithNoHost,
51
+ PortWithNoHost,
52
+ InvalidSchemeError,
53
+ InvalidUserinfoError,
54
+ InvalidHostError,
55
+ InvalidPortError,
56
+ InvalidPathError,
57
+ InvalidQueryError,
58
+ InvalidFragmentError,
59
+ }
60
+ ```
61
+
62
+ Represents an error encountered while constructing a URI with `make` or `update`.
63
+
64
+ ### Uri.**ResolveReferenceError**
65
+
66
+ ```grain
67
+ enum ResolveReferenceError {
68
+ BaseNotAbsolute,
69
+ }
70
+ ```
71
+
72
+ Represents an error encountered while attempting to resolve a URI reference to a target URI.
73
+
74
+ ### Uri.**DecodingError**
75
+
76
+ ```grain
77
+ enum DecodingError {
78
+ InvalidEncoding,
79
+ }
80
+ ```
81
+
82
+ Represents an error encountered while attempting to percent-decode a string.
83
+
84
+ ### Uri.**EncodeSet**
85
+
86
+ ```grain
87
+ enum EncodeSet {
88
+ EncodeNonUnreserved,
89
+ EncodeUserinfo,
90
+ EncodeRegisteredHost,
91
+ EncodePath,
92
+ EncodePathSegment,
93
+ EncodeQueryOrFragment,
94
+ EncodeCustom((Char => Bool)),
95
+ }
96
+ ```
97
+
98
+ Used to specify which characters to percent-encode from a string.
99
+
100
+ ## Values
101
+
102
+ Functions and constants included in the Uri module.
103
+
104
+ ### Uri.**encode**
105
+
106
+ <details disabled>
107
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
108
+ No other changes yet.
109
+ </details>
110
+
111
+ ```grain
112
+ encode : (str: String, ?encodeSet: EncodeSet) => String
113
+ ```
114
+
115
+ Percent-encodes characters in a string based on the specified `EncodeSet`.
116
+
117
+ Parameters:
118
+
119
+ |param|type|description|
120
+ |-----|----|-----------|
121
+ |`str`|`String`|The string to encode|
122
+ |`?encodeSet`|`EncodeSet`|An indication for which characters to percent-encode. `EncodeNonUnreserved` by default|
123
+
124
+ Returns:
125
+
126
+ |type|description|
127
+ |----|-----------|
128
+ |`String`|A percent-encoding of the given string|
129
+
130
+ Examples:
131
+
132
+ ```grain
133
+ Uri.encode("h3ll0_.w ?o+rld", encodeSet=Uri.EncodeNonUnreserved) // "h3ll0_.w%20%3Fo%2Brld"
134
+ ```
135
+
136
+ ```grain
137
+ Uri.encode("d+om@i:n.com", encodeSet=Uri.EncodeRegisteredHost) // "d+om%40i%3An.com"
138
+ ```
139
+
140
+ ```grain
141
+ Uri.encode("word", encodeSet=Uri.EncodeCustom(c => c == 'o')) // "w%6Frd"
142
+ ```
143
+
144
+ ### Uri.**decode**
145
+
146
+ <details disabled>
147
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
148
+ No other changes yet.
149
+ </details>
150
+
151
+ ```grain
152
+ decode : (str: String) => Result<String, DecodingError>
153
+ ```
154
+
155
+ Decodes any percent-encoded characters in a string.
156
+
157
+ Parameters:
158
+
159
+ |param|type|description|
160
+ |-----|----|-----------|
161
+ |`str`|`String`|The string to decode|
162
+
163
+ Returns:
164
+
165
+ |type|description|
166
+ |----|-----------|
167
+ |`Result<String, DecodingError>`|`Ok(decoded)` containing the decoded string or `Err(err)` if the decoding failed|
168
+
169
+ ### Uri.**encodeQuery**
170
+
171
+ <details disabled>
172
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
173
+ No other changes yet.
174
+ </details>
175
+
176
+ ```grain
177
+ encodeQuery :
178
+ (urlVals: List<(String, String)>, ?encodeSet: EncodeSet) => String
179
+ ```
180
+
181
+ Encodes a list of key-value pairs into an query string.
182
+
183
+ Parameters:
184
+
185
+ |param|type|description|
186
+ |-----|----|-----------|
187
+ |`urlVals`|`List<(String, String)>`|A list of key-value pairs|
188
+
189
+ Returns:
190
+
191
+ |type|description|
192
+ |----|-----------|
193
+ |`String`|A query string|
194
+
195
+ ### Uri.**decodeQuery**
196
+
197
+ <details disabled>
198
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
199
+ No other changes yet.
200
+ </details>
201
+
202
+ ```grain
203
+ decodeQuery : (str: String) => Result<List<(String, String)>, DecodingError>
204
+ ```
205
+
206
+ Decodes a query string into a list of pairs.
207
+
208
+ Parameters:
209
+
210
+ |param|type|description|
211
+ |-----|----|-----------|
212
+ |`str`|`String`|A query string|
213
+
214
+ Returns:
215
+
216
+ |type|description|
217
+ |----|-----------|
218
+ |`Result<List<(String, String)>, DecodingError>`|`Ok(decoded)` containing a list of key-value pairs from the decoded string or `Err(err)` if the decoding failed|
219
+
220
+ ### Uri.**parse**
221
+
222
+ <details disabled>
223
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
224
+ No other changes yet.
225
+ </details>
226
+
227
+ ```grain
228
+ parse : (str: String) => Result<Uri, ParseError>
229
+ ```
230
+
231
+ Parses a string into a `Uri` according to RFC 3986. If the URI string has a
232
+ path it will be automatically normalized, removing unnecessary `.` and `..`
233
+ segments.
234
+
235
+ Parameters:
236
+
237
+ |param|type|description|
238
+ |-----|----|-----------|
239
+ |`str`|`String`|The RFC 3986 URI string to parse|
240
+
241
+ Returns:
242
+
243
+ |type|description|
244
+ |----|-----------|
245
+ |`Result<Uri, ParseError>`|`Ok(uri)` containing a `Uri` if the given string is a valid URI or `Err(ParseError)` otherwise|
246
+
247
+ Examples:
248
+
249
+ ```grain
250
+ Uri.parse("https://grain-lang.org") == Ok(...)
251
+ ```
252
+
253
+ ```grain
254
+ Uri.parse("http://@*^%") == Err(Uri.ParseError)
255
+ ```
256
+
257
+ ### Uri.**resolveReference**
258
+
259
+ <details disabled>
260
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
261
+ No other changes yet.
262
+ </details>
263
+
264
+ ```grain
265
+ resolveReference :
266
+ (base: Uri, ref: Uri) => Result<Uri, ResolveReferenceError>
267
+ ```
268
+
269
+ Transforms a base URI and a URI reference into a target URI
270
+
271
+ Parameters:
272
+
273
+ |param|type|description|
274
+ |-----|----|-----------|
275
+ |`base`|`Uri`|The base URI to resolve a URI reference on|
276
+ |`ref`|`Uri`|The URI reference to apply onto the base|
277
+
278
+ Returns:
279
+
280
+ |type|description|
281
+ |----|-----------|
282
+ |`Result<Uri, ResolveReferenceError>`|`Ok(uri)` containing the target `Uri` or `Err(err)` if the input is malformed|
283
+
284
+ Examples:
285
+
286
+ ```grain
287
+ resolveReference(unwrap(parse("https://grain-lang.org/docs/stdlib/uri")), unwrap(parse("../intro"))) // https://grain-lang.org/docs/intro
288
+ ```
289
+
290
+ ```grain
291
+ resolveReference(unwrap(parse("https://grain-lang.org/docs")), unwrap(parse("?key=val"))) // https://grain-lang.org/docs?key=val
292
+ ```
293
+
294
+ ```grain
295
+ resolveReference(unwrap(parse("https://grain-lang.org/docs")), unwrap(parse("google.com/search"))) // https://google.com/search
296
+ ```
297
+
298
+ ### Uri.**make**
299
+
300
+ <details disabled>
301
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
302
+ No other changes yet.
303
+ </details>
304
+
305
+ ```grain
306
+ make :
307
+ (?scheme: Option<String>, ?userinfo: Option<String>, ?host: Option<String>,
308
+ ?port: Option<Number>, ?path: String, ?query: Option<String>,
309
+ ?fragment: Option<String>, ?encodeComponents: Bool) =>
310
+ Result<Uri, ConstructUriError>
311
+ ```
312
+
313
+ Constructs a new `Uri` from components.
314
+
315
+ Parameters:
316
+
317
+ |param|type|description|
318
+ |-----|----|-----------|
319
+ |`?scheme`|`Option<String>`|`Some(scheme)` containing the desired scheme component or `None` for a scheme-less URI|
320
+ |`?userinfo`|`Option<String>`|`Some(userinfo)` containing the desired userinfo component or `None` for a userinfo-less URI|
321
+ |`?host`|`Option<String>`|`Some(host)` containing the desired host component or `None` for a host-less URI|
322
+ |`?port`|`Option<Number>`|`Some(port)` containing the desired port component or `None` for a port-less URI|
323
+ |`?path`|`String`|The desired path for the URI. `""` by default|
324
+ |`?query`|`Option<String>`|`Some(query)` containing the desired query string component or `None` for a query-less URI|
325
+ |`?fragment`|`Option<String>`|`Some(fragment)` containing the desired fragment component or `None` for a fragment-less URI|
326
+ |`?encodeComponents`|`Bool`|Whether or not to apply percent encoding for each component to remove unsafe characters for each component|
327
+
328
+ Examples:
329
+
330
+ ```grain
331
+ Uri.make(scheme=Some("https"), host=Some("grain-lang.org")) // https://grain-lang.org
332
+ ```
333
+
334
+ ```grain
335
+ Uri.make(host=Some("g/r@in"), encodeComponents=false) // Err(Uri.InvalidHostError)
336
+ ```
337
+
338
+ ```grain
339
+ Uri.make(scheme=Some("abc"), host=Some("g/r@in"), query=Some("k/ey=v^@l"), encodeComponents=true) // abc://g%2Fr%40in?k/ey=v%5E@l
340
+ ```
341
+
342
+ ```grain
343
+ Uri.make(port=Some(80)) // Err(Uri.PortWithNoHost)
344
+ ```
345
+
346
+ ### Uri.**update**
347
+
348
+ <details disabled>
349
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
350
+ No other changes yet.
351
+ </details>
352
+
353
+ ```grain
354
+ update :
355
+ (uri: Uri, ?scheme: Option<Option<String>>,
356
+ ?userinfo: Option<Option<String>>, ?host: Option<Option<String>>,
357
+ ?port: Option<Option<Number>>, ?path: Option<String>,
358
+ ?query: Option<Option<String>>, ?fragment: Option<Option<String>>,
359
+ ?encodeComponents: Bool) => Result<Uri, ConstructUriError>
360
+ ```
361
+
362
+ Constructs a new `Uri` from a base `Uri` and components to update. The
363
+ pattern used to update each component is that `None` means the base URI's
364
+ component should be used and `Some(val)` means that a new value should be
365
+ used for that component.
366
+
367
+ Parameters:
368
+
369
+ |param|type|description|
370
+ |-----|----|-----------|
371
+ |`uri`|`Uri`|The `Uri` to update|
372
+ |`?scheme`|`Option<Option<String>>`|`Some(scheme)` containing the desired updated scheme component or `None` to maintain the base URI's scheme|
373
+ |`?userinfo`|`Option<Option<String>>`|`Some(userinfo)` containing the desired updated userinfo component or `None` to maintain the base URI's userinfo|
374
+ |`?host`|`Option<Option<String>>`|`Some(host)` containing the desired updated host component or `None` to maintain the base URI's host|
375
+ |`?port`|`Option<Option<Number>>`|`Some(port)` containing the desired updated port component or `None` to maintain the base URI's port|
376
+ |`?path`|`Option<String>`|`Some(path)` containing the desired updated path component or `None` to maintain the base URI's path|
377
+ |`?query`|`Option<Option<String>>`|`Some(query)` containing the desired updated query string component or `None` to maintain the base URI's query|
378
+ |`?fragment`|`Option<Option<String>>`|`Some(fragment)` containing the desired updated fragment component or `None` to maintain the base URI's fragment|
379
+ |`?encodeComponents`|`Bool`|Whether or not to apply percent encoding for each updated component to remove unsafe characters|
380
+
381
+ Examples:
382
+
383
+ ```grain
384
+ let uri = Result.unwrap(Uri.parse("https://grain-lang.org/docs?k=v")) // Base URI for following examples
385
+ ```
386
+
387
+ ```grain
388
+ Uri.update(uri, scheme=Some(Some("ftp"))) // ftp://grain-lang.org/docs?k=v
389
+ ```
390
+
391
+ ```grain
392
+ Uri.update(uri, query=Some(None)) // https://grain-lang.org/docs
393
+ ```
394
+
395
+ ```grain
396
+ Uri.update(uri, host=Some(Some("g/r@in")), encodeComponents=true) // https://g%2Fr%40in/docs?k=v
397
+ ```
398
+
399
+ ```grain
400
+ Uri.update(uri, host=Some(None), port=Some(Some(80))) // Err(Uri.PortWithNoHost)
401
+ ```
402
+
403
+ ### Uri.**hasAuthority**
404
+
405
+ <details disabled>
406
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
407
+ No other changes yet.
408
+ </details>
409
+
410
+ ```grain
411
+ hasAuthority : (uri: Uri) => Bool
412
+ ```
413
+
414
+ Determines whether a `Uri` has an authority (i.e. has a host component)
415
+
416
+ Parameters:
417
+
418
+ |param|type|description|
419
+ |-----|----|-----------|
420
+ |`uri`|`Uri`|The `Uri` to consider|
421
+
422
+ Returns:
423
+
424
+ |type|description|
425
+ |----|-----------|
426
+ |`Bool`|`true` if the `Uri` has an authority component or `false` otherwise|
427
+
428
+ ### Uri.**isAbsolute**
429
+
430
+ <details disabled>
431
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
432
+ No other changes yet.
433
+ </details>
434
+
435
+ ```grain
436
+ isAbsolute : (uri: Uri) => Bool
437
+ ```
438
+
439
+ Determines whether a `Uri` is an absolute URI (has a scheme component)
440
+
441
+ Parameters:
442
+
443
+ |param|type|description|
444
+ |-----|----|-----------|
445
+ |`uri`|`Uri`|The `Uri` to consider|
446
+
447
+ Returns:
448
+
449
+ |type|description|
450
+ |----|-----------|
451
+ |`Bool`|`true` if the `Uri` is absolute (has a scheme component) or `false` otherwise|
452
+
453
+ ### Uri.**toString**
454
+
455
+ <details disabled>
456
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
457
+ No other changes yet.
458
+ </details>
459
+
460
+ ```grain
461
+ toString : (uri: Uri) => String
462
+ ```
463
+
464
+ Converts the given `Uri` into a string.
465
+
466
+ Parameters:
467
+
468
+ |param|type|description|
469
+ |-----|----|-----------|
470
+ |`uri`|`Uri`|The `Uri` to convert|
471
+
472
+ Returns:
473
+
474
+ |type|description|
475
+ |----|-----------|
476
+ |`String`|A string representation of the `Uri`|
477
+