Use ArrayList less.

This commit is contained in:
Kitteh 2021-06-02 19:58:02 +01:00
parent 2ac78e21e6
commit 918cde57c1
4 changed files with 69 additions and 66 deletions

View file

@ -41,7 +41,7 @@ pub const Client = struct {
try map.put("ClientDate", .{ .String = "Wed, 02 Jun 2021 17:30:30 +0100" }); try map.put("ClientDate", .{ .String = "Wed, 02 Jun 2021 17:30:30 +0100" });
try map.put("Features", .{ .Int = 0 }); try map.put("Features", .{ .Int = 0 });
var empty = std.ArrayList([]const u8).init(s.allocator); var empty = std.ArrayList([]const u8).init(s.allocator);
try map.put("FeatureList", .{ .QStringList = empty }); try map.put("FeatureList", .{ .QStringList = empty.items });
try write.writeFrame(list.writer(), s.allocator, map); try write.writeFrame(list.writer(), s.allocator, map);
try dumpDebug("ClientInit.bin", list); try dumpDebug("ClientInit.bin", list);

View file

@ -7,10 +7,10 @@ pub const QVariant = union(enum) {
Short: u16, Short: u16,
Byte: u8, Byte: u8,
String: []const u8, String: []const u8,
QByteArray: std.ArrayList(u8), QByteArray: []u8,
QStringList: std.ArrayList([]const u8), QStringList: [][]const u8,
QVariantList: []QVariant,
QVariantMap: std.StringHashMap(QVariant), QVariantMap: std.StringHashMap(QVariant),
QVariantList: std.ArrayList(QVariant),
}; };
pub const QVariantTypes = enum(u32) { pub const QVariantTypes = enum(u32) {
@ -91,7 +91,7 @@ pub fn pretty_print_variant(variant: QVariant, indentLevel: u64) void {
.QVariantList => |l| { .QVariantList => |l| {
std.debug.print("QVariantList:\n", .{}); std.debug.print("QVariantList:\n", .{});
for (l.items) |varient, i| { for (l) |varient, i| {
print_indent_level(indentLevel + 1); print_indent_level(indentLevel + 1);
std.debug.print("Item #{d}\n", .{i}); std.debug.print("Item #{d}\n", .{i});
pretty_print_variant(varient, indentLevel + 2); pretty_print_variant(varient, indentLevel + 2);
@ -103,7 +103,7 @@ pub fn pretty_print_variant(variant: QVariant, indentLevel: u64) void {
.QStringList => |l| { .QStringList => |l| {
std.debug.print("QStringList:\n", .{}); std.debug.print("QStringList:\n", .{});
for (l.items) |str, i| { for (l) |str, i| {
print_indent_level(indentLevel + 1); print_indent_level(indentLevel + 1);
std.debug.print("\"{s}\" \n", .{str}); std.debug.print("\"{s}\" \n", .{str});
} }

View file

@ -22,16 +22,17 @@ pub fn readSignedByte(reader: anytype) !i8 {
return try reader.readIntBig(i8); return try reader.readIntBig(i8);
} }
pub fn readQByteArray(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList(u8) { pub fn readQByteArray(reader: anytype, allocator: *std.mem.Allocator) ![]u8 {
var byteList = std.ArrayList(u8).init(allocator);
var length = try readUInt(reader); var length = try readUInt(reader);
var byteList = try allocator.alloc(u8, @intCast(usize, length));
var index: usize = 0; var index: usize = 0;
while (true) { while (true) {
if (index == length) return byteList; if (index == length) return byteList;
const byte = try readByte(reader); const byte = try readByte(reader);
try byteList.append(byte); byteList[index] = byte;
index += 1; index += 1;
} }
} }
@ -60,26 +61,42 @@ pub fn readString(reader: anytype, allocator: *std.mem.Allocator) ![]u8 {
return ut8Str; return ut8Str;
} }
pub fn readQVariantList(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList(qvar.QVariant) { pub fn readQVariantList(reader: anytype, allocator: *std.mem.Allocator) ![]qvar.QVariant {
var varlist = std.ArrayList(qvar.QVariant).init(allocator); var length = try readUInt(reader);
var size = try readUInt(reader); var variantList = try allocator.alloc(qvar.QVariant, @intCast(usize, length));
var i: u32 = 0;
while (i < size) { var iter = range(u32, 0, length);
try varlist.append(try readQVariant(reader, allocator)); while (iter.next()) |i| {
i = i + 1; variantList[i] = try readQVariant(reader, allocator);
} }
return varlist;
return variantList;
} }
pub fn readQStringList(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList([]const u8) { pub fn readQStringList(reader: anytype, allocator: *std.mem.Allocator) ![][]const u8 {
var strList = std.ArrayList([]const u8).init(allocator); var length = try readUInt(reader);
var size = try readUInt(reader); var stringList = try allocator.alloc([]const u8, @intCast(usize, length));
var i: u32 = 0;
while (i < size) { var iter = range(u32, 0, length);
try strList.append(try readString(reader, allocator)); while (iter.next()) |i| {
i = i + 1; stringList[i] = try readString(reader, allocator);
} }
return strList;
return stringList;
}
pub fn readQVariantMap(reader: anytype, allocator: *std.mem.Allocator) !std.StringHashMap(qvar.QVariant) {
var map = std.StringHashMap(qvar.QVariant).init(allocator);
var numItems = try readUInt(reader);
var iter = range(u32, 0, numItems);
while (iter.next()) |i| {
var key = try readString(reader, allocator);
var value = try readQVariant(reader, allocator);
try map.put(key, value);
}
return map;
} }
pub fn readQVariantT(reader: anytype, type_id: u32, allocator: *std.mem.Allocator) (@TypeOf(reader).Error || error{EndOfStream} || error{OutOfMemory})!qvar.QVariant { pub fn readQVariantT(reader: anytype, type_id: u32, allocator: *std.mem.Allocator) (@TypeOf(reader).Error || error{EndOfStream} || error{OutOfMemory})!qvar.QVariant {
@ -121,8 +138,8 @@ pub fn readQVariantT(reader: anytype, type_id: u32, allocator: *std.mem.Allocato
return qvar.QVariant{ .Short = short }; return qvar.QVariant{ .Short = short };
}, },
else => { else => {
@panic("Unknown Type"); @panic("Unknown Type");
} },
} }
return qvar.QVariant{ .empty = true }; return qvar.QVariant{ .empty = true };
} }
@ -132,17 +149,3 @@ pub fn readQVariant(reader: anytype, allocator: *std.mem.Allocator) !qvar.QVaria
_ = try readByte(reader); _ = try readByte(reader);
return try readQVariantT(reader, type_id, allocator); return try readQVariantT(reader, type_id, allocator);
} }
pub fn readQVariantMap(reader: anytype, allocator: *std.mem.Allocator) !std.StringHashMap(qvar.QVariant) {
var map = std.StringHashMap(qvar.QVariant).init(allocator);
var numItems = try readUInt(reader);
var iter = range(u32, 0, numItems);
while (iter.next()) |i| {
var key = try readString(reader, allocator);
var value = try readQVariant(reader, allocator);
try map.put(key, value);
}
return map;
}

View file

@ -19,9 +19,9 @@ pub fn writeByte(writer: anytype, byte: u8) !void {
try writer.writeByte(byte); try writer.writeByte(byte);
} }
pub fn writeByteArray(writer: anytype, array: std.ArrayList(u8)) !void { pub fn writeByteArray(writer: anytype, array: []u8) !void {
try writeUInt(writer, @intCast(u32, array.items.len)); try writeUInt(writer, @intCast(u32, array.len));
try writer.writeAll(array.items); try writer.writeAll(array);
} }
pub fn writeString(writer: anytype, allocator: *std.mem.Allocator, str: []const u8) !void { pub fn writeString(writer: anytype, allocator: *std.mem.Allocator, str: []const u8) !void {
@ -35,20 +35,36 @@ pub fn writeString(writer: anytype, allocator: *std.mem.Allocator, str: []const
try writer.writeAll(std.mem.sliceAsBytes(ut16Str)); try writer.writeAll(std.mem.sliceAsBytes(ut16Str));
} }
pub fn writeQStringList(writer: anytype, allocator: *std.mem.Allocator, strList: std.ArrayList([]const u8)) !void { pub fn writeQStringList(writer: anytype, allocator: *std.mem.Allocator, strList: [][]const u8) !void {
try writeUInt(writer, @intCast(u32, strList.items.len)); try writeUInt(writer, @intCast(u32, strList.len));
for (strList.items) |string| { for (strList) |string| {
try writeString(writer, allocator, string); try writeString(writer, allocator, string);
} }
} }
pub fn writeQVariantList(writer: anytype, allocator: *std.mem.Allocator, varList: std.ArrayList(qvar.QVariant)) AllKnownErrors!void { pub fn writeQVariantList(writer: anytype, allocator: *std.mem.Allocator, varList: []qvar.QVariant) AllKnownErrors!void {
try writeUInt(writer, @intCast(u32, varList.items.len)); try writeUInt(writer, @intCast(u32, varList.len));
for (varList.items) |v| { for (varList) |v| {
try writeQVariant(writer, allocator, v); try writeQVariant(writer, allocator, v);
} }
} }
pub fn writeQVariantMap(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(qvar.QVariant)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void {
var data = std.ArrayList(u8).init(allocator);
defer data.deinit();
var writeIterator = map.iterator();
while (writeIterator.next()) |entry| {
try writeString(data.writer(), allocator, entry.key);
try writeQVariant(data.writer(), allocator, entry.value);
}
try writeUInt(writer, map.count());
try writer.writeAll(data.items);
return;
}
// Call this and Then write your type // Call this and Then write your type
pub fn writeQVariantHeader(writer: anytype, type_id: u32) !void { pub fn writeQVariantHeader(writer: anytype, type_id: u32) !void {
try writeUInt(writer, type_id); try writeUInt(writer, type_id);
@ -91,22 +107,6 @@ pub fn writeQVariant(writer: anytype, allocator: *std.mem.Allocator, variant: qv
} }
} }
pub fn writeQVariantMap(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(qvar.QVariant)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void {
var data = std.ArrayList(u8).init(allocator);
defer data.deinit();
var writeIterator = map.iterator();
while (writeIterator.next()) |entry| {
try writeString(data.writer(), allocator, entry.key);
try writeQVariant(data.writer(), allocator, entry.value);
}
try writeUInt(writer, map.count());
try writer.writeAll(data.items);
return;
}
pub fn writeFrame(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(qvar.QVariant)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void { pub fn writeFrame(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(qvar.QVariant)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void {
var data = std.ArrayList(u8).init(allocator); var data = std.ArrayList(u8).init(allocator);
try writeQVariant(data.writer(), allocator, .{ try writeQVariant(data.writer(), allocator, .{