handle commas in compact form
This commit is contained in:
parent
19246e5f83
commit
a99adb0b29
1 changed files with 23 additions and 1 deletions
24
src/srf.zig
24
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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue