From a99adb0b2952d2700f82633ebe3c8e3e325646e3 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Mon, 25 May 2026 13:03:59 -0700 Subject: [PATCH] handle commas in compact form --- src/srf.zig | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/srf.zig b/src/srf.zig index 077e87a..e98dc1e 100644 --- a/src/srf.zig +++ b/src/srf.zig @@ -1226,8 +1226,9 @@ pub const RecordFormatter = struct { switch (f.value.?) { .string => |s| { const newlines = std.mem.containsAtLeastScalar(u8, s, 1, '\n'); + const commas = !self.options.long_format and std.mem.containsAtLeastScalar(u8, s, 1, ','); // Output the count if newlines exist - const count = if (newlines) s.len else null; + const count = if (newlines or commas) s.len else null; if (count) |c| try writer.print("{d}", .{c}); try writer.writeByte(':'); try writer.writeAll(s); @@ -2016,6 +2017,27 @@ test fmtFrom { \\ , result); } +test "fmtFrom commas" { + // Example: serialize typed Zig values directly to SRF format. + const Data = struct { + name: []const u8 = "bob", + age: u8, + }; + const values: []const Data = &.{ + .{ .name = "alice, yo", .age = 30 }, + }; + var buf: [4096]u8 = undefined; + const result = try std.fmt.bufPrint( + &buf, + "{f}", + .{fmtFrom(Data, std.testing.allocator, values, .{})}, + ); + try std.testing.expectEqualStrings( + \\#!srfv1 + \\name:9:alice, yo,age:num:30 + \\ + , result); +} test "parse with diagnostics" { // Example: batch parsing collects all records and fields into slices. // Prefer `iterator` for streaming; use `parse` when random access to