From 804d3471a5ad83925a278b2a17f36e36623f9df7 Mon Sep 17 00:00:00 2001 From: Wes Koerber Date: Mon, 20 May 2024 23:00:31 -0400 Subject: [PATCH] feat: formatAlloc, rename toString to formatBuf --- lib/MacAddress.zig | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/MacAddress.zig b/lib/MacAddress.zig index 68ab193..8f59182 100644 --- a/lib/MacAddress.zig +++ b/lib/MacAddress.zig @@ -30,7 +30,7 @@ pub fn parse(buf: []const u8) !Self { }; } -pub fn toString(self: Self, buf: []u8) ![]u8 { +pub fn formatBuf(self: Self, buf: []u8) ![]u8 { return std.fmt.bufPrint(buf, "{x:0>2}:{x:0>2}:{x:0>2}:{x:0>2}:{x:0>2}:{x:0>2}", .{ self.data[0], self.data[1], @@ -41,6 +41,11 @@ pub fn toString(self: Self, buf: []u8) ![]u8 { }) catch return FormatError.NoSpaceLeft; } +pub fn formatAlloc(self: Self, allocator: std.mem.Allocator) ![]u8 { + const buf = try allocator.alloc(u8, 17); + return self.formatBuf(buf); +} + const std = @import("std"); const testing = std.testing; @@ -79,17 +84,26 @@ test "parse_error_malformed2" { try testing.expectError(ParseError.InvalidInput, Self.parse(str)); } -test "tostring_success" { +test "format_buf_success" { const addr = Self{ .is_loopback = false, .data = .{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 } }; const expected = "00:11:22:33:44:55"; var buf: [17]u8 = undefined; - try testing.expectEqualSlices(u8, expected, try addr.toString(&buf)); + try testing.expectEqualSlices(u8, expected, try addr.formatBuf(&buf)); } -test "tostring_too_small" { +test "format_buf_too_small" { const addr = Self{ .is_loopback = false, .data = .{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 } }; var buf: [16]u8 = undefined; - try testing.expectError(FormatError.NoSpaceLeft, addr.toString(&buf)); + try testing.expectError(FormatError.NoSpaceLeft, addr.formatBuf(&buf)); +} + +test "format_alloc_success" { + const addr = Self{ .is_loopback = false, .data = .{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 } }; + const expected = "00:11:22:33:44:55"; + const buf = try addr.formatAlloc(testing.allocator); + defer testing.allocator.free(buf); + + try testing.expectEqualSlices(u8, expected, buf); }