authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-28 03:07:11-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-28 03:07:11-05:00
log9eb29e81f956080b358a79d3567204f656350620
tree112ce872d3e8150cb7e624d314385fc058c6f50d
parent1195994880b6a83e61807381df2721ac5256e876

rename CBuf to Buffer0 and some minor std API changes


5 files changed, 115 insertions(+), 69 deletions(-)

std/cstr.zig+59-48
...@@ -34,131 +34,142 @@ pub fn toSlice(str: &u8) -> []u8 {...@@ -34,131 +34,142 @@ pub fn toSlice(str: &u8) -> []u8 {
3434
3535
36/// A buffer that allocates memory and maintains a null byte at the end.36/// A buffer that allocates memory and maintains a null byte at the end.
37pub const CBuf = struct {37pub const Buffer0 = struct {
38 list: List(u8),38 list: List(u8),
3939
40 /// Must deinitialize with deinit.40 /// Must deinitialize with deinit.
41 pub fn initEmpty(allocator: &Allocator) -> %CBuf {41 pub fn initEmpty(allocator: &Allocator) -> %Buffer0 {
42 var self = CBuf {42 return initSize(allocator, 0);
43 .list = List(u8).init(allocator),
44 };
45 %return self.resize(0);
46 return self;
47 }43 }
4844
49 /// Must deinitialize with deinit.45 /// Must deinitialize with deinit.
50 pub fn initFromMem(allocator: &Allocator, m: []const u8) -> %CBuf {46 pub fn initFromMem(allocator: &Allocator, m: []const u8) -> %Buffer0 {
51 var self = CBuf {47 var self = %return initSize(allocator, m.len);
52 .list = List(u8).init(allocator),
53 };
54 %return self.resize(m.len);
55 mem.copy(u8, self.list.items, m);48 mem.copy(u8, self.list.items, m);
56 return self;49 return self;
57 }50 }
5851
59 /// Must deinitialize with deinit.52 /// Must deinitialize with deinit.
60 pub fn initFromCStr(allocator: &Allocator, s: &const u8) -> %CBuf {53 pub fn initFromCStr(allocator: &Allocator, s: &const u8) -> %Buffer0 {
61 return CBuf.initFromMem(allocator, s[0...strlen(s)]);54 return Buffer0.initFromMem(allocator, s[0...strlen(s)]);
62 }55 }
6356
64 /// Must deinitialize with deinit.57 /// Must deinitialize with deinit.
65 pub fn initFromCBuf(cbuf: &const CBuf) -> %CBuf {58 pub fn initFromOther(cbuf: &const Buffer0) -> %Buffer0 {
66 return CBuf.initFromMem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()]);59 return Buffer0.initFromMem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()]);
67 }60 }
6861
69 /// Must deinitialize with deinit.62 /// Must deinitialize with deinit.
70 pub fn initFromSlice(other: &const CBuf, start: usize, end: usize) -> %CBuf {63 pub fn initFromSlice(other: &const Buffer0, start: usize, end: usize) -> %Buffer0 {
71 return CBuf.initFromMem(other.list.allocator, other.list.items[start...end]);64 return Buffer0.initFromMem(other.list.allocator, other.list.items[start...end]);
65 }
66
67 /// Must deinitialize with deinit.
68 pub fn initSize(allocator: &Allocator, size: usize) -> %Buffer0 {
69 var self = Buffer0 {
70 .list = List(u8).init(allocator),
71 };
72 %return self.resize(size);
73 return self;
72 }74 }
7375
74 pub fn deinit(self: &CBuf) {76 pub fn deinit(self: &Buffer0) {
75 self.list.deinit();77 self.list.deinit();
76 }78 }
7779
78 pub fn resize(self: &CBuf, new_len: usize) -> %void {80 pub fn toSlice(self: &Buffer0) -> []u8 {
81 return self.list.toSlice()[0...self.len()];
82 }
83
84 pub fn toSliceConst(self: &const Buffer0) -> []const u8 {
85 return self.list.toSliceConst()[0...self.len()];
86 }
87
88 pub fn resize(self: &Buffer0, new_len: usize) -> %void {
79 %return self.list.resize(new_len + 1);89 %return self.list.resize(new_len + 1);
80 self.list.items[self.len()] = 0;90 self.list.items[self.len()] = 0;
81 }91 }
8292
83 pub fn len(self: &const CBuf) -> usize {93 pub fn len(self: &const Buffer0) -> usize {
84 return self.list.len - 1;94 return self.list.len - 1;
85 }95 }
8696
87 pub fn appendMem(self: &CBuf, m: []const u8) -> %void {97 pub fn appendMem(self: &Buffer0, m: []const u8) -> %void {
88 const old_len = self.len();98 const old_len = self.len();
89 %return self.resize(old_len + m.len);99 %return self.resize(old_len + m.len);
90 mem.copy(u8, self.list.items[old_len...], m);100 mem.copy(u8, self.list.toSlice()[old_len...], m);
91 }101 }
92102
93 pub fn appendCStr(self: &CBuf, s: &const u8) -> %void {103 pub fn appendOther(self: &Buffer0, other: &const Buffer0) -> %void {
104 return self.appendMem(other.toSliceConst());
105 }
106
107 pub fn appendCStr(self: &Buffer0, s: &const u8) -> %void {
94 self.appendMem(s[0...strlen(s)])108 self.appendMem(s[0...strlen(s)])
95 }109 }
96110
97 pub fn appendChar(self: &CBuf, c: u8) -> %void {111 pub fn appendByte(self: &Buffer0, byte: u8) -> %void {
98 %return self.resize(self.len() + 1);112 %return self.resize(self.len() + 1);
99 self.list.items[self.len() - 1] = c;113 self.list.items[self.len() - 1] = byte;
100 }114 }
101115
102 pub fn eqlMem(self: &const CBuf, m: []const u8) -> bool {116 pub fn eqlMem(self: &const Buffer0, m: []const u8) -> bool {
103 if (self.len() != m.len) return false;117 if (self.len() != m.len) return false;
104 return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal;118 return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal;
105 }119 }
106120
107 pub fn eqlCStr(self: &const CBuf, s: &const u8) -> bool {121 pub fn eqlCStr(self: &const Buffer0, s: &const u8) -> bool {
108 self.eqlMem(s[0...strlen(s)])122 self.eqlMem(s[0...strlen(s)])
109 }123 }
110124
111 pub fn eqlCBuf(self: &const CBuf, other: &const CBuf) -> bool {125 pub fn eqlOther(self: &const Buffer0, other: &const Buffer0) -> bool {
112 self.eqlMem(other.list.items[0...other.len()])126 self.eqlMem(other.list.items[0...other.len()])
113 }127 }
114128
115 pub fn startsWithMem(self: &const CBuf, m: []const u8) -> bool {129 pub fn startsWithMem(self: &const Buffer0, m: []const u8) -> bool {
116 if (self.len() < m.len) return false;130 if (self.len() < m.len) return false;
117 return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal;131 return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal;
118 }132 }
119133
120 pub fn startsWithCBuf(self: &const CBuf, other: &const CBuf) -> bool {134 pub fn startsWithOther(self: &const Buffer0, other: &const Buffer0) -> bool {
121 self.startsWithMem(other.list.items[0...other.len()])135 self.startsWithMem(other.list.items[0...other.len()])
122 }136 }
123137
124 pub fn startsWithCStr(self: &const CBuf, s: &const u8) -> bool {138 pub fn startsWithCStr(self: &const Buffer0, s: &const u8) -> bool {
125 self.startsWithMem(s[0...strlen(s)])139 self.startsWithMem(s[0...strlen(s)])
126 }140 }
127};141};
128142
129fn testSimpleCBuf() {143fn testSimpleBuffer0() {
130 @setFnTest(this);144 @setFnTest(this);
131145
132 var buf = %%CBuf.initEmpty(&debug.global_allocator);146 var buf = %%Buffer0.initEmpty(&debug.global_allocator);
133 assert(buf.len() == 0);147 assert(buf.len() == 0);
134 %%buf.appendCStr(c"hello");148 %%buf.appendCStr(c"hello");
135 %%buf.appendChar(' ');149 %%buf.appendByte(' ');
136 %%buf.appendMem("world");150 %%buf.appendMem("world");
137 assert(buf.eqlCStr(c"hello world"));151 assert(buf.eqlCStr(c"hello world"));
138 assert(buf.eqlMem("hello world"));152 assert(buf.eqlMem("hello world"));
153 assert(mem.eql(u8, buf.toSliceConst(), "hello world"));
139154
140 var buf2 = %%CBuf.initFromCBuf(&buf);155 var buf2 = %%Buffer0.initFromOther(&buf);
141 assert(buf.eqlCBuf(&buf2));156 assert(buf.eqlOther(&buf2));
142157
143 assert(buf.startsWithMem("hell"));158 assert(buf.startsWithMem("hell"));
144 assert(buf.startsWithCStr(c"hell"));159 assert(buf.startsWithCStr(c"hell"));
145160
146 %%buf2.resize(4);161 %%buf2.resize(4);
147 assert(buf.startsWithCBuf(&buf2));162 assert(buf.startsWithOther(&buf2));
148}163}
149164
150// TODO do this without globals165fn testCStrFns() {
151
152fn testCompileTimeStrCmp() {
153 @setFnTest(this);166 @setFnTest(this);
154167
155 assert(test_compile_time_str_cmp_result);168 comptime testCStrFnsImpl();
169 testCStrFnsImpl();
156}170}
157const test_compile_time_str_cmp_result = (cmp(c"aoeu", c"aoez") == -1);
158
159fn testCompileTimeStrLen() {
160 @setFnTest(this);
161171
162 assert(test_comptime_str_len_result);172fn testCStrFnsImpl() {
173 assert(cmp(c"aoeu", c"aoez") == -1);
174 assert(len(c"123456789") == 9);
163}175}
164const test_comptime_str_len_result = (len(c"123456789") == 9);
std/debug.zig+3-3
...@@ -164,7 +164,7 @@ const Die = struct {...@@ -164,7 +164,7 @@ const Die = struct {
164 };164 };
165165
166 fn getAttr(self: &const Die, id: u64) -> ?&const FormValue {166 fn getAttr(self: &const Die, id: u64) -> ?&const FormValue {
167 for (self.attrs.toSlice()) |*attr| {167 for (self.attrs.toSliceConst()) |*attr| {
168 if (attr.id == id)168 if (attr.id == id)
169 return &attr.value;169 return &attr.value;
170 }170 }
...@@ -362,7 +362,7 @@ fn getAbbrevTable(st: &ElfStackTrace, abbrev_offset: u64) -> %&const AbbrevTable...@@ -362,7 +362,7 @@ fn getAbbrevTable(st: &ElfStackTrace, abbrev_offset: u64) -> %&const AbbrevTable
362}362}
363363
364fn getAbbrevTableEntry(abbrev_table: &const AbbrevTable, abbrev_code: u64) -> ?&const AbbrevTableEntry {364fn getAbbrevTableEntry(abbrev_table: &const AbbrevTable, abbrev_code: u64) -> ?&const AbbrevTableEntry {
365 for (abbrev_table.toSlice()) |*table_entry| {365 for (abbrev_table.toSliceConst()) |*table_entry| {
366 if (table_entry.abbrev_code == abbrev_code)366 if (table_entry.abbrev_code == abbrev_code)
367 return table_entry;367 return table_entry;
368 }368 }
...@@ -379,7 +379,7 @@ fn parseDie(in_stream: &io.InStream, abbrev_table: &const AbbrevTable, is_64: bo...@@ -379,7 +379,7 @@ fn parseDie(in_stream: &io.InStream, abbrev_table: &const AbbrevTable, is_64: bo
379 .attrs = List(Die.Attr).init(&global_allocator),379 .attrs = List(Die.Attr).init(&global_allocator),
380 };380 };
381 %return result.attrs.resize(table_entry.attrs.len);381 %return result.attrs.resize(table_entry.attrs.len);
382 for (table_entry.attrs.toSlice()) |attr, i| {382 for (table_entry.attrs.toSliceConst()) |attr, i| {
383 result.attrs.items[i] = Die.Attr {383 result.attrs.items[i] = Die.Attr {
384 .id = attr.attr_id,384 .id = attr.attr_id,
385 .value = %return parseFormValue(in_stream, attr.form_id, is_64),385 .value = %return parseFormValue(in_stream, attr.form_id, is_64),
std/io.zig+18
...@@ -10,6 +10,7 @@ const debug = @import("debug.zig");...@@ -10,6 +10,7 @@ const debug = @import("debug.zig");
10const assert = debug.assert;10const assert = debug.assert;
11const os = @import("os.zig");11const os = @import("os.zig");
12const mem = @import("mem.zig");12const mem = @import("mem.zig");
13const Buffer0 = @import("cstr.zig").Buffer0;
1314
14pub const stdin_fileno = 0;15pub const stdin_fileno = 0;
15pub const stdout_fileno = 1;16pub const stdout_fileno = 1;
...@@ -486,6 +487,23 @@ pub const InStream = struct {...@@ -486,6 +487,23 @@ pub const InStream = struct {
486487
487 return usize(stat.size);488 return usize(stat.size);
488 }489 }
490
491 pub fn readAll(is: &InStream, buf: &Buffer0) -> %void {
492 %return buf.resize(buffer_size);
493
494 var actual_buf_len: usize = 0;
495 while (true) {
496 const dest_slice = buf.toSlice()[actual_buf_len...];
497 const bytes_read = %return is.read(dest_slice);
498 actual_buf_len += bytes_read;
499
500 if (bytes_read != dest_slice.len) {
501 return buf.resize(actual_buf_len);
502 }
503
504 %return buf.resize(actual_buf_len + buffer_size);
505 }
506 }
489};507};
490508
491pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) -> %T {509pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) -> %T {
std/list.zig+18-5
...@@ -7,6 +7,9 @@ pub fn List(comptime T: type) -> type{...@@ -7,6 +7,9 @@ pub fn List(comptime T: type) -> type{
7 struct {7 struct {
8 const Self = this;8 const Self = this;
99
10 /// Use toSlice instead of slicing this directly, because if you don't
11 /// specify the end position of the slice, this will potentially give
12 /// you uninitialized memory.
10 items: []T,13 items: []T,
11 len: usize,14 len: usize,
12 allocator: &Allocator,15 allocator: &Allocator,
...@@ -23,15 +26,17 @@ pub fn List(comptime T: type) -> type{...@@ -23,15 +26,17 @@ pub fn List(comptime T: type) -> type{
23 l.allocator.free(l.items);26 l.allocator.free(l.items);
24 }27 }
2528
26 pub fn toSlice(l: &const Self) -> []const T {29 pub fn toSlice(l: &Self) -> []T {
30 return l.items[0...l.len];
31 }
32
33 pub fn toSliceConst(l: &const Self) -> []const T {
27 return l.items[0...l.len];34 return l.items[0...l.len];
28 }35 }
2936
30 pub fn append(l: &Self, item: T) -> %void {37 pub fn append(l: &Self, item: T) -> %void {
31 const new_length = l.len + 1;38 const new_item_ptr = %return l.addOne();
32 %return l.ensureCapacity(new_length);39 *new_item_ptr = item;
33 l.items[l.len] = item;
34 l.len = new_length;
35 }40 }
3641
37 pub fn resize(l: &Self, new_len: usize) -> %void {42 pub fn resize(l: &Self, new_len: usize) -> %void {
...@@ -48,6 +53,14 @@ pub fn List(comptime T: type) -> type{...@@ -48,6 +53,14 @@ pub fn List(comptime T: type) -> type{
48 }53 }
49 l.items = %return l.allocator.realloc(T, l.items, better_capacity);54 l.items = %return l.allocator.realloc(T, l.items, better_capacity);
50 }55 }
56
57 pub fn addOne(l: &Self) -> %&T {
58 const new_length = l.len + 1;
59 %return l.ensureCapacity(new_length);
60 const result = &l.items[l.len];
61 l.len = new_length;
62 return result;
63 }
51 }64 }
52}65}
5366
test/cases/try.zig+17-13
...@@ -3,6 +3,12 @@ const assert = @import("std").debug.assert;...@@ -3,6 +3,12 @@ const assert = @import("std").debug.assert;
3fn tryOnErrorUnion() {3fn tryOnErrorUnion() {
4 @setFnTest(this);4 @setFnTest(this);
55
6 tryOnErrorUnionImpl();
7 comptime tryOnErrorUnionImpl();
8
9}
10
11fn tryOnErrorUnionImpl() {
6 const x = try (const val = returnsTen()) {12 const x = try (const val = returnsTen()) {
7 val + 113 val + 1
8 } else |err| switch (err) {14 } else |err| switch (err) {
...@@ -12,19 +18,6 @@ fn tryOnErrorUnion() {...@@ -12,19 +18,6 @@ fn tryOnErrorUnion() {
12 assert(x == 11);18 assert(x == 11);
13}19}
1420
15fn tryOnErrorUnionComptime() {
16 @setFnTest(this);
17
18 comptime {
19 const x = try (const val = returnsTen()) {
20 val + 1
21 } else |err| switch (err) {
22 error.ItBroke, error.NoMem => 1,
23 error.CrappedOut => i32(2),
24 };
25 assert(x == 11);
26 }
27}
28error ItBroke;21error ItBroke;
29error NoMem;22error NoMem;
30error CrappedOut;23error CrappedOut;
...@@ -57,3 +50,14 @@ fn failIfTrue(ok: bool) -> %void {...@@ -57,3 +50,14 @@ fn failIfTrue(ok: bool) -> %void {
57 return;50 return;
58 }51 }
59}52}
53
54// TODO
55//fn tryThenNotExecutedWithAssignment() {
56// @setFnTest(this);
57//
58// try (_ = failIfTrue(true)) {
59// @unreachable();
60// } else |err| {
61// assert(err == error.ItBroke);
62// }
63//}