portingtools/src/StringPacket.zig

22 lines
477 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.readInt(u32, .big);
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.writeInt(u32, @intCast(self.str.len), .big);
try writer.writeAll(self.str);
}