goscript 0.0.28 → 0.0.29

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 (73) hide show
  1. package/compiler/analysis.go +41 -2
  2. package/compiler/expr-call.go +87 -15
  3. package/compiler/expr-selector.go +100 -0
  4. package/compiler/spec-value.go +79 -8
  5. package/compiler/spec.go +236 -0
  6. package/dist/gs/builtin/builtin.d.ts +1 -0
  7. package/dist/gs/builtin/builtin.js +11 -0
  8. package/dist/gs/builtin/builtin.js.map +1 -1
  9. package/dist/gs/internal/oserror/errors.d.ts +6 -0
  10. package/dist/gs/internal/oserror/errors.js +7 -0
  11. package/dist/gs/internal/oserror/errors.js.map +1 -0
  12. package/dist/gs/internal/oserror/index.d.ts +1 -0
  13. package/dist/gs/internal/oserror/index.js +2 -0
  14. package/dist/gs/internal/oserror/index.js.map +1 -0
  15. package/dist/gs/io/fs/format.d.ts +3 -0
  16. package/dist/gs/io/fs/format.js +56 -0
  17. package/dist/gs/io/fs/format.js.map +1 -0
  18. package/dist/gs/io/fs/fs.d.ts +79 -0
  19. package/dist/gs/io/fs/fs.js +200 -0
  20. package/dist/gs/io/fs/fs.js.map +1 -0
  21. package/dist/gs/io/fs/glob.d.ts +10 -0
  22. package/dist/gs/io/fs/glob.js +141 -0
  23. package/dist/gs/io/fs/glob.js.map +1 -0
  24. package/dist/gs/io/fs/index.d.ts +8 -0
  25. package/dist/gs/io/fs/index.js +9 -0
  26. package/dist/gs/io/fs/index.js.map +1 -0
  27. package/dist/gs/io/fs/readdir.d.ts +7 -0
  28. package/dist/gs/io/fs/readdir.js +152 -0
  29. package/dist/gs/io/fs/readdir.js.map +1 -0
  30. package/dist/gs/io/fs/readfile.d.ts +6 -0
  31. package/dist/gs/io/fs/readfile.js +118 -0
  32. package/dist/gs/io/fs/readfile.js.map +1 -0
  33. package/dist/gs/io/fs/stat.d.ts +6 -0
  34. package/dist/gs/io/fs/stat.js +87 -0
  35. package/dist/gs/io/fs/stat.js.map +1 -0
  36. package/dist/gs/io/fs/sub.d.ts +6 -0
  37. package/dist/gs/io/fs/sub.js +172 -0
  38. package/dist/gs/io/fs/sub.js.map +1 -0
  39. package/dist/gs/io/fs/walk.d.ts +7 -0
  40. package/dist/gs/io/fs/walk.js +76 -0
  41. package/dist/gs/io/fs/walk.js.map +1 -0
  42. package/dist/gs/path/index.d.ts +2 -0
  43. package/dist/gs/path/index.js +3 -0
  44. package/dist/gs/path/index.js.map +1 -0
  45. package/dist/gs/path/match.d.ts +6 -0
  46. package/dist/gs/path/match.js +281 -0
  47. package/dist/gs/path/match.js.map +1 -0
  48. package/dist/gs/path/path.d.ts +7 -0
  49. package/dist/gs/path/path.js +256 -0
  50. package/dist/gs/path/path.js.map +1 -0
  51. package/dist/gs/time/time.d.ts +11 -2
  52. package/dist/gs/time/time.js +337 -12
  53. package/dist/gs/time/time.js.map +1 -1
  54. package/gs/builtin/builtin.ts +13 -0
  55. package/gs/internal/oserror/errors.ts +14 -0
  56. package/gs/internal/oserror/index.ts +1 -0
  57. package/gs/io/fs/format.ts +65 -0
  58. package/gs/io/fs/fs.ts +359 -0
  59. package/gs/io/fs/glob.ts +167 -0
  60. package/gs/io/fs/godoc.txt +35 -0
  61. package/gs/io/fs/index.ts +8 -0
  62. package/gs/io/fs/readdir.ts +126 -0
  63. package/gs/io/fs/readfile.ts +77 -0
  64. package/gs/io/fs/stat.ts +38 -0
  65. package/gs/io/fs/sub.ts +208 -0
  66. package/gs/io/fs/walk.ts +89 -0
  67. package/gs/path/index.ts +2 -0
  68. package/gs/path/match.ts +307 -0
  69. package/gs/path/path.ts +301 -0
  70. package/gs/strings/reader.test.ts +0 -1
  71. package/gs/time/time.ts +325 -12
  72. package/package.json +1 -1
  73. package/gs/time/godoc.md +0 -116
