handle greedy labels

This commit is contained in:
Emil Lerch 2022-05-28 18:02:31 -07:00
parent 46e3a4de11
commit 652a04d0c3
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -539,19 +539,23 @@ fn buildPath(allocator: std.mem.Allocator, raw_uri: []const u8, comptime ActionR
var buffer = try std.ArrayList(u8).initCapacity(allocator, raw_uri.len); var buffer = try std.ArrayList(u8).initCapacity(allocator, raw_uri.len);
// const writer = buffer.writer(); // const writer = buffer.writer();
defer buffer.deinit(); defer buffer.deinit();
var in_var = false; var in_label = false;
var start: usize = 0; var start: usize = 0;
for (raw_uri) |c, inx| { for (raw_uri) |c, inx| {
switch (c) { switch (c) {
'{' => { '{' => {
in_var = true; in_label = true;
start = inx + 1; start = inx + 1;
}, },
'}' => { '}' => {
in_var = false; in_label = false;
const replacement_var = raw_uri[start..inx]; // The label may be "greedy" (uses a '+' at the end), but
// it's not clear if that effects this processing
var end = inx;
if (raw_uri[inx - 1] == '+') end -= 1;
const replacement_label = raw_uri[start..end];
inline for (std.meta.fields(ActionRequest)) |field| { inline for (std.meta.fields(ActionRequest)) |field| {
if (std.mem.eql(u8, request.fieldNameFor(field.name), replacement_var)) { if (std.mem.eql(u8, request.fieldNameFor(field.name), replacement_label)) {
var replacement_buffer = try std.ArrayList(u8).initCapacity(allocator, raw_uri.len); var replacement_buffer = try std.ArrayList(u8).initCapacity(allocator, raw_uri.len);
defer replacement_buffer.deinit(); defer replacement_buffer.deinit();
var encoded_buffer = try std.ArrayList(u8).initCapacity(allocator, raw_uri.len); var encoded_buffer = try std.ArrayList(u8).initCapacity(allocator, raw_uri.len);
@ -569,7 +573,7 @@ fn buildPath(allocator: std.mem.Allocator, raw_uri: []const u8, comptime ActionR
} }
} }
}, },
else => if (!in_var) { else => if (!in_label) {
try buffer.append(c); try buffer.append(c);
} else {}, } else {},
} }