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 {
3434
3535
3636/// A buffer that allocates memory and maintains a null byte at the end.
37pub const CBuf = struct {
37pub const Buffer0 = struct {
3838 list: List(u8),
3939
4040 /// Must deinitialize with deinit.
41 pub fn initEmpty(allocator: &Allocator) -> %CBuf {
42 var self = CBuf {
43 .list = List(u8).init(allocator),
44 };
45 %return self.resize(0);
46 return self;
41 pub fn initEmpty(allocator: &Allocator) -> %Buffer0 {
42 return initSize(allocator, 0);
4743 }
4844
4945 /// Must deinitialize with deinit.
50 pub fn initFromMem(allocator: &Allocator, m: []const u8) -> %CBuf {
51 var self = CBuf {
52 .list = List(u8).init(allocator),
53 };
54 %return self.resize(m.len);
46 pub fn initFromMem(allocator: &Allocator, m: []const u8) -> %Buffer0 {
47 var self = %return initSize(allocator, m.len);
5548 mem.copy(u8, self.list.items, m);
5649 return self;
5750 }
5851
5952 /// Must deinitialize with deinit.
60 pub fn initFromCStr(allocator: &Allocator, s: &const u8) -> %CBuf {
61 return CBuf.initFromMem(allocator, s[0...strlen(s)]);
53 pub fn initFromCStr(allocator: &Allocator, s: &const u8) -> %Buffer0 {
54 return Buffer0.initFromMem(allocator, s[0...strlen(s)]);
6255 }
6356
6457 /// Must deinitialize with deinit.
65 pub fn initFromCBuf(cbuf: &const CBuf) -> %CBuf {
66 return CBuf.initFromMem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()]);
58 pub fn initFromOther(cbuf: &const Buffer0) -> %Buffer0 {
59 return Buffer0.initFromMem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()]);
6760 }
6861
6962 /// Must deinitialize with deinit.
70 pub fn initFromSlice(other: &const CBuf, start: usize, end: usize) -> %CBuf {
71 return CBuf.initFromMem(other.list.allocator, other.list.items[start...end]);
63 pub fn initFromSlice(other: &const Buffer0, start: usize, end: usize) -> %Buffer0 {
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;
7274 }
7375
74 pub fn deinit(self: &CBuf) {
76 pub fn deinit(self: &Buffer0) {
7577 self.list.deinit();
7678 }
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 {
7989 %return self.list.resize(new_len + 1);
8090 self.list.items[self.len()] = 0;
8191 }
8292
83 pub fn len(self: &const CBuf) -> usize {
93 pub fn len(self: &const Buffer0) -> usize {
8494 return self.list.len - 1;
8595 }
8696
87 pub fn appendMem(self: &CBuf, m: []const u8) -> %void {
97 pub fn appendMem(self: &Buffer0, m: []const u8) -> %void {
8898 const old_len = self.len();
8999 %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);
91101 }
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 {
94108 self.appendMem(s[0...strlen(s)])
95109 }
96110
97 pub fn appendChar(self: &CBuf, c: u8) -> %void {
111 pub fn appendByte(self: &Buffer0, byte: u8) -> %void {
98112 %return self.resize(self.len() + 1);
99 self.list.items[self.len() - 1] = c;
113 self.list.items[self.len() - 1] = byte;
100114 }
101115
102 pub fn eqlMem(self: &const CBuf, m: []const u8) -> bool {
116 pub fn eqlMem(self: &const Buffer0, m: []const u8) -> bool {
103117 if (self.len() != m.len) return false;
104118 return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal;
105119 }
106120
107 pub fn eqlCStr(self: &const CBuf, s: &const u8) -> bool {
121 pub fn eqlCStr(self: &const Buffer0, s: &const u8) -> bool {
108122 self.eqlMem(s[0...strlen(s)])
109123 }
110124
111 pub fn eqlCBuf(self: &const CBuf, other: &const CBuf) -> bool {
125 pub fn eqlOther(self: &const Buffer0, other: &const Buffer0) -> bool {
112126 self.eqlMem(other.list.items[0...other.len()])
113127 }
114128
115 pub fn startsWithMem(self: &const CBuf, m: []const u8) -> bool {
129 pub fn startsWithMem(self: &const Buffer0, m: []const u8) -> bool {
116130 if (self.len() < m.len) return false;
117131 return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal;
118132 }
119133
120 pub fn startsWithCBuf(self: &const CBuf, other: &const CBuf) -> bool {
134 pub fn startsWithOther(self: &const Buffer0, other: &const Buffer0) -> bool {
121135 self.startsWithMem(other.list.items[0...other.len()])
122136 }
123137
124 pub fn startsWithCStr(self: &const CBuf, s: &const u8) -> bool {
138 pub fn startsWithCStr(self: &const Buffer0, s: &const u8) -> bool {
125139 self.startsWithMem(s[0...strlen(s)])
126140 }
127141};
128142
129fn testSimpleCBuf() {
143fn testSimpleBuffer0() {
130144 @setFnTest(this);
131145
132 var buf = %%CBuf.initEmpty(&debug.global_allocator);
146 var buf = %%Buffer0.initEmpty(&debug.global_allocator);
133147 assert(buf.len() == 0);
134148 %%buf.appendCStr(c"hello");
135 %%buf.appendChar(' ');
149 %%buf.appendByte(' ');
136150 %%buf.appendMem("world");
137151 assert(buf.eqlCStr(c"hello world"));
138152 assert(buf.eqlMem("hello world"));
153 assert(mem.eql(u8, buf.toSliceConst(), "hello world"));
139154
140 var buf2 = %%CBuf.initFromCBuf(&buf);
141 assert(buf.eqlCBuf(&buf2));
155 var buf2 = %%Buffer0.initFromOther(&buf);
156 assert(buf.eqlOther(&buf2));
142157
143158 assert(buf.startsWithMem("hell"));
144159 assert(buf.startsWithCStr(c"hell"));
145160
146161 %%buf2.resize(4);
147 assert(buf.startsWithCBuf(&buf2));
162 assert(buf.startsWithOther(&buf2));
148163}
149164
150// TODO do this without globals
151
152fn testCompileTimeStrCmp() {
165fn testCStrFns() {
153166 @setFnTest(this);
154167
155 assert(test_compile_time_str_cmp_result);
168 comptime testCStrFnsImpl();
169 testCStrFnsImpl();
156170}
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);
163175}
164const test_comptime_str_len_result = (len(c"123456789") == 9);
std/debug.zig+3-3
......@@ -164,7 +164,7 @@ const Die = struct {
164164 };
165165
166166 fn getAttr(self: &const Die, id: u64) -> ?&const FormValue {
167 for (self.attrs.toSlice()) |*attr| {
167 for (self.attrs.toSliceConst()) |*attr| {
168168 if (attr.id == id)
169169 return &attr.value;
170170 }
......@@ -362,7 +362,7 @@ fn getAbbrevTable(st: &ElfStackTrace, abbrev_offset: u64) -> %&const AbbrevTable
362362}
363363
364364fn getAbbrevTableEntry(abbrev_table: &const AbbrevTable, abbrev_code: u64) -> ?&const AbbrevTableEntry {
365 for (abbrev_table.toSlice()) |*table_entry| {
365 for (abbrev_table.toSliceConst()) |*table_entry| {
366366 if (table_entry.abbrev_code == abbrev_code)
367367 return table_entry;
368368 }
......@@ -379,7 +379,7 @@ fn parseDie(in_stream: &io.InStream, abbrev_table: &const AbbrevTable, is_64: bo
379379 .attrs = List(Die.Attr).init(&global_allocator),
380380 };
381381 %return result.attrs.resize(table_entry.attrs.len);
382 for (table_entry.attrs.toSlice()) |attr, i| {
382 for (table_entry.attrs.toSliceConst()) |attr, i| {
383383 result.attrs.items[i] = Die.Attr {
384384 .id = attr.attr_id,
385385 .value = %return parseFormValue(in_stream, attr.form_id, is_64),
std/io.zig+18
......@@ -10,6 +10,7 @@ const debug = @import("debug.zig");
1010const assert = debug.assert;
1111const os = @import("os.zig");
1212const mem = @import("mem.zig");
13const Buffer0 = @import("cstr.zig").Buffer0;
1314
1415pub const stdin_fileno = 0;
1516pub const stdout_fileno = 1;
......@@ -486,6 +487,23 @@ pub const InStream = struct {
486487
487488 return usize(stat.size);
488489 }
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 }
489507};
490508
491509pub 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{
77 struct {
88 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.
1013 items: []T,
1114 len: usize,
1215 allocator: &Allocator,
......@@ -23,15 +26,17 @@ pub fn List(comptime T: type) -> type{
2326 l.allocator.free(l.items);
2427 }
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 {
2734 return l.items[0...l.len];
2835 }
2936
3037 pub fn append(l: &Self, item: T) -> %void {
31 const new_length = l.len + 1;
32 %return l.ensureCapacity(new_length);
33 l.items[l.len] = item;
34 l.len = new_length;
38 const new_item_ptr = %return l.addOne();
39 *new_item_ptr = item;
3540 }
3641
3742 pub fn resize(l: &Self, new_len: usize) -> %void {
......@@ -48,6 +53,14 @@ pub fn List(comptime T: type) -> type{
4853 }
4954 l.items = %return l.allocator.realloc(T, l.items, better_capacity);
5055 }
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 }
5164 }
5265}
5366
test/cases/try.zig+17-13
......@@ -3,6 +3,12 @@ const assert = @import("std").debug.assert;
33fn tryOnErrorUnion() {
44 @setFnTest(this);
55
6 tryOnErrorUnionImpl();
7 comptime tryOnErrorUnionImpl();
8
9}
10
11fn tryOnErrorUnionImpl() {
612 const x = try (const val = returnsTen()) {
713 val + 1
814 } else |err| switch (err) {
......@@ -12,19 +18,6 @@ fn tryOnErrorUnion() {
1218 assert(x == 11);
1319}
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}
2821error ItBroke;
2922error NoMem;
3023error CrappedOut;
......@@ -57,3 +50,14 @@ fn failIfTrue(ok: bool) -> %void {
5750 return;
5851 }
5952}
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//}