add predicate option to xml parser

This commit is contained in:
Emil Lerch 2022-02-10 15:07:50 -08:00
parent c9369504fa
commit 744d834cfd
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -69,8 +69,8 @@ pub const Element = struct {
}; };
} }
pub fn findChildByTag(self: *Element, tag: []const u8) ?*Element { pub fn findChildByTag(self: *Element, tag: []const u8) !?*Element {
return self.findChildrenByTag(tag).next(); return try self.findChildrenByTag(tag).next();
} }
pub fn findChildrenByTag(self: *Element, tag: []const u8) FindChildrenByTagIterator { 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 { pub const FindChildrenByTagIterator = struct {
inner: ChildElementIterator, inner: ChildElementIterator,
tag: []const u8, 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| { 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; continue;
} }
@ -128,6 +133,9 @@ pub const Element = struct {
}; };
}; };
pub const PredicateOptions = struct {
allocator: ?std.mem.Allocator = null,
};
pub const XmlDecl = struct { pub const XmlDecl = struct {
version: []const u8, version: []const u8,
encoding: ?[]const u8, encoding: ?[]const u8,