From 4787127cf6418f7a819c9d6f07a9046d76e0de65 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 10 May 2018 00:29:49 -0400 Subject: partial conversion to post-fix pointer deref using zig fmt --- std/buffer.zig | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'std/buffer.zig') diff --git a/std/buffer.zig b/std/buffer.zig index 041d891dec..cf530c3c9e 100644 --- a/std/buffer.zig +++ b/std/buffer.zig @@ -31,9 +31,7 @@ pub const Buffer = struct { /// * ::replaceContentsBuffer /// * ::resize pub fn initNull(allocator: &Allocator) Buffer { - return Buffer { - .list = ArrayList(u8).init(allocator), - }; + return Buffer{ .list = ArrayList(u8).init(allocator) }; } /// Must deinitialize with deinit. @@ -45,9 +43,7 @@ pub const Buffer = struct { /// allocated with `allocator`. /// Must deinitialize with deinit. pub fn fromOwnedSlice(allocator: &Allocator, slice: []u8) Buffer { - var self = Buffer { - .list = ArrayList(u8).fromOwnedSlice(allocator, slice), - }; + var self = Buffer{ .list = ArrayList(u8).fromOwnedSlice(allocator, slice) }; self.list.append(0); return self; } @@ -57,11 +53,10 @@ pub const Buffer = struct { pub fn toOwnedSlice(self: &Buffer) []u8 { const allocator = self.list.allocator; const result = allocator.shrink(u8, self.list.items, self.len()); - *self = initNull(allocator); + self.* = initNull(allocator); return result; } - pub fn deinit(self: &Buffer) void { self.list.deinit(); } -- cgit v1.2.3 From 6e821078f625a03eb8b7794c983da0f7793366ab Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 11 May 2018 14:08:16 -0400 Subject: update std.Buffer API * remove Buffer.appendFormat * remove Buffer.appendByte * remove Buffer.appendByteNTimes Added test to demo what to use instead of the above functions --- std/buffer.zig | 24 ++++-------------------- std/io_test.zig | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 21 deletions(-) (limited to 'std/buffer.zig') diff --git a/std/buffer.zig b/std/buffer.zig index 041d891dec..42fec7f988 100644 --- a/std/buffer.zig +++ b/std/buffer.zig @@ -99,26 +99,10 @@ pub const Buffer = struct { mem.copy(u8, self.list.toSlice()[old_len..], m); } - // TODO: remove, use OutStream for this - pub fn appendFormat(self: &Buffer, comptime format: []const u8, args: ...) !void { - return fmt.format(self, append, format, args); - } - - // TODO: remove, use OutStream for this pub fn appendByte(self: &Buffer, byte: u8) !void { - return self.appendByteNTimes(byte, 1); - } - - // TODO: remove, use OutStream for this - pub fn appendByteNTimes(self: &Buffer, byte: u8, count: usize) !void { - var prev_size: usize = self.len(); - const new_size = prev_size + count; - try self.resize(new_size); - - var i: usize = prev_size; - while (i < new_size) : (i += 1) { - self.list.items[i] = byte; - } + const old_len = self.len(); + try self.resize(old_len + 1); + self.list.toSlice()[old_len] = byte; } pub fn eql(self: &const Buffer, m: []const u8) bool { @@ -154,7 +138,7 @@ test "simple Buffer" { var buf = try Buffer.init(debug.global_allocator, ""); assert(buf.len() == 0); try buf.append("hello"); - try buf.appendByte(' '); + try buf.append(" "); try buf.append("world"); assert(buf.eql("hello world")); assert(mem.eql(u8, cstr.toSliceConst(buf.toSliceConst().ptr), buf.toSliceConst())); diff --git a/std/io_test.zig b/std/io_test.zig index 89959b7b54..5f53556785 100644 --- a/std/io_test.zig +++ b/std/io_test.zig @@ -1,6 +1,5 @@ const std = @import("index.zig"); const io = std.io; -const allocator = std.debug.global_allocator; const DefaultPrng = std.rand.DefaultPrng; const assert = std.debug.assert; const mem = std.mem; @@ -8,6 +7,9 @@ const os = std.os; const builtin = @import("builtin"); test "write a file, read it, then delete it" { + var raw_bytes: [200 * 1024]u8 = undefined; + var allocator = &std.heap.FixedBufferAllocator.init(raw_bytes[0..]).allocator; + var data: [1024]u8 = undefined; var prng = DefaultPrng.init(1234); prng.random.bytes(data[0..]); @@ -44,3 +46,17 @@ test "write a file, read it, then delete it" { } try os.deleteFile(allocator, tmp_file_name); } + +test "BufferOutStream" { + var bytes: [100]u8 = undefined; + var allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; + + var buffer = try std.Buffer.initSize(allocator, 0); + var buf_stream = &std.io.BufferOutStream.init(&buffer).stream; + + const x: i32 = 42; + const y: i32 = 1234; + try buf_stream.print("x: {}\ny: {}\n", x, y); + + assert(mem.eql(u8, buffer.toSlice(), "x: 42\ny: 1234\n")); +} -- cgit v1.2.3