@grain/stdlib 0.5.4 → 0.5.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.
Files changed (59) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/array.gr +60 -57
  3. package/array.md +24 -6
  4. package/buffer.gr +71 -1
  5. package/buffer.md +142 -0
  6. package/bytes.gr +52 -3
  7. package/bytes.md +117 -0
  8. package/char.gr +21 -18
  9. package/char.md +18 -3
  10. package/immutablepriorityqueue.gr +13 -13
  11. package/int32.gr +39 -37
  12. package/int32.md +6 -0
  13. package/int64.gr +39 -37
  14. package/int64.md +6 -0
  15. package/list.gr +31 -22
  16. package/list.md +39 -10
  17. package/map.gr +19 -28
  18. package/number.gr +81 -5
  19. package/number.md +64 -2
  20. package/option.gr +30 -26
  21. package/option.md +12 -0
  22. package/package.json +1 -1
  23. package/path.gr +787 -0
  24. package/path.md +727 -0
  25. package/pervasives.gr +3 -4
  26. package/pervasives.md +6 -1
  27. package/queue.gr +11 -9
  28. package/queue.md +2 -0
  29. package/regex.gr +76 -3
  30. package/regex.md +70 -0
  31. package/result.gr +24 -20
  32. package/result.md +12 -0
  33. package/runtime/atof/common.gr +198 -0
  34. package/runtime/atof/common.md +243 -0
  35. package/runtime/atof/decimal.gr +663 -0
  36. package/runtime/atof/decimal.md +59 -0
  37. package/runtime/atof/lemire.gr +264 -0
  38. package/runtime/atof/lemire.md +6 -0
  39. package/runtime/atof/parse.gr +615 -0
  40. package/runtime/atof/parse.md +12 -0
  41. package/runtime/atof/slow.gr +238 -0
  42. package/runtime/atof/slow.md +6 -0
  43. package/runtime/atof/table.gr +2016 -0
  44. package/runtime/atof/table.md +12 -0
  45. package/runtime/{stringUtils.gr → atoi/parse.gr} +1 -1
  46. package/runtime/{stringUtils.md → atoi/parse.md} +1 -1
  47. package/runtime/bigint.gr +3 -3
  48. package/runtime/equal.gr +1 -1
  49. package/runtime/numberUtils.gr +2 -2
  50. package/runtime/numberUtils.md +6 -0
  51. package/runtime/numbers.gr +4 -4
  52. package/runtime/unsafe/conv.gr +21 -41
  53. package/runtime/unsafe/conv.md +0 -3
  54. package/runtime/unsafe/printWasm.gr +4 -40
  55. package/runtime/utils/printing.gr +3 -3
  56. package/stack.gr +4 -2
  57. package/stack.md +2 -0
  58. package/string.gr +1 -1
  59. package/sys/time.gr +4 -4
