summaryrefslogtreecommitdiff
path: root/src/host/fields.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/fields.zig')
-rw-r--r--src/host/fields.zig37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/host/fields.zig b/src/host/fields.zig
index 2e70414..fc1a1e7 100644
--- a/src/host/fields.zig
+++ b/src/host/fields.zig
@@ -123,6 +123,25 @@ pub fn set(leaf: Leaf, memory: []u8, text: []const u8) !void {
}
}
+/// Sets every value of the row of scalars `path` names (an array of
+/// numbers, a vector, `grid[1]` of a `grid: [3][2]u16`), in memory order,
+/// from `text`'s words, one each; nothing when one is not a value.
+pub fn setRow(layout: Layout, memory: []u8, path: []const u8, text: []const u8) !void {
+ for (layout.fields) |leaf| {
+ if (leaf.whole() or !std.mem.endsWith(u8, leaf.path, "[]")) continue;
+ const at = abi.layout.match(leaf.path[0 .. leaf.path.len - 2], leaf.dims[0 .. leaf.dims.len - 1], path) orelse continue;
+ const d = leaf.dims[leaf.dims.len - 1];
+ var scratch: [16]u8 = undefined;
+ var words = std.mem.tokenizeAny(u8, text, " \t\n");
+ for (0..d.count) |_| try set(leaf.at(0), &scratch, words.next() orelse return error.TooFewValues);
+ if (words.next() != null) return error.TooManyValues;
+ words.reset();
+ for (0..d.count) |i| try set(leaf.at(leaf.offset + at + i * d.stride), memory, words.next().?);
+ return;
+ }
+ return error.UnsupportedField;
+}
+
/// True when the field's formatted value equals `text` (enum names compare
/// with or without the leading dot).
pub fn matches(leaf: Leaf, memory: []const u8, text: []const u8) bool {
@@ -206,6 +225,24 @@ test "fields format, parse and compare by kind" {
try std.testing.expectError(error.InvalidValue, set(layout.find("x").?, bytes, "banana"));
}
+test "a row of scalars is set from its words, all or none" {
+ const S = struct { grid: [3][2]i16 = @splat(@splat(0)), pos: @Vector(2, f32) = .{ 0, 0 } };
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const layout = try Layout.copy(arena.allocator(), abi.layout.of(S, .state));
+ var s: S = .{};
+ const bytes = std.mem.asBytes(&s);
+ try setRow(layout, bytes, "grid[1]", "4 -5");
+ try std.testing.expectEqual([2]i16{ 4, -5 }, s.grid[1]);
+ try setRow(layout, bytes, "pos", "1.5\n2");
+ try std.testing.expectEqual(@as(f32, 2), s.pos[1]);
+ try std.testing.expectError(error.TooFewValues, setRow(layout, bytes, "grid[2]", "7"));
+ try std.testing.expectError(error.TooManyValues, setRow(layout, bytes, "grid[2]", "7 8 9"));
+ try std.testing.expectError(error.InvalidValue, setRow(layout, bytes, "grid[2]", "7 x"));
+ try std.testing.expectEqual([2]i16{ 0, 0 }, s.grid[2]);
+ try std.testing.expectError(error.UnsupportedField, setRow(layout, bytes, "grid", "1 2"));
+}
+
test "indexed paths reach array elements and vector lanes" {
const S = struct { pos: @Vector(2, f32) = .{ 250, 3 }, boids: struct { buffer: [4]struct { pos: [2]f32 = .{ 0, 0 }, mood: enum(u8) { calm = 1, wild = 2 } = .calm } = @splat(.{}), len: u32 = 0 } = .{} };
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);