gofront 1.7.0 → 1.9.0

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 (194) hide show
  1. package/README.md +14 -14
  2. package/package.json +10 -38
  3. package/src/backend/js/expressions.js +2060 -389
  4. package/src/backend/js/index.js +586 -174
  5. package/src/backend/js/natives.js +383 -0
  6. package/src/backend/js/runtime.js +936 -1644
  7. package/src/backend/js/statements.js +1349 -132
  8. package/src/backend/js/stdlib/index.js +51 -94
  9. package/src/backend/js/templ.js +67 -118
  10. package/src/backend/js/type-info.js +436 -0
  11. package/src/backend/shared/fixed-float.js +20 -0
  12. package/src/backend/shared/fmt-types.js +80 -0
  13. package/src/backend/shared/host-unicode.js +36 -0
  14. package/src/backend/wasm/boundary.js +753 -872
  15. package/src/backend/wasm/emit-builtins.js +914 -237
  16. package/src/backend/wasm/emit-exprs.js +1456 -195
  17. package/src/backend/wasm/emit-maps.js +78 -380
  18. package/src/backend/wasm/emit-stmts.js +1659 -214
  19. package/src/backend/wasm/emit.js +614 -88
  20. package/src/backend/wasm/encode.js +43 -13
  21. package/src/backend/wasm/globals.js +491 -0
  22. package/src/backend/wasm/glue.js +2 -4
  23. package/src/backend/wasm/index.js +406 -3195
  24. package/src/backend/wasm/method-expr.js +392 -0
  25. package/src/backend/wasm/module-emitter.js +1615 -0
  26. package/src/backend/wasm/monomorph.js +164 -9
  27. package/src/backend/wasm/natives.js +596 -0
  28. package/src/backend/wasm/prescan.js +249 -0
  29. package/src/backend/wasm/prune-funcs.js +89 -0
  30. package/src/backend/wasm/stdlib/index.js +2 -37
  31. package/src/backend/wasm/stdlib/simd.js +293 -28
  32. package/src/backend/wasm/types.js +260 -27
  33. package/src/backend/wasm/variadic.js +93 -0
  34. package/src/backend/wasm/wat.js +47 -20
  35. package/src/{cli-core.js → cli/cli-core.js} +241 -11
  36. package/src/{test-runner.js → cli/test-runner.js} +188 -122
  37. package/src/{vendor.js → cli/vendor.js} +3 -4
  38. package/src/compiler.js +202 -386
  39. package/src/index.js +15 -169
  40. package/src/{lexer.js → lexer/lexer.js} +203 -62
  41. package/src/{templ-lexer.js → lexer/templ-lexer.js} +61 -18
  42. package/src/{tokens.js → lexer/tokens.js} +2 -0
  43. package/src/link/sourcemap.js +97 -0
  44. package/src/link/stdlib.js +737 -0
  45. package/src/link/type-names.js +148 -0
  46. package/src/link/wasm.js +318 -0
  47. package/src/lower/bounds.js +547 -0
  48. package/src/lower/boxing.js +1 -1
  49. package/src/lower/captures.js +267 -64
  50. package/src/lower/embedding.js +32 -3
  51. package/src/lower/errors-as.js +196 -0
  52. package/src/lower/fmt-formatters.js +905 -0
  53. package/src/lower/functions.js +2 -0
  54. package/src/lower/gen-types.js +274 -0
  55. package/src/lower/index.js +33 -19
  56. package/src/lower/json-codecs.js +1379 -0
  57. package/src/lower/ownership.js +47 -8
  58. package/src/lower/range.js +86 -1
  59. package/src/lower/reachability.js +150 -0
  60. package/src/lower/walk.js +12 -0
  61. package/src/parser/declarations.js +83 -15
  62. package/src/parser/expressions.js +110 -18
  63. package/src/parser/index.js +2 -1
  64. package/src/parser/statements.js +38 -22
  65. package/src/{templ-parser.js → parser/templ.js} +179 -17
  66. package/src/parser/types.js +70 -9
  67. package/src/resolver.js +21 -1
  68. package/src/typechecker/assignability.js +291 -19
  69. package/src/typechecker/const-fold.js +399 -0
  70. package/src/typechecker/constants.js +282 -0
  71. package/src/typechecker/expressions.js +1220 -183
  72. package/src/typechecker/index.js +659 -92
  73. package/src/typechecker/init-order.js +426 -0
  74. package/src/typechecker/operand-text.js +40 -0
  75. package/src/typechecker/resolve.js +613 -62
  76. package/src/typechecker/statements.js +212 -20
  77. package/src/typechecker/stdlib/excluded.js +902 -0
  78. package/src/typechecker/stdlib/index.js +38 -0
  79. package/src/typechecker/stdlib/simd.js +65 -15
  80. package/src/typechecker/termination.js +11 -2
  81. package/src/typechecker/types.js +268 -514
  82. package/stdlib/bytes/buffer.go +479 -0
  83. package/stdlib/bytes/bytes.go +1010 -0
  84. package/stdlib/bytes/reader.go +161 -0
  85. package/stdlib/cmp/cmp.go +65 -0
  86. package/stdlib/context/context.go +115 -0
  87. package/stdlib/crypto/rand/rand.go +34 -0
  88. package/stdlib/crypto/sha256/sha256.go +249 -0
  89. package/stdlib/encoding/base64/base64.go +331 -0
  90. package/stdlib/encoding/binary/binary.go +332 -0
  91. package/stdlib/encoding/hex/hex.go +140 -0
  92. package/stdlib/encoding/json/errors.go +31 -0
  93. package/stdlib/encoding/json/indent.go +71 -0
  94. package/stdlib/encoding/json/json.go +62 -0
  95. package/stdlib/encoding/json/number.go +12 -0
  96. package/stdlib/encoding/json/stream.go +173 -0
  97. package/stdlib/errors/errors.go +174 -0
  98. package/stdlib/fmt/errors.go +110 -0
  99. package/stdlib/fmt/format.go +732 -0
  100. package/stdlib/fmt/print.go +1232 -0
  101. package/stdlib/gom/gom.go +293 -0
  102. package/stdlib/hash/hash.go +53 -0
  103. package/stdlib/html/html.go +251 -0
  104. package/stdlib/internal/fmtrt/fmtrt.go +163 -0
  105. package/stdlib/internal/jsonrt/dec.go +871 -0
  106. package/stdlib/internal/jsonrt/enc.go +516 -0
  107. package/stdlib/internal/jsonrt/indent.go +135 -0
  108. package/stdlib/internal/jsonrt/number.go +27 -0
  109. package/stdlib/internal/jsonrt/reader.go +73 -0
  110. package/stdlib/internal/jsonrt/scanner.go +658 -0
  111. package/stdlib/io/io.go +502 -0
  112. package/stdlib/iter/iter.go +18 -0
  113. package/stdlib/maps/maps.go +131 -0
  114. package/stdlib/math/bits/bits.go +507 -0
  115. package/stdlib/math/extra.go +272 -0
  116. package/stdlib/math/math.go +439 -0
  117. package/stdlib/math/rand/rand.go +297 -0
  118. package/stdlib/os/os.go +93 -0
  119. package/stdlib/path/filepath/filepath.go +47 -0
  120. package/stdlib/path/path.go +394 -0
  121. package/stdlib/regexp/regexp.go +231 -0
  122. package/stdlib/slices/slices.go +526 -0
  123. package/stdlib/slices/sort.go +209 -0
  124. package/stdlib/slices/zsort_cmp.go +480 -0
  125. package/stdlib/slices/zsort_ordered.go +482 -0
  126. package/stdlib/sort/sort.go +500 -0
  127. package/stdlib/sort/zsort_func.go +480 -0
  128. package/stdlib/sort/zsort_iface.go +480 -0
  129. package/stdlib/strconv/float.go +1699 -0
  130. package/stdlib/strconv/isprint.go +752 -0
  131. package/stdlib/strconv/quote.go +526 -0
  132. package/stdlib/strconv/strconv.go +616 -0
  133. package/stdlib/strings/builder.go +95 -0
  134. package/stdlib/strings/reader.go +163 -0
  135. package/stdlib/strings/replacer.go +102 -0
  136. package/stdlib/strings/strings.go +807 -0
  137. package/stdlib/sync/sync.go +268 -0
  138. package/stdlib/testing/async_js.go +61 -0
  139. package/stdlib/testing/benchmark.go +297 -0
  140. package/stdlib/testing/testing.go +523 -0
  141. package/stdlib/time/duration.go +233 -0
  142. package/stdlib/time/format.go +745 -0
  143. package/stdlib/time/parse.go +790 -0
  144. package/stdlib/time/sleep_js.go +63 -0
  145. package/stdlib/time/time.go +834 -0
  146. package/stdlib/unicode/unicode.go +250 -0
  147. package/stdlib/unicode/utf8/utf8.go +464 -0
  148. package/src/backend/js/stdlib/base64.js +0 -12
  149. package/src/backend/js/stdlib/binary.js +0 -102
  150. package/src/backend/js/stdlib/bits.js +0 -46
  151. package/src/backend/js/stdlib/builder.js +0 -63
  152. package/src/backend/js/stdlib/bytes.js +0 -122
  153. package/src/backend/js/stdlib/errors.js +0 -44
  154. package/src/backend/js/stdlib/fmt.js +0 -178
  155. package/src/backend/js/stdlib/gom.js +0 -155
  156. package/src/backend/js/stdlib/hex.js +0 -17
  157. package/src/backend/js/stdlib/html.js +0 -20
  158. package/src/backend/js/stdlib/io.js +0 -137
  159. package/src/backend/js/stdlib/json.js +0 -972
  160. package/src/backend/js/stdlib/maps.js +0 -45
  161. package/src/backend/js/stdlib/math.js +0 -69
  162. package/src/backend/js/stdlib/os.js +0 -13
  163. package/src/backend/js/stdlib/path.js +0 -48
  164. package/src/backend/js/stdlib/rand.js +0 -32
  165. package/src/backend/js/stdlib/regexp.js +0 -54
  166. package/src/backend/js/stdlib/sha256.js +0 -12
  167. package/src/backend/js/stdlib/shared.js +0 -17
  168. package/src/backend/js/stdlib/simd.js +0 -24
  169. package/src/backend/js/stdlib/slices.js +0 -77
  170. package/src/backend/js/stdlib/sort.js +0 -30
  171. package/src/backend/js/stdlib/strconv.js +0 -74
  172. package/src/backend/js/stdlib/strings.js +0 -222
  173. package/src/backend/js/stdlib/sync.js +0 -13
  174. package/src/backend/js/stdlib/testing.js +0 -34
  175. package/src/backend/js/stdlib/time.js +0 -128
  176. package/src/backend/js/stdlib/unicode.js +0 -25
  177. package/src/backend/js/stdlib/utf8.js +0 -26
  178. package/src/backend/wasm/const-fold.js +0 -205
  179. package/src/backend/wasm/emit-stdlib.js +0 -13
  180. package/src/backend/wasm/stdlib/bytes.js +0 -5039
  181. package/src/backend/wasm/stdlib/core.js +0 -8903
  182. package/src/backend/wasm/stdlib/io.js +0 -2161
  183. package/src/backend/wasm/stdlib/packages.js +0 -2400
  184. package/src/backend/wasm/stdlib/pure.js +0 -720
  185. package/src/backend/wasm/stdlib/time.js +0 -1200
  186. package/src/typechecker/stdlib/core.js +0 -801
  187. package/src/typechecker/stdlib/extended.js +0 -914
  188. package/src/typechecker/stdlib.js +0 -22
  189. /package/src/{asset-manager.js → cli/asset-manager.js} +0 -0
  190. /package/src/{colors.js → cli/colors.js} +0 -0
  191. /package/src/{dev-server.js → cli/dev-server.js} +0 -0
  192. /package/src/{minifier.js → cli/minifier.js} +0 -0
  193. /package/src/{project-config.js → cli/project-config.js} +0 -0
  194. /package/src/{pwa.js → cli/pwa.js} +0 -0
