mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
Initial commit
This commit is contained in:
45
base64/base64.v
Normal file
45
base64/base64.v
Normal file
@@ -0,0 +1,45 @@
|
||||
module base64
|
||||
|
||||
const (
|
||||
INDEX = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
62, 63, 62, 62, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0,
|
||||
0, 0, 0, 0, 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, 0, 0, 0, 0, 63, 0, 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]
|
||||
)
|
||||
|
||||
fn decode(data string) string {
|
||||
p := data.str
|
||||
len := data.len
|
||||
mut pad := 0
|
||||
if len > 0 && (len % 4 != 0 || p[len - 1] == `=`) {
|
||||
pad = 1
|
||||
}
|
||||
L := ((len + 3) / 4 - pad) * 4
|
||||
str_len := L / 4 * 3 + pad
|
||||
mut str := malloc(str_len + 2)
|
||||
mut j := 0
|
||||
for i := 0; i < L; i += 4 {
|
||||
n := (INDEX[p[i]] << 18) | (INDEX[p[i + 1]] << 12) |
|
||||
(INDEX[p[i + 2]] << 6) | (INDEX[p[i + 3]])
|
||||
str[j] = n >> 16
|
||||
j++
|
||||
str[j] = n >> 8 & 0xff
|
||||
j++
|
||||
str[j] = n & 0xff
|
||||
j++
|
||||
}
|
||||
if pad > 0 {
|
||||
mut nn := (INDEX[p[L]] << 18) | (INDEX[p[L + 1]] << 12)
|
||||
str[str_len - 1] = nn >> 16
|
||||
if len > L + 2 && p[L + 2] != `=` {
|
||||
nn = nn | (INDEX[p[L + 2]] << 6)
|
||||
str[str_len] = nn >> 8 & 0xff
|
||||
}
|
||||
}
|
||||
str[str_len + 1] = `\0`
|
||||
return string(str)
|
||||
}
|
||||
|
||||
8
base64/base64_test.v
Normal file
8
base64/base64_test.v
Normal file
@@ -0,0 +1,8 @@
|
||||
import base64
|
||||
|
||||
fn test_decode() {
|
||||
assert base64.decode('TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=')
|
||||
== 'Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.'
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user