chore: Rename base64_{encode,decode}
This commit is contained in:
parent
ac3ffb9e58
commit
798cbcc6a3
1 changed files with 22 additions and 8 deletions
30
src/main.zig
30
src/main.zig
|
@ -45,7 +45,7 @@ pub fn main() !void {
|
||||||
|
|
||||||
if (res.args.encode != 0) {
|
if (res.args.encode != 0) {
|
||||||
for (res.positionals) |pos| {
|
for (res.positionals) |pos| {
|
||||||
const encodedString = try base64_encode(allocator, pos);
|
const encodedString = try encode(allocator, pos);
|
||||||
|
|
||||||
defer allocator.free(encodedString);
|
defer allocator.free(encodedString);
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ pub fn main() !void {
|
||||||
var input = try read_from_stream(allocator, stdin);
|
var input = try read_from_stream(allocator, stdin);
|
||||||
defer input.deinit();
|
defer input.deinit();
|
||||||
|
|
||||||
const encodedString = try base64_encode(allocator, input.items);
|
const encodedString = try encode(allocator, input.items);
|
||||||
defer allocator.free(encodedString);
|
defer allocator.free(encodedString);
|
||||||
|
|
||||||
try stdout.writer().print("{s}", .{encodedString});
|
try stdout.writer().print("{s}", .{encodedString});
|
||||||
|
@ -68,7 +68,7 @@ pub fn main() !void {
|
||||||
|
|
||||||
if (res.args.decode != 0) {
|
if (res.args.decode != 0) {
|
||||||
for (res.positionals) |pos| {
|
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});
|
std.debug.print("unable to decode string '{s}'\n", .{pos});
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
|
@ -88,7 +88,7 @@ pub fn main() !void {
|
||||||
};
|
};
|
||||||
defer input.deinit();
|
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")});
|
std.debug.print("unable to decode string '{s}'\n", .{std.mem.trim(u8, input.items, "\r\n")});
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
|
@ -127,7 +127,7 @@ fn read_from_stream(allocator: std.mem.Allocator, stream: std.fs.File) !std.Arra
|
||||||
return input;
|
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 encoder = std.base64.standard.Encoder;
|
||||||
const encoded = try allocator.alloc(u8, encoder.calcSize(string.len));
|
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;
|
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;
|
const decoder = std.base64.standard.Decoder;
|
||||||
var decodeSize = decoder.calcSizeForSlice(b64string) catch |err| {
|
var decodeSize = decoder.calcSizeForSlice(b64string) catch |err| {
|
||||||
return err;
|
return err;
|
||||||
|
@ -159,12 +159,26 @@ fn base64_decode(allocator: std.mem.Allocator, b64string: []const u8) ![]u8 {
|
||||||
return decodedString;
|
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" {
|
test "base64_decode() returns decoded string" {
|
||||||
const test_allocator = std.testing.allocator;
|
const test_allocator = std.testing.allocator;
|
||||||
const base64String = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";
|
const base64String = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";
|
||||||
var expectedString = "hej lksjdlkajsd laksjdlkjasd";
|
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);
|
defer test_allocator.free(decodedString);
|
||||||
|
|
||||||
try std.testing.expectEqualStrings(expectedString, decodedString);
|
try std.testing.expectEqualStrings(expectedString, decodedString);
|
||||||
|
@ -175,7 +189,7 @@ test "base64_encode() returns encoded string" {
|
||||||
const expectedEncodedString = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";
|
const expectedEncodedString = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";
|
||||||
var string = "hej lksjdlkajsd laksjdlkjasd";
|
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);
|
defer test_allocator.free(encodedString);
|
||||||
|
|
||||||
try std.testing.expectEqualStrings(expectedEncodedString, encodedString);
|
try std.testing.expectEqualStrings(expectedEncodedString, encodedString);
|
||||||
|
|
Loading…
Reference in a new issue