goscript 0.0.26 → 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 (228) hide show
  1. package/README.md +4 -4
  2. package/cmd/goscript/cmd_compile.go +0 -3
  3. package/cmd/goscript/deps.go +11 -0
  4. package/compiler/analysis.go +298 -55
  5. package/compiler/assignment.go +2 -2
  6. package/compiler/builtin_test.go +1 -1
  7. package/compiler/compiler.go +200 -68
  8. package/compiler/compiler_test.go +17 -24
  9. package/compiler/composite-lit.go +32 -8
  10. package/compiler/decl.go +6 -6
  11. package/compiler/expr-call.go +170 -15
  12. package/compiler/expr-selector.go +100 -0
  13. package/compiler/expr.go +1 -1
  14. package/compiler/protobuf.go +557 -0
  15. package/compiler/spec-struct.go +4 -0
  16. package/compiler/spec-value.go +89 -10
  17. package/compiler/spec.go +254 -1
  18. package/compiler/stmt-assign.go +35 -0
  19. package/compiler/type-assert.go +87 -0
  20. package/compiler/type.go +4 -1
  21. package/dist/gs/builtin/builtin.d.ts +20 -1
  22. package/dist/gs/builtin/builtin.js +95 -4
  23. package/dist/gs/builtin/builtin.js.map +1 -1
  24. package/dist/gs/builtin/slice.d.ts +1 -1
  25. package/dist/gs/builtin/slice.js +21 -2
  26. package/dist/gs/builtin/slice.js.map +1 -1
  27. package/dist/gs/errors/errors.d.ts +5 -6
  28. package/dist/gs/errors/errors.js.map +1 -1
  29. package/dist/gs/internal/oserror/errors.d.ts +6 -0
  30. package/dist/gs/internal/oserror/errors.js +7 -0
  31. package/dist/gs/internal/oserror/errors.js.map +1 -0
  32. package/dist/gs/internal/oserror/index.d.ts +1 -0
  33. package/dist/gs/internal/oserror/index.js +2 -0
  34. package/dist/gs/internal/oserror/index.js.map +1 -0
  35. package/dist/gs/io/fs/format.d.ts +3 -0
  36. package/dist/gs/io/fs/format.js +56 -0
  37. package/dist/gs/io/fs/format.js.map +1 -0
  38. package/dist/gs/io/fs/fs.d.ts +79 -0
  39. package/dist/gs/io/fs/fs.js +200 -0
  40. package/dist/gs/io/fs/fs.js.map +1 -0
  41. package/dist/gs/io/fs/glob.d.ts +10 -0
  42. package/dist/gs/io/fs/glob.js +141 -0
  43. package/dist/gs/io/fs/glob.js.map +1 -0
  44. package/dist/gs/io/fs/index.d.ts +8 -0
  45. package/dist/gs/io/fs/index.js +9 -0
  46. package/dist/gs/io/fs/index.js.map +1 -0
  47. package/dist/gs/io/fs/readdir.d.ts +7 -0
  48. package/dist/gs/io/fs/readdir.js +152 -0
  49. package/dist/gs/io/fs/readdir.js.map +1 -0
  50. package/dist/gs/io/fs/readfile.d.ts +6 -0
  51. package/dist/gs/io/fs/readfile.js +118 -0
  52. package/dist/gs/io/fs/readfile.js.map +1 -0
  53. package/dist/gs/io/fs/stat.d.ts +6 -0
  54. package/dist/gs/io/fs/stat.js +87 -0
  55. package/dist/gs/io/fs/stat.js.map +1 -0
  56. package/dist/gs/io/fs/sub.d.ts +6 -0
  57. package/dist/gs/io/fs/sub.js +172 -0
  58. package/dist/gs/io/fs/sub.js.map +1 -0
  59. package/dist/gs/io/fs/walk.d.ts +7 -0
  60. package/dist/gs/io/fs/walk.js +76 -0
  61. package/dist/gs/io/fs/walk.js.map +1 -0
  62. package/dist/gs/io/index.d.ts +1 -0
  63. package/dist/gs/io/index.js +2 -0
  64. package/dist/gs/io/index.js.map +1 -0
  65. package/dist/gs/io/io.d.ts +107 -0
  66. package/dist/gs/io/io.js +385 -0
  67. package/dist/gs/io/io.js.map +1 -0
  68. package/dist/gs/path/index.d.ts +2 -0
  69. package/dist/gs/path/index.js +3 -0
  70. package/dist/gs/path/index.js.map +1 -0
  71. package/dist/gs/path/match.d.ts +6 -0
  72. package/dist/gs/path/match.js +281 -0
  73. package/dist/gs/path/match.js.map +1 -0
  74. package/dist/gs/path/path.d.ts +7 -0
  75. package/dist/gs/path/path.js +256 -0
  76. package/dist/gs/path/path.js.map +1 -0
  77. package/dist/gs/strings/builder.d.ts +18 -0
  78. package/dist/gs/strings/builder.js +205 -0
  79. package/dist/gs/strings/builder.js.map +1 -0
  80. package/dist/gs/strings/clone.d.ts +1 -0
  81. package/dist/gs/strings/clone.js +16 -0
  82. package/dist/gs/strings/clone.js.map +1 -0
  83. package/dist/gs/strings/compare.d.ts +1 -0
  84. package/dist/gs/strings/compare.js +14 -0
  85. package/dist/gs/strings/compare.js.map +1 -0
  86. package/dist/gs/strings/index.d.ts +2 -0
  87. package/dist/gs/strings/index.js +3 -0
  88. package/dist/gs/strings/index.js.map +1 -0
  89. package/dist/gs/strings/iter.d.ts +8 -0
  90. package/dist/gs/strings/iter.js +160 -0
  91. package/dist/gs/strings/iter.js.map +1 -0
  92. package/dist/gs/strings/reader.d.ts +34 -0
  93. package/dist/gs/strings/reader.js +418 -0
  94. package/dist/gs/strings/reader.js.map +1 -0
  95. package/dist/gs/strings/replace.d.ts +106 -0
  96. package/dist/gs/strings/replace.js +1136 -0
  97. package/dist/gs/strings/replace.js.map +1 -0
  98. package/dist/gs/strings/search.d.ts +24 -0
  99. package/dist/gs/strings/search.js +169 -0
  100. package/dist/gs/strings/search.js.map +1 -0
  101. package/dist/gs/strings/strings.d.ts +47 -0
  102. package/dist/gs/strings/strings.js +418 -0
  103. package/dist/gs/strings/strings.js.map +1 -0
  104. package/dist/gs/stringslite/index.d.ts +1 -0
  105. package/dist/gs/stringslite/index.js +2 -0
  106. package/dist/gs/stringslite/index.js.map +1 -0
  107. package/dist/gs/stringslite/strings.d.ts +11 -0
  108. package/dist/gs/stringslite/strings.js +67 -0
  109. package/dist/gs/stringslite/strings.js.map +1 -0
  110. package/dist/gs/sync/index.d.ts +1 -0
  111. package/dist/gs/sync/index.js +2 -0
  112. package/dist/gs/sync/index.js.map +1 -0
  113. package/dist/gs/sync/sync.d.ts +79 -0
  114. package/dist/gs/sync/sync.js +392 -0
  115. package/dist/gs/sync/sync.js.map +1 -0
  116. package/dist/gs/time/time.d.ts +11 -2
  117. package/dist/gs/time/time.js +337 -12
  118. package/dist/gs/time/time.js.map +1 -1
  119. package/dist/gs/unicode/index.d.ts +1 -0
  120. package/dist/gs/unicode/index.js +2 -0
  121. package/dist/gs/unicode/index.js.map +1 -0
  122. package/dist/gs/unicode/unicode.d.ts +105 -0
  123. package/dist/gs/unicode/unicode.js +332 -0
  124. package/dist/gs/unicode/unicode.js.map +1 -0
  125. package/dist/gs/unicode/utf8/index.d.ts +1 -0
  126. package/dist/gs/unicode/utf8/index.js +3 -0
  127. package/dist/gs/unicode/utf8/index.js.map +1 -0
  128. package/dist/gs/unicode/utf8/utf8.d.ts +20 -0
  129. package/dist/gs/unicode/utf8/utf8.js +196 -0
  130. package/dist/gs/unicode/utf8/utf8.js.map +1 -0
  131. package/dist/gs/unsafe/index.d.ts +1 -0
  132. package/dist/gs/unsafe/index.js +2 -0
  133. package/dist/gs/unsafe/index.js.map +1 -0
  134. package/dist/gs/unsafe/unsafe.d.ts +11 -0
  135. package/dist/gs/unsafe/unsafe.js +44 -0
  136. package/dist/gs/unsafe/unsafe.js.map +1 -0
  137. package/go.mod +2 -1
  138. package/go.sum +6 -2
  139. package/gs/README.md +6 -0
  140. package/gs/builtin/builtin.ts +171 -0
  141. package/gs/builtin/channel.ts +683 -0
  142. package/gs/builtin/defer.ts +58 -0
  143. package/gs/builtin/index.ts +1 -0
  144. package/gs/builtin/io.ts +22 -0
  145. package/gs/builtin/map.ts +50 -0
  146. package/gs/builtin/slice.ts +1030 -0
  147. package/gs/builtin/type.ts +1106 -0
  148. package/gs/builtin/varRef.ts +25 -0
  149. package/gs/cmp/godoc.txt +8 -0
  150. package/gs/cmp/index.ts +29 -0
  151. package/gs/context/context.ts +401 -0
  152. package/gs/context/godoc.txt +69 -0
  153. package/gs/context/index.ts +1 -0
  154. package/gs/errors/errors.ts +223 -0
  155. package/gs/errors/godoc.txt +63 -0
  156. package/gs/errors/index.ts +1 -0
  157. package/gs/internal/goarch/godoc.txt +39 -0
  158. package/gs/internal/goarch/index.ts +18 -0
  159. package/gs/internal/oserror/errors.ts +14 -0
  160. package/gs/internal/oserror/index.ts +1 -0
  161. package/gs/io/fs/format.ts +65 -0
  162. package/gs/io/fs/fs.ts +359 -0
  163. package/gs/io/fs/glob.ts +167 -0
  164. package/gs/io/fs/godoc.txt +35 -0
  165. package/gs/io/fs/index.ts +8 -0
  166. package/gs/io/fs/readdir.ts +126 -0
  167. package/gs/io/fs/readfile.ts +77 -0
  168. package/gs/io/fs/stat.ts +38 -0
  169. package/gs/io/fs/sub.ts +208 -0
  170. package/gs/io/fs/walk.ts +89 -0
  171. package/gs/io/godoc.txt +61 -0
  172. package/gs/io/index.ts +1 -0
  173. package/gs/io/io.go +75 -0
  174. package/gs/io/io.ts +546 -0
  175. package/gs/iter/godoc.txt +203 -0
  176. package/gs/iter/index.ts +1 -0
  177. package/gs/iter/iter.ts +117 -0
  178. package/gs/math/bits/index.ts +356 -0
  179. package/gs/math/godoc.txt +76 -0
  180. package/gs/path/index.ts +2 -0
  181. package/gs/path/match.ts +307 -0
  182. package/gs/path/path.ts +301 -0
  183. package/gs/runtime/godoc.txt +331 -0
  184. package/gs/runtime/index.ts +1 -0
  185. package/gs/runtime/runtime.ts +178 -0
  186. package/gs/slices/godoc.txt +44 -0
  187. package/gs/slices/index.ts +1 -0
  188. package/gs/slices/slices.ts +22 -0
  189. package/gs/strings/builder.test.ts +121 -0
  190. package/gs/strings/builder.ts +223 -0
  191. package/gs/strings/clone.test.ts +43 -0
  192. package/gs/strings/clone.ts +17 -0
  193. package/gs/strings/compare.test.ts +84 -0
  194. package/gs/strings/compare.ts +13 -0
  195. package/gs/strings/godoc.txt +66 -0
  196. package/gs/strings/index.ts +2 -0
  197. package/gs/strings/iter.test.ts +343 -0
  198. package/gs/strings/iter.ts +171 -0
  199. package/gs/strings/reader.test.ts +242 -0
  200. package/gs/strings/reader.ts +451 -0
  201. package/gs/strings/replace.test.ts +181 -0
  202. package/gs/strings/replace.ts +1310 -0
  203. package/gs/strings/search.test.ts +214 -0
  204. package/gs/strings/search.ts +213 -0
  205. package/gs/strings/strings.test.ts +477 -0
  206. package/gs/strings/strings.ts +510 -0
  207. package/gs/stringslite/godoc.txt +17 -0
  208. package/gs/stringslite/index.ts +1 -0
  209. package/gs/stringslite/strings.ts +82 -0
  210. package/gs/sync/godoc.txt +21 -0
  211. package/gs/sync/index.ts +1 -0
  212. package/gs/sync/sync.go +64 -0
  213. package/gs/sync/sync.ts +449 -0
  214. package/gs/time/godoc.txt +116 -0
  215. package/gs/time/index.ts +1 -0
  216. package/gs/time/time.ts +585 -0
  217. package/gs/unicode/godoc.txt +52 -0
  218. package/gs/unicode/index.ts +1 -0
  219. package/gs/unicode/unicode.go +38 -0
  220. package/gs/unicode/unicode.ts +418 -0
  221. package/gs/unicode/utf8/godoc.txt +22 -0
  222. package/gs/unicode/utf8/index.ts +2 -0
  223. package/gs/unicode/utf8/utf8.ts +227 -0
  224. package/gs/unsafe/godoc.txt +19 -0
  225. package/gs/unsafe/index.ts +1 -0
  226. package/gs/unsafe/unsafe.test.ts +68 -0
  227. package/gs/unsafe/unsafe.ts +77 -0
  228. package/package.json +4 -3