@@ -0,0 +1,307 @@
1
+ import * as $ from "@goscript/builtin/builtin.js";
2
+
3
+ import * as errors from "@goscript/errors/index.js"
4
+
5
+ import * as utf8 from "@goscript/unicode/utf8/index.js"
6
+
7
+ export let ErrBadPattern: $.GoError = errors.New("syntax error in pattern")
8
+
9
+ // Match reports whether name matches the shell pattern.
10
+ // The pattern syntax is:
11
+ //
12
+ // pattern:
13
+ // { term }
14
+ // term:
15
+ // '*' matches any sequence of non-/ characters
16
+ // '?' matches any single non-/ character
17
+ // '[' [ '^' ] { character-range } ']'
18
+ // character class (must be non-empty)
19
+ // c matches character c (c != '*', '?', '\\', '[')
20
+ // '\\' c matches character c
21
+ //
22
+ // character-range:
23
+ // c matches character c (c != '\\', '-', ']')
24
+ // '\\' c matches character c
25
+ // lo '-' hi matches character c for lo <= c <= hi
26
+ //
27
+ // Match requires pattern to match all of name, not just a substring.
28
+ // The only possible returned error is [ErrBadPattern], when pattern
29
+ // is malformed.
30
+ export function Match(pattern: string, name: string): [boolean, $.GoError] {
31
+ let matched: boolean = false
32
+ let err: $.GoError = null
33
+ {
34
+
35
+ // Trailing * matches rest of string unless it has a /.
36
+
37
+ // Look for match at current position.
38
+
39
+ // if we're the last chunk, make sure we've exhausted the name
40
+ // otherwise we'll give a false result even if we could still match
41
+ // using the star
42
+
43
+ // Look for match skipping i+1 bytes.
44
+ // Cannot skip /.
45
+
46
+ // if we're the last chunk, make sure we exhausted the name
47
+
48
+ // Before returning false with no error,
49
+ // check that the remainder of the pattern is syntactically valid.
50
+ Pattern: for (; $.len(pattern) > 0; ) {
51
+ let star: boolean = false
52
+ let chunk: string = ""
53
+ let rest: string = ""
54
+ let scanResult = scanChunk(pattern)
55
+ star = scanResult[0]
56
+ chunk = scanResult[1]
57
+ rest = scanResult[2]
58
+ pattern = rest
59
+
60
+ // Trailing * matches rest of string unless it has a /.
61
+ if (star && chunk == "") {
62
+ // Trailing * matches rest of string unless it has a /.
63
+ return [name.indexOf("/") < 0, null]
64
+ }
65
+ // Look for match at current position.
66
+ let [t, ok, err] = matchChunk(chunk, name)
67
+ // if we're the last chunk, make sure we've exhausted the name
68
+ // otherwise we'll give a false result even if we could still match
69
+ // using the star
70
+ if (ok && ($.len(t) == 0 || $.len(pattern) > 0)) {
71
+ name = t
72
+ continue
73
+ }
74
+ if (err != null) {
75
+ return [false, err]
76
+ }
77
+
78
+ // Look for match skipping i+1 bytes.
79
+ // Cannot skip /.
80
+
81
+ // if we're the last chunk, make sure we exhausted the name
82
+ if (star) {
83
+ // Look for match skipping i+1 bytes.
84
+ // Cannot skip /.
85
+
86
+ // if we're the last chunk, make sure we exhausted the name
87
+ for (let i = 0; i < $.len(name) && $.indexString(name, i) != 47; i++) {
88
+ let [t, ok, err] = matchChunk(chunk, $.sliceString(name, i, undefined))
89
+
90
+ // if we're the last chunk, make sure we exhausted the name
91
+ if (ok) {
92
+ // if we're the last chunk, make sure we exhausted the name
93
+ if ($.len(pattern) == 0 && $.len(t) > 0) {
94
+ continue
95
+ }
96
+ name = t
97
+ continue Pattern
98
+ }
99
+ if (err != null) {
100
+ return [false, err]
101
+ }
102
+ }
103
+ }
104
+ // Before returning false with no error,
105
+ // check that the remainder of the pattern is syntactically valid.
106
+ for (; $.len(pattern) > 0; ) {
107
+ let star2: boolean = false
108
+ let chunk2: string = ""
109
+ let rest2: string = ""
110
+ let scanResult2 = scanChunk(pattern)
111
+ star2 = scanResult2[0]
112
+ chunk2 = scanResult2[1]
113
+ rest2 = scanResult2[2]
114
+ pattern = rest2
115
+ {
116
+ let [, , err] = matchChunk(chunk2, "")
117
+ if (err != null) {
118
+ return [false, err]
119
+ }
120
+ }
121
+ }
122
+ return [false, null]
123
+ }
124
+ return [$.len(name) == 0, null]
125
+ }
126
+ }
127
+
128
+ // scanChunk gets the next segment of pattern, which is a non-star string
129
+ // possibly preceded by a star.
130
+ export function scanChunk(pattern: string): [boolean, string, string] {
131
+ let star: boolean = false
132
+ let chunk: string = ""
133
+ let rest: string = ""
134
+ {
135
+ for (; $.len(pattern) > 0 && $.indexString(pattern, 0) == 42; ) {
136
+ pattern = $.sliceString(pattern, 1, undefined)
137
+ star = true
138
+ }
139
+ let inrange = false
140
+ let i: number = 0
141
+
142
+ // error check handled in matchChunk: bad pattern.
143
+ Scan: for (i = 0; i < $.len(pattern); i++) {
144
+
145
+ // error check handled in matchChunk: bad pattern.
146
+ switch ($.indexString(pattern, i)) {
147
+ case 92:
148
+ if (i + 1 < $.len(pattern)) {
149
+ i++
150
+ }
151
+ break
152
+ case 91:
153
+ inrange = true
154
+ break
155
+ case 93:
156
+ inrange = false
157
+ break
158
+ case 42:
159
+ if (!inrange) {
160
+ break Scan
161
+ }
162
+ break
163
+ }
164
+ }
165
+ return [star, $.sliceString(pattern, 0, i), $.sliceString(pattern, i, undefined)]
166
+ }
167
+ }
168
+
169
+ // matchChunk checks whether chunk matches the beginning of s.
170
+ // If so, it returns the remainder of s (after the match).
171
+ // Chunk is all single-character operators: literals, char classes, and ?.
172
+ export function matchChunk(chunk: string, s: string): [string, boolean, $.GoError] {
173
+ let rest: string = ""
174
+ let ok: boolean = false
175
+ let err: $.GoError = null
176
+ {
177
+ // failed records whether the match has failed.
178
+ // After the match fails, the loop continues on processing chunk,
179
+ // checking that the pattern is well-formed but no longer reading s.
180
+ let failed = false
181
+
182
+ // character class
183
+
184
+ // possibly negated
185
+
186
+ // parse all ranges
187
+ for (; $.len(chunk) > 0; ) {
188
+ if (!failed && $.len(s) == 0) {
189
+ failed = true
190
+ }
191
+
192
+ // character class
193
+
194
+ // possibly negated
195
+
196
+ // parse all ranges
197
+ switch ($.indexString(chunk, 0)) {
198
+ case 91:
199
+ let r: number = 0
200
+ if (!failed) {
201
+ let n: number = 0
202
+ let decoded = utf8.DecodeRuneInString(s)
203
+ r = decoded[0]
204
+ n = decoded[1]
205
+ s = $.sliceString(s, n, undefined)
206
+ }
207
+ chunk = $.sliceString(chunk, 1, undefined)
208
+ let negated = false
209
+ if ($.len(chunk) > 0 && $.indexString(chunk, 0) == 94) {
210
+ negated = true
211
+ chunk = $.sliceString(chunk, 1, undefined)
212
+ }
213
+ let match = false
214
+ let nrange = 0
215
+ for (; ; ) {
216
+ if ($.len(chunk) > 0 && $.indexString(chunk, 0) == 93 && nrange > 0) {
217
+ chunk = $.sliceString(chunk, 1, undefined)
218
+ break
219
+ }
220
+ let lo: number = 0
221
+ let hi: number = 0
222
+ {
223
+ let escResult = getEsc(chunk)
224
+ lo = escResult[0]
225
+ chunk = escResult[1]
226
+ err = escResult[2]
227
+ if (err != null) {
228
+ return ["", false, err]
229
+ }
230
+ }
231
+ hi = lo
232
+ if ($.indexString(chunk, 0) == 45) {
233
+ {
234
+ let escResult2 = getEsc($.sliceString(chunk, 1, undefined))
235
+ hi = escResult2[0]
236
+ chunk = escResult2[1]
237
+ err = escResult2[2]
238
+ if (err != null) {
239
+ return ["", false, err]
240
+ }
241
+ }
242
+ }
243
+ if (lo <= r && r <= hi) {
244
+ match = true
245
+ }
246
+ nrange++
247
+ }
248
+ if (match == negated) {
249
+ failed = true
250
+ }
251
+ break
252
+ case 63:
253
+ if (!failed) {
254
+ if ($.indexString(s, 0) == 47) {
255
+ failed = true
256
+ }
257
+ let [, n] = utf8.DecodeRuneInString(s)
258
+ s = $.sliceString(s, n, undefined)
259
+ }
260
+ chunk = $.sliceString(chunk, 1, undefined)
261
+ break
262
+ case 92:
263
+ chunk = $.sliceString(chunk, 1, undefined)
264
+ if ($.len(chunk) == 0) {
265
+ return ["", false, ErrBadPattern]
266
+ }
267
+ // unhandled branch statement token: fallthrough
268
+ break
269
+ default:
270
+ if (!failed) {
271
+ if ($.indexString(chunk, 0) != $.indexString(s, 0)) {
272
+ failed = true
273
+ }
274
+ s = $.sliceString(s, 1, undefined)
275
+ }
276
+ chunk = $.sliceString(chunk, 1, undefined)
277
+ break
278
+ }
279
+ }
280
+ if (failed) {
281
+ return ["", false, null]
282
+ }
283
+ return [s, true, null]
284
+ }
285
+ }
286
+
287
+ export function getEsc(chunk: string): [number, string, $.GoError] {
288
+ if ($.len(chunk) == 0 || $.indexString(chunk, 0) == 45 || $.indexString(chunk, 0) == 93) {
289
+ return [0, "", ErrBadPattern]
290
+ }
291
+ if ($.indexString(chunk, 0) == 92) {
292
+ chunk = $.sliceString(chunk, 1, undefined)
293
+ if ($.len(chunk) == 0) {
294
+ return [0, "", ErrBadPattern]
295
+ }
296
+ }
297
+ let [r, n] = utf8.DecodeRuneInString(chunk)
298
+ if (r == utf8.RuneError && n == 1) {
299
+ return [0, "", ErrBadPattern]
300
+ }
301
+ chunk = $.sliceString(chunk, n, undefined)
302
+ if ($.len(chunk) == 0 || $.indexString(chunk, 0) != 45 || $.len(chunk) == 1) {
303
+ return [r, chunk, null]
304
+ }
305
+ return [r, chunk, null]
306
+ }
307
+
@@ -0,0 +1,301 @@
1
+ import * as $ from "@goscript/builtin/builtin.js";
2
+
3
+ class lazybuf {
4
+ public get s(): string {
5
+ return this._fields.s.value
6
+ }
7
+ public set s(value: string) {
8
+ this._fields.s.value = value
9
+ }
10
+
11
+ public get buf(): Uint8Array | null {
12
+ return this._fields.buf.value
13
+ }
14
+ public set buf(value: Uint8Array | null) {
15
+ this._fields.buf.value = value
16
+ }
17
+
18
+ public get w(): number {
19
+ return this._fields.w.value
20
+ }
21
+ public set w(value: number) {
22
+ this._fields.w.value = value
23
+ }
24
+
25
+ public _fields: {
26
+ s: $.VarRef<string>;
27
+ buf: $.VarRef<Uint8Array | null>;
28
+ w: $.VarRef<number>;
29
+ }
30
+
31
+ constructor(init?: Partial<{buf?: Uint8Array, s?: string, w?: number}>) {
32
+ this._fields = {
33
+ s: $.varRef(init?.s ?? ""),
34
+ buf: $.varRef(init?.buf ?? null),
35
+ w: $.varRef(init?.w ?? 0)
36
+ }
37
+ }
38
+
39
+ public clone(): lazybuf {
40
+ const cloned = new lazybuf()
41
+ cloned._fields = {
42
+ s: $.varRef(this._fields.s.value),
43
+ buf: $.varRef(this._fields.buf.value),
44
+ w: $.varRef(this._fields.w.value)
45
+ }
46
+ return cloned
47
+ }
48
+
49
+ public index(i: number): number {
50
+ const b = this
51
+ if (b!.buf != null) {
52
+ return b!.buf![i]
53
+ }
54
+ return $.indexString(b!.s, i)
55
+ }
56
+
57
+ public append(c: number): void {
58
+ const b = this
59
+ if (b!.buf == null) {
60
+ if (b!.w < $.len(b!.s) && $.indexString(b!.s, b!.w) == c) {
61
+ b!.w++
62
+ return
63
+ }
64
+ b!.buf = new Uint8Array($.len(b!.s))
65
+ $.copy(b!.buf, $.stringToBytes($.sliceString(b!.s, undefined, b!.w)))
66
+ }
67
+ b!.buf![b!.w] = c
68
+ b!.w++
69
+ }
70
+
71
+ public _string(): string {
72
+ const b = this
73
+ if (b!.buf == null) {
74
+ return $.sliceString(b!.s, undefined, b!.w)
75
+ }
76
+ return $.bytesToString(b!.buf.subarray(0, b!.w))
77
+ }
78
+
79
+ // Register this type with the runtime type system
80
+ static __typeInfo = $.registerStructType(
81
+ 'lazybuf',
82
+ new lazybuf(),
83
+ [{ name: "index", args: [{ name: "i", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [{ type: { kind: $.TypeKind.Basic, name: "number" } }] }, { name: "append", args: [{ name: "c", type: { kind: $.TypeKind.Basic, name: "number" } }], returns: [] }, { name: "string", args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: "string" } }] }],
84
+ lazybuf,
85
+ {"s": { kind: $.TypeKind.Basic, name: "string" }, "buf": { kind: $.TypeKind.Slice, elemType: { kind: $.TypeKind.Basic, name: "number" } }, "w": { kind: $.TypeKind.Basic, name: "number" }}
86
+ );
87
+ }
88
+
89
+ // Clean returns the shortest path name equivalent to path
90
+ // by purely lexical processing. It applies the following rules
91
+ // iteratively until no further processing can be done:
92
+ //
93
+ // 1. Replace multiple slashes with a single slash.
94
+ // 2. Eliminate each . path name element (the current directory).
95
+ // 3. Eliminate each inner .. path name element (the parent directory)
96
+ // along with the non-.. element that precedes it.
97
+ // 4. Eliminate .. elements that begin a rooted path:
98
+ // that is, replace "/.." by "/" at the beginning of a path.
99
+ //
100
+ // The returned path ends in a slash only if it is the root "/".
101
+ //
102
+ // If the result of this process is an empty string, Clean
103
+ // returns the string ".".
104
+ //
105
+ // See also Rob Pike, "Lexical File Names in Plan 9 or
106
+ // Getting Dot-Dot Right,"
107
+ // https://9p.io/sys/doc/lexnames.html
108
+ export function Clean(path: string): string {
109
+ if (path == "") {
110
+ return "."
111
+ }
112
+
113
+ let rooted = $.indexString(path, 0) == 47
114
+ let n = $.len(path)
115
+
116
+ // Invariants:
117
+ // reading from path; r is index of next byte to process.
118
+ // writing to buf; w is index of next byte to write.
119
+ // dotdot is index in buf where .. must stop, either because
120
+ // it is the leading slash or it is a leading ../../.. prefix.
121
+ let out = new lazybuf({s: path})
122
+ let r = 0
123
+ let dotdot = 0
124
+ if (rooted) {
125
+ out.append(47)
126
+ r = 1
127
+ dotdot = 1
128
+ }
129
+
130
+ // empty path element
131
+
132
+ // . element
133
+
134
+ // .. element: remove to last /
135
+
136
+ // can backtrack
137
+
138
+ // cannot backtrack, but not rooted, so append .. element.
139
+
140
+ // real path element.
141
+ // add slash if needed
142
+
143
+ // copy element
144
+ for (; r < n; ) {
145
+
146
+ // empty path element
147
+
148
+ // . element
149
+
150
+ // .. element: remove to last /
151
+
152
+ // can backtrack
153
+
154
+ // cannot backtrack, but not rooted, so append .. element.
155
+
156
+ // real path element.
157
+ // add slash if needed
158
+
159
+ // copy element
160
+ switch (true) {
161
+ case $.indexString(path, r) == 47:
162
+ r++
163
+ break
164
+ case $.indexString(path, r) == 46 && (r + 1 == n || $.indexString(path, r + 1) == 47):
165
+ r++
166
+ break
167
+ case $.indexString(path, r) == 46 && $.indexString(path, r + 1) == 46 && (r + 2 == n || $.indexString(path, r + 2) == 47):
168
+ r += 2
169
+ switch (true) {
170
+ case out.w > dotdot:
171
+ out.w--
172
+ for (; out.w > dotdot && out.index(out.w) != 47; ) {
173
+ out.w--
174
+ }
175
+ break
176
+ case !rooted:
177
+ if (out.w > 0) {
178
+ out.append(47)
179
+ }
180
+ out.append(46)
181
+ out.append(46)
182
+ dotdot = out.w
183
+ break
184
+ }
185
+ break
186
+ default:
187
+ if (rooted && out.w != 1 || !rooted && out.w != 0) {
188
+ out.append(47)
189
+ }
190
+ for (; r < n && $.indexString(path, r) != 47; r++) {
191
+ out.append($.indexString(path, r))
192
+ }
193
+ break
194
+ }
195
+ }
196
+
197
+ // Turn empty string into "."
198
+ if (out.w == 0) {
199
+ return "."
200
+ }
201
+
202
+ return out._string()
203
+ }
204
+
205
+ // Split splits path immediately following the final slash,
206
+ // separating it into a directory and file name component.
207
+ // If there is no slash in path, Split returns an empty dir and
208
+ // file set to path.
209
+ // The returned values have the property that path = dir+file.
210
+ export function Split(path: string): [string, string] {
211
+ let i = path.lastIndexOf("/")
212
+ return [$.sliceString(path, undefined, i + 1), $.sliceString(path, i + 1, undefined)]
213
+ }
214
+
215
+ // Join joins any number of path elements into a single path,
216
+ // separating them with slashes. Empty elements are ignored.
217
+ // The result is Cleaned. However, if the argument list is
218
+ // empty or all its elements are empty, Join returns
219
+ // an empty string.
220
+ export function Join(...elem: string[]): string {
221
+ let size = 0
222
+ for (let _i = 0; _i < $.len(elem); _i++) {
223
+ const e = elem![_i]
224
+ {
225
+ size += $.len(e)
226
+ }
227
+ }
228
+ if (size == 0) {
229
+ return ""
230
+ }
231
+ let buf: string[] = []
232
+ for (let _i = 0; _i < $.len(elem); _i++) {
233
+ const e = elem![_i]
234
+ {
235
+ if ($.len(buf) > 0 || e != "") {
236
+ if ($.len(buf) > 0) {
237
+ buf.push("/")
238
+ }
239
+ buf.push(e)
240
+ }
241
+ }
242
+ }
243
+ return Clean(buf.join(""))
244
+ }
245
+
246
+ // Ext returns the file name extension used by path.
247
+ // The extension is the suffix beginning at the final dot
248
+ // in the final slash-separated element of path;
249
+ // it is empty if there is no dot.
250
+ export function Ext(path: string): string {
251
+ for (let i = $.len(path) - 1; i >= 0 && $.indexString(path, i) != 47; i--) {
252
+ if ($.indexString(path, i) == 46) {
253
+ return $.sliceString(path, i, undefined)
254
+ }
255
+ }
256
+ return ""
257
+ }
258
+
259
+ // Base returns the last element of path.
260
+ // Trailing slashes are removed before extracting the last element.
261
+ // If the path is empty, Base returns ".".
262
+ // If the path consists entirely of slashes, Base returns "/".
263
+ export function Base(path: string): string {
264
+ if (path == "") {
265
+ return "."
266
+ }
267
+ // Strip trailing slashes.
268
+ for (; $.len(path) > 0 && $.indexString(path, $.len(path) - 1) == 47; ) {
269
+ path = $.sliceString(path, 0, $.len(path) - 1)
270
+ }
271
+ // Find the last element
272
+ {
273
+ let i = path.lastIndexOf("/")
274
+ if (i >= 0) {
275
+ path = $.sliceString(path, i + 1, undefined)
276
+ }
277
+ }
278
+ // If empty now, it had only slashes.
279
+ if (path == "") {
280
+ return "/"
281
+ }
282
+ return path
283
+ }
284
+
285
+ // IsAbs reports whether the path is absolute.
286
+ export function IsAbs(path: string): boolean {
287
+ return $.len(path) > 0 && $.indexString(path, 0) == 47
288
+ }
289
+
290
+ // Dir returns all but the last element of path, typically the path's directory.
291
+ // After dropping the final element using [Split], the path is Cleaned and trailing
292
+ // slashes are removed.
293
+ // If the path is empty, Dir returns ".".
294
+ // If the path consists entirely of slashes followed by non-slash bytes, Dir
295
+ // returns a single slash. In any other case, the returned path does not end in a
296
+ // slash.
297
+ export function Dir(path: string): string {
298
+ let [dir, ] = Split(path)
299
+ return Clean(dir)
300
+ }
301
+
@@ -1,5 +1,4 @@
1
1
  import { describe, it, expect } from 'vitest'
2
- import * as $ from '@goscript/builtin/builtin.js'
3
2
  import { Reader, NewReader } from './reader.js'
4
3
  import * as io from '@goscript/io/index.js'
5
4