From 744d834cfdae126bafa558a5a7d7c124661fc8dc Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Thu, 10 Feb 2022 15:07:50 -0800 Subject: [PATCH] add predicate option to xml parser --- src/xml.zig | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/xml.zig b/src/xml.zig index d8a5fdc..9f8d063 100644 --- a/src/xml.zig +++ b/src/xml.zig @@ -69,8 +69,8 @@ pub const Element = struct { }; } - pub fn findChildByTag(self: *Element, tag: []const u8) ?*Element { - return self.findChildrenByTag(tag).next(); + pub fn findChildByTag(self: *Element, tag: []const u8) !?*Element { + return try self.findChildrenByTag(tag).next(); } pub fn findChildrenByTag(self: *Element, tag: []const u8) FindChildrenByTagIterator { @@ -110,13 +110,18 @@ pub const Element = struct { } }; + fn strictEqual(a: []const u8, b: []const u8, _: PredicateOptions) !bool { + return mem.eql(u8, a, b); + } pub const FindChildrenByTagIterator = struct { inner: ChildElementIterator, tag: []const u8, + predicate: fn (a: []const u8, b: []const u8, options: PredicateOptions) anyerror!bool = strictEqual, + predicate_options: PredicateOptions = .{}, - pub fn next(self: *FindChildrenByTagIterator) ?*Element { + pub fn next(self: *FindChildrenByTagIterator) !?*Element { while (self.inner.next()) |child| { - if (!mem.eql(u8, child.tag, self.tag)) { + if (!try self.predicate(child.tag, self.tag, self.predicate_options)) { continue; } @@ -128,6 +133,9 @@ pub const Element = struct { }; }; +pub const PredicateOptions = struct { + allocator: ?std.mem.Allocator = null, +}; pub const XmlDecl = struct { version: []const u8, encoding: ?[]const u8,