chore: Rename base64_{encode,decode}

This commit is contained in:
Kalle Carlbark 2023-10-29 16:21:18 +01:00
parent ac3ffb9e58
commit 798cbcc6a3
No known key found for this signature in database

View file

@ -45,7 +45,7 @@ pub fn main() !void {
if (res.args.encode != 0) {
for (res.positionals) |pos| {
const encodedString = try base64_encode(allocator, pos);
const encodedString = try encode(allocator, pos);
defer allocator.free(encodedString);
@ -57,7 +57,7 @@ pub fn main() !void {
var input = try read_from_stream(allocator, stdin);
defer input.deinit();
const encodedString = try base64_encode(allocator, input.items);
const encodedString = try encode(allocator, input.items);
defer allocator.free(encodedString);
try stdout.writer().print("{s}", .{encodedString});
@ -68,7 +68,7 @@ pub fn main() !void {
if (res.args.decode != 0) {
for (res.positionals) |pos| {
const decodedString = base64_decode(allocator, pos) catch |err| {
const decodedString = decode(allocator, pos) catch |err| {
std.debug.print("unable to decode string '{s}'\n", .{pos});
return err;
@ -88,7 +88,7 @@ pub fn main() !void {
};
defer input.deinit();
const decodedString = base64_decode(allocator, input.items) catch |err| {
const decodedString = decode(allocator, input.items) catch |err| {
std.debug.print("unable to decode string '{s}'\n", .{std.mem.trim(u8, input.items, "\r\n")});
return err;
@ -127,7 +127,7 @@ fn read_from_stream(allocator: std.mem.Allocator, stream: std.fs.File) !std.Arra
return input;
}
fn base64_encode(allocator: std.mem.Allocator, string: []const u8) ![]const u8 {
fn encode(allocator: std.mem.Allocator, string: []const u8) ![]const u8 {
const encoder = std.base64.standard.Encoder;
const encoded = try allocator.alloc(u8, encoder.calcSize(string.len));
@ -138,7 +138,7 @@ fn base64_encode(allocator: std.mem.Allocator, string: []const u8) ![]const u8 {
return encodedString;
}
fn base64_decode(allocator: std.mem.Allocator, b64string: []const u8) ![]u8 {
fn decode(allocator: std.mem.Allocator, b64string: []const u8) ![]u8 {
const decoder = std.base64.standard.Decoder;
var decodeSize = decoder.calcSizeForSlice(b64string) catch |err| {
return err;
@ -159,12 +159,26 @@ fn base64_decode(allocator: std.mem.Allocator, b64string: []const u8) ![]u8 {
return decodedString;
}
fn benchmark_encode(allocator: std.mem.Allocator, timer: *std.time.Timer) !void {
const input = "hej lksjdlkajsd laksjdlkjasd";
timer.reset();
std.mem.doNotOptimizeAway(try encode(allocator, input));
}
fn benchmark_decode(allocator: std.mem.Allocator, timer: *std.time.Timer) !void {
const input = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";
timer.reset();
std.mem.doNotOptimizeAway(try decode(allocator, input));
}
test "base64_decode() returns decoded string" {
const test_allocator = std.testing.allocator;
const base64String = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";
var expectedString = "hej lksjdlkajsd laksjdlkjasd";
const decodedString = try base64_decode(test_allocator, base64String);
const decodedString = try decode(test_allocator, base64String);
defer test_allocator.free(decodedString);
try std.testing.expectEqualStrings(expectedString, decodedString);
@ -175,7 +189,7 @@ test "base64_encode() returns encoded string" {
const expectedEncodedString = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";
var string = "hej lksjdlkajsd laksjdlkjasd";
const encodedString = try base64_encode(test_allocator, string);
const encodedString = try encode(test_allocator, string);
defer test_allocator.free(encodedString);
try std.testing.expectEqualStrings(expectedEncodedString, encodedString);