portingtools/src/StringPacket.zig

22 lines
476 B
Zig

const std = @import("std");
str: []const u8,
const Self = @This();
pub fn read(reader: anytype, alloc: std.mem.Allocator) !Self {
const strlen = try reader.readIntBig(u32);
const str = try alloc.alloc(u8, strlen);
errdefer alloc.free(str);
try reader.readNoEof(str);
return .{ .str = str };
}
pub fn write(self: *const Self, writer: anytype) !void {
try writer.writeIntBig(u32, @intCast(u32, self.str.len));
try writer.writeAll(self.str);
}