authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2021-03-26 16:01:44+01:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2021-03-28 14:32:34+02:00
log99bed37fc7156edbca692f84eced575ec0c3846f
treefba5a1523dfe267ef00a321055e81e9063fbae0c
parentb8c019ef49be54d76acc6721d5d8d493193bcf5d

Add more variants


1 files changed, 26 insertions(+), 6 deletions(-)

lib/std/base64.zig+26-6
...@@ -39,13 +39,33 @@ pub const standard = Codecs{...@@ -39,13 +39,33 @@ pub const standard = Codecs{
39 .DecoderUnsafe = Base64DecoderUnsafe.init(standard_alphabet_chars, '='),39 .DecoderUnsafe = Base64DecoderUnsafe.init(standard_alphabet_chars, '='),
40};40};
4141
42/// Standard Base64 codecs, without padding
43pub const standard_no_pad = Codecs{
44 .alphabet_chars = standard_alphabet_chars,
45 .pad_char = null,
46 .decoderWithIgnore = standardBase64DecoderWithIgnore,
47 .Encoder = Base64Encoder.init(standard_alphabet_chars, null),
48 .Decoder = Base64Decoder.init(standard_alphabet_chars, null),
49 .DecoderUnsafe = Base64DecoderUnsafe.init(standard_alphabet_chars, null),
50};
51
42pub const url_safe_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".*;52pub const url_safe_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".*;
43fn urlSafeBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore {53fn urlSafeBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore {
44 return Base64DecoderWithIgnore.init(url_safe_alphabet_chars, null, ignore);54 return Base64DecoderWithIgnore.init(url_safe_alphabet_chars, null, ignore);
45}55}
4656
47/// URL-safe Base64 codecs, without padding57/// URL-safe Base64 codecs, with padding
48pub const url_safe = Codecs{58pub const url_safe = Codecs{
59 .alphabet_chars = url_safe_alphabet_chars,
60 .pad_char = '=',
61 .decoderWithIgnore = urlSafeBase64DecoderWithIgnore,
62 .Encoder = Base64Encoder.init(url_safe_alphabet_chars, '='),
63 .Decoder = Base64Decoder.init(url_safe_alphabet_chars, '='),
64 .DecoderUnsafe = Base64DecoderUnsafe.init(url_safe_alphabet_chars, '='),
65};
66
67/// URL-safe Base64 codecs, without padding
68pub const url_safe_no_pad = Codecs{
49 .alphabet_chars = url_safe_alphabet_chars,69 .alphabet_chars = url_safe_alphabet_chars,
50 .pad_char = null,70 .pad_char = null,
51 .decoderWithIgnore = urlSafeBase64DecoderWithIgnore,71 .decoderWithIgnore = urlSafeBase64DecoderWithIgnore,
...@@ -378,10 +398,10 @@ test "base64" {...@@ -378,10 +398,10 @@ test "base64" {
378 comptime testAllApis(standard, "comptime", "Y29tcHRpbWU=") catch unreachable;398 comptime testAllApis(standard, "comptime", "Y29tcHRpbWU=") catch unreachable;
379}399}
380400
381test "base64 url_safe" {401test "base64 url_safe_no_pad" {
382 @setEvalBranchQuota(8000);402 @setEvalBranchQuota(8000);
383 testBase64UrlSafe() catch unreachable;403 testBase64UrlSafeNoPad() catch unreachable;
384 comptime testAllApis(url_safe, "comptime", "Y29tcHRpbWU") catch unreachable;404 comptime testAllApis(url_safe_no_pad, "comptime", "Y29tcHRpbWU") catch unreachable;
385}405}
386406
387fn testBase64() !void {407fn testBase64() !void {
...@@ -420,8 +440,8 @@ fn testBase64() !void {...@@ -420,8 +440,8 @@ fn testBase64() !void {
420 try testNoSpaceLeftError(codecs, "AAAAAA==");440 try testNoSpaceLeftError(codecs, "AAAAAA==");
421}441}
422442
423fn testBase64UrlSafe() !void {443fn testBase64UrlSafeNoPad() !void {
424 const codecs = url_safe;444 const codecs = url_safe_no_pad;
425445
426 try testAllApis(codecs, "", "");446 try testAllApis(codecs, "", "");
427 try testAllApis(codecs, "f", "Zg");447 try testAllApis(codecs, "f", "Zg");