@@ -0,0 +1,76 @@
1
+ package math // import "math"
2
+
3
+ Package math provides basic constants and mathematical functions.
4
+
5
+ This package does not guarantee bit-identical results across architectures.
6
+
7
+ const E = 2.71828182845904523536028747135266249775724709369995957496696763 ...
8
+ const MaxFloat32 = 0x1p127 * (1 + (1 - 0x1p-23)) ...
9
+ const MaxInt ...
10
+ func Abs(x float64) float64
11
+ func Acos(x float64) float64
12
+ func Acosh(x float64) float64
13
+ func Asin(x float64) float64
14
+ func Asinh(x float64) float64
15
+ func Atan(x float64) float64
16
+ func Atan2(y, x float64) float64
17
+ func Atanh(x float64) float64
18
+ func Cbrt(x float64) float64
19
+ func Ceil(x float64) float64
20
+ func Copysign(f, sign float64) float64
21
+ func Cos(x float64) float64
22
+ func Cosh(x float64) float64
23
+ func Dim(x, y float64) float64
24
+ func Erf(x float64) float64
25
+ func Erfc(x float64) float64
26
+ func Erfcinv(x float64) float64
27
+ func Erfinv(x float64) float64
28
+ func Exp(x float64) float64
29
+ func Exp2(x float64) float64
30
+ func Expm1(x float64) float64
31
+ func FMA(x, y, z float64) float64
32
+ func Float32bits(f float32) uint32
33
+ func Float32frombits(b uint32) float32
34
+ func Float64bits(f float64) uint64
35
+ func Float64frombits(b uint64) float64
36
+ func Floor(x float64) float64
37
+ func Frexp(f float64) (frac float64, exp int)
38
+ func Gamma(x float64) float64
39
+ func Hypot(p, q float64) float64
40
+ func Ilogb(x float64) int
41
+ func Inf(sign int) float64
42
+ func IsInf(f float64, sign int) bool
43
+ func IsNaN(f float64) (is bool)
44
+ func J0(x float64) float64
45
+ func J1(x float64) float64
46
+ func Jn(n int, x float64) float64
47
+ func Ldexp(frac float64, exp int) float64
48
+ func Lgamma(x float64) (lgamma float64, sign int)
49
+ func Log(x float64) float64
50
+ func Log10(x float64) float64
51
+ func Log1p(x float64) float64
52
+ func Log2(x float64) float64
53
+ func Logb(x float64) float64
54
+ func Max(x, y float64) float64
55
+ func Min(x, y float64) float64
56
+ func Mod(x, y float64) float64
57
+ func Modf(f float64) (int float64, frac float64)
58
+ func NaN() float64
59
+ func Nextafter(x, y float64) (r float64)
60
+ func Nextafter32(x, y float32) (r float32)
61
+ func Pow(x, y float64) float64
62
+ func Pow10(n int) float64
63
+ func Remainder(x, y float64) float64
64
+ func Round(x float64) float64
65
+ func RoundToEven(x float64) float64
66
+ func Signbit(x float64) bool
67
+ func Sin(x float64) float64
68
+ func Sincos(x float64) (sin, cos float64)
69
+ func Sinh(x float64) float64
70
+ func Sqrt(x float64) float64
71
+ func Tan(x float64) float64
72
+ func Tanh(x float64) float64
73
+ func Trunc(x float64) float64
74
+ func Y0(x float64) float64
75
+ func Y1(x float64) float64
76
+ func Yn(n int, x float64) float64
@@ -0,0 +1,2 @@
1
+ export { ErrBadPattern, Match } from "./match.js"
2
+ export { Base, Clean, Dir, Ext, IsAbs, Join, Split } from "./path.js"
@@ -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
+