Replace std.SegmentedList by std.ArrayList

This commit is contained in:
Robin Voetter
2020-12-01 14:17:04 +01:00
parent 7fdb6eaad3
commit 9aa2899f06
3 changed files with 47 additions and 27 deletions

View File

@@ -156,22 +156,22 @@ pub const CTokenizer = struct {
}; };
pub const XmlCTokenizer = struct { pub const XmlCTokenizer = struct {
it: xml.Element.ContentList.Iterator, it: xml.Element.ChildIterator,
ctok: ?CTokenizer = null, ctok: ?CTokenizer = null,
current: ?Token = null, current: ?Token = null,
pub fn init(elem: *xml.Element) XmlCTokenizer { pub fn init(elem: *xml.Element) XmlCTokenizer {
return .{ return .{
.it = elem.children.iterator(0), .it = elem.iterator(),
}; };
} }
fn elemToToken(elem: *xml.Element) !?Token { fn elemToToken(elem: *xml.Element) !?Token {
if (elem.children.count() != 1 or elem.children.at(0).* != .CharData) { if (elem.children.items.len != 1 or elem.children.items[0] != .CharData) {
return error.InvalidXml; return error.InvalidXml;
} }
const text = elem.children.at(0).CharData; const text = elem.children.items[0].CharData;
if (mem.eql(u8, elem.tag, "type")) { if (mem.eql(u8, elem.tag, "type")) {
return Token{.kind = .type_name, .text = text}; return Token{.kind = .type_name, .text = text};
} else if (mem.eql(u8, elem.tag, "enum")) { } else if (mem.eql(u8, elem.tag, "enum")) {

View File

@@ -42,7 +42,7 @@ fn parseDeclarations(allocator: *Allocator, root: *xml.Element) ![]registry.Decl
var types_elem = root.findChildByTag("types") orelse return error.InvalidRegistry; var types_elem = root.findChildByTag("types") orelse return error.InvalidRegistry;
var commands_elem = root.findChildByTag("commands") orelse return error.InvalidRegistry; var commands_elem = root.findChildByTag("commands") orelse return error.InvalidRegistry;
const decl_upper_bound = types_elem.children.count() + commands_elem.children.count(); const decl_upper_bound = types_elem.children.items.len + commands_elem.children.items.len;
const decls = try allocator.alloc(registry.Declaration, decl_upper_bound); const decls = try allocator.alloc(registry.Declaration, decl_upper_bound);
var count: usize = 0; var count: usize = 0;
@@ -167,7 +167,7 @@ fn parseContainer(allocator: *Allocator, ty: *xml.Element, is_union: bool) !regi
}; };
} }
var members = try allocator.alloc(registry.Container.Field, ty.children.count()); var members = try allocator.alloc(registry.Container.Field, ty.children.items.len);
var i: usize = 0; var i: usize = 0;
var it = ty.findChildrenByTag("member"); var it = ty.findChildrenByTag("member");
@@ -322,7 +322,7 @@ fn parseEnumFields(allocator: *Allocator, elem: *xml.Element) !registry.Enum {
return error.InvalidRegistry; return error.InvalidRegistry;
} }
const fields = try allocator.alloc(registry.Enum.Field, elem.children.count()); const fields = try allocator.alloc(registry.Enum.Field, elem.children.items.len);
var i: usize = 0; var i: usize = 0;
var it = elem.findChildrenByTag("enum"); var it = elem.findChildrenByTag("enum");
@@ -414,7 +414,7 @@ fn parseCommand(allocator: *Allocator, elem: *xml.Element) !registry.Declaration
var proto_xctok = cparse.XmlCTokenizer.init(proto); var proto_xctok = cparse.XmlCTokenizer.init(proto);
const command_decl = try cparse.parseParamOrProto(allocator, &proto_xctok); const command_decl = try cparse.parseParamOrProto(allocator, &proto_xctok);
var params = try allocator.alloc(registry.Command.Param, elem.children.count()); var params = try allocator.alloc(registry.Command.Param, elem.children.items.len);
var i: usize = 0; var i: usize = 0;
var it = elem.findChildrenByTag("param"); var it = elem.findChildrenByTag("param");
@@ -490,7 +490,7 @@ fn parseApiConstants(allocator: *Allocator, root: *xml.Element) ![]registry.ApiC
break :blk n_defines; break :blk n_defines;
}; };
const constants = try allocator.alloc(registry.ApiConstant, enums.children.count() + n_defines); const constants = try allocator.alloc(registry.ApiConstant, enums.children.items.len + n_defines);
var i: usize = 0; var i: usize = 0;
var it = enums.findChildrenByTag("enum"); var it = enums.findChildrenByTag("enum");
@@ -527,7 +527,7 @@ fn parseDefines(types: *xml.Element, out: []registry.ApiConstant) !usize {
if (mem.eql(u8, name, "VK_HEADER_VERSION")) { if (mem.eql(u8, name, "VK_HEADER_VERSION")) {
out[i] = .{ out[i] = .{
.name = name, .name = name,
.value = .{.expr = mem.trim(u8, ty.children.at(2).CharData, " ")}, .value = .{.expr = mem.trim(u8, ty.children.items[2].CharData, " ")},
}; };
} else { } else {
var xctok = cparse.XmlCTokenizer.init(ty); var xctok = cparse.XmlCTokenizer.init(ty);
@@ -546,7 +546,7 @@ fn parseDefines(types: *xml.Element, out: []registry.ApiConstant) !usize {
fn parseTags(allocator: *Allocator, root: *xml.Element) ![]registry.Tag { fn parseTags(allocator: *Allocator, root: *xml.Element) ![]registry.Tag {
var tags_elem = root.findChildByTag("tags") orelse return error.InvalidRegistry; var tags_elem = root.findChildByTag("tags") orelse return error.InvalidRegistry;
const tags = try allocator.alloc(registry.Tag, tags_elem.children.count()); const tags = try allocator.alloc(registry.Tag, tags_elem.children.items.len);
var i: usize = 0; var i: usize = 0;
var it = tags_elem.findChildrenByTag("tag"); var it = tags_elem.findChildrenByTag("tag");
@@ -585,7 +585,7 @@ fn parseFeature(allocator: *Allocator, feature: *xml.Element) !registry.Feature
break :blk try splitFeatureLevel(number, "."); break :blk try splitFeatureLevel(number, ".");
}; };
var requires = try allocator.alloc(registry.Require, feature.children.count()); var requires = try allocator.alloc(registry.Require, feature.children.items.len);
var i: usize = 0; var i: usize = 0;
var it = feature.findChildrenByTag("require"); var it = feature.findChildrenByTag("require");
while (it.next()) |require| { while (it.next()) |require| {
@@ -707,7 +707,7 @@ fn parseRequire(allocator: *Allocator, require: *xml.Element, extnumber: ?u31) !
fn parseExtensions(allocator: *Allocator, root: *xml.Element) ![]registry.Extension { fn parseExtensions(allocator: *Allocator, root: *xml.Element) ![]registry.Extension {
const extensions_elem = root.findChildByTag("extensions") orelse return error.InvalidRegistry; const extensions_elem = root.findChildByTag("extensions") orelse return error.InvalidRegistry;
const extensions = try allocator.alloc(registry.Extension, extensions_elem.children.count()); const extensions = try allocator.alloc(registry.Extension, extensions_elem.children.items.len);
var i: usize = 0; var i: usize = 0;
var it = extensions_elem.findChildrenByTag("extension"); var it = extensions_elem.findChildrenByTag("extension");
while (it.next()) |extension| { while (it.next()) |extension| {
@@ -786,7 +786,7 @@ fn parseExtension(allocator: *Allocator, extension: *xml.Element) !registry.Exte
break :blk try splitCommaAlloc(allocator, requires_str); break :blk try splitCommaAlloc(allocator, requires_str);
}; };
var requires = try allocator.alloc(registry.Require, extension.children.count()); var requires = try allocator.alloc(registry.Require, extension.children.items.len);
var i: usize = 0; var i: usize = 0;
var it = extension.findChildrenByTag("require"); var it = extension.findChildrenByTag("require");
while (it.next()) |require| { while (it.next()) |require| {

View File

@@ -3,7 +3,7 @@ const mem = std.mem;
const testing = std.testing; const testing = std.testing;
const Allocator = mem.Allocator; const Allocator = mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator; const ArenaAllocator = std.heap.ArenaAllocator;
const SegmentedList = std.SegmentedList; const ArrayList = std.ArrayList;
pub const Attribute = struct { pub const Attribute = struct {
name: []const u8, name: []const u8,
@@ -17,8 +17,8 @@ pub const Content = union(enum) {
}; };
pub const Element = struct { pub const Element = struct {
pub const AttributeList = SegmentedList(*Attribute, 1); pub const AttributeList = ArrayList(*Attribute);
pub const ContentList = SegmentedList(Content, 0); pub const ContentList = ArrayList(Content);
tag: []const u8, tag: []const u8,
attributes: AttributeList, attributes: AttributeList,
@@ -33,10 +33,9 @@ pub const Element = struct {
} }
pub fn getAttribute(self: *Element, attrib_name: []const u8) ?[]const u8 { pub fn getAttribute(self: *Element, attrib_name: []const u8) ?[]const u8 {
var it = self.attributes.iterator(0); for (self.attributes.items) |child| {
while (it.next()) |child| { if (mem.eql(u8, child.name, attrib_name)) {
if (mem.eql(u8, child.*.name, attrib_name)) { return child.value;
return child.*.value;
} }
} }
@@ -45,19 +44,26 @@ pub const Element = struct {
pub fn getCharData(self: *Element, child_tag: []const u8) ?[]const u8 { pub fn getCharData(self: *Element, child_tag: []const u8) ?[]const u8 {
const child = self.findChildByTag(child_tag) orelse return null; const child = self.findChildByTag(child_tag) orelse return null;
if (child.children.count() != 1) { if (child.children.items.len != 1) {
return null; return null;
} }
return switch (child.children.at(0).*) { return switch (child.children.items[0]) {
.CharData => |char_data| char_data, .CharData => |char_data| char_data,
else => null else => null
}; };
} }
pub fn iterator(self: *Element) ChildIterator {
return .{
.items = self.children.items,
.i = 0,
};
}
pub fn elements(self: *Element) ChildElementIterator { pub fn elements(self: *Element) ChildElementIterator {
return .{ return .{
.inner = self.children.iterator(0), .inner = self.iterator(),
}; };
} }
@@ -72,8 +78,22 @@ pub const Element = struct {
}; };
} }
pub const ChildIterator = struct {
items: []Content,
i: usize,
pub fn next(self: *ChildIterator) ?*Content {
if (self.i < self.items.len) {
self.i += 1;
return &self.items[self.i - 1];
}
return null;
}
};
pub const ChildElementIterator = struct { pub const ChildElementIterator = struct {
inner: ContentList.Iterator, inner: ChildIterator,
pub fn next(self: *ChildElementIterator) ?*Element { pub fn next(self: *ChildElementIterator) ?*Element {
while (self.inner.next()) |child| { while (self.inner.next()) |child| {
@@ -415,7 +435,7 @@ fn tryParseElement(ctx: *ParseContext, alloc: *Allocator) !?*Element {
while (ctx.eatWs()) { while (ctx.eatWs()) {
const attr = (try tryParseAttr(ctx, alloc)) orelse break; const attr = (try tryParseAttr(ctx, alloc)) orelse break;
try element.attributes.push(attr); try element.attributes.append(attr);
} }
if (ctx.eatStr("/>")) { if (ctx.eatStr("/>")) {
@@ -432,7 +452,7 @@ fn tryParseElement(ctx: *ParseContext, alloc: *Allocator) !?*Element {
} }
const content = try parseContent(ctx, alloc); const content = try parseContent(ctx, alloc);
try element.children.push(content); try element.children.append(content);
} }
const closing_tag = try parseNameNoDupe(ctx); const closing_tag = try parseNameNoDupe(ctx);