package/path.md ADDED
@@ -0,0 +1,727 @@
1
+ ---
2
+ title: Path
3
+ ---
4
+
5
+ Utilities for working with system paths.
6
+
7
+ This module treats paths purely as a data representation and does not
8
+ provide functionality for interacting with the file system.
9
+
10
+ This module explicitly encodes whether a path is absolute or relative, and
11
+ whether it refers to a file or a directory, as part of the `Path` type.
12
+
13
+ Paths in this module abide by a special POSIX-like representation/grammar
14
+ rather than one defined by a specific operating system. The rules are as
15
+ follows:
16
+
17
+ - Path separators are denoted by `/` for POSIX-like paths
18
+ - Absolute paths may be rooted either at the POSIX-like root `/` or at Windows-like drive roots like `C:/`
19
+ - Paths referencing files must not include trailing forward slashes, but paths referencing directories may
20
+ - The path segment `.` indicates the relative "current" directory of a path, and `..` indicates the parent directory of a path
21
+
22
+ <details disabled>
23
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
24
+ No other changes yet.
25
+ </details>
26
+
27
+ ```grain
28
+ import Path from "path"
29
+ ```
30
+
31
+ ## Types
32
+
33
+ Type declarations included in the Path module.
34
+
35
+ ### Path.**AbsoluteRoot**
36
+
37
+ ```grain
38
+ enum AbsoluteRoot {
39
+ Root,
40
+ Drive(Char),
41
+ }
42
+ ```
43
+
44
+ Represents an absolute path's anchor point.
45
+
46
+ ### Path.**Relative**
47
+
48
+ ```grain
49
+ type Relative
50
+ ```
51
+
52
+ Represents a relative path.
53
+
54
+ ### Path.**Absolute**
55
+
56
+ ```grain
57
+ type Absolute
58
+ ```
59
+
60
+ Represents an absolute path.
61
+
62
+ ### Path.**File**
63
+
64
+ ```grain
65
+ type File
66
+ ```
67
+
68
+ Represents a path referencing a file.
69
+
70
+ ### Path.**Directory**
71
+
72
+ ```grain
73
+ type Directory
74
+ ```
75
+
76
+ Represents a path referencing a directory.
77
+
78
+ ### Path.**TypedPath**
79
+
80
+ ```grain
81
+ type TypedPath<a, b>
82
+ ```
83
+
84
+ Represents a path typed on (`Absolute` or `Relative`) and (`File` or
85
+ `Directory`)
86
+
87
+ ### Path.**Path**
88
+
89
+ ```grain
90
+ enum Path {
91
+ AbsoluteFile(TypedPath<Absolute, File>),
92
+ AbsoluteDir(TypedPath<Absolute, Directory>),
93
+ RelativeFile(TypedPath<Relative, File>),
94
+ RelativeDir(TypedPath<Relative, Directory>),
95
+ }
96
+ ```
97
+
98
+ Represents a system path.
99
+
100
+ ### Path.**Platform**
101
+
102
+ ```grain
103
+ enum Platform {
104
+ Windows,
105
+ Posix,
106
+ }
107
+ ```
108
+
109
+ Represents a platform-specific path encoding scheme.
110
+
111
+ ### Path.**PathOperationError**
112
+
113
+ ```grain
114
+ enum PathOperationError {
115
+ IncompatiblePathType,
116
+ }
117
+ ```
118
+
119
+ Represents an error that can occur when finding a property of a path.
120
+
121
+ ### Path.**AppendError**
122
+
123
+ ```grain
124
+ enum AppendError {
125
+ AppendToFile,
126
+ AppendAbsolute,
127
+ }
128
+ ```
129
+
130
+ Represents an error that can occur when appending paths.
131
+
132
+ ### Path.**AncestryStatus**
133
+
134
+ ```grain
135
+ enum AncestryStatus {
136
+ Descendant,
137
+ Ancestor,
138
+ Self,
139
+ NoLineage,
140
+ }
141
+ ```
142
+
143
+ Represents the status of an ancestry check between two paths.
144
+
145
+ ### Path.**IncompatibilityError**
146
+
147
+ ```grain
148
+ enum IncompatibilityError {
149
+ DifferentRoots,
150
+ DifferentBases,
151
+ }
152
+ ```
153
+
154
+ Represents an error that can occur when the types of paths are incompatible
155
+ for an operation.
156
+
157
+ ### Path.**RelativizationError**
158
+
159
+ ```grain
160
+ enum RelativizationError {
161
+ Incompatible(IncompatibilityError),
162
+ ImpossibleRelativization,
163
+ }
164
+ ```
165
+
166
+ Represents possible errors for the `relativeTo` operation.
167
+
168
+ ## Values
169
+
170
+ Functions for working with Paths.
171
+
172
+ ### Path.**fromString**
173
+
174
+ <details disabled>
175
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
176
+ No other changes yet.
177
+ </details>
178
+
179
+ ```grain
180
+ fromString : String -> Path
181
+ ```
182
+
183
+ Parses a path string into a `Path`. Paths will be parsed as file paths
184
+ rather than directory paths if there is ambiguity.
185
+
186
+ Parameters:
187
+
188
+ |param|type|description|
189
+ |-----|----|-----------|
190
+ |`pathStr`|`String`|The string to parse as a path|
191
+
192
+ Returns:
193
+
194
+ |type|description|
195
+ |----|-----------|
196
+ |`Path`|The path wrapped with details encoded within the type|
197
+
198
+ Examples:
199
+
200
+ ```grain
201
+ fromString("/bin/") // an absolute Path referencing the directory /bin/
202
+ ```
203
+
204
+ ```grain
205
+ fromString("file.txt") // a relative Path referencing the file ./file.txt
206
+ ```
207
+
208
+ ```grain
209
+ fromString(".") // a relative Path referencing the current directory
210
+ ```
211
+
212
+ ### Path.**fromPlatformString**
213
+
214
+ <details disabled>
215
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
216
+ No other changes yet.
217
+ </details>
218
+
219
+ ```grain
220
+ fromPlatformString : (String, Platform) -> Path
221
+ ```
222
+
223
+ Parses a path string into a `Path` using the path separators appropriate to
224
+ the given platform (`/` for `Posix` and either `/` or `\` for `Windows`).
225
+ Paths will be parsed as file paths rather than directory paths if there is
226
+ ambiguity.
227
+
228
+ Parameters:
229
+
230
+ |param|type|description|
231
+ |-----|----|-----------|
232
+ |`pathStr`|`String`|The string to parse as a path|
233
+ |`platform`|`Platform`|The platform whose path separators should be used for parsing|
234
+
235
+ Returns:
236
+
237
+ |type|description|
238
+ |----|-----------|
239
+ |`Path`|The path wrapped with details encoded within the type|
240
+
241
+ Examples:
242
+
243
+ ```grain
244
+ fromPlatformString("/bin/", Posix) // an absolute Path referencing the directory /bin/
245
+ ```
246
+
247
+ ```grain
248
+ fromPlatformString("C:\\file.txt", Windows) // a relative Path referencing the file C:\file.txt
249
+ ```
250
+
251
+ ### Path.**toString**
252
+
253
+ <details disabled>
254
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
255
+ No other changes yet.
256
+ </details>
257
+
258
+ ```grain
259
+ toString : Path -> String
260
+ ```
261
+
262
+ Converts the given `Path` into a string, using the `/` path separator.
263
+ A trailing slash is added to directory paths.
264
+
265
+ Parameters:
266
+
267
+ |param|type|description|
268
+ |-----|----|-----------|
269
+ |`path`|`Path`|The path to convert to a string|
270
+
271
+ Returns:
272
+
273
+ |type|description|
274
+ |----|-----------|
275
+ |`String`|A string representing the given path|
276
+
277
+ Examples:
278
+
279
+ ```grain
280
+ toString(fromString("/file.txt")) == "/file.txt"
281
+ ```
282
+
283
+ ```grain
284
+ toString(fromString("dir/")) == "./dir/"
285
+ ```
286
+
287
+ ### Path.**toPlatformString**
288
+
289
+ <details disabled>
290
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
291
+ No other changes yet.
292
+ </details>
293
+
294
+ ```grain
295
+ toPlatformString : (Path, Platform) -> String
296
+ ```
297
+
298
+ Converts the given `Path` into a string, using the canonical path separator
299
+ appropriate to the given platform (`/` for `Posix` and `\` for `Windows`).
300
+ A trailing slash is added to directory paths.
301
+
302
+ Parameters:
303
+
304
+ |param|type|description|
305
+ |-----|----|-----------|
306
+ |`path`|`Path`|The path to convert to a string|
307
+ |`platform`|`Platform`|The `Platform` to use to represent the path as a string|
308
+
309
+ Returns:
310
+
311
+ |type|description|
312
+ |----|-----------|
313
+ |`String`|A string representing the given path|
314
+
315
+ Examples:
316
+
317
+ ```grain
318
+ toPlatformString(fromString("dir/"), Posix) == "./dir/"
319
+ ```
320
+
321
+ ```grain
322
+ toPlatformString(fromString("C:/file.txt"), Windows) == "C:\\file.txt"
323
+ ```
324
+
325
+ ### Path.**isDirectory**
326
+
327
+ <details disabled>
328
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
329
+ No other changes yet.
330
+ </details>
331
+
332
+ ```grain
333
+ isDirectory : Path -> Bool
334
+ ```
335
+
336
+ Determines whether the path is a directory path.
337
+
338
+ Parameters:
339
+
340
+ |param|type|description|
341
+ |-----|----|-----------|
342
+ |`path`|`Path`|The path to inspect|
343
+
344
+ Returns:
345
+
346
+ |type|description|
347
+ |----|-----------|
348
+ |`Bool`|`true` if the path is a directory path or `false` otherwise|
349
+
350
+ Examples:
351
+
352
+ ```grain
353
+ isDirectory(fromString("file.txt")) == false
354
+ ```
355
+
356
+ ```grain
357
+ isDirectory(fromString("/bin/")) == true
358
+ ```
359
+
360
+ ### Path.**isAbsolute**
361
+
362
+ ```grain
363
+ isAbsolute : Path -> Bool
364
+ ```
365
+
366
+ Determines whether the path is an absolute path.
367
+
368
+ Parameters:
369
+
370
+ |param|type|description|
371
+ |-----|----|-----------|
372
+ |`path`|`Path`|The path to inspect|
373
+
374
+ Returns:
375
+
376
+ |type|description|
377
+ |----|-----------|
378
+ |`Bool`|`true` if the path is absolute or `false` otherwise|
379
+
380
+ Examples:
381
+
382
+ ```grain
383
+ isAbsolute(fromString("/Users/me")) == true
384
+ ```
385
+
386
+ ```grain
387
+ isAbsolute(fromString("./file.txt")) == false
388
+ ```
389
+
390
+ ### Path.**append**
391
+
392
+ <details disabled>
393
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
394
+ No other changes yet.
395
+ </details>
396
+
397
+ ```grain
398
+ append : (Path, Path) -> Result<Path, AppendError>
399
+ ```
400
+
401
+ Creates a new path by appending a relative path segment to a directory path.
402
+
403
+ Parameters:
404
+
405
+ |param|type|description|
406
+ |-----|----|-----------|
407
+ |`path`|`Path`|The base path|
408
+ |`toAppend`|`Path`|The relative path to append|
409
+
410
+ Returns:
411
+
412
+ |type|description|
413
+ |----|-----------|
414
+ |`Result<Path, AppendError>`|`Ok(path)` combining the base and appended paths or `Err(err)` if the paths are incompatible|
415
+
416
+ Examples:
417
+
418
+ ```grain
419
+ append(fromString("./dir/"), fromString("file.txt")) == Ok(fromString("./dir/file.txt"))
420
+ ```
421
+
422
+ ```grain
423
+ append(fromString("a.txt"), fromString("b.sh")) == Err(AppendToFile) // cannot append to file path
424
+ ```
425
+
426
+ ```grain
427
+ append(fromString("./dir/"), fromString("/dir2")) == Err(AppendAbsolute) // cannot append an absolute path
428
+ ```
429
+
430
+ ### Path.**relativeTo**
431
+
432
+ <details disabled>
433
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
434
+ No other changes yet.
435
+ </details>
436
+
437
+ ```grain
438
+ relativeTo : (Path, Path) -> Result<Path, RelativizationError>
439
+ ```
440
+
441
+ Attempts to construct a new relative path which will lead to the destination
442
+ path from the source path.
443
+
444
+ If the source and destination are incompatible in their bases, the result
445
+ will be `Err(IncompatibilityError)`.
446
+
447
+ If the route to the destination cannot be concretely determined from the
448
+ source, the result will be `Err(ImpossibleRelativization)`.
449
+
450
+ Parameters:
451
+
452
+ |param|type|description|
453
+ |-----|----|-----------|
454
+ |`source`|`Path`|The source path|
455
+ |`dest`|`Path`|The destination path to resolve|
456
+
457
+ Returns:
458
+
459
+ |type|description|
460
+ |----|-----------|
461
+ |`Result<Path, RelativizationError>`|`Ok(path)` containing the relative path if successfully resolved or `Err(err)` otherwise|
462
+
463
+ Examples:
464
+
465
+ ```grain
466
+ relativeTo(fromString("/usr"), fromString("/usr/bin")) == Ok(fromString("./bin"))
467
+ ```
468
+
469
+ ```grain
470
+ relativeTo(fromString("/home/me"), fromString("/home/me")) == Ok(fromString("."))
471
+ ```
472
+
473
+ ```grain
474
+ relativeTo(fromString("/file.txt"), fromString("/etc/")) == Ok(fromString("../etc/"))
475
+ ```
476
+
477
+ ```grain
478
+ relativeTo(fromString(".."), fromString("../../thing")) Ok(fromString("../thing"))
479
+ ```
480
+
481
+ ```grain
482
+ relativeTo(fromString("/usr/bin"), fromString("C:/Users")) == Err(Incompatible(DifferentRoots))
483
+ ```
484
+
485
+ ```grain
486
+ relativeTo(fromString("../here"), fromString("./there")) == Err(ImpossibleRelativization)
487
+ ```
488
+
489
+ ### Path.**ancestry**
490
+
491
+ <details disabled>
492
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
493
+ No other changes yet.
494
+ </details>
495
+
496
+ ```grain
497
+ ancestry : (Path, Path) -> Result<AncestryStatus, IncompatibilityError>
498
+ ```
499
+
500
+ Determines the relative ancestry betwen two paths.
501
+
502
+ Parameters:
503
+
504
+ |param|type|description|
505
+ |-----|----|-----------|
506
+ |`base`|`Path`|The first path to consider|
507
+ |`path`|`Path`|The second path to consider|
508
+
509
+ Returns:
510
+
511
+ |type|description|
512
+ |----|-----------|
513
+ |`Result<AncestryStatus, IncompatibilityError>`|`Ok(ancestryStatus)` with the relative ancestry between the paths if they are compatible or `Err(err)` if they are incompatible|
514
+
515
+ Examples:
516
+
517
+ ```grain
518
+ ancestry(fromString("/usr"), fromString("/usr/bin/bash")) == Ok(Ancestor)
519
+ ```
520
+
521
+ ```grain
522
+ ancestry(fromString("/Users/me"), fromString("/Users")) == Ok(Descendant)
523
+ ```
524
+
525
+ ```grain
526
+ ancestry(fromString("/usr"), fromString("/etc")) == Ok(Neither)
527
+ ```
528
+
529
+ ```grain
530
+ ancestry(fromString("C:/dir1"), fromString("/dir2")) == Err(DifferentRoots)
531
+ ```
532
+
533
+ ### Path.**parent**
534
+
535
+ <details disabled>
536
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
537
+ No other changes yet.
538
+ </details>
539
+
540
+ ```grain
541
+ parent : Path -> Path
542
+ ```
543
+
544
+ Retrieves the path corresponding to the parent directory of the given path.
545
+
546
+ Parameters:
547
+
548
+ |param|type|description|
549
+ |-----|----|-----------|
550
+ |`path`|`Path`|The path to inspect|
551
+
552
+ Returns:
553
+
554
+ |type|description|
555
+ |----|-----------|
556
+ |`Path`|A path corresponding to the parent directory of the given path|
557
+
558
+ Examples:
559
+
560
+ ```grain
561
+ parent(fromString("./dir/inner")) == fromString("./dir/")
562
+ ```
563
+
564
+ ```grain
565
+ parent(fromString("/")) == fromString("/")
566
+ ```
567
+
568
+ ### Path.**basename**
569
+
570
+ <details disabled>
571
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
572
+ No other changes yet.
573
+ </details>
574
+
575
+ ```grain
576
+ basename : Path -> Option<String>
577
+ ```
578
+
579
+ Retrieves the basename (named final segment) of a path.
580
+
581
+ Parameters:
582
+
583
+ |param|type|description|
584
+ |-----|----|-----------|
585
+ |`path`|`Path`|The path to inspect|
586
+
587
+ Returns:
588
+
589
+ |type|description|
590
+ |----|-----------|
591
+ |`Option<String>`|`Some(path)` containing the basename of the path or `None` if the path does not have one|
592
+
593
+ Examples:
594
+
595
+ ```grain
596
+ basename(fromString("./dir/file.txt")) == Some("file.txt")
597
+ ```
598
+
599
+ ```grain
600
+ basename(fromString(".."))) == None
601
+ ```
602
+
603
+ ### Path.**stem**
604
+
605
+ <details disabled>
606
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
607
+ No other changes yet.
608
+ </details>
609
+
610
+ ```grain
611
+ stem : Path -> Result<String, PathOperationError>
612
+ ```
613
+
614
+ Retrieves the basename of a file path without the extension.
615
+
616
+ Parameters:
617
+
618
+ |param|type|description|
619
+ |-----|----|-----------|
620
+ |`path`|`Path`|The path to inspect|
621
+
622
+ Returns:
623
+
624
+ |type|description|
625
+ |----|-----------|
626
+ |`Result<String, PathOperationError>`|`Ok(path)` containing the stem of the file path or `Err(err)` if the path is a directory path|
627
+
628
+ Examples:
629
+
630
+ ```grain
631
+ stem(fromString("file.txt")) == Ok("file")
632
+ ```
633
+
634
+ ```grain
635
+ stem(fromString(".gitignore")) == Ok(".gitignore")
636
+ ```
637
+
638
+ ```grain
639
+ stem(fromString(".a.tar.gz")) == Ok(".a")
640
+ ```
641
+
642
+ ```grain
643
+ stem(fromString("/dir/")) == Err(IncompatiblePathType) // can only take stem of a file path
644
+ ```
645
+
646
+ ### Path.**extension**
647
+
648
+ <details disabled>
649
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
650
+ No other changes yet.
651
+ </details>
652
+
653
+ ```grain
654
+ extension : Path -> Result<String, PathOperationError>
655
+ ```
656
+
657
+ Retrieves the extension on the basename of a file path.
658
+
659
+ Parameters:
660
+
661
+ |param|type|description|
662
+ |-----|----|-----------|
663
+ |`path`|`Path`|The path to inspect|
664
+
665
+ Returns:
666
+
667
+ |type|description|
668
+ |----|-----------|
669
+ |`Result<String, PathOperationError>`|`Ok(path)` containing the extension of the file path or `Err(err)` if the path is a directory path|
670
+
671
+ Examples:
672
+
673
+ ```grain
674
+ extension(fromString("file.txt")) == Ok(".txt")
675
+ ```
676
+
677
+ ```grain
678
+ extension(fromString(".gitignore")) == Ok("")
679
+ ```
680
+
681
+ ```grain
682
+ extension(fromString(".a.tar.gz")) == Ok(".tar.gz")
683
+ ```
684
+
685
+ ```grain
686
+ extension(fromString("/dir/")) == Err(IncompatiblePathType) // can only take extension of a file path
687
+ ```
688
+
689
+ ### Path.**root**
690
+
691
+ <details disabled>
692
+ <summary tabindex="-1">Added in <code>0.5.5</code></summary>
693
+ No other changes yet.
694
+ </details>
695
+
696
+ ```grain
697
+ root : Path -> Result<AbsoluteRoot, PathOperationError>
698
+ ```
699
+
700
+ Retrieves the root of the absolute path.
701
+
702
+ Parameters:
703
+
704
+ |param|type|description|
705
+ |-----|----|-----------|
706
+ |`path`|`Path`|The path to inspect|
707
+
708
+ Returns:
709
+
710
+ |type|description|
711
+ |----|-----------|
712
+ |`Result<AbsoluteRoot, PathOperationError>`|`Ok(root)` containing the root of the path or `Err(err)` if the path is a relative path|
713
+
714
+ Examples:
715
+
716
+ ```grain
717
+ root(fromString("C:/Users/me/")) == Ok(Drive('C'))
718
+ ```
719
+
720
+ ```grain
721
+ root(fromString("/home/me/")) == Ok(Root)
722
+ ```
723
+
724
+ ```grain
725
+ root(fromString("./file.txt")) == Err(IncompatiblePathType)
726
+ ```
727
+