Pass test for decodeChunksVectored

This commit is contained in:
Nathan Fisher 2024-06-03 19:09:03 -04:00
parent dc1c6d6d66
commit b4444d04c3

View File

@ -116,12 +116,13 @@ pub const b64Alphabet = struct {
var numvec: @Vector(4, u24) = @splat(0);
const shlvec: @Vector(4, usize) = @splat(6);
for (0..4) |i| {
const idx: u8 = @truncate(i);
numvec <<= shlvec;
var inarr: [4]u6 = undefined;
inarr[0] = try self.getIdx(i);
inarr[1] = try self.getIdx(in[i + 4]);
inarr[2] = try self.getIdx(in[i + 8]);
inarr[3] = try self.getIdx(in[i + 12]);
inarr[0] = try self.getIdx(in[idx]);
inarr[1] = try self.getIdx(in[idx + 4]);
inarr[2] = try self.getIdx(in[idx + 8]);
inarr[3] = try self.getIdx(in[idx + 12]);
const invec: @Vector(4, u6) = inarr;
numvec |= invec;
}
@ -133,10 +134,10 @@ pub const b64Alphabet = struct {
numvec >>= shrvec;
}) {
const cvec = numvec & maskvec;
outbuf[idx - 1] = cvec[0];
outbuf[idx + 2] = cvec[1];
outbuf[idx + 5] = cvec[2];
outbuf[idx + 8] = cvec[3];
outbuf[idx - 1] = @truncate(cvec[0]);
outbuf[idx + 2] = @truncate(cvec[1]);
outbuf[idx + 5] = @truncate(cvec[2]);
outbuf[idx + 8] = @truncate(cvec[3]);
}
return outbuf;
}
@ -177,6 +178,13 @@ test "encode chunks vectored" {
try testing.expect(std.mem.eql(u8, obuf[0..], "SGVsbG8sIFdvcmxk"));
}
test "decode chunks vectored" {
const chunk = [16]u8{ 'S', 'G', 'V', 's', 'b', 'G', '8', 's', 'I', 'F', 'd', 'v', 'c', 'm', 'x', 'k' };
var out: [12]u8 = undefined;
const obuf = try rfc4648B64Alphabet.decodeChunksVectored(chunk, &out);
try testing.expect(std.mem.eql(u8, obuf[0..], "Hello, World"));
}
test "decode chunk" {
const chunk = [4]u8{ 'S', 'G', 'V', 's' };
var out: [3]u8 = undefined;