Add string type.

This commit is contained in:
Kitteh 2021-06-01 11:22:56 +01:00
parent ed251c7e54
commit 22026ad078
7 changed files with 15 additions and 9 deletions

View file

@ -1,3 +1,6 @@
const std = @import("std");
pub fn get_int(reader: anytype) !u32 {
return try reader.readIntBig(u32);
}
@ -10,12 +13,12 @@ pub fn get_byte(reader: anytype) !u8 {
return try reader.readByte();
}
pub fn get_string(reader: anytype, allocator: std.mem.Allocator) ![]u8 {
pub fn get_string(reader: anytype, allocator: *std.mem.Allocator) ![]u8 {
var buffer: [1024]u8 = undefined;
return try reader.readUntilDelimiterAlloc(
allocator,
'\0',
'\x00',
1024,
);
}

View file

@ -44,10 +44,12 @@ test "read/write string" {
var byteList = std.ArrayList(u8).init(std.heap.page_allocator);
defer byteList.deinit();
try write.add_string(byteList.writer(), "Hello World!");
var arr = "Hello World!".*;
try write.add_string(byteList.writer(), &arr);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_string(fBS.reader());
var val = try read.get_string(fBS.reader(), std.heap.page_allocator);
try expect(std.mem.eql(u8, val, "Hello World!"));
}

View file

@ -10,7 +10,7 @@ pub fn add_byte(writer: anytype, byte: u8) !void {
try writer.writeByte(byte);
}
pub fn add_string(writer: anytype, str: []u8) !void {
pub fn add_string(writer: anytype, str: []const u8) !void {
try writer.writeAll(str);
try writer.writeByte('\0');
try writer.writeByte('\x00');
}

View file

@ -1,4 +1,4 @@
1062 1052843 1622541843780976996 537312898de534a981824375ec7211f7 /home/username/Projects/QuasselClient/src/qtshit/test.zig
1460 1052843 1622542926812273719 1cda0203593f0bff7b4c0428d9cabadb /home/username/Projects/QuasselClient/src/qtshit/test.zig
5588 1081028 1622401981000000000 b28e8846a1fb551a569b56fcf123a093 /home/username/Zig/lib/std/std.zig
23635 1081029 1622401981000000000 00fbb8fea25ef05eeba646e26bb20cb6 /home/username/Zig/lib/std/builtin.zig
5086 1080039 1622401981000000000 d6192441c7258db0547cd11c747ddbd1 /home/username/Zig/lib/std/special/test_runner.zig
@ -12,11 +12,11 @@
102633 1080836 1622401981000000000 826ba94a31dac302fd13d076454534d6 /home/username/Zig/lib/std/mem.zig
20541 1081068 1622401981000000000 df4b1840f51e60f5bdbc1dbedc7e092a /home/username/Zig/lib/std/mem/Allocator.zig
44450 1080705 1622401981000000000 61ecf1dfc1bbc08a4fc62b44be4f2fcc /home/username/Zig/lib/std/heap.zig
389 1068873 1622542030348440681 088f9cea3c75b70a90a403a80b9d43e4 /home/username/Projects/QuasselClient/src/qtshit/write.zig
397 1068873 1622542904155510143 b895c1d606d849b64504a3a6cd360971 /home/username/Projects/QuasselClient/src/qtshit/write.zig
6937 1081250 1622401981000000000 13d91209e7be79b263b6bdba75c5d7bd /home/username/Zig/lib/std/io.zig
3733 1081259 1622401981000000000 21a631d05e989504b4ff3183a5d4268d /home/username/Zig/lib/std/io/writer.zig
5865 1081271 1622401981000000000 e4969eefc8d5e0daa48c3e49f4d7bf71 /home/username/Zig/lib/std/io/fixed_buffer_stream.zig
232 1061251 1622541451275968778 79d4a90da2f289f7bb97bfddaf2a44ec /home/username/Projects/QuasselClient/src/qtshit/read.zig
463 1061251 1622542961032420085 8ee4d73272b01493368324a481b35af9 /home/username/Projects/QuasselClient/src/qtshit/read.zig
14707 1081262 1622401981000000000 ef35513a84751410d70fd0001a426f7c /home/username/Zig/lib/std/io/reader.zig
17680 1081150 1622401981000000000 d776aafb7ce0e1372623be706e5dba7c /home/username/Zig/lib/std/testing.zig
22756 1081122 1622401981000000000 d07ee73ddafae63b19ceef726d648e26 /home/username/Zig/lib/std/Thread.zig

View file

@ -0,0 +1 @@
75055f28f06f4f90c24164e5a3f6ae2300