dom: no need to allocate mem for case insensitive equality

thanks to
[std.ascii.eqlIgnoreCase](https://ziglang.org/documentation/master/std/#A;std:ascii.eqlIgnoreCase)
This commit is contained in:
Pierre Tachoire
2023-11-17 17:11:39 +01:00
parent 9b94a4ec49
commit e9edb8bab4
2 changed files with 9 additions and 12 deletions

View File

@@ -50,9 +50,9 @@ pub const Document = struct {
// the spec changed to return an HTMLCollection instead.
// That's why we reimplemented getElementsByTagName by using an
// HTMLCollection in zig here.
pub fn _getElementsByTagName(self: *parser.Document, allocator: std.mem.Allocator, tag_name: []const u8) !collection.HTMLCollection {
pub fn _getElementsByTagName(self: *parser.Document, tag_name: []const u8) !collection.HTMLCollection {
const root = parser.documentGetDocumentElement(self);
return collection.HTMLCollectionByTagName(allocator, parser.elementToNode(root), tag_name);
return collection.HTMLCollectionByTagName(parser.elementToNode(root), tag_name);
}
};

View File

@@ -21,8 +21,9 @@ const Match = union(enum) {
}
pub fn deinit(self: Match, allocator: std.mem.Allocator) void {
_ = allocator;
switch (self) {
inline else => |case| case.deinit(allocator),
inline else => return,
}
}
};
@@ -33,27 +34,23 @@ pub const MatchByTagName = struct {
tag: []const u8,
is_wildcard: bool,
fn init(allocator: std.mem.Allocator, tag_name: []const u8) !MatchByTagName {
fn init(tag_name: []const u8) !MatchByTagName {
return MatchByTagName{
.tag = try std.ascii.allocUpperString(allocator, tag_name),
.tag = tag_name,
.is_wildcard = std.mem.eql(u8, tag_name, "*"),
};
}
fn deinit(self: *MatchByTagName, allocator: std.mem.Allocator) void {
allocator.free(self.tag);
}
pub fn match(self: MatchByTagName, node: *parser.Node) bool {
return self.is_wildcard or std.mem.eql(u8, self.tag, parser.nodeName(node));
return self.is_wildcard or std.ascii.eqlIgnoreCase(self.tag, parser.nodeName(node));
}
};
pub fn HTMLCollectionByTagName(allocator: std.mem.Allocator, root: *parser.Node, tag_name: []const u8) !HTMLCollection {
pub fn HTMLCollectionByTagName(root: *parser.Node, tag_name: []const u8) !HTMLCollection {
return HTMLCollection{
.root = root,
.match = Match{
.matchByTagName = try MatchByTagName.init(allocator, tag_name),
.matchByTagName = try MatchByTagName.init(tag_name),
},
};
}