@grain/stdlib 0.5.3 → 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.
- package/CHANGELOG.md +61 -0
- package/array.gr +65 -57
- package/array.md +54 -6
- package/buffer.gr +71 -1
- package/buffer.md +142 -0
- package/bytes.gr +52 -3
- package/bytes.md +117 -0
- package/char.gr +23 -20
- package/char.md +18 -3
- package/immutablemap.gr +493 -0
- package/immutablemap.md +479 -0
- package/immutablepriorityqueue.gr +44 -16
- package/immutablepriorityqueue.md +44 -1
- package/immutableset.gr +498 -0
- package/immutableset.md +449 -0
- package/int32.gr +39 -37
- package/int32.md +6 -0
- package/int64.gr +39 -37
- package/int64.md +6 -0
- package/list.gr +33 -24
- package/list.md +39 -10
- package/map.gr +19 -28
- package/marshal.gr +4 -4
- package/number.gr +727 -26
- package/number.md +345 -23
- package/option.gr +30 -26
- package/option.md +12 -0
- package/package.json +1 -1
- package/path.gr +787 -0
- package/path.md +727 -0
- package/pervasives.gr +3 -4
- package/pervasives.md +6 -1
- package/priorityqueue.gr +25 -5
- package/priorityqueue.md +30 -0
- package/queue.gr +22 -7
- package/queue.md +18 -1
- package/regex.gr +161 -65
- package/regex.md +70 -0
- package/result.gr +24 -20
- package/result.md +12 -0
- package/runtime/atof/common.gr +198 -0
- package/runtime/atof/common.md +243 -0
- package/runtime/atof/decimal.gr +663 -0
- package/runtime/atof/decimal.md +59 -0
- package/runtime/atof/lemire.gr +264 -0
- package/runtime/atof/lemire.md +6 -0
- package/runtime/atof/parse.gr +615 -0
- package/runtime/atof/parse.md +12 -0
- package/runtime/atof/slow.gr +238 -0
- package/runtime/atof/slow.md +6 -0
- package/runtime/atof/table.gr +2016 -0
- package/runtime/atof/table.md +12 -0
- package/runtime/{stringUtils.gr → atoi/parse.gr} +1 -1
- package/runtime/{stringUtils.md → atoi/parse.md} +1 -1
- package/runtime/bigint.gr +7 -7
- package/runtime/compare.gr +2 -1
- package/runtime/equal.gr +3 -2
- package/runtime/exception.gr +9 -5
- package/runtime/exception.md +8 -2
- package/runtime/gc.gr +2 -1
- package/runtime/malloc.gr +1 -3
- package/runtime/numberUtils.gr +13 -13
- package/runtime/numberUtils.md +6 -0
- package/runtime/numbers.gr +123 -39
- package/runtime/numbers.md +26 -0
- package/runtime/string.gr +4 -2
- package/runtime/unsafe/conv.gr +21 -41
- package/runtime/unsafe/conv.md +0 -3
- package/runtime/unsafe/printWasm.gr +4 -40
- package/runtime/utils/printing.gr +3 -3
- package/set.gr +25 -25
- package/stack.gr +14 -0
- package/stack.md +17 -0
- package/string.gr +313 -39
- package/string.md +99 -0
- package/sys/file.gr +1 -1
- package/sys/time.gr +4 -4
package/path.gr
ADDED
|
@@ -0,0 +1,787 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Path: Utilities for working with system paths.
|
|
3
|
+
*
|
|
4
|
+
* This module treats paths purely as a data representation and does not
|
|
5
|
+
* provide functionality for interacting with the file system.
|
|
6
|
+
*
|
|
7
|
+
* This module explicitly encodes whether a path is absolute or relative, and
|
|
8
|
+
* whether it refers to a file or a directory, as part of the `Path` type.
|
|
9
|
+
*
|
|
10
|
+
* Paths in this module abide by a special POSIX-like representation/grammar
|
|
11
|
+
* rather than one defined by a specific operating system. The rules are as
|
|
12
|
+
* follows:
|
|
13
|
+
*
|
|
14
|
+
* - Path separators are denoted by `/` for POSIX-like paths
|
|
15
|
+
* - Absolute paths may be rooted either at the POSIX-like root `/` or at Windows-like drive roots like `C:/`
|
|
16
|
+
* - Paths referencing files must not include trailing forward slashes, but paths referencing directories may
|
|
17
|
+
* - The path segment `.` indicates the relative "current" directory of a path, and `..` indicates the parent directory of a path
|
|
18
|
+
*
|
|
19
|
+
* @example import Path from "path"
|
|
20
|
+
*
|
|
21
|
+
* @since v0.5.5
|
|
22
|
+
*/
|
|
23
|
+
import String from "string"
|
|
24
|
+
import List from "list"
|
|
25
|
+
import Option from "option"
|
|
26
|
+
import Result from "result"
|
|
27
|
+
import Char from "char"
|
|
28
|
+
|
|
29
|
+
// this module is influenced by https://github.com/reasonml/reason-native/blob/a0ddab6ab25237961e32d8732b0a222ec2372d4a/src/fp/Fp.re
|
|
30
|
+
// with some modifications; reason-native license:
|
|
31
|
+
|
|
32
|
+
// MIT License
|
|
33
|
+
//
|
|
34
|
+
// Copyright (c) Facebook, Inc. and its affiliates.
|
|
35
|
+
//
|
|
36
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
37
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
38
|
+
// in the Software without restriction, including without limitation the rights
|
|
39
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
40
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
41
|
+
// furnished to do so, subject to the following conditions:
|
|
42
|
+
//
|
|
43
|
+
// The above copyright notice and this permission notice shall be included in all
|
|
44
|
+
// copies or substantial portions of the Software.
|
|
45
|
+
//
|
|
46
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
47
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
48
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
49
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
50
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
51
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
52
|
+
// SOFTWARE.
|
|
53
|
+
|
|
54
|
+
enum Token {
|
|
55
|
+
Slash,
|
|
56
|
+
Dot,
|
|
57
|
+
Dotdot,
|
|
58
|
+
DriveTok(Char),
|
|
59
|
+
Text(String),
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
enum DirsUp {
|
|
63
|
+
Zero,
|
|
64
|
+
Positive,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
enum FileType {
|
|
68
|
+
File,
|
|
69
|
+
Dir,
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// hack to be able to concretely distinguish TypedPath from PathInfo and
|
|
73
|
+
// enforce TypedPath's type parameters
|
|
74
|
+
record TFileType<a> {
|
|
75
|
+
fileType: FileType,
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Rel(Number) represents the number of directories up from the base point
|
|
79
|
+
enum Base {
|
|
80
|
+
Rel(Number),
|
|
81
|
+
Abs(AbsoluteRoot),
|
|
82
|
+
},
|
|
83
|
+
type PathInfo = (Base, FileType, List<String>),
|
|
84
|
+
record TBase<a> {
|
|
85
|
+
base: Base,
|
|
86
|
+
},
|
|
87
|
+
/**
|
|
88
|
+
* @section Types: Type declarations included in the Path module.
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Represents an absolute path's anchor point.
|
|
93
|
+
*/
|
|
94
|
+
export enum AbsoluteRoot {
|
|
95
|
+
Root,
|
|
96
|
+
Drive(Char),
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Dummy record names put here just to distinguish the two. These could be
|
|
100
|
+
// replaced with opaque types if they get added to the language
|
|
101
|
+
/**
|
|
102
|
+
* Represents a relative path.
|
|
103
|
+
*/
|
|
104
|
+
record Relative {
|
|
105
|
+
_rel: Void,
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Represents an absolute path.
|
|
110
|
+
*/
|
|
111
|
+
record Absolute {
|
|
112
|
+
_abs: Void,
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Represents a path referencing a file.
|
|
117
|
+
*/
|
|
118
|
+
record File {
|
|
119
|
+
_file: Void,
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Represents a path referencing a directory.
|
|
124
|
+
*/
|
|
125
|
+
record Directory {
|
|
126
|
+
_directory: Void,
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Represents a path typed on (`Absolute` or `Relative`) and (`File` or
|
|
131
|
+
* `Directory`)
|
|
132
|
+
*/
|
|
133
|
+
type TypedPath<a, b> = (TBase<a>, TFileType<b>, List<String>),
|
|
134
|
+
/**
|
|
135
|
+
* Represents a system path.
|
|
136
|
+
*/
|
|
137
|
+
export enum Path {
|
|
138
|
+
AbsoluteFile(TypedPath<Absolute, File>),
|
|
139
|
+
AbsoluteDir(TypedPath<Absolute, Directory>),
|
|
140
|
+
RelativeFile(TypedPath<Relative, File>),
|
|
141
|
+
RelativeDir(TypedPath<Relative, Directory>),
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Represents a platform-specific path encoding scheme.
|
|
146
|
+
*/
|
|
147
|
+
export enum Platform {
|
|
148
|
+
Windows,
|
|
149
|
+
Posix,
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Represents an error that can occur when finding a property of a path.
|
|
154
|
+
*/
|
|
155
|
+
export enum PathOperationError {
|
|
156
|
+
IncompatiblePathType,
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Represents an error that can occur when appending paths.
|
|
161
|
+
*/
|
|
162
|
+
export enum AppendError {
|
|
163
|
+
AppendToFile,
|
|
164
|
+
AppendAbsolute,
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Represents the status of an ancestry check between two paths.
|
|
169
|
+
*/
|
|
170
|
+
export enum AncestryStatus {
|
|
171
|
+
Descendant,
|
|
172
|
+
Ancestor,
|
|
173
|
+
Self,
|
|
174
|
+
NoLineage,
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Represents an error that can occur when the types of paths are incompatible
|
|
179
|
+
* for an operation.
|
|
180
|
+
*/
|
|
181
|
+
export enum IncompatibilityError {
|
|
182
|
+
DifferentRoots,
|
|
183
|
+
DifferentBases,
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Represents possible errors for the `relativeTo` operation.
|
|
188
|
+
*/
|
|
189
|
+
export enum RelativizationError {
|
|
190
|
+
Incompatible(IncompatibilityError),
|
|
191
|
+
ImpossibleRelativization,
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @section Values: Functions for working with Paths.
|
|
196
|
+
*/
|
|
197
|
+
|
|
198
|
+
let makeToken = str => {
|
|
199
|
+
match (str) {
|
|
200
|
+
"." => Dot,
|
|
201
|
+
".." => Dotdot,
|
|
202
|
+
_ when String.length(str) == 2 && String.charAt(1, str) == ':' =>
|
|
203
|
+
DriveTok(String.charAt(0, str)),
|
|
204
|
+
_ => Text(str),
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
let parseNextToken = (path: PathInfo, nextToken) => {
|
|
209
|
+
let (base, ft, subs) = path
|
|
210
|
+
match (nextToken) {
|
|
211
|
+
Slash | Dot => path,
|
|
212
|
+
DriveTok(label) => (base, ft, [Char.toString(label) ++ ":", ...subs]),
|
|
213
|
+
Text(str) => (base, ft, [str, ...subs]),
|
|
214
|
+
Dotdot => {
|
|
215
|
+
match (path) {
|
|
216
|
+
(_, _, [_, ...rest]) => (base, ft, rest),
|
|
217
|
+
(Rel(upDirs), _, []) => (Rel(upDirs + 1), ft, []),
|
|
218
|
+
(Abs(_), _, []) => path,
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// splits a path on forward slashes
|
|
225
|
+
let lexPath = (pathStr, platform) => {
|
|
226
|
+
let isSeparator = char => {
|
|
227
|
+
char == '/' || platform == Windows && char == '\\'
|
|
228
|
+
}
|
|
229
|
+
let len = String.length(pathStr)
|
|
230
|
+
let mut revTokens = []
|
|
231
|
+
let mut segBeginI = 0
|
|
232
|
+
for (let mut i = 0; i < len; i += 1) {
|
|
233
|
+
if (isSeparator(String.charAt(i, pathStr))) {
|
|
234
|
+
if (segBeginI != i) {
|
|
235
|
+
let tok = makeToken(String.slice(segBeginI, i, pathStr))
|
|
236
|
+
revTokens = [tok, ...revTokens]
|
|
237
|
+
}
|
|
238
|
+
revTokens = [Slash, ...revTokens]
|
|
239
|
+
segBeginI = i + 1
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (segBeginI < len) {
|
|
243
|
+
let lastPart = String.slice(segBeginI, len, pathStr)
|
|
244
|
+
revTokens = [makeToken(lastPart), ...revTokens]
|
|
245
|
+
}
|
|
246
|
+
List.reverse(revTokens)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
let isFilePath = tokens => {
|
|
250
|
+
let revTokens = List.reverse(tokens)
|
|
251
|
+
match (revTokens) {
|
|
252
|
+
[Dot | Dotdot | Slash, ..._] => false,
|
|
253
|
+
_ => true,
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// utility functions to translate path types
|
|
258
|
+
|
|
259
|
+
let toTyped = (pathInfo: PathInfo) => {
|
|
260
|
+
let (base, fileType, subs) = pathInfo
|
|
261
|
+
({ base, }, { fileType, }, subs): TypedPath<a, b>
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
let toUntyped = (typedPath: TypedPath<a, b>) => {
|
|
265
|
+
let (base, fileType, subs) = typedPath
|
|
266
|
+
let { base } = base
|
|
267
|
+
let { fileType } = fileType
|
|
268
|
+
(base, fileType, subs): PathInfo
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
let pathInfo = (path: Path) => {
|
|
272
|
+
match (path) {
|
|
273
|
+
AbsoluteDir(x) => toUntyped(x),
|
|
274
|
+
AbsoluteFile(x) => toUntyped(x),
|
|
275
|
+
RelativeDir(x) => toUntyped(x),
|
|
276
|
+
RelativeFile(x) => toUntyped(x),
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
let toPath = (path: PathInfo) => {
|
|
281
|
+
match (path) {
|
|
282
|
+
(Abs(_), File, _) as p => AbsoluteFile(toTyped(p)),
|
|
283
|
+
(Abs(_), Dir, _) as p => AbsoluteDir(toTyped(p)),
|
|
284
|
+
(Rel(_), File, _) as p => RelativeFile(toTyped(p)),
|
|
285
|
+
(Rel(_), Dir, _) as p => RelativeDir(toTyped(p)),
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
let parseAbs = (tokens, fileType) => {
|
|
290
|
+
match (tokens) {
|
|
291
|
+
[] => None,
|
|
292
|
+
[first, ...rest] as tokens => {
|
|
293
|
+
if (fileType == File && !isFilePath(tokens)) {
|
|
294
|
+
None
|
|
295
|
+
} else {
|
|
296
|
+
let init = match (first) {
|
|
297
|
+
Slash => Some((Abs(Root), fileType, [])),
|
|
298
|
+
DriveTok(label) => Some((Abs(Drive(label)), fileType, [])),
|
|
299
|
+
_ => None,
|
|
300
|
+
}
|
|
301
|
+
Option.map(init => List.reduce(parseNextToken, init, rest), init)
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// TODO(#1496): expose these functions once module system added
|
|
308
|
+
|
|
309
|
+
let absoluteFile = tokens => {
|
|
310
|
+
let pathOpt = parseAbs(tokens, File)
|
|
311
|
+
Option.map(toTyped, pathOpt): Option<TypedPath<Absolute, File>>
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
let absoluteDir = tokens => {
|
|
315
|
+
let pathOpt = parseAbs(tokens, Dir)
|
|
316
|
+
Option.map(toTyped, pathOpt): Option<TypedPath<Absolute, Directory>>
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
let parseRel = (tokens, fileType) => {
|
|
320
|
+
let (first, rest) = match (tokens) {
|
|
321
|
+
[] => (Dot, []),
|
|
322
|
+
[first, ...rest] => (first, rest),
|
|
323
|
+
}
|
|
324
|
+
let tokens = [first, ...rest]
|
|
325
|
+
|
|
326
|
+
if (fileType == File && !isFilePath(tokens)) {
|
|
327
|
+
None
|
|
328
|
+
} else {
|
|
329
|
+
let init = match (first) {
|
|
330
|
+
Dot => Some((Rel(0), fileType, [])),
|
|
331
|
+
Dotdot => Some((Rel(1), fileType, [])),
|
|
332
|
+
Text(str) => Some((Rel(0), fileType, [str])),
|
|
333
|
+
_ => None,
|
|
334
|
+
}
|
|
335
|
+
Option.map(init => List.reduce(parseNextToken, init, rest), init)
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
let relativeFile = tokens => {
|
|
340
|
+
let pathOpt = parseRel(tokens, File)
|
|
341
|
+
Option.map(toTyped, pathOpt): Option<TypedPath<Relative, File>>
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
let relativeDir = tokens => {
|
|
345
|
+
let pathOpt = parseRel(tokens, Dir)
|
|
346
|
+
Option.map(toTyped, pathOpt): Option<TypedPath<Relative, Directory>>
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// TODO(#1496): reuse this and the other helper functions for the typed implementations
|
|
350
|
+
let fromStringHelper = (pathStr, platform) => {
|
|
351
|
+
let tokens = match (lexPath(pathStr, platform)) {
|
|
352
|
+
// will cause empty strings to get parsed as relative directory '.'
|
|
353
|
+
[] => [Dot],
|
|
354
|
+
tokens => tokens,
|
|
355
|
+
}
|
|
356
|
+
let isAbs = match (tokens) {
|
|
357
|
+
[Slash | DriveTok(_), ..._] => true,
|
|
358
|
+
_ => false,
|
|
359
|
+
}
|
|
360
|
+
let isDir = !isFilePath(tokens)
|
|
361
|
+
|
|
362
|
+
let path = (variant, mkPath, pathType) => {
|
|
363
|
+
variant(
|
|
364
|
+
Option.expect("Impossible: failed parse of " ++ pathType, mkPath(tokens))
|
|
365
|
+
)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
match ((isAbs, isDir)) {
|
|
369
|
+
(true, true) => path(AbsoluteDir, absoluteDir, "absolute dir"),
|
|
370
|
+
(true, false) => path(AbsoluteFile, absoluteFile, "absolute file"),
|
|
371
|
+
(false, true) => path(RelativeDir, relativeDir, "relative dir"),
|
|
372
|
+
(false, false) => path(RelativeFile, relativeFile, "relative file"),
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Parses a path string into a `Path`. Paths will be parsed as file paths
|
|
378
|
+
* rather than directory paths if there is ambiguity.
|
|
379
|
+
*
|
|
380
|
+
* @param pathStr: The string to parse as a path
|
|
381
|
+
* @returns The path wrapped with details encoded within the type
|
|
382
|
+
*
|
|
383
|
+
* @example fromString("/bin/") // an absolute Path referencing the directory /bin/
|
|
384
|
+
* @example fromString("file.txt") // a relative Path referencing the file ./file.txt
|
|
385
|
+
* @example fromString(".") // a relative Path referencing the current directory
|
|
386
|
+
*
|
|
387
|
+
* @since v0.5.5
|
|
388
|
+
*/
|
|
389
|
+
export let fromString = pathStr => {
|
|
390
|
+
fromStringHelper(pathStr, Posix)
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Parses a path string into a `Path` using the path separators appropriate to
|
|
395
|
+
* the given platform (`/` for `Posix` and either `/` or `\` for `Windows`).
|
|
396
|
+
* Paths will be parsed as file paths rather than directory paths if there is
|
|
397
|
+
* ambiguity.
|
|
398
|
+
*
|
|
399
|
+
* @param pathStr: The string to parse as a path
|
|
400
|
+
* @param platform: The platform whose path separators should be used for parsing
|
|
401
|
+
* @returns The path wrapped with details encoded within the type
|
|
402
|
+
*
|
|
403
|
+
* @example fromPlatformString("/bin/", Posix) // an absolute Path referencing the directory /bin/
|
|
404
|
+
* @example fromPlatformString("C:\\file.txt", Windows) // a relative Path referencing the file C:\file.txt
|
|
405
|
+
*
|
|
406
|
+
* @since v0.5.5
|
|
407
|
+
*/
|
|
408
|
+
export let fromPlatformString = (pathStr, platform) => {
|
|
409
|
+
fromStringHelper(pathStr, platform)
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
let toStringHelper = (path, platform) => {
|
|
413
|
+
let (base, fileType, revSegs) = path
|
|
414
|
+
let sep = match (platform) {
|
|
415
|
+
Windows => "\\",
|
|
416
|
+
Posix => "/",
|
|
417
|
+
}
|
|
418
|
+
let segs = List.reverse(revSegs)
|
|
419
|
+
let segs = match (base) {
|
|
420
|
+
Abs(absFrom) => {
|
|
421
|
+
let prefix = match (absFrom) {
|
|
422
|
+
Root => "",
|
|
423
|
+
Drive(label) => Char.toString(label) ++ ":",
|
|
424
|
+
}
|
|
425
|
+
[prefix, ...segs]
|
|
426
|
+
},
|
|
427
|
+
Rel(upDirs) => {
|
|
428
|
+
let goUp = List.init(upDirs, (_) => "..")
|
|
429
|
+
if (goUp != []) List.append(goUp, segs) else [".", ...segs]
|
|
430
|
+
},
|
|
431
|
+
}
|
|
432
|
+
let segs = match (fileType) {
|
|
433
|
+
File => segs,
|
|
434
|
+
Dir => List.append(segs, [""]),
|
|
435
|
+
}
|
|
436
|
+
List.join(sep, segs)
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Converts the given `Path` into a string, using the `/` path separator.
|
|
441
|
+
* A trailing slash is added to directory paths.
|
|
442
|
+
*
|
|
443
|
+
* @param path: The path to convert to a string
|
|
444
|
+
* @returns A string representing the given path
|
|
445
|
+
*
|
|
446
|
+
* @example toString(fromString("/file.txt")) == "/file.txt"
|
|
447
|
+
* @example toString(fromString("dir/")) == "./dir/"
|
|
448
|
+
*
|
|
449
|
+
* @since v0.5.5
|
|
450
|
+
*/
|
|
451
|
+
export let toString = path => {
|
|
452
|
+
toStringHelper(pathInfo(path), Posix)
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Converts the given `Path` into a string, using the canonical path separator
|
|
457
|
+
* appropriate to the given platform (`/` for `Posix` and `\` for `Windows`).
|
|
458
|
+
* A trailing slash is added to directory paths.
|
|
459
|
+
*
|
|
460
|
+
* @param path: The path to convert to a string
|
|
461
|
+
* @param platform: The `Platform` to use to represent the path as a string
|
|
462
|
+
* @returns A string representing the given path
|
|
463
|
+
*
|
|
464
|
+
* @example toPlatformString(fromString("dir/"), Posix) == "./dir/"
|
|
465
|
+
* @example toPlatformString(fromString("C:/file.txt"), Windows) == "C:\\file.txt"
|
|
466
|
+
*
|
|
467
|
+
* @since v0.5.5
|
|
468
|
+
*/
|
|
469
|
+
export let toPlatformString = (path, platform) => {
|
|
470
|
+
toStringHelper(pathInfo(path), platform)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Determines whether the path is a directory path.
|
|
475
|
+
*
|
|
476
|
+
* @param path: The path to inspect
|
|
477
|
+
* @returns `true` if the path is a directory path or `false` otherwise
|
|
478
|
+
*
|
|
479
|
+
* @example isDirectory(fromString("file.txt")) == false
|
|
480
|
+
* @example isDirectory(fromString("/bin/")) == true
|
|
481
|
+
*
|
|
482
|
+
* @since v0.5.5
|
|
483
|
+
*/
|
|
484
|
+
export let isDirectory = path => {
|
|
485
|
+
let (_, fileType, _) = pathInfo(path)
|
|
486
|
+
fileType == Dir
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Determines whether the path is an absolute path.
|
|
491
|
+
*
|
|
492
|
+
* @param path: The path to inspect
|
|
493
|
+
* @returns `true` if the path is absolute or `false` otherwise
|
|
494
|
+
*
|
|
495
|
+
* @example isAbsolute(fromString("/Users/me")) == true
|
|
496
|
+
* @example isAbsolute(fromString("./file.txt")) == false
|
|
497
|
+
*/
|
|
498
|
+
export let isAbsolute = path => {
|
|
499
|
+
let (base, _, _) = pathInfo(path)
|
|
500
|
+
match (base) {
|
|
501
|
+
Abs(_) => true,
|
|
502
|
+
_ => false,
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// should only be used on relative path appended to directory path
|
|
507
|
+
let rec appendHelper = (path: PathInfo, toAppend: PathInfo) =>
|
|
508
|
+
match (toAppend) {
|
|
509
|
+
(Rel(up2), ft, s2) =>
|
|
510
|
+
match (path) {
|
|
511
|
+
(Rel(up1), _, []) => (Rel(up1 + up2), ft, s2),
|
|
512
|
+
(Abs(_) as d, _, []) => (d, ft, s2),
|
|
513
|
+
(d, pft, [_, ...rest] as s1) => {
|
|
514
|
+
if (up2 > 0) appendHelper((d, pft, rest), (Rel(up2 - 1), ft, s2))
|
|
515
|
+
else (d, ft, List.append(s2, s1))
|
|
516
|
+
},
|
|
517
|
+
},
|
|
518
|
+
(Abs(_), _, _) => fail "Impossible: relative path encoded as absolute path",
|
|
519
|
+
}: PathInfo
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Creates a new path by appending a relative path segment to a directory path.
|
|
523
|
+
*
|
|
524
|
+
* @param path: The base path
|
|
525
|
+
* @param toAppend: The relative path to append
|
|
526
|
+
* @returns `Ok(path)` combining the base and appended paths or `Err(err)` if the paths are incompatible
|
|
527
|
+
*
|
|
528
|
+
* @example append(fromString("./dir/"), fromString("file.txt")) == Ok(fromString("./dir/file.txt"))
|
|
529
|
+
* @example append(fromString("a.txt"), fromString("b.sh")) == Err(AppendToFile) // cannot append to file path
|
|
530
|
+
* @example append(fromString("./dir/"), fromString("/dir2")) == Err(AppendAbsolute) // cannot append an absolute path
|
|
531
|
+
*
|
|
532
|
+
* @since v0.5.5
|
|
533
|
+
*/
|
|
534
|
+
export let append = (path: Path, toAppend: Path) => {
|
|
535
|
+
match ((pathInfo(path), pathInfo(toAppend))) {
|
|
536
|
+
((_, File, _), _) => Err(AppendToFile),
|
|
537
|
+
(_, (Abs(_), _, _)) => Err(AppendAbsolute),
|
|
538
|
+
(pathInfo1, pathInfo2) => Ok(toPath(appendHelper(pathInfo1, pathInfo2))),
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
let dirsUp = x => if (x == 0) Zero else Positive
|
|
543
|
+
|
|
544
|
+
// helper function for relativizing paths; handles the correct number of
|
|
545
|
+
// directories to "go up" from one path to another
|
|
546
|
+
let rec relativizeDepth = ((up1, s1), (up2, s2)) =>
|
|
547
|
+
match ((dirsUp(up1), dirsUp(up2), s1, s2)) {
|
|
548
|
+
(Zero, Zero, [hd1, ...tl1], [hd2, ...tl2]) when hd1 == hd2 =>
|
|
549
|
+
relativizeDepth((0, tl1), (0, tl2)),
|
|
550
|
+
(Zero, Zero, [], _) => Ok((up2, s2)),
|
|
551
|
+
(Zero, Zero, _, _) => Ok((List.length(s1), s2)),
|
|
552
|
+
(Positive, Positive, _, _) => relativizeDepth((up1 - 1, s1), (up2 - 1, s2)),
|
|
553
|
+
(Zero, Positive, _, _) => Ok((List.length(s1) + up2, s2)),
|
|
554
|
+
(Positive, Zero, _, _) => Err(ImpossibleRelativization),
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
let relativeToHelper = (source: PathInfo, dest: PathInfo) => {
|
|
558
|
+
// first branch handles special case of two identical file paths; to return
|
|
559
|
+
// '../<name>' instead of '.' (a directory path) because the result file type
|
|
560
|
+
// is expected to be the same as the second arg
|
|
561
|
+
let result = match ((source, dest)) {
|
|
562
|
+
((_, File, [name, ..._]), _) when source == dest => Ok((1, [name])),
|
|
563
|
+
((Abs(r1), _, s1), (Abs(r2), _, s2)) =>
|
|
564
|
+
if (r1 != r2) Err(Incompatible(DifferentRoots))
|
|
565
|
+
else relativizeDepth((0, List.reverse(s1)), (0, List.reverse(s2))),
|
|
566
|
+
((Rel(up1), _, s1), (Rel(up2), _, s2)) =>
|
|
567
|
+
relativizeDepth((up1, List.reverse(s1)), (up2, List.reverse(s2))),
|
|
568
|
+
_ => fail "Impossible: paths should have both been absolute or relative",
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
let (_, fileType, _) = dest
|
|
572
|
+
match (result) {
|
|
573
|
+
Ok((depth, segs)) => Ok((Rel(depth), fileType, List.reverse(segs))),
|
|
574
|
+
Err(err) => Err(err),
|
|
575
|
+
}: Result<PathInfo, RelativizationError>
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* Attempts to construct a new relative path which will lead to the destination
|
|
580
|
+
* path from the source path.
|
|
581
|
+
*
|
|
582
|
+
* If the source and destination are incompatible in their bases, the result
|
|
583
|
+
* will be `Err(IncompatibilityError)`.
|
|
584
|
+
*
|
|
585
|
+
* If the route to the destination cannot be concretely determined from the
|
|
586
|
+
* source, the result will be `Err(ImpossibleRelativization)`.
|
|
587
|
+
*
|
|
588
|
+
* @param source: The source path
|
|
589
|
+
* @param dest: The destination path to resolve
|
|
590
|
+
* @returns `Ok(path)` containing the relative path if successfully resolved or `Err(err)` otherwise
|
|
591
|
+
*
|
|
592
|
+
* @example relativeTo(fromString("/usr"), fromString("/usr/bin")) == Ok(fromString("./bin"))
|
|
593
|
+
* @example relativeTo(fromString("/home/me"), fromString("/home/me")) == Ok(fromString("."))
|
|
594
|
+
* @example relativeTo(fromString("/file.txt"), fromString("/etc/")) == Ok(fromString("../etc/"))
|
|
595
|
+
* @example relativeTo(fromString(".."), fromString("../../thing")) Ok(fromString("../thing"))
|
|
596
|
+
* @example relativeTo(fromString("/usr/bin"), fromString("C:/Users")) == Err(Incompatible(DifferentRoots))
|
|
597
|
+
* @example relativeTo(fromString("../here"), fromString("./there")) == Err(ImpossibleRelativization)
|
|
598
|
+
*
|
|
599
|
+
* @since v0.5.5
|
|
600
|
+
*/
|
|
601
|
+
export let relativeTo = (source, dest) => {
|
|
602
|
+
let pathInfo1 = pathInfo(source)
|
|
603
|
+
let (base1, _, _) = pathInfo1
|
|
604
|
+
let pathInfo2 = pathInfo(dest)
|
|
605
|
+
let (base2, _, _) = pathInfo2
|
|
606
|
+
match ((base1, base2)) {
|
|
607
|
+
(Abs(_), Rel(_)) | (Abs(_), Rel(_)) => Err(Incompatible(DifferentBases)),
|
|
608
|
+
_ => Result.map(toPath, relativeToHelper(pathInfo1, pathInfo2)),
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
let rec segsAncestry = (baseSegs, pathSegs) =>
|
|
613
|
+
match ((baseSegs, pathSegs)) {
|
|
614
|
+
([], []) => Self,
|
|
615
|
+
([], _) => Descendant,
|
|
616
|
+
(_, []) => Ancestor,
|
|
617
|
+
([first1, ..._], [first2, ..._]) when first1 != first2 => NoLineage,
|
|
618
|
+
([_, ...rest1], [_, ...rest2]) => segsAncestry(rest1, rest2),
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// should be used on paths with same absolute/relativeness
|
|
622
|
+
let ancestryHelper = (base: PathInfo, path: PathInfo) => {
|
|
623
|
+
let (b1, _, s1) = base
|
|
624
|
+
let (b2, _, s2) = path
|
|
625
|
+
match ((b1, b2)) {
|
|
626
|
+
(Abs(d1), Abs(d2)) when d1 != d2 => Err(DifferentRoots),
|
|
627
|
+
_ => Ok(segsAncestry(List.reverse(s1), List.reverse(s2))),
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Determines the relative ancestry betwen two paths.
|
|
633
|
+
*
|
|
634
|
+
* @param base: The first path to consider
|
|
635
|
+
* @param path: The second path to consider
|
|
636
|
+
* @returns `Ok(ancestryStatus)` with the relative ancestry between the paths if they are compatible or `Err(err)` if they are incompatible
|
|
637
|
+
*
|
|
638
|
+
* @example ancestry(fromString("/usr"), fromString("/usr/bin/bash")) == Ok(Ancestor)
|
|
639
|
+
* @example ancestry(fromString("/Users/me"), fromString("/Users")) == Ok(Descendant)
|
|
640
|
+
* @example ancestry(fromString("/usr"), fromString("/etc")) == Ok(Neither)
|
|
641
|
+
* @example ancestry(fromString("C:/dir1"), fromString("/dir2")) == Err(DifferentRoots)
|
|
642
|
+
*
|
|
643
|
+
* @since v0.5.5
|
|
644
|
+
*/
|
|
645
|
+
export let ancestry = (path1: Path, path2: Path) => {
|
|
646
|
+
let pathInfo1 = pathInfo(path1)
|
|
647
|
+
let (base1, _, _) = pathInfo1
|
|
648
|
+
let pathInfo2 = pathInfo(path2)
|
|
649
|
+
let (base2, _, _) = pathInfo2
|
|
650
|
+
match ((base1, base2)) {
|
|
651
|
+
(Rel(_), Abs(_)) | (Abs(_), Rel(_)) => Err(DifferentBases),
|
|
652
|
+
_ => ancestryHelper(pathInfo1, pathInfo2),
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
let parentHelper = (path: PathInfo) =>
|
|
657
|
+
match (path) {
|
|
658
|
+
(base, _, [_, ...rest]) => (base, Dir, rest),
|
|
659
|
+
(Rel(upDirs), _, []) => (Rel(upDirs + 1), Dir, []),
|
|
660
|
+
(Abs(_) as base, _, []) => (base, Dir, []),
|
|
661
|
+
}: PathInfo
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* Retrieves the path corresponding to the parent directory of the given path.
|
|
665
|
+
*
|
|
666
|
+
* @param path: The path to inspect
|
|
667
|
+
* @returns A path corresponding to the parent directory of the given path
|
|
668
|
+
*
|
|
669
|
+
* @example parent(fromString("./dir/inner")) == fromString("./dir/")
|
|
670
|
+
* @example parent(fromString("/")) == fromString("/")
|
|
671
|
+
*
|
|
672
|
+
* @since v0.5.5
|
|
673
|
+
*/
|
|
674
|
+
export let parent = (path: Path) => {
|
|
675
|
+
toPath(parentHelper(pathInfo(path)))
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
let basenameHelper = (path: PathInfo) =>
|
|
679
|
+
match (path) {
|
|
680
|
+
(_, _, [name, ..._]) => Some(name),
|
|
681
|
+
_ => None,
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Retrieves the basename (named final segment) of a path.
|
|
686
|
+
*
|
|
687
|
+
* @param path: The path to inspect
|
|
688
|
+
* @returns `Some(path)` containing the basename of the path or `None` if the path does not have one
|
|
689
|
+
*
|
|
690
|
+
* @example basename(fromString("./dir/file.txt")) == Some("file.txt")
|
|
691
|
+
* @example basename(fromString(".."))) == None
|
|
692
|
+
*
|
|
693
|
+
* @since v0.5.5
|
|
694
|
+
*/
|
|
695
|
+
export let basename = (path: Path) => {
|
|
696
|
+
basenameHelper(pathInfo(path))
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// should only be used on file paths
|
|
700
|
+
let stemExtHelper = (path: PathInfo) =>
|
|
701
|
+
match (path) {
|
|
702
|
+
(_, _, [name, ..._]) => {
|
|
703
|
+
let len = String.length(name)
|
|
704
|
+
// trim first character (which is possibly a .) off as trick for
|
|
705
|
+
// splitting .a.b.c into .a, .b.c
|
|
706
|
+
match (String.indexOf(".", String.slice(1, len, name))) {
|
|
707
|
+
Some(dotI) => {
|
|
708
|
+
let dotI = dotI + 1
|
|
709
|
+
(String.slice(0, dotI, name), String.slice(dotI, len, name))
|
|
710
|
+
},
|
|
711
|
+
None => (name, ""),
|
|
712
|
+
}
|
|
713
|
+
},
|
|
714
|
+
_ => ("", ""),
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* Retrieves the basename of a file path without the extension.
|
|
719
|
+
*
|
|
720
|
+
* @param path: The path to inspect
|
|
721
|
+
* @returns `Ok(path)` containing the stem of the file path or `Err(err)` if the path is a directory path
|
|
722
|
+
*
|
|
723
|
+
* @example stem(fromString("file.txt")) == Ok("file")
|
|
724
|
+
* @example stem(fromString(".gitignore")) == Ok(".gitignore")
|
|
725
|
+
* @example stem(fromString(".a.tar.gz")) == Ok(".a")
|
|
726
|
+
* @example stem(fromString("/dir/")) == Err(IncompatiblePathType) // can only take stem of a file path
|
|
727
|
+
*
|
|
728
|
+
* @since v0.5.5
|
|
729
|
+
*/
|
|
730
|
+
export let stem = (path: Path) => {
|
|
731
|
+
match (pathInfo(path)) {
|
|
732
|
+
(_, Dir, _) => Err(IncompatiblePathType),
|
|
733
|
+
pathInfo => {
|
|
734
|
+
let (stem, _) = stemExtHelper(pathInfo)
|
|
735
|
+
Ok(stem)
|
|
736
|
+
},
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
/**
|
|
741
|
+
* Retrieves the extension on the basename of a file path.
|
|
742
|
+
*
|
|
743
|
+
* @param path: The path to inspect
|
|
744
|
+
* @returns `Ok(path)` containing the extension of the file path or `Err(err)` if the path is a directory path
|
|
745
|
+
*
|
|
746
|
+
* @example extension(fromString("file.txt")) == Ok(".txt")
|
|
747
|
+
* @example extension(fromString(".gitignore")) == Ok("")
|
|
748
|
+
* @example extension(fromString(".a.tar.gz")) == Ok(".tar.gz")
|
|
749
|
+
* @example extension(fromString("/dir/")) == Err(IncompatiblePathType) // can only take extension of a file path
|
|
750
|
+
*
|
|
751
|
+
* @since v0.5.5
|
|
752
|
+
*/
|
|
753
|
+
export let extension = (path: Path) => {
|
|
754
|
+
match (pathInfo(path)) {
|
|
755
|
+
(_, Dir, _) => Err(IncompatiblePathType),
|
|
756
|
+
pathInfo => {
|
|
757
|
+
let (_, ext) = stemExtHelper(pathInfo)
|
|
758
|
+
Ok(ext)
|
|
759
|
+
},
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
// should only be used on absolute paths
|
|
764
|
+
let rootHelper = (path: PathInfo) =>
|
|
765
|
+
match (path) {
|
|
766
|
+
(Abs(root), _, _) => root,
|
|
767
|
+
_ => fail "Impossible: malformed absolute path data",
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
/**
|
|
771
|
+
* Retrieves the root of the absolute path.
|
|
772
|
+
*
|
|
773
|
+
* @param path: The path to inspect
|
|
774
|
+
* @returns `Ok(root)` containing the root of the path or `Err(err)` if the path is a relative path
|
|
775
|
+
*
|
|
776
|
+
* @example root(fromString("C:/Users/me/")) == Ok(Drive('C'))
|
|
777
|
+
* @example root(fromString("/home/me/")) == Ok(Root)
|
|
778
|
+
* @example root(fromString("./file.txt")) == Err(IncompatiblePathType)
|
|
779
|
+
*
|
|
780
|
+
* @since v0.5.5
|
|
781
|
+
*/
|
|
782
|
+
export let root = (path: Path) => {
|
|
783
|
+
match (pathInfo(path)) {
|
|
784
|
+
(Rel(_), _, _) => Err(IncompatiblePathType),
|
|
785
|
+
pathInfo => Ok(rootHelper(pathInfo)),
|
|
786
|
+
}
|
|
787
|
+
}
|