1717// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
1919const std = @import ("std" );
20+ const Allocator = std .mem .Allocator ;
2021
2122const parser = @import ("../netsurf.zig" );
2223
@@ -42,25 +43,11 @@ const Matcher = union(enum) {
4243
4344 pub fn match (self : Matcher , node : * parser.Node ) ! bool {
4445 switch (self ) {
45- inline .matchTrue = > return true ,
46- inline .matchFalse = > return false ,
47- inline .matchByTagName = > | case | return case .match (node ),
48- inline .matchByClassName = > | case | return case .match (node ),
49- inline .matchByName = > | case | return case .match (node ),
50- inline .matchByLinks = > return MatchByLinks .match (node ),
51- inline .matchByAnchors = > return MatchByAnchors .match (node ),
52- }
53- }
54-
55- pub fn deinit (self : Matcher , alloc : std.mem.Allocator ) void {
56- switch (self ) {
57- inline .matchTrue = > return ,
58- inline .matchFalse = > return ,
59- inline .matchByTagName = > | case | return case .deinit (alloc ),
60- inline .matchByClassName = > | case | return case .deinit (alloc ),
61- inline .matchByName = > | case | return case .deinit (alloc ),
62- inline .matchByLinks = > return ,
63- inline .matchByAnchors = > return ,
46+ .matchTrue = > return true ,
47+ .matchFalse = > return false ,
48+ .matchByLinks = > return MatchByLinks .match (node ),
49+ .matchByAnchors = > return MatchByAnchors .match (node ),
50+ inline else = > | m | return m .match (node ),
6451 }
6552 }
6653};
@@ -71,54 +58,49 @@ pub const MatchByTagName = struct {
7158 tag : []const u8 ,
7259 is_wildcard : bool ,
7360
74- fn init (alloc : std.mem.Allocator , tag_name : []const u8 ) ! MatchByTagName {
75- const tag_name_alloc = try alloc .alloc (u8 , tag_name .len );
76- @memcpy (tag_name_alloc , tag_name );
77- return MatchByTagName {
78- .tag = tag_name_alloc ,
79- .is_wildcard = std .mem .eql (u8 , tag_name , "*" ),
61+ fn init (arena : Allocator , tag_name : []const u8 ) ! MatchByTagName {
62+ if (std .mem .eql (u8 , tag_name , "*" )) {
63+ return .{ .tag = "*" , .is_wildcard = true };
64+ }
65+
66+ return .{
67+ .tag = try arena .dupe (u8 , tag_name ),
68+ .is_wildcard = false ,
8069 };
8170 }
8271
8372 pub fn match (self : MatchByTagName , node : * parser.Node ) ! bool {
8473 return self .is_wildcard or std .ascii .eqlIgnoreCase (self .tag , try parser .nodeName (node ));
8574 }
86-
87- fn deinit (self : MatchByTagName , alloc : std.mem.Allocator ) void {
88- alloc .free (self .tag );
89- }
9075};
9176
9277pub fn HTMLCollectionByTagName (
93- alloc : std.mem. Allocator ,
78+ arena : Allocator ,
9479 root : ? * parser.Node ,
9580 tag_name : []const u8 ,
9681 include_root : bool ,
9782) ! HTMLCollection {
9883 return HTMLCollection {
9984 .root = root ,
100- .walker = Walker { .walkerDepthFirst = .{} },
101- .matcher = Matcher {
102- .matchByTagName = try MatchByTagName .init (alloc , tag_name ),
103- },
85+ .walker = .{ .walkerDepthFirst = .{} },
86+ .matcher = .{ .matchByTagName = try MatchByTagName .init (arena , tag_name ) },
10487 .include_root = include_root ,
10588 };
10689}
10790
10891pub const MatchByClassName = struct {
109- classNames : []const u8 ,
92+ class_names : []const u8 ,
11093
111- fn init (alloc : std.mem.Allocator , classNames : []const u8 ) ! MatchByClassName {
112- const class_names_alloc = try alloc .alloc (u8 , classNames .len );
113- @memcpy (class_names_alloc , classNames );
114- return MatchByClassName {
115- .classNames = class_names_alloc ,
94+ fn init (arena : Allocator , class_names : []const u8 ) ! MatchByClassName {
95+ return .{
96+ .class_names = try arena .dupe (u8 , class_names ),
11697 };
11798 }
11899
119100 pub fn match (self : MatchByClassName , node : * parser.Node ) ! bool {
120- var it = std .mem .splitAny (u8 , self .classNames , " " );
121101 const e = parser .nodeToElement (node );
102+
103+ var it = std .mem .splitScalar (u8 , self .class_names , ' ' );
122104 while (it .next ()) | c | {
123105 if (! try parser .elementHasClass (e , c )) {
124106 return false ;
@@ -127,36 +109,28 @@ pub const MatchByClassName = struct {
127109
128110 return true ;
129111 }
130-
131- fn deinit (self : MatchByClassName , alloc : std.mem.Allocator ) void {
132- alloc .free (self .classNames );
133- }
134112};
135113
136114pub fn HTMLCollectionByClassName (
137- alloc : std.mem. Allocator ,
115+ arena : Allocator ,
138116 root : ? * parser.Node ,
139117 classNames : []const u8 ,
140118 include_root : bool ,
141119) ! HTMLCollection {
142120 return HTMLCollection {
143121 .root = root ,
144- .walker = Walker { .walkerDepthFirst = .{} },
145- .matcher = Matcher {
146- .matchByClassName = try MatchByClassName .init (alloc , classNames ),
147- },
122+ .walker = .{ .walkerDepthFirst = .{} },
123+ .matcher = .{ .matchByClassName = try MatchByClassName .init (arena , classNames ) },
148124 .include_root = include_root ,
149125 };
150126}
151127
152128pub const MatchByName = struct {
153129 name : []const u8 ,
154130
155- fn init (alloc : std.mem.Allocator , name : []const u8 ) ! MatchByName {
156- const names_alloc = try alloc .alloc (u8 , name .len );
157- @memcpy (names_alloc , name );
158- return MatchByName {
159- .name = names_alloc ,
131+ fn init (arena : Allocator , name : []const u8 ) ! MatchByName {
132+ return .{
133+ .name = try arena .dupe (u8 , name ),
160134 };
161135 }
162136
@@ -165,24 +139,18 @@ pub const MatchByName = struct {
165139 const nname = try parser .elementGetAttribute (e , "name" ) orelse return false ;
166140 return std .mem .eql (u8 , self .name , nname );
167141 }
168-
169- fn deinit (self : MatchByName , alloc : std.mem.Allocator ) void {
170- alloc .free (self .name );
171- }
172142};
173143
174144pub fn HTMLCollectionByName (
175- alloc : std.mem. Allocator ,
145+ arena : Allocator ,
176146 root : ? * parser.Node ,
177147 name : []const u8 ,
178148 include_root : bool ,
179149) ! HTMLCollection {
180150 return HTMLCollection {
181151 .root = root ,
182- .walker = Walker { .walkerDepthFirst = .{} },
183- .matcher = Matcher {
184- .matchByName = try MatchByName .init (alloc , name ),
185- },
152+ .walker = .{ .walkerDepthFirst = .{} },
153+ .matcher = .{ .matchByName = try MatchByName .init (arena , name ) },
186154 .include_root = include_root ,
187155 };
188156}
@@ -193,8 +161,8 @@ pub fn HTMLCollectionAll(
193161) ! HTMLCollection {
194162 return HTMLCollection {
195163 .root = root ,
196- .walker = Walker { .walkerDepthFirst = .{} },
197- .matcher = Matcher { .matchTrue = .{} },
164+ .walker = . { .walkerDepthFirst = .{} },
165+ .matcher = . { .matchTrue = .{} },
198166 .include_root = include_root ,
199167 };
200168}
@@ -205,17 +173,17 @@ pub fn HTMLCollectionChildren(
205173) ! HTMLCollection {
206174 return HTMLCollection {
207175 .root = root ,
208- .walker = Walker { .walkerChildren = .{} },
209- .matcher = Matcher { .matchTrue = .{} },
176+ .walker = . { .walkerChildren = .{} },
177+ .matcher = . { .matchTrue = .{} },
210178 .include_root = include_root ,
211179 };
212180}
213181
214182pub fn HTMLCollectionEmpty () ! HTMLCollection {
215183 return HTMLCollection {
216184 .root = null ,
217- .walker = Walker { .walkerNone = .{} },
218- .matcher = Matcher { .matchFalse = .{} },
185+ .walker = . { .walkerNone = .{} },
186+ .matcher = . { .matchFalse = .{} },
219187 .include_root = false ,
220188 };
221189}
@@ -240,10 +208,8 @@ pub fn HTMLCollectionByLinks(
240208) ! HTMLCollection {
241209 return HTMLCollection {
242210 .root = root ,
243- .walker = Walker { .walkerDepthFirst = .{} },
244- .matcher = Matcher {
245- .matchByLinks = MatchByLinks {},
246- },
211+ .walker = .{ .walkerDepthFirst = .{} },
212+ .matcher = .{ .matchByLinks = MatchByLinks {} },
247213 .include_root = include_root ,
248214 };
249215}
@@ -267,10 +233,8 @@ pub fn HTMLCollectionByAnchors(
267233) ! HTMLCollection {
268234 return HTMLCollection {
269235 .root = root ,
270- .walker = Walker { .walkerDepthFirst = .{} },
271- .matcher = Matcher {
272- .matchByAnchors = MatchByAnchors {},
273- },
236+ .walker = .{ .walkerDepthFirst = .{} },
237+ .matcher = .{ .matchByAnchors = MatchByAnchors {} },
274238 .include_root = include_root ,
275239 };
276240}
@@ -321,7 +285,7 @@ pub const HTMLCollection = struct {
321285 cur_node : ? * parser.Node = undefined ,
322286
323287 // start returns the first node to walk on.
324- fn start (self : HTMLCollection ) ! ? * parser.Node {
288+ fn start (self : * const HTMLCollection ) ! ? * parser.Node {
325289 if (self .root == null ) return null ;
326290
327291 if (self .include_root ) {
0 commit comments