goscript 0.0.25 → 0.0.28

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 (190) 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 +259 -55
  5. package/compiler/assignment.go +2 -2
  6. package/compiler/builtin_test.go +1 -1
  7. package/compiler/compiler.go +201 -49
  8. package/compiler/compiler_test.go +53 -0
  9. package/compiler/composite-lit.go +32 -8
  10. package/compiler/decl.go +6 -6
  11. package/compiler/expr-call.go +83 -0
  12. package/compiler/expr.go +1 -1
  13. package/compiler/protobuf.go +557 -0
  14. package/compiler/spec-struct.go +4 -0
  15. package/compiler/spec-value.go +11 -3
  16. package/compiler/spec.go +18 -1
  17. package/compiler/stmt-assign.go +35 -0
  18. package/compiler/type-assert.go +87 -0
  19. package/compiler/type.go +5 -2
  20. package/dist/gs/builtin/builtin.d.ts +19 -1
  21. package/dist/gs/builtin/builtin.js +85 -5
  22. package/dist/gs/builtin/builtin.js.map +1 -1
  23. package/dist/gs/builtin/channel.js.map +1 -1
  24. package/dist/gs/builtin/slice.d.ts +1 -1
  25. package/dist/gs/builtin/slice.js +59 -26
  26. package/dist/gs/builtin/slice.js.map +1 -1
  27. package/dist/gs/cmp/index.js.map +1 -1
  28. package/dist/gs/context/context.d.ts +1 -1
  29. package/dist/gs/context/context.js +20 -11
  30. package/dist/gs/context/context.js.map +1 -1
  31. package/dist/gs/errors/errors.d.ts +7 -0
  32. package/dist/gs/errors/errors.js +190 -0
  33. package/dist/gs/errors/errors.js.map +1 -0
  34. package/dist/gs/errors/index.d.ts +1 -0
  35. package/dist/gs/errors/index.js +2 -0
  36. package/dist/gs/errors/index.js.map +1 -0
  37. package/dist/gs/internal/goarch/index.js +1 -1
  38. package/dist/gs/internal/goarch/index.js.map +1 -1
  39. package/dist/gs/io/index.d.ts +1 -0
  40. package/dist/gs/io/index.js +2 -0
  41. package/dist/gs/io/index.js.map +1 -0
  42. package/dist/gs/io/io.d.ts +107 -0
  43. package/dist/gs/io/io.js +385 -0
  44. package/dist/gs/io/io.js.map +1 -0
  45. package/dist/gs/iter/iter.js.map +1 -1
  46. package/dist/gs/math/bits/index.js +34 -32
  47. package/dist/gs/math/bits/index.js.map +1 -1
  48. package/dist/gs/runtime/runtime.d.ts +1 -0
  49. package/dist/gs/runtime/runtime.js +15 -18
  50. package/dist/gs/runtime/runtime.js.map +1 -1
  51. package/dist/gs/slices/slices.d.ts +1 -1
  52. package/dist/gs/slices/slices.js +1 -1
  53. package/dist/gs/slices/slices.js.map +1 -1
  54. package/dist/gs/strings/builder.d.ts +18 -0
  55. package/dist/gs/strings/builder.js +205 -0
  56. package/dist/gs/strings/builder.js.map +1 -0
  57. package/dist/gs/strings/clone.d.ts +1 -0
  58. package/dist/gs/strings/clone.js +16 -0
  59. package/dist/gs/strings/clone.js.map +1 -0
  60. package/dist/gs/strings/compare.d.ts +1 -0
  61. package/dist/gs/strings/compare.js +14 -0
  62. package/dist/gs/strings/compare.js.map +1 -0
  63. package/dist/gs/strings/index.d.ts +2 -0
  64. package/dist/gs/strings/index.js +3 -0
  65. package/dist/gs/strings/index.js.map +1 -0
  66. package/dist/gs/strings/iter.d.ts +8 -0
  67. package/dist/gs/strings/iter.js +160 -0
  68. package/dist/gs/strings/iter.js.map +1 -0
  69. package/dist/gs/strings/reader.d.ts +34 -0
  70. package/dist/gs/strings/reader.js +418 -0
  71. package/dist/gs/strings/reader.js.map +1 -0
  72. package/dist/gs/strings/replace.d.ts +106 -0
  73. package/dist/gs/strings/replace.js +1136 -0
  74. package/dist/gs/strings/replace.js.map +1 -0
  75. package/dist/gs/strings/search.d.ts +24 -0
  76. package/dist/gs/strings/search.js +169 -0
  77. package/dist/gs/strings/search.js.map +1 -0
  78. package/dist/gs/strings/strings.d.ts +47 -0
  79. package/dist/gs/strings/strings.js +418 -0
  80. package/dist/gs/strings/strings.js.map +1 -0
  81. package/dist/gs/stringslite/index.d.ts +1 -0
  82. package/dist/gs/stringslite/index.js +2 -0
  83. package/dist/gs/stringslite/index.js.map +1 -0
  84. package/dist/gs/stringslite/strings.d.ts +11 -0
  85. package/dist/gs/stringslite/strings.js +67 -0
  86. package/dist/gs/stringslite/strings.js.map +1 -0
  87. package/dist/gs/sync/index.d.ts +1 -0
  88. package/dist/gs/sync/index.js +2 -0
  89. package/dist/gs/sync/index.js.map +1 -0
  90. package/dist/gs/sync/sync.d.ts +79 -0
  91. package/dist/gs/sync/sync.js +392 -0
  92. package/dist/gs/sync/sync.js.map +1 -0
  93. package/dist/gs/time/time.js +7 -7
  94. package/dist/gs/time/time.js.map +1 -1
  95. package/dist/gs/unicode/index.d.ts +1 -0
  96. package/dist/gs/unicode/index.js +2 -0
  97. package/dist/gs/unicode/index.js.map +1 -0
  98. package/dist/gs/unicode/unicode.d.ts +105 -0
  99. package/dist/gs/unicode/unicode.js +332 -0
  100. package/dist/gs/unicode/unicode.js.map +1 -0
  101. package/dist/gs/unicode/utf8/index.d.ts +1 -0
  102. package/dist/gs/unicode/utf8/index.js +3 -0
  103. package/dist/gs/unicode/utf8/index.js.map +1 -0
  104. package/dist/gs/unicode/utf8/utf8.d.ts +20 -0
  105. package/dist/gs/unicode/utf8/utf8.js +196 -0
  106. package/dist/gs/unicode/utf8/utf8.js.map +1 -0
  107. package/dist/gs/unsafe/index.d.ts +1 -0
  108. package/dist/gs/unsafe/index.js +2 -0
  109. package/dist/gs/unsafe/index.js.map +1 -0
  110. package/dist/gs/unsafe/unsafe.d.ts +11 -0
  111. package/dist/gs/unsafe/unsafe.js +44 -0
  112. package/dist/gs/unsafe/unsafe.js.map +1 -0
  113. package/go.mod +2 -1
  114. package/go.sum +6 -2
  115. package/gs/README.md +6 -0
  116. package/gs/builtin/builtin.ts +158 -0
  117. package/gs/builtin/channel.ts +683 -0
  118. package/gs/builtin/defer.ts +58 -0
  119. package/gs/builtin/index.ts +1 -0
  120. package/gs/builtin/io.ts +22 -0
  121. package/gs/builtin/map.ts +50 -0
  122. package/gs/builtin/slice.ts +1030 -0
  123. package/gs/builtin/type.ts +1106 -0
  124. package/gs/builtin/varRef.ts +25 -0
  125. package/gs/cmp/godoc.txt +8 -0
  126. package/gs/cmp/index.ts +29 -0
  127. package/gs/context/context.ts +401 -0
  128. package/gs/context/godoc.txt +69 -0
  129. package/gs/context/index.ts +1 -0
  130. package/gs/errors/errors.ts +223 -0
  131. package/gs/errors/godoc.txt +63 -0
  132. package/gs/errors/index.ts +1 -0
  133. package/gs/internal/goarch/godoc.txt +39 -0
  134. package/gs/internal/goarch/index.ts +18 -0
  135. package/gs/io/godoc.txt +61 -0
  136. package/gs/io/index.ts +1 -0
  137. package/gs/io/io.go +75 -0
  138. package/gs/io/io.ts +546 -0
  139. package/gs/iter/godoc.txt +203 -0
  140. package/gs/iter/index.ts +1 -0
  141. package/gs/iter/iter.ts +117 -0
  142. package/gs/math/bits/index.ts +356 -0
  143. package/gs/math/godoc.txt +76 -0
  144. package/gs/runtime/godoc.txt +331 -0
  145. package/gs/runtime/index.ts +1 -0
  146. package/gs/runtime/runtime.ts +178 -0
  147. package/gs/slices/godoc.txt +44 -0
  148. package/gs/slices/index.ts +1 -0
  149. package/gs/slices/slices.ts +22 -0
  150. package/gs/strings/builder.test.ts +121 -0
  151. package/gs/strings/builder.ts +223 -0
  152. package/gs/strings/clone.test.ts +43 -0
  153. package/gs/strings/clone.ts +17 -0
  154. package/gs/strings/compare.test.ts +84 -0
  155. package/gs/strings/compare.ts +13 -0
  156. package/gs/strings/godoc.txt +66 -0
  157. package/gs/strings/index.ts +2 -0
  158. package/gs/strings/iter.test.ts +343 -0
  159. package/gs/strings/iter.ts +171 -0
  160. package/gs/strings/reader.test.ts +243 -0
  161. package/gs/strings/reader.ts +451 -0
  162. package/gs/strings/replace.test.ts +181 -0
  163. package/gs/strings/replace.ts +1310 -0
  164. package/gs/strings/search.test.ts +214 -0
  165. package/gs/strings/search.ts +213 -0
  166. package/gs/strings/strings.test.ts +477 -0
  167. package/gs/strings/strings.ts +510 -0
  168. package/gs/stringslite/godoc.txt +17 -0
  169. package/gs/stringslite/index.ts +1 -0
  170. package/gs/stringslite/strings.ts +82 -0
  171. package/gs/sync/godoc.txt +21 -0
  172. package/gs/sync/index.ts +1 -0
  173. package/gs/sync/sync.go +64 -0
  174. package/gs/sync/sync.ts +449 -0
  175. package/gs/time/godoc.md +116 -0
  176. package/gs/time/godoc.txt +116 -0
  177. package/gs/time/index.ts +1 -0
  178. package/gs/time/time.ts +272 -0
  179. package/gs/unicode/godoc.txt +52 -0
  180. package/gs/unicode/index.ts +1 -0
  181. package/gs/unicode/unicode.go +38 -0
  182. package/gs/unicode/unicode.ts +418 -0
  183. package/gs/unicode/utf8/godoc.txt +22 -0
  184. package/gs/unicode/utf8/index.ts +2 -0
  185. package/gs/unicode/utf8/utf8.ts +227 -0
  186. package/gs/unsafe/godoc.txt +19 -0
  187. package/gs/unsafe/index.ts +1 -0
  188. package/gs/unsafe/unsafe.test.ts +68 -0
  189. package/gs/unsafe/unsafe.ts +77 -0
  190. package/package.json +6 -4
@@ -0,0 +1,418 @@
1
+ // Package unicode provides data and functions to test some properties of Unicode code points.
2
+
3
+ // Constants
4
+ export const MaxRune = 0x10ffff
5
+ export const ReplacementChar = 0xfffd
6
+ export const MaxASCII = 0x7f
7
+ export const MaxLatin1 = 0xff
8
+ export const Version = '15.0.0'
9
+
10
+ // Case constants
11
+ export const UpperCase = 0
12
+ export const LowerCase = 1
13
+ export const TitleCase = 2
14
+ export const MaxCase = 3
15
+
16
+ export const UpperLower = MaxRune + 1
17
+
18
+ // Range16 represents a range of 16-bit Unicode code points
19
+ export class Range16 {
20
+ public Lo: number
21
+ public Hi: number
22
+ public Stride: number
23
+
24
+ constructor(lo: number, hi: number, stride: number) {
25
+ this.Lo = lo
26
+ this.Hi = hi
27
+ this.Stride = stride
28
+ }
29
+
30
+ public clone(): Range16 {
31
+ return new Range16(this.Lo, this.Hi, this.Stride)
32
+ }
33
+ }
34
+
35
+ // Range32 represents a range of 32-bit Unicode code points
36
+ export class Range32 {
37
+ public Lo: number
38
+ public Hi: number
39
+ public Stride: number
40
+
41
+ constructor(lo: number, hi: number, stride: number) {
42
+ this.Lo = lo
43
+ this.Hi = hi
44
+ this.Stride = stride
45
+ }
46
+
47
+ public clone(): Range32 {
48
+ return new Range32(this.Lo, this.Hi, this.Stride)
49
+ }
50
+ }
51
+
52
+ // RangeTable defines a set of Unicode code points by listing the ranges of code points within the set
53
+ export class RangeTable {
54
+ public R16: Range16[]
55
+ public R32: Range32[]
56
+ public LatinOffset: number
57
+
58
+ constructor(
59
+ r16: Range16[] = [],
60
+ r32: Range32[] = [],
61
+ latinOffset: number = 0,
62
+ ) {
63
+ this.R16 = r16
64
+ this.R32 = r32
65
+ this.LatinOffset = latinOffset
66
+ }
67
+
68
+ public clone(): RangeTable {
69
+ return new RangeTable(
70
+ this.R16.map((r) => r.clone()),
71
+ this.R32.map((r) => r.clone()),
72
+ this.LatinOffset,
73
+ )
74
+ }
75
+ }
76
+
77
+ // CaseRange represents a range of Unicode code points for case mapping
78
+ export class CaseRange {
79
+ public Lo: number
80
+ public Hi: number
81
+ public Delta: [number, number, number]
82
+
83
+ constructor(lo: number, hi: number, delta: [number, number, number]) {
84
+ this.Lo = lo
85
+ this.Hi = hi
86
+ this.Delta = delta
87
+ }
88
+
89
+ public clone(): CaseRange {
90
+ return new CaseRange(this.Lo, this.Hi, [...this.Delta] as [
91
+ number,
92
+ number,
93
+ number,
94
+ ])
95
+ }
96
+ }
97
+
98
+ // SpecialCase represents language-specific case mappings
99
+ export type SpecialCase = CaseRange[]
100
+
101
+ // Basic character classification functions using JavaScript's built-in Unicode support
102
+
103
+ // IsControl reports whether the rune is a control character
104
+ export function IsControl(r: number): boolean {
105
+ // Control characters are in categories Cc, Cf, Co, Cs
106
+ if (r < 0 || r > MaxRune) return false
107
+ const char = String.fromCodePoint(r)
108
+ // Use regex to match control characters
109
+ return /[\p{Cc}\p{Cf}\p{Co}\p{Cs}]/u.test(char)
110
+ }
111
+
112
+ // IsDigit reports whether the rune is a decimal digit
113
+ export function IsDigit(r: number): boolean {
114
+ if (r < 0 || r > MaxRune) return false
115
+ const char = String.fromCodePoint(r)
116
+ return /\p{Nd}/u.test(char)
117
+ }
118
+
119
+ // IsGraphic reports whether the rune is defined as a Graphic by Unicode
120
+ export function IsGraphic(r: number): boolean {
121
+ if (r < 0 || r > MaxRune) return false
122
+ return IsLetter(r) || IsMark(r) || IsNumber(r) || IsPunct(r) || IsSymbol(r)
123
+ }
124
+
125
+ // IsLetter reports whether the rune is a letter (category L)
126
+ export function IsLetter(r: number): boolean {
127
+ if (r < 0 || r > MaxRune) return false
128
+ const char = String.fromCodePoint(r)
129
+ return /\p{L}/u.test(char)
130
+ }
131
+
132
+ // IsLower reports whether the rune is a lower case letter
133
+ export function IsLower(r: number): boolean {
134
+ if (r < 0 || r > MaxRune) return false
135
+ const char = String.fromCodePoint(r)
136
+ return /\p{Ll}/u.test(char)
137
+ }
138
+
139
+ // IsMark reports whether the rune is a mark character (category M)
140
+ export function IsMark(r: number): boolean {
141
+ if (r < 0 || r > MaxRune) return false
142
+ const char = String.fromCodePoint(r)
143
+ return /\p{M}/u.test(char)
144
+ }
145
+
146
+ // IsNumber reports whether the rune is a number (category N)
147
+ export function IsNumber(r: number): boolean {
148
+ if (r < 0 || r > MaxRune) return false
149
+ const char = String.fromCodePoint(r)
150
+ return /\p{N}/u.test(char)
151
+ }
152
+
153
+ // IsPrint reports whether the rune is defined as printable by Go
154
+ export function IsPrint(r: number): boolean {
155
+ if (r < 0 || r > MaxRune) return false
156
+ if (IsGraphic(r)) return true
157
+ return r === 0x20 // space character
158
+ }
159
+
160
+ // IsPunct reports whether the rune is a punctuation character (category P)
161
+ export function IsPunct(r: number): boolean {
162
+ if (r < 0 || r > MaxRune) return false
163
+ const char = String.fromCodePoint(r)
164
+ return /\p{P}/u.test(char)
165
+ }
166
+
167
+ // IsSpace reports whether the rune is a space character
168
+ export function IsSpace(r: number): boolean {
169
+ if (r < 0 || r > MaxRune) return false
170
+ const char = String.fromCodePoint(r)
171
+ return /\s/u.test(char) || /\p{Z}/u.test(char)
172
+ }
173
+
174
+ // IsSymbol reports whether the rune is a symbol character (category S)
175
+ export function IsSymbol(r: number): boolean {
176
+ if (r < 0 || r > MaxRune) return false
177
+ const char = String.fromCodePoint(r)
178
+ return /\p{S}/u.test(char)
179
+ }
180
+
181
+ // IsTitle reports whether the rune is a title case letter
182
+ export function IsTitle(r: number): boolean {
183
+ if (r < 0 || r > MaxRune) return false
184
+ const char = String.fromCodePoint(r)
185
+ return /\p{Lt}/u.test(char)
186
+ }
187
+
188
+ // IsUpper reports whether the rune is an upper case letter
189
+ export function IsUpper(r: number): boolean {
190
+ if (r < 0 || r > MaxRune) return false
191
+ const char = String.fromCodePoint(r)
192
+ return /\p{Lu}/u.test(char)
193
+ }
194
+
195
+ // Case conversion functions
196
+
197
+ // ToLower returns the lowercase mapping of the rune
198
+ export function ToLower(r: number): number {
199
+ if (r < 0 || r > MaxRune) return r
200
+ const char = String.fromCodePoint(r)
201
+ const lower = char.toLowerCase()
202
+ return lower.codePointAt(0) || r
203
+ }
204
+
205
+ // ToUpper returns the uppercase mapping of the rune
206
+ export function ToUpper(r: number): number {
207
+ if (r < 0 || r > MaxRune) return r
208
+ const char = String.fromCodePoint(r)
209
+ const upper = char.toUpperCase()
210
+ return upper.codePointAt(0) || r
211
+ }
212
+
213
+ // ToTitle returns the title case mapping of the rune
214
+ export function ToTitle(r: number): number {
215
+ // For most characters, title case is the same as uppercase
216
+ return ToUpper(r)
217
+ }
218
+
219
+ // To returns the case mapping of the rune
220
+ export function To(_case: number, r: number): number {
221
+ switch (_case) {
222
+ case UpperCase:
223
+ return ToUpper(r)
224
+ case LowerCase:
225
+ return ToLower(r)
226
+ case TitleCase:
227
+ return ToTitle(r)
228
+ default:
229
+ return r
230
+ }
231
+ }
232
+
233
+ // SimpleFold returns the next rune in the simple case folding sequence
234
+ export function SimpleFold(r: number): number {
235
+ if (r < 0 || r > MaxRune) return r
236
+
237
+ // Simple implementation - just toggle between upper and lower case
238
+ if (IsUpper(r)) {
239
+ return ToLower(r)
240
+ } else if (IsLower(r)) {
241
+ return ToUpper(r)
242
+ }
243
+ return r
244
+ }
245
+
246
+ // Is reports whether the rune is in the specified table of ranges
247
+ export function Is(rangeTab: RangeTable, r: number): boolean {
248
+ if (r < 0 || r > MaxRune) return false
249
+
250
+ // Check 16-bit ranges
251
+ for (const range of rangeTab.R16) {
252
+ if (r < range.Lo) break
253
+ if (r <= range.Hi) {
254
+ return range.Stride === 1 || (r - range.Lo) % range.Stride === 0
255
+ }
256
+ }
257
+
258
+ // Check 32-bit ranges
259
+ for (const range of rangeTab.R32) {
260
+ if (r < range.Lo) break
261
+ if (r <= range.Hi) {
262
+ return range.Stride === 1 || (r - range.Lo) % range.Stride === 0
263
+ }
264
+ }
265
+
266
+ return false
267
+ }
268
+
269
+ // In reports whether the rune is a member of one of the ranges
270
+ export function In(r: number, ...ranges: RangeTable[]): boolean {
271
+ for (const rangeTab of ranges) {
272
+ if (Is(rangeTab, r)) {
273
+ return true
274
+ }
275
+ }
276
+ return false
277
+ }
278
+
279
+ // IsOneOf reports whether the rune is a member of one of the ranges
280
+ export function IsOneOf(ranges: RangeTable[], r: number): boolean {
281
+ return In(r, ...ranges)
282
+ }
283
+
284
+ // Predefined range tables for common character categories
285
+ // These are simplified versions - in a full implementation, these would contain
286
+ // the complete Unicode range data
287
+
288
+ export const Letter = new RangeTable(
289
+ [new Range16(0x0041, 0x005a, 1), new Range16(0x0061, 0x007a, 1)], // Basic Latin letters
290
+ [],
291
+ )
292
+
293
+ export const Digit = new RangeTable(
294
+ [new Range16(0x0030, 0x0039, 1)], // ASCII digits
295
+ [],
296
+ )
297
+
298
+ export const Space = new RangeTable(
299
+ [new Range16(0x0009, 0x000d, 1), new Range16(0x0020, 0x0020, 1)], // Basic whitespace
300
+ [],
301
+ )
302
+
303
+ export const Upper = new RangeTable(
304
+ [new Range16(0x0041, 0x005a, 1)], // ASCII uppercase
305
+ [],
306
+ )
307
+
308
+ export const Lower = new RangeTable(
309
+ [new Range16(0x0061, 0x007a, 1)], // ASCII lowercase
310
+ [],
311
+ )
312
+
313
+ export const Title = new RangeTable([], [])
314
+
315
+ export const Punct = new RangeTable(
316
+ [
317
+ new Range16(0x0021, 0x002f, 1), // !"#$%&'()*+,-./
318
+ new Range16(0x003a, 0x0040, 1), // :;<=>?@
319
+ new Range16(0x005b, 0x0060, 1), // [\]^_`
320
+ new Range16(0x007b, 0x007e, 1), // {|}~
321
+ ],
322
+ [],
323
+ )
324
+
325
+ export const Symbol = new RangeTable([], [])
326
+
327
+ export const Mark = new RangeTable([], [])
328
+
329
+ export const Number = new RangeTable(
330
+ [new Range16(0x0030, 0x0039, 1)], // ASCII digits
331
+ [],
332
+ )
333
+
334
+ // Categories map
335
+ export const Categories = new Map<string, RangeTable>([
336
+ ['L', Letter],
337
+ ['Ll', Lower],
338
+ ['Lu', Upper],
339
+ ['Lt', Title],
340
+ ['M', Mark],
341
+ ['N', Number],
342
+ ['Nd', Digit],
343
+ ['P', Punct],
344
+ ['S', Symbol],
345
+ ['Z', Space],
346
+ ])
347
+
348
+ // Scripts and Properties maps (simplified)
349
+ export const Scripts = new Map<string, RangeTable>()
350
+ export const Properties = new Map<string, RangeTable>()
351
+ export const FoldCategory = new Map<string, RangeTable>()
352
+ export const FoldScript = new Map<string, RangeTable>()
353
+
354
+ // Graphic ranges
355
+ export const GraphicRanges = [Letter, Mark, Number, Punct, Symbol]
356
+
357
+ // Print ranges
358
+ export const PrintRanges = [Letter, Mark, Number, Punct, Symbol, Space]
359
+
360
+ // Case ranges (simplified)
361
+ export const CaseRanges: CaseRange[] = []
362
+
363
+ // Special cases
364
+ export const TurkishCase: SpecialCase = []
365
+ export const AzeriCase: SpecialCase = TurkishCase
366
+
367
+ // Predefined character categories (simplified implementations)
368
+ export const Cc = new RangeTable(
369
+ [new Range16(0x0000, 0x001f, 1), new Range16(0x007f, 0x009f, 1)],
370
+ [],
371
+ )
372
+ export const Cf = new RangeTable([], [])
373
+ export const Co = new RangeTable([], [])
374
+ export const Cs = new RangeTable([new Range16(0xd800, 0xdfff, 1)], [])
375
+ export const Lm = new RangeTable([], [])
376
+ export const Lo = new RangeTable([], [])
377
+ export const Mc = new RangeTable([], [])
378
+ export const Me = new RangeTable([], [])
379
+ export const Mn = new RangeTable([], [])
380
+ export const Nl = new RangeTable([], [])
381
+ export const No = new RangeTable([], [])
382
+ export const Pc = new RangeTable([new Range16(0x005f, 0x005f, 1)], []) // underscore
383
+ export const Pd = new RangeTable([new Range16(0x002d, 0x002d, 1)], []) // hyphen
384
+ export const Pe = new RangeTable(
385
+ [
386
+ new Range16(0x0029, 0x0029, 1),
387
+ new Range16(0x005d, 0x005d, 1),
388
+ new Range16(0x007d, 0x007d, 1),
389
+ ],
390
+ [],
391
+ )
392
+ export const Pf = new RangeTable([], [])
393
+ export const Pi = new RangeTable([], [])
394
+ export const Po = new RangeTable(
395
+ [new Range16(0x0021, 0x0023, 1), new Range16(0x0025, 0x0027, 1)],
396
+ [],
397
+ )
398
+ export const Ps = new RangeTable(
399
+ [
400
+ new Range16(0x0028, 0x0028, 1),
401
+ new Range16(0x005b, 0x005b, 1),
402
+ new Range16(0x007b, 0x007b, 1),
403
+ ],
404
+ [],
405
+ )
406
+ export const Sc = new RangeTable([new Range16(0x0024, 0x0024, 1)], []) // dollar sign
407
+ export const Sk = new RangeTable(
408
+ [new Range16(0x005e, 0x005e, 1), new Range16(0x0060, 0x0060, 1)],
409
+ [],
410
+ )
411
+ export const Sm = new RangeTable(
412
+ [new Range16(0x002b, 0x002b, 1), new Range16(0x003c, 0x003e, 1)],
413
+ [],
414
+ )
415
+ export const So = new RangeTable([], [])
416
+ export const Zl = new RangeTable([], [])
417
+ export const Zp = new RangeTable([], [])
418
+ export const Zs = new RangeTable([new Range16(0x0020, 0x0020, 1)], []) // space
@@ -0,0 +1,22 @@
1
+ package utf8 // import "unicode/utf8"
2
+
3
+ Package utf8 implements functions and constants to support text encoded
4
+ in UTF-8. It includes functions to translate between runes and UTF-8 byte
5
+ sequences. See https://en.wikipedia.org/wiki/UTF-8
6
+
7
+ const RuneError = '\uFFFD' ...
8
+ func AppendRune(p []byte, r rune) []byte
9
+ func DecodeLastRune(p []byte) (r rune, size int)
10
+ func DecodeLastRuneInString(s string) (r rune, size int)
11
+ func DecodeRune(p []byte) (r rune, size int)
12
+ func DecodeRuneInString(s string) (r rune, size int)
13
+ func EncodeRune(p []byte, r rune) int
14
+ func FullRune(p []byte) bool
15
+ func FullRuneInString(s string) bool
16
+ func RuneCount(p []byte) int
17
+ func RuneCountInString(s string) (n int)
18
+ func RuneLen(r rune) int
19
+ func RuneStart(b byte) bool
20
+ func Valid(p []byte) bool
21
+ func ValidRune(r rune) bool
22
+ func ValidString(s string) bool
@@ -0,0 +1,2 @@
1
+ // Package utf8 implements functions and constants to support text encoded in UTF-8.
2
+ export * from './utf8.js'
@@ -0,0 +1,227 @@
1
+ // Package utf8 implements functions and constants to support text encoded in UTF-8.
2
+
3
+ import * as $ from '@goscript/builtin/builtin.js'
4
+
5
+ // RuneError is the "error" Rune or "Unicode replacement character"
6
+ export const RuneError = 0xfffd
7
+
8
+ // RuneSelf is the maximum rune value that can be represented as a single byte.
9
+ export const RuneSelf = 0x80
10
+
11
+ // MaxRune is the maximum valid Unicode code point.
12
+ export const MaxRune = 0x10ffff
13
+
14
+ // UTFMax is the maximum number of bytes of a UTF-8 encoded Unicode character.
15
+ export const UTFMax = 4
16
+
17
+ // AppendRune appends the UTF-8 encoding of r to the end of p and returns the extended buffer.
18
+ export function AppendRune(p: Uint8Array, r: number): Uint8Array {
19
+ const temp = new Uint8Array(UTFMax)
20
+ const n = EncodeRune(temp, r)
21
+ const result = new Uint8Array(p.length + n)
22
+ result.set(p)
23
+ result.set(temp.slice(0, n), p.length)
24
+ return result
25
+ }
26
+
27
+ // DecodeLastRune unpacks the last UTF-8 encoding in p and returns the rune and its width in bytes.
28
+ export function DecodeLastRune(p: Uint8Array): [number, number] {
29
+ if (p.length === 0) {
30
+ return [RuneError, 0]
31
+ }
32
+
33
+ // Simple implementation - find the start of the last rune
34
+ let start = p.length - 1
35
+ while (start > 0 && !RuneStart(p[start])) {
36
+ start--
37
+ }
38
+
39
+ const [r, size] = DecodeRune(p.slice(start))
40
+ if (start + size !== p.length) {
41
+ return [RuneError, 1]
42
+ }
43
+ return [r, size]
44
+ }
45
+
46
+ // DecodeLastRuneInString is like DecodeLastRune but its input is a string.
47
+ export function DecodeLastRuneInString(s: string): [number, number] {
48
+ if (s.length === 0) {
49
+ return [RuneError, 0]
50
+ }
51
+
52
+ // Use JavaScript's built-in Unicode handling
53
+ const chars = [...s]
54
+ if (chars.length === 0) {
55
+ return [RuneError, 0]
56
+ }
57
+
58
+ const lastChar = chars[chars.length - 1]
59
+ const codePoint = lastChar.codePointAt(0) || RuneError
60
+ const encoder = new TextEncoder()
61
+ const encoded = encoder.encode(lastChar)
62
+ return [codePoint, encoded.length]
63
+ }
64
+
65
+ // DecodeRune unpacks the first UTF-8 encoding in p and returns the rune and its width in bytes.
66
+ export function DecodeRune(p: Uint8Array): [number, number] {
67
+ if (p.length === 0) {
68
+ return [RuneError, 0]
69
+ }
70
+
71
+ if (p[0] < RuneSelf) {
72
+ return [p[0], 1]
73
+ }
74
+
75
+ // Convert bytes to string and decode
76
+ const decoder = new TextDecoder('utf-8', { fatal: false })
77
+ const str = decoder.decode(p.slice(0, Math.min(4, p.length)))
78
+ if (str.length === 0 || str === '\uFFFD') {
79
+ return [RuneError, 1]
80
+ }
81
+
82
+ const codePoint = str.codePointAt(0) || RuneError
83
+ const char = String.fromCodePoint(codePoint)
84
+ const encoder = new TextEncoder()
85
+ const encoded = encoder.encode(char)
86
+ return [codePoint, encoded.length]
87
+ }
88
+
89
+ // DecodeRuneInString is like DecodeRune but its input is a string.
90
+ export function DecodeRuneInString(s: string): [number, number] {
91
+ if (s.length === 0) {
92
+ return [RuneError, 0]
93
+ }
94
+
95
+ const c = s.charCodeAt(0)
96
+ if (c < RuneSelf) {
97
+ return [c, 1]
98
+ }
99
+
100
+ // Use JavaScript's built-in Unicode handling
101
+ const codePoint = s.codePointAt(0) || RuneError
102
+ const char = String.fromCodePoint(codePoint)
103
+ const encoder = new TextEncoder()
104
+ const encoded = encoder.encode(char)
105
+ return [codePoint, encoded.length]
106
+ }
107
+
108
+ // EncodeRune writes into p (which must be large enough) the UTF-8 encoding of the rune.
109
+ export function EncodeRune(p: Uint8Array | $.Slice<number>, r: number): number {
110
+ if (p === null) return 0
111
+
112
+ if (!ValidRune(r)) {
113
+ r = RuneError
114
+ }
115
+
116
+ const str = String.fromCodePoint(r)
117
+ const encoder = new TextEncoder()
118
+ const encoded = encoder.encode(str)
119
+
120
+ // Handle both Uint8Array and Slice types
121
+ if (p instanceof Uint8Array) {
122
+ for (let i = 0; i < Math.min(encoded.length, p.length); i++) {
123
+ p[i] = encoded[i]
124
+ }
125
+ return Math.min(encoded.length, p.length)
126
+ } else {
127
+ // Handle Slice<number>
128
+ const len = $.len(p!)
129
+ for (let i = 0; i < Math.min(encoded.length, len); i++) {
130
+ ;(p as $.Slice<number>)![i] = encoded[i]
131
+ }
132
+ return Math.min(encoded.length, len)
133
+ }
134
+ }
135
+
136
+ // FullRune reports whether the bytes in p begin with a full UTF-8 encoding of a rune.
137
+ export function FullRune(p: Uint8Array): boolean {
138
+ if (p.length === 0) {
139
+ return false
140
+ }
141
+
142
+ if (p[0] < RuneSelf) {
143
+ return true
144
+ }
145
+
146
+ const [, size] = DecodeRune(p)
147
+ return size <= p.length && size > 0
148
+ }
149
+
150
+ // FullRuneInString is like FullRune but its input is a string.
151
+ export function FullRuneInString(s: string): boolean {
152
+ if (s.length === 0) {
153
+ return false
154
+ }
155
+
156
+ const c = s.charCodeAt(0)
157
+ if (c < RuneSelf) {
158
+ return true
159
+ }
160
+
161
+ const [, size] = DecodeRuneInString(s)
162
+ return size <= s.length && size > 0
163
+ }
164
+
165
+ // RuneCount returns the number of runes in p.
166
+ export function RuneCount(p: Uint8Array): number {
167
+ const decoder = new TextDecoder('utf-8', { fatal: false })
168
+ const str = decoder.decode(p)
169
+ return [...str].length
170
+ }
171
+
172
+ // RuneCountInString is like RuneCount but its input is a string.
173
+ export function RuneCountInString(s: string): number {
174
+ return [...s].length
175
+ }
176
+
177
+ // RuneLen returns the number of bytes required to encode the rune.
178
+ export function RuneLen(r: number): number {
179
+ if (!ValidRune(r)) {
180
+ return -1
181
+ }
182
+
183
+ if (r < RuneSelf) {
184
+ return 1
185
+ }
186
+
187
+ const str = String.fromCodePoint(r)
188
+ const encoder = new TextEncoder()
189
+ return encoder.encode(str).length
190
+ }
191
+
192
+ // RuneStart reports whether the byte could be the first byte of an encoded, possibly invalid rune.
193
+ export function RuneStart(b: number): boolean {
194
+ return (b & 0xc0) !== 0x80
195
+ }
196
+
197
+ // Valid reports whether p consists entirely of valid UTF-8-encoded runes.
198
+ export function Valid(p: Uint8Array): boolean {
199
+ try {
200
+ const decoder = new TextDecoder('utf-8', { fatal: true })
201
+ decoder.decode(p)
202
+ return true
203
+ } catch {
204
+ return false
205
+ }
206
+ }
207
+
208
+ // ValidRune reports whether r can be legally encoded as UTF-8.
209
+ export function ValidRune(r: number): boolean {
210
+ return r >= 0 && r <= MaxRune && !(r >= 0xd800 && r <= 0xdfff)
211
+ }
212
+
213
+ // ValidString reports whether s consists entirely of valid UTF-8-encoded runes.
214
+ export function ValidString(s: string): boolean {
215
+ // JavaScript strings are always valid UTF-16, but we check for surrogates
216
+ for (let i = 0; i < s.length; i++) {
217
+ const code = s.charCodeAt(i)
218
+ if (code >= 0xd800 && code <= 0xdfff) {
219
+ // Surrogate pair
220
+ if (i + 1 >= s.length) return false
221
+ const next = s.charCodeAt(i + 1)
222
+ if (code >= 0xdc00 || next < 0xdc00 || next > 0xdfff) return false
223
+ i++ // Skip the next character as it's part of the surrogate pair
224
+ }
225
+ }
226
+ return true
227
+ }
@@ -0,0 +1,19 @@
1
+ package unsafe // import "unsafe"
2
+
3
+ Package unsafe contains operations that step around the type safety of Go
4
+ programs.
5
+
6
+ Packages that import unsafe may be non-portable and are not protected by the Go
7
+ 1 compatibility guidelines.
8
+
9
+ func Alignof(x ArbitraryType) uintptr
10
+ func Offsetof(x ArbitraryType) uintptr
11
+ func Sizeof(x ArbitraryType) uintptr
12
+ func String(ptr *byte, len IntegerType) string
13
+ func StringData(str string) *byte
14
+ type ArbitraryType int
15
+ func Slice(ptr *ArbitraryType, len IntegerType) []ArbitraryType
16
+ func SliceData(slice []ArbitraryType) *ArbitraryType
17
+ type IntegerType int
18
+ type Pointer *ArbitraryType
19
+ func Add(ptr Pointer, len IntegerType) Pointer
@@ -0,0 +1 @@
1
+ export * from './unsafe.js'