refactor(detectArrayStyle): avoid nested loop by using a hash map
This commit is contained in:
parent
007c5a8a15
commit
622a815628
1 changed files with 10 additions and 7 deletions
|
@ -109,26 +109,29 @@ fn detectArrayStyle(comptime T: type, element: *xml.Element, options: ParseOptio
|
||||||
|
|
||||||
// does the element have child elements that match our expected struct?
|
// does the element have child elements that match our expected struct?
|
||||||
const field_names = comptime blk: {
|
const field_names = comptime blk: {
|
||||||
var result: [std.meta.fieldNames(T).len][]const u8 = undefined;
|
var result: [std.meta.fieldNames(T).len]struct {
|
||||||
|
[]const u8,
|
||||||
|
void,
|
||||||
|
} = undefined;
|
||||||
|
|
||||||
for (std.meta.fieldNames(T), 0..) |field_name, i| {
|
for (std.meta.fieldNames(T), 0..) |field_name, i| {
|
||||||
result[i] = if (@hasDecl(T, "fieldNameFor"))
|
const key = if (@hasDecl(T, "fieldNameFor"))
|
||||||
T.fieldNameFor(undefined, field_name)
|
T.fieldNameFor(undefined, field_name)
|
||||||
else
|
else
|
||||||
field_name;
|
field_name;
|
||||||
|
|
||||||
|
result[i] = .{ key, {} };
|
||||||
}
|
}
|
||||||
|
|
||||||
break :blk result;
|
break :blk std.StaticStringMap(void).initComptime(result);
|
||||||
};
|
};
|
||||||
|
|
||||||
var matching_fields: usize = 0;
|
var matching_fields: usize = 0;
|
||||||
var element_iterator = element.elements();
|
var element_iterator = element.elements();
|
||||||
|
|
||||||
while (element_iterator.next()) |el| {
|
while (element_iterator.next()) |el| {
|
||||||
for (field_names) |field_name| {
|
if (field_names.has(el.tag)) {
|
||||||
if (std.mem.eql(u8, field_name, el.tag)) {
|
matching_fields += 1;
|
||||||
matching_fields += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue