Use ArrayList less.
This commit is contained in:
parent
2ac78e21e6
commit
918cde57c1
|
@ -41,7 +41,7 @@ pub const Client = struct {
|
|||
try map.put("ClientDate", .{ .String = "Wed, 02 Jun 2021 17:30:30 +0100" });
|
||||
try map.put("Features", .{ .Int = 0 });
|
||||
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 dumpDebug("ClientInit.bin", list);
|
||||
|
|
|
@ -7,10 +7,10 @@ pub const QVariant = union(enum) {
|
|||
Short: u16,
|
||||
Byte: u8,
|
||||
String: []const u8,
|
||||
QByteArray: std.ArrayList(u8),
|
||||
QStringList: std.ArrayList([]const u8),
|
||||
QByteArray: []u8,
|
||||
QStringList: [][]const u8,
|
||||
QVariantList: []QVariant,
|
||||
QVariantMap: std.StringHashMap(QVariant),
|
||||
QVariantList: std.ArrayList(QVariant),
|
||||
};
|
||||
|
||||
pub const QVariantTypes = enum(u32) {
|
||||
|
@ -91,7 +91,7 @@ pub fn pretty_print_variant(variant: QVariant, indentLevel: u64) void {
|
|||
.QVariantList => |l| {
|
||||
std.debug.print("QVariantList:\n", .{});
|
||||
|
||||
for (l.items) |varient, i| {
|
||||
for (l) |varient, i| {
|
||||
print_indent_level(indentLevel + 1);
|
||||
std.debug.print("Item #{d}\n", .{i});
|
||||
pretty_print_variant(varient, indentLevel + 2);
|
||||
|
@ -103,7 +103,7 @@ pub fn pretty_print_variant(variant: QVariant, indentLevel: u64) void {
|
|||
.QStringList => |l| {
|
||||
std.debug.print("QStringList:\n", .{});
|
||||
|
||||
for (l.items) |str, i| {
|
||||
for (l) |str, i| {
|
||||
print_indent_level(indentLevel + 1);
|
||||
std.debug.print("\"{s}\" \n", .{str});
|
||||
}
|
||||
|
|
|
@ -22,16 +22,17 @@ pub fn readSignedByte(reader: anytype) !i8 {
|
|||
return try reader.readIntBig(i8);
|
||||
}
|
||||
|
||||
pub fn readQByteArray(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList(u8) {
|
||||
var byteList = std.ArrayList(u8).init(allocator);
|
||||
pub fn readQByteArray(reader: anytype, allocator: *std.mem.Allocator) ![]u8 {
|
||||
var length = try readUInt(reader);
|
||||
|
||||
var byteList = try allocator.alloc(u8, @intCast(usize, length));
|
||||
|
||||
var index: usize = 0;
|
||||
while (true) {
|
||||
if (index == length) return byteList;
|
||||
|
||||
const byte = try readByte(reader);
|
||||
try byteList.append(byte);
|
||||
byteList[index] = byte;
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
@ -60,26 +61,42 @@ pub fn readString(reader: anytype, allocator: *std.mem.Allocator) ![]u8 {
|
|||
return ut8Str;
|
||||
}
|
||||
|
||||
pub fn readQVariantList(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList(qvar.QVariant) {
|
||||
var varlist = std.ArrayList(qvar.QVariant).init(allocator);
|
||||
var size = try readUInt(reader);
|
||||
var i: u32 = 0;
|
||||
while (i < size) {
|
||||
try varlist.append(try readQVariant(reader, allocator));
|
||||
i = i + 1;
|
||||
pub fn readQVariantList(reader: anytype, allocator: *std.mem.Allocator) ![]qvar.QVariant {
|
||||
var length = try readUInt(reader);
|
||||
var variantList = try allocator.alloc(qvar.QVariant, @intCast(usize, length));
|
||||
|
||||
var iter = range(u32, 0, length);
|
||||
while (iter.next()) |i| {
|
||||
variantList[i] = try readQVariant(reader, allocator);
|
||||
}
|
||||
return varlist;
|
||||
|
||||
return variantList;
|
||||
}
|
||||
|
||||
pub fn readQStringList(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList([]const u8) {
|
||||
var strList = std.ArrayList([]const u8).init(allocator);
|
||||
var size = try readUInt(reader);
|
||||
var i: u32 = 0;
|
||||
while (i < size) {
|
||||
try strList.append(try readString(reader, allocator));
|
||||
i = i + 1;
|
||||
pub fn readQStringList(reader: anytype, allocator: *std.mem.Allocator) ![][]const u8 {
|
||||
var length = try readUInt(reader);
|
||||
var stringList = try allocator.alloc([]const u8, @intCast(usize, length));
|
||||
|
||||
var iter = range(u32, 0, length);
|
||||
while (iter.next()) |i| {
|
||||
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 {
|
||||
|
@ -121,8 +138,8 @@ pub fn readQVariantT(reader: anytype, type_id: u32, allocator: *std.mem.Allocato
|
|||
return qvar.QVariant{ .Short = short };
|
||||
},
|
||||
else => {
|
||||
@panic("Unknown Type");
|
||||
}
|
||||
@panic("Unknown Type");
|
||||
},
|
||||
}
|
||||
return qvar.QVariant{ .empty = true };
|
||||
}
|
||||
|
@ -132,17 +149,3 @@ pub fn readQVariant(reader: anytype, allocator: *std.mem.Allocator) !qvar.QVaria
|
|||
_ = try readByte(reader);
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -19,9 +19,9 @@ pub fn writeByte(writer: anytype, byte: u8) !void {
|
|||
try writer.writeByte(byte);
|
||||
}
|
||||
|
||||
pub fn writeByteArray(writer: anytype, array: std.ArrayList(u8)) !void {
|
||||
try writeUInt(writer, @intCast(u32, array.items.len));
|
||||
try writer.writeAll(array.items);
|
||||
pub fn writeByteArray(writer: anytype, array: []u8) !void {
|
||||
try writeUInt(writer, @intCast(u32, array.len));
|
||||
try writer.writeAll(array);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
pub fn writeQStringList(writer: anytype, allocator: *std.mem.Allocator, strList: std.ArrayList([]const u8)) !void {
|
||||
try writeUInt(writer, @intCast(u32, strList.items.len));
|
||||
for (strList.items) |string| {
|
||||
pub fn writeQStringList(writer: anytype, allocator: *std.mem.Allocator, strList: [][]const u8) !void {
|
||||
try writeUInt(writer, @intCast(u32, strList.len));
|
||||
for (strList) |string| {
|
||||
try writeString(writer, allocator, string);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn writeQVariantList(writer: anytype, allocator: *std.mem.Allocator, varList: std.ArrayList(qvar.QVariant)) AllKnownErrors!void {
|
||||
try writeUInt(writer, @intCast(u32, varList.items.len));
|
||||
for (varList.items) |v| {
|
||||
pub fn writeQVariantList(writer: anytype, allocator: *std.mem.Allocator, varList: []qvar.QVariant) AllKnownErrors!void {
|
||||
try writeUInt(writer, @intCast(u32, varList.len));
|
||||
for (varList) |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
|
||||
pub fn writeQVariantHeader(writer: anytype, type_id: u32) !void {
|
||||
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 {
|
||||
var data = std.ArrayList(u8).init(allocator);
|
||||
try writeQVariant(data.writer(), allocator, .{
|
||||
|
|
Loading…
Reference in a new issue