aboutsummaryrefslogtreecommitdiff
path: root/examples/base64/base64.tm
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-11-04 15:24:10 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-11-04 15:24:10 -0500
commit2fa26e6af3ec1599396d9260ef44b0d035b1f686 (patch)
treef35124d2259af4183b222ef0f84a89f9286e9ea2 /examples/base64/base64.tm
parentda5bd87c135749b11c866aaf341c6c2c7c2ab9b2 (diff)
Be much more permissive about using integer literals for fixed-size ints
or nums or bytes
Diffstat (limited to 'examples/base64/base64.tm')
-rw-r--r--examples/base64/base64.tm83
1 files changed, 42 insertions, 41 deletions
diff --git a/examples/base64/base64.tm b/examples/base64/base64.tm
index e765d86a..f22883cd 100644
--- a/examples/base64/base64.tm
+++ b/examples/base64/base64.tm
@@ -5,22 +5,23 @@ _enc := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/":utf8_
_EQUAL_BYTE := 0x3D[B]
_dec := [
- 255[B], 255[B], 255[B], 255[B], 255[B], 255[B], 255[B], 255[B],
- 255[B], 255[B], 255[B], 255[B], 255[B], 255[B], 255[B], 255[B],
- 255[B], 255[B], 255[B], 255[B], 255[B], 255[B], 255[B], 255[B],
- 255[B], 255[B], 255[B], 255[B], 255[B], 255[B], 255[B], 255[B],
- 255[B], 255[B], 255[B], 255[B], 255[B], 255[B], 255[B], 255[B],
- 255[B], 255[B], 255[B], 62[B], 255[B], 255[B], 255[B], 63[B],
- 52[B], 53[B], 54[B], 55[B], 56[B], 57[B], 58[B], 59[B],
- 60[B], 61[B], 255[B], 255[B], 255[B], 255[B], 255[B], 255[B],
- 255[B], 0[B], 1[B], 2[B], 3[B], 4[B], 5[B], 6[B],
- 7[B], 8[B], 9[B], 10[B], 11[B], 12[B], 13[B], 14[B],
- 15[B], 16[B], 17[B], 18[B], 19[B], 20[B], 21[B], 22[B],
- 23[B], 24[B], 25[B], 255[B], 255[B], 255[B], 255[B], 255[B],
- 255[B], 26[B], 27[B], 28[B], 29[B], 30[B], 31[B], 32[B],
- 33[B], 34[B], 35[B], 36[B], 37[B], 38[B], 39[B], 40[B],
- 41[B], 42[B], 43[B], 44[B], 45[B], 46[B], 47[B], 48[B],
- 49[B], 50[B], 51[B], 255[B], 255[B], 255[B], 255[B], 255[B],
+ :Byte
+ 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 62, 255, 255, 255, 63,
+ 52, 53, 54, 55, 56, 57, 58, 59,
+ 60, 61, 255, 255, 255, 255, 255, 255,
+ 255, 0, 1, 2, 3, 4, 5, 6,
+ 7, 8, 9, 10, 11, 12, 13, 14,
+ 15, 16, 17, 18, 19, 20, 21, 22,
+ 23, 24, 25, 255, 255, 255, 255, 255,
+ 255, 26, 27, 28, 29, 30, 31, 32,
+ 33, 34, 35, 36, 37, 38, 39, 40,
+ 41, 42, 43, 44, 45, 46, 47, 48,
+ 49, 50, 51, 255, 255, 255, 255, 255,
]
lang Base64:
@@ -31,32 +32,32 @@ lang Base64:
output := [0[B] for _ in bytes.length * 4 / 3 + 4]
src := 1[64]
dest := 1[64]
- while src + 2[64] <= bytes.length:
+ while src + 2 <= bytes.length:
chunk24 := (
- (Int32(bytes[src]) <<< 16) or (Int32(bytes[src+1[64]]) <<< 8) or Int32(bytes[src+2[64]])
+ (Int32(bytes[src]) <<< 16) or (Int32(bytes[src+1]) <<< 8) or Int32(bytes[src+2])
)
src += 3
- output[dest] = _enc[1[32] + ((chunk24 >>> 18) and 0b111111[32])]
- output[dest+1[64]] = _enc[1[32] + ((chunk24 >>> 12) and 0b111111[32])]
- output[dest+2[64]] = _enc[1[32] + ((chunk24 >>> 6) and 0b111111[32])]
- output[dest+3[64]] = _enc[1[32] + (chunk24 and 0b111111[32])]
+ output[dest] = _enc[1 + ((chunk24 >>> 18) and 0b111111)]
+ output[dest+1] = _enc[1 + ((chunk24 >>> 12) and 0b111111)]
+ output[dest+2] = _enc[1 + ((chunk24 >>> 6) and 0b111111)]
+ output[dest+3] = _enc[1 + (chunk24 and 0b111111)]
dest += 4
- if src + 1[64] == bytes.length:
+ if src + 1 == bytes.length:
chunk16 := (
- (Int32(bytes[src]) <<< 8) or Int32(bytes[src+1[64]])
+ (Int32(bytes[src]) <<< 8) or Int32(bytes[src+1])
)
- output[dest] = _enc[1[32] + ((chunk16 >>> 10) and 0b111111[32])]
- output[dest+1[64]] = _enc[1[32] + ((chunk16 >>> 4) and 0b111111[32])]
- output[dest+2[64]] = _enc[1[32] + ((chunk16 <<< 2)and 0b111111[32])]
- output[dest+3[64]] = _EQUAL_BYTE
+ output[dest] = _enc[1 + ((chunk16 >>> 10) and 0b11111)]
+ output[dest+1] = _enc[1 + ((chunk16 >>> 4) and 0b111111)]
+ output[dest+2] = _enc[1 + ((chunk16 <<< 2)and 0b111111)]
+ output[dest+3] = _EQUAL_BYTE
else if src == bytes.length:
chunk8 := Int32(bytes[src])
- output[dest] = _enc[1[32] + ((chunk8 >>> 2) and 0b111111[32])]
- output[dest+1[64]] = _enc[1[32] + ((chunk8 <<< 4) and 0b111111[32])]
- output[dest+2[64]] = _EQUAL_BYTE
- output[dest+3[64]] = _EQUAL_BYTE
+ output[dest] = _enc[1 + ((chunk8 >>> 2) and 0b111111)]
+ output[dest+1] = _enc[1 + ((chunk8 <<< 4) and 0b111111)]
+ output[dest+2] = _EQUAL_BYTE
+ output[dest+3] = _EQUAL_BYTE
return Base64.without_escaping(Text.from_bytes(output) or return !Base64)
@@ -68,21 +69,21 @@ lang Base64:
output := [0[B] for _ in bytes.length/4 * 3]
src := 1[64]
dest := 1[64]
- while src + 3[64] <= bytes.length:
+ while src + 3 <= bytes.length:
chunk24 := (
- (Int32(_dec[1[B]+bytes[src]]) <<< 18) or
- (Int32(_dec[1[B]+bytes[src+1[64]]]) <<< 12) or
- (Int32(_dec[1[B]+bytes[src+2[64]]]) <<< 6) or
- Int32(_dec[1[B]+bytes[src+3[64]]])
+ (Int32(_dec[1+bytes[src]]) <<< 18) or
+ (Int32(_dec[1+bytes[src+1]]) <<< 12) or
+ (Int32(_dec[1+bytes[src+2]]) <<< 6) or
+ Int32(_dec[1+bytes[src+3]])
)
src += 4
- output[dest] = Byte((chunk24 >>> 16) and 0xFF[32])
- output[dest+1[64]] = Byte((chunk24 >>> 8) and 0xFF[32])
- output[dest+2[64]] = Byte(chunk24 and 0xFF[32])
+ output[dest] = Byte((chunk24 >>> 16) and 0xFF)
+ output[dest+1] = Byte((chunk24 >>> 8) and 0xFF)
+ output[dest+2] = Byte(chunk24 and 0xFF)
dest += 3
- while output[-1] == 0xFF[B]:
+ while output[-1] == 0xFF:
output = output:to(-2)
return output