@@ -0,0 +1,331 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ // Copyright 2009 The Go Authors. All rights reserved.
6
+ // Use of this source code is governed by a BSD-style
7
+ // license that can be found in the LICENSE file.
8
+
9
+ // Package base64 implements base64 encoding as specified by RFC 4648.
10
+ package base64
11
+
12
+ import "strconv"
13
+
14
+ // An Encoding is a radix 64 encoding/decoding scheme, defined by a
15
+ // 64-character alphabet. The most common encoding is the "base64"
16
+ // encoding defined in RFC 4648 and used in MIME (RFC 2045) and PEM
17
+ // (RFC 1421). RFC 4648 also defines an alternate encoding, which is
18
+ // the standard encoding with - and _ substituted for + and /.
19
+ type Encoding struct {
20
+ encode [64]byte
21
+ decodeMap [256]uint8
22
+ padChar rune
23
+ }
24
+
25
+ const (
26
+ StdPadding rune = '=' // Standard padding character
27
+ NoPadding rune = -1 // No padding
28
+ )
29
+
30
+ const (
31
+ encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
32
+ encodeURL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
33
+ )
34
+
35
+ // NewEncoding returns a new padded Encoding defined by the given alphabet,
36
+ // which must be a 64-byte string that does not contain the padding character
37
+ // or CR / LF ('\r', '\n'). The alphabet is treated as a sequence of byte
38
+ // values without any special treatment for multi-byte UTF-8.
39
+ // The resulting Encoding uses the default padding character ('='),
40
+ // which may be changed or disabled via [Encoding.WithPadding].
41
+ func NewEncoding(encoder string) *Encoding {
42
+ if len(encoder) != 64 {
43
+ panic("encoding alphabet is not 64-bytes long")
44
+ }
45
+
46
+ e := new(Encoding)
47
+ e.padChar = StdPadding
48
+ for i := 0; i < len(e.decodeMap); i++ {
49
+ e.decodeMap[i] = 0xff
50
+ }
51
+ for i := 0; i < len(encoder); i++ {
52
+ c := byte(encoder[i])
53
+ switch {
54
+ case c == '\n' || c == '\r':
55
+ panic("encoding alphabet contains newline character")
56
+ case e.decodeMap[c] != 0xff:
57
+ panic("encoding alphabet includes duplicate symbols")
58
+ }
59
+ e.encode[i] = c
60
+ e.decodeMap[c] = uint8(i)
61
+ }
62
+ return e
63
+ }
64
+
65
+ // WithPadding creates a new encoding identical to enc except
66
+ // with a specified padding character, or NoPadding to disable padding.
67
+ // The padding character must not be '\r' or '\n',
68
+ // must not be contained in the encoding's alphabet,
69
+ // must not be negative, and must be a rune equal or below '\xff'.
70
+ // Padding characters above '\x7f' are encoded as their exact byte value
71
+ // rather than using the UTF-8 representation of the codepoint.
72
+ func (enc Encoding) WithPadding(padding rune) *Encoding {
73
+ switch {
74
+ case padding < NoPadding || padding == '\r' || padding == '\n' || padding > 0xff:
75
+ panic("invalid padding")
76
+ case padding != NoPadding && enc.decodeMap[byte(padding)] != 0xff:
77
+ panic("padding contained in alphabet")
78
+ }
79
+ enc.padChar = padding
80
+ return &enc
81
+ }
82
+
83
+ // StdEncoding is the standard base64 encoding, as defined in RFC 4648.
84
+ var StdEncoding = NewEncoding(encodeStd)
85
+
86
+ // URLEncoding is the alternate base64 encoding defined in RFC 4648.
87
+ // It is typically used in URLs and file names.
88
+ var URLEncoding = NewEncoding(encodeURL)
89
+
90
+ // RawStdEncoding is the standard raw, unpadded base64 encoding,
91
+ // as defined in RFC 4648 section 3.2.
92
+ // This is the same as [StdEncoding] but omits padding characters.
93
+ var RawStdEncoding = StdEncoding.WithPadding(NoPadding)
94
+
95
+ // RawURLEncoding is the unpadded alternate base64 encoding defined in RFC 4648.
96
+ // It is typically used in URLs and file names.
97
+ // This is the same as [URLEncoding] but omits padding characters.
98
+ var RawURLEncoding = URLEncoding.WithPadding(NoPadding)
99
+
100
+ // Encode encodes src using the encoding enc,
101
+ // writing [Encoding.EncodedLen](len(src)) bytes to dst.
102
+ //
103
+ // The encoding pads the output to a multiple of 4 bytes,
104
+ // so Encode is not appropriate for use on individual blocks
105
+ // of a large data stream.
106
+ func (enc *Encoding) Encode(dst, src []byte) {
107
+ if len(src) == 0 {
108
+ return
109
+ }
110
+
111
+ di, si := 0, 0
112
+ n := (len(src) / 3) * 3
113
+ for si < n {
114
+ // Convert 3x 8bit source bytes into 4 bytes
115
+ val := uint(src[si+0])<<16 | uint(src[si+1])<<8 | uint(src[si+2])
116
+
117
+ dst[di+0] = enc.encode[val>>18&0x3F]
118
+ dst[di+1] = enc.encode[val>>12&0x3F]
119
+ dst[di+2] = enc.encode[val>>6&0x3F]
120
+ dst[di+3] = enc.encode[val&0x3F]
121
+
122
+ si += 3
123
+ di += 4
124
+ }
125
+
126
+ remain := len(src) - si
127
+ if remain == 0 {
128
+ return
129
+ }
130
+ // Add the remaining small block
131
+ val := uint(src[si+0]) << 16
132
+ if remain == 2 {
133
+ val |= uint(src[si+1]) << 8
134
+ }
135
+
136
+ dst[di+0] = enc.encode[val>>18&0x3F]
137
+ dst[di+1] = enc.encode[val>>12&0x3F]
138
+
139
+ switch remain {
140
+ case 2:
141
+ dst[di+2] = enc.encode[val>>6&0x3F]
142
+ if enc.padChar != NoPadding {
143
+ dst[di+3] = byte(enc.padChar)
144
+ }
145
+ case 1:
146
+ if enc.padChar != NoPadding {
147
+ dst[di+2] = byte(enc.padChar)
148
+ dst[di+3] = byte(enc.padChar)
149
+ }
150
+ }
151
+ }
152
+
153
+ // AppendEncode appends the base64 encoded src to dst
154
+ // and returns the extended buffer.
155
+ func (enc *Encoding) AppendEncode(dst, src []byte) []byte {
156
+ buf := make([]byte, enc.EncodedLen(len(src)))
157
+ enc.Encode(buf, src)
158
+ return append(dst, buf...)
159
+ }
160
+
161
+ // EncodeToString returns the base64 encoding of src.
162
+ func (enc *Encoding) EncodeToString(src []byte) string {
163
+ buf := make([]byte, enc.EncodedLen(len(src)))
164
+ enc.Encode(buf, src)
165
+ return string(buf)
166
+ }
167
+
168
+ // EncodedLen returns the length in bytes of the base64 encoding
169
+ // of an input buffer of length n.
170
+ func (enc *Encoding) EncodedLen(n int) int {
171
+ if enc.padChar == NoPadding {
172
+ return n/3*4 + (n%3*8+5)/6 // minimum # chars at 6 bits per char
173
+ }
174
+ return (n + 2) / 3 * 4 // minimum # 4-char quanta, 3 bytes each
175
+ }
176
+
177
+ /*
178
+ * Decoder
179
+ */
180
+
181
+ type CorruptInputError int64
182
+
183
+ func (e CorruptInputError) Error() string {
184
+ return "illegal base64 data at input byte " + strconv.FormatInt(int64(e), 10)
185
+ }
186
+
187
+ // decodeQuantum decodes up to 4 base64 bytes. The received parameters are
188
+ // the destination buffer dst, the source buffer src and an index in the
189
+ // source buffer si.
190
+ // It returns the number of bytes read from src, the number of bytes written
191
+ // to dst, and an error, if any.
192
+ func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err error) {
193
+ // Decode quantum using the base64 alphabet
194
+ var dbuf [4]byte
195
+ dlen := 4
196
+
197
+ for j := 0; j < len(dbuf); j++ {
198
+ if len(src) == si {
199
+ if j == 0 {
200
+ return si, 0, nil
201
+ }
202
+ if j == 1 || enc.padChar != NoPadding {
203
+ return si, 0, CorruptInputError(si - j)
204
+ }
205
+ dlen = j
206
+ break
207
+ }
208
+ in := src[si]
209
+ si++
210
+
211
+ out := enc.decodeMap[in]
212
+ if out != 0xff {
213
+ dbuf[j] = out
214
+ continue
215
+ }
216
+
217
+ if in == '\n' || in == '\r' {
218
+ j--
219
+ continue
220
+ }
221
+
222
+ if rune(in) != enc.padChar {
223
+ return si, 0, CorruptInputError(si - 1)
224
+ }
225
+
226
+ // We've reached the end and there's padding
227
+ switch j {
228
+ case 0, 1:
229
+ // incorrect padding
230
+ return si, 0, CorruptInputError(si - 1)
231
+ case 2:
232
+ // "==" is expected, the first "=" is already consumed.
233
+ // skip over newlines
234
+ for si < len(src) && (src[si] == '\n' || src[si] == '\r') {
235
+ si++
236
+ }
237
+ if si == len(src) {
238
+ // not enough padding
239
+ return si, 0, CorruptInputError(len(src))
240
+ }
241
+ if rune(src[si]) != enc.padChar {
242
+ // incorrect padding
243
+ return si, 0, CorruptInputError(si - 1)
244
+ }
245
+
246
+ si++
247
+ }
248
+
249
+ // skip over newlines
250
+ for si < len(src) && (src[si] == '\n' || src[si] == '\r') {
251
+ si++
252
+ }
253
+ if si < len(src) {
254
+ // trailing garbage
255
+ err = CorruptInputError(si)
256
+ }
257
+ dlen = j
258
+ break
259
+ }
260
+
261
+ // Convert 4x 6bit source bytes into 3 bytes
262
+ val := uint(dbuf[0])<<18 | uint(dbuf[1])<<12 | uint(dbuf[2])<<6 | uint(dbuf[3])
263
+ dbuf[2], dbuf[1], dbuf[0] = byte(val>>0), byte(val>>8), byte(val>>16)
264
+ if dlen >= 4 {
265
+ dst[2] = dbuf[2]
266
+ }
267
+ if dlen >= 3 {
268
+ dst[1] = dbuf[1]
269
+ }
270
+ if dlen >= 2 {
271
+ dst[0] = dbuf[0]
272
+ }
273
+ return si, dlen - 1, err
274
+ }
275
+
276
+ // DecodeString returns the bytes represented by the base64 string s.
277
+ // If the input is malformed, DecodeString returns the bytes decoded before
278
+ // the error.
279
+ func (enc *Encoding) DecodeString(s string) ([]byte, error) {
280
+ dbuf := make([]byte, enc.DecodedLen(len(s)))
281
+ n, err := enc.Decode(dbuf, []byte(s))
282
+ return dbuf[:n], err
283
+ }
284
+
285
+ // Decode decodes src using the encoding enc. It writes at most
286
+ // [Encoding.DecodedLen](len(src)) bytes to dst and returns the number of bytes
287
+ // written. The caller must ensure that dst is large enough to hold all
288
+ // the decoded data. If src contains invalid base64 data, it will return the
289
+ // number of bytes successfully written and [CorruptInputError].
290
+ // New line characters (\r and \n) are ignored.
291
+ func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
292
+ if len(src) == 0 {
293
+ return 0, nil
294
+ }
295
+
296
+ si := 0
297
+ for si < len(src) {
298
+ var ninc int
299
+ si, ninc, err = enc.decodeQuantum(dst[n:], src, si)
300
+ n += ninc
301
+ if err != nil {
302
+ return n, err
303
+ }
304
+ }
305
+ return n, err
306
+ }
307
+
308
+ // AppendDecode appends the base64 decoded src to dst
309
+ // and returns the extended buffer.
310
+ // If the input is malformed, it returns the partially decoded src and an error.
311
+ // New line characters (\r and \n) are ignored.
312
+ func (enc *Encoding) AppendDecode(dst, src []byte) ([]byte, error) {
313
+ buf := make([]byte, enc.DecodedLen(len(src)))
314
+ n, err := enc.Decode(buf, src)
315
+ return append(dst, buf[:n]...), err
316
+ }
317
+
318
+ // DecodedLen returns the maximum length in bytes of the decoded data
319
+ // corresponding to n bytes of base64-encoded data.
320
+ func (enc *Encoding) DecodedLen(n int) int {
321
+ return decodedLen(n, enc.padChar)
322
+ }
323
+
324
+ func decodedLen(n int, padChar rune) int {
325
+ if padChar == NoPadding {
326
+ // Unpadded data may end with partial block of 2-3 characters.
327
+ return n / 4 * 3 + n % 4 * 6 / 8
328
+ }
329
+ // Padded base64 should always be a multiple of 4 characters in length.
330
+ return n / 4 * 3
331
+ }
@@ -0,0 +1,332 @@
1
+ //go:build gofront
2
+
3
+ //gofront:target both
4
+
5
+ // Copyright 2009 The Go Authors. All rights reserved.
6
+ // Use of this source code is governed by a BSD-style
7
+ // license that can be found in the LICENSE file.
8
+
9
+ // Overlay on the emitter-backed encoding/binary package: byte orders and
10
+ // variable-length integers written in Go. Read, Write and Size walk the
11
+ // fields of an `any`, so they remain emitter members.
12
+ package binary
13
+
14
+ import (
15
+ "errors"
16
+ "io"
17
+ )
18
+
19
+ // A ByteOrder specifies how to convert byte slices into
20
+ // 16-, 32-, or 64-bit unsigned integers.
21
+ type ByteOrder interface {
22
+ Uint16([]byte) uint16
23
+ Uint32([]byte) uint32
24
+ Uint64([]byte) uint64
25
+ PutUint16([]byte, uint16)
26
+ PutUint32([]byte, uint32)
27
+ PutUint64([]byte, uint64)
28
+ String() string
29
+ }
30
+
31
+ // AppendByteOrder specifies how to append 16-, 32-, or 64-bit unsigned integers
32
+ // into a byte slice.
33
+ type AppendByteOrder interface {
34
+ AppendUint16([]byte, uint16) []byte
35
+ AppendUint32([]byte, uint32) []byte
36
+ AppendUint64([]byte, uint64) []byte
37
+ String() string
38
+ }
39
+
40
+ // LittleEndian is the little-endian implementation of [ByteOrder] and [AppendByteOrder].
41
+ var LittleEndian littleEndian
42
+
43
+ // BigEndian is the big-endian implementation of [ByteOrder] and [AppendByteOrder].
44
+ var BigEndian bigEndian
45
+
46
+ type littleEndian struct{}
47
+
48
+ func (littleEndian) Uint16(b []byte) uint16 {
49
+ _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
50
+ return uint16(b[0]) | uint16(b[1])<<8
51
+ }
52
+
53
+ func (littleEndian) PutUint16(b []byte, v uint16) {
54
+ _ = b[1] // early bounds check to guarantee safety of writes below
55
+ b[0] = byte(v)
56
+ b[1] = byte(v >> 8)
57
+ }
58
+
59
+ func (littleEndian) AppendUint16(b []byte, v uint16) []byte {
60
+ return append(b,
61
+ byte(v),
62
+ byte(v>>8),
63
+ )
64
+ }
65
+
66
+ func (littleEndian) Uint32(b []byte) uint32 {
67
+ _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
68
+ return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
69
+ }
70
+
71
+ func (littleEndian) PutUint32(b []byte, v uint32) {
72
+ _ = b[3] // early bounds check to guarantee safety of writes below
73
+ b[0] = byte(v)
74
+ b[1] = byte(v >> 8)
75
+ b[2] = byte(v >> 16)
76
+ b[3] = byte(v >> 24)
77
+ }
78
+
79
+ func (littleEndian) AppendUint32(b []byte, v uint32) []byte {
80
+ return append(b,
81
+ byte(v),
82
+ byte(v>>8),
83
+ byte(v>>16),
84
+ byte(v>>24),
85
+ )
86
+ }
87
+
88
+ func (littleEndian) Uint64(b []byte) uint64 {
89
+ _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
90
+ return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
91
+ uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
92
+ }
93
+
94
+ func (littleEndian) PutUint64(b []byte, v uint64) {
95
+ _ = b[7] // early bounds check to guarantee safety of writes below
96
+ b[0] = byte(v)
97
+ b[1] = byte(v >> 8)
98
+ b[2] = byte(v >> 16)
99
+ b[3] = byte(v >> 24)
100
+ b[4] = byte(v >> 32)
101
+ b[5] = byte(v >> 40)
102
+ b[6] = byte(v >> 48)
103
+ b[7] = byte(v >> 56)
104
+ }
105
+
106
+ func (littleEndian) AppendUint64(b []byte, v uint64) []byte {
107
+ return append(b,
108
+ byte(v),
109
+ byte(v>>8),
110
+ byte(v>>16),
111
+ byte(v>>24),
112
+ byte(v>>32),
113
+ byte(v>>40),
114
+ byte(v>>48),
115
+ byte(v>>56),
116
+ )
117
+ }
118
+
119
+ func (littleEndian) String() string { return "LittleEndian" }
120
+
121
+ func (littleEndian) GoString() string { return "binary.LittleEndian" }
122
+
123
+ type bigEndian struct{}
124
+
125
+ func (bigEndian) Uint16(b []byte) uint16 {
126
+ _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
127
+ return uint16(b[1]) | uint16(b[0])<<8
128
+ }
129
+
130
+ func (bigEndian) PutUint16(b []byte, v uint16) {
131
+ _ = b[1] // early bounds check to guarantee safety of writes below
132
+ b[0] = byte(v >> 8)
133
+ b[1] = byte(v)
134
+ }
135
+
136
+ func (bigEndian) AppendUint16(b []byte, v uint16) []byte {
137
+ return append(b,
138
+ byte(v>>8),
139
+ byte(v),
140
+ )
141
+ }
142
+
143
+ func (bigEndian) Uint32(b []byte) uint32 {
144
+ _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
145
+ return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
146
+ }
147
+
148
+ func (bigEndian) PutUint32(b []byte, v uint32) {
149
+ _ = b[3] // early bounds check to guarantee safety of writes below
150
+ b[0] = byte(v >> 24)
151
+ b[1] = byte(v >> 16)
152
+ b[2] = byte(v >> 8)
153
+ b[3] = byte(v)
154
+ }
155
+
156
+ func (bigEndian) AppendUint32(b []byte, v uint32) []byte {
157
+ return append(b,
158
+ byte(v>>24),
159
+ byte(v>>16),
160
+ byte(v>>8),
161
+ byte(v),
162
+ )
163
+ }
164
+
165
+ func (bigEndian) Uint64(b []byte) uint64 {
166
+ _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
167
+ return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
168
+ uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
169
+ }
170
+
171
+ func (bigEndian) PutUint64(b []byte, v uint64) {
172
+ _ = b[7] // early bounds check to guarantee safety of writes below
173
+ b[0] = byte(v >> 56)
174
+ b[1] = byte(v >> 48)
175
+ b[2] = byte(v >> 40)
176
+ b[3] = byte(v >> 32)
177
+ b[4] = byte(v >> 24)
178
+ b[5] = byte(v >> 16)
179
+ b[6] = byte(v >> 8)
180
+ b[7] = byte(v)
181
+ }
182
+
183
+ func (bigEndian) AppendUint64(b []byte, v uint64) []byte {
184
+ return append(b,
185
+ byte(v>>56),
186
+ byte(v>>48),
187
+ byte(v>>40),
188
+ byte(v>>32),
189
+ byte(v>>24),
190
+ byte(v>>16),
191
+ byte(v>>8),
192
+ byte(v),
193
+ )
194
+ }
195
+
196
+ func (bigEndian) String() string { return "BigEndian" }
197
+
198
+ func (bigEndian) GoString() string { return "binary.BigEndian" }
199
+
200
+ // MaxVarintLenN is the maximum length of a varint-encoded N-bit integer.
201
+ const (
202
+ MaxVarintLen16 = 3
203
+ MaxVarintLen32 = 5
204
+ MaxVarintLen64 = 10
205
+ )
206
+
207
+ // AppendUvarint appends the varint-encoded form of x,
208
+ // as generated by [PutUvarint], to buf and returns the extended buffer.
209
+ func AppendUvarint(buf []byte, x uint64) []byte {
210
+ for x >= 0x80 {
211
+ buf = append(buf, byte(x)|0x80)
212
+ x >>= 7
213
+ }
214
+ return append(buf, byte(x))
215
+ }
216
+
217
+ // PutUvarint encodes a uint64 into buf and returns the number of bytes written.
218
+ // If the buffer is too small, PutUvarint will panic.
219
+ func PutUvarint(buf []byte, x uint64) int {
220
+ i := 0
221
+ for x >= 0x80 {
222
+ buf[i] = byte(x) | 0x80
223
+ x >>= 7
224
+ i++
225
+ }
226
+ buf[i] = byte(x)
227
+ return i + 1
228
+ }
229
+
230
+ // Uvarint decodes a uint64 from buf and returns that value and the
231
+ // number of bytes read (> 0). If an error occurred, the value is 0
232
+ // and the number of bytes n is <= 0 meaning:
233
+ // - n == 0: buf too small;
234
+ // - n < 0: value larger than 64 bits (overflow) and -n is the number of
235
+ // bytes read.
236
+ func Uvarint(buf []byte) (uint64, int) {
237
+ var x uint64
238
+ var s uint
239
+ for i, b := range buf {
240
+ if i == MaxVarintLen64 {
241
+ // Catch byte reads past MaxVarintLen64.
242
+ // See issue https://golang.org/issues/41185
243
+ return 0, -(i + 1) // overflow
244
+ }
245
+ if b < 0x80 {
246
+ if i == MaxVarintLen64-1 && b > 1 {
247
+ return 0, -(i + 1) // overflow
248
+ }
249
+ return x | uint64(b)<<s, i + 1
250
+ }
251
+ x |= uint64(b&0x7f) << s
252
+ s += 7
253
+ }
254
+ return 0, 0
255
+ }
256
+
257
+ // AppendVarint appends the varint-encoded form of x,
258
+ // as generated by [PutVarint], to buf and returns the extended buffer.
259
+ func AppendVarint(buf []byte, x int64) []byte {
260
+ ux := uint64(x) << 1
261
+ if x < 0 {
262
+ ux = ^ux
263
+ }
264
+ return AppendUvarint(buf, ux)
265
+ }
266
+
267
+ // PutVarint encodes an int64 into buf and returns the number of bytes written.
268
+ // If the buffer is too small, PutVarint will panic.
269
+ func PutVarint(buf []byte, x int64) int {
270
+ ux := uint64(x) << 1
271
+ if x < 0 {
272
+ ux = ^ux
273
+ }
274
+ return PutUvarint(buf, ux)
275
+ }
276
+
277
+ // Varint decodes an int64 from buf and returns that value and the
278
+ // number of bytes read (> 0). If an error occurred, the value is 0
279
+ // and the number of bytes n is <= 0 with the following meaning:
280
+ // - n == 0: buf too small;
281
+ // - n < 0: value larger than 64 bits (overflow) and -n is the number of
282
+ // bytes read.
283
+ func Varint(buf []byte) (int64, int) {
284
+ ux, n := Uvarint(buf) // ok to continue in presence of error
285
+ x := int64(ux >> 1)
286
+ if ux&1 != 0 {
287
+ x = ^x
288
+ }
289
+ return x, n
290
+ }
291
+
292
+ var errOverflow = errors.New("binary: varint overflows a 64-bit integer")
293
+
294
+ // ReadUvarint reads an encoded unsigned integer from r and returns it as a uint64.
295
+ // The error is [io.EOF] only if no bytes were read.
296
+ // If an [io.EOF] happens after reading some but not all the bytes,
297
+ // ReadUvarint returns [io.ErrUnexpectedEOF].
298
+ func ReadUvarint(r io.ByteReader) (uint64, error) {
299
+ var x uint64
300
+ var s uint
301
+ for i := 0; i < MaxVarintLen64; i++ {
302
+ b, err := r.ReadByte()
303
+ if err != nil {
304
+ if i > 0 && err == io.EOF {
305
+ err = io.ErrUnexpectedEOF
306
+ }
307
+ return x, err
308
+ }
309
+ if b < 0x80 {
310
+ if i == MaxVarintLen64-1 && b > 1 {
311
+ return x, errOverflow
312
+ }
313
+ return x | uint64(b)<<s, nil
314
+ }
315
+ x |= uint64(b&0x7f) << s
316
+ s += 7
317
+ }
318
+ return x, errOverflow
319
+ }
320
+
321
+ // ReadVarint reads an encoded signed integer from r and returns it as an int64.
322
+ // The error is [io.EOF] only if no bytes were read.
323
+ // If an [io.EOF] happens after reading some but not all the bytes,
324
+ // ReadVarint returns [io.ErrUnexpectedEOF].
325
+ func ReadVarint(r io.ByteReader) (int64, error) {
326
+ ux, err := ReadUvarint(r) // ok to continue in presence of error
327
+ x := int64(ux >> 1)
328
+ if ux&1 != 0 {
329
+ x = ^x
330
+ }
331
+ return x, err
332
+ }