| ... | ... | @@ -34,131 +34,142 @@ pub fn toSlice(str: &u8) -> []u8 { |
| 34 | 34 | |
| 35 | 35 | |
| 36 | 36 | /// A buffer that allocates memory and maintains a null byte at the end. |
| 37 | | pub const CBuf = struct { |
| 37 | pub const Buffer0 = struct { |
| 38 | 38 | list: List(u8), |
| 39 | 39 | |
| 40 | 40 | /// 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); |
| 47 | 43 | } |
| 48 | 44 | |
| 49 | 45 | /// 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); |
| 55 | 48 | mem.copy(u8, self.list.items, m); |
| 56 | 49 | return self; |
| 57 | 50 | } |
| 58 | 51 | |
| 59 | 52 | /// 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)]); |
| 62 | 55 | } |
| 63 | 56 | |
| 64 | 57 | /// 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()]); |
| 67 | 60 | } |
| 68 | 61 | |
| 69 | 62 | /// 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; |
| 72 | 74 | } |
| 73 | 75 | |
| 74 | | pub fn deinit(self: &CBuf) { |
| 76 | pub fn deinit(self: &Buffer0) { |
| 75 | 77 | self.list.deinit(); |
| 76 | 78 | } |
| 77 | 79 | |
| 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 | 89 | %return self.list.resize(new_len + 1); |
| 80 | 90 | self.list.items[self.len()] = 0; |
| 81 | 91 | } |
| 82 | 92 | |
| 83 | | pub fn len(self: &const CBuf) -> usize { |
| 93 | pub fn len(self: &const Buffer0) -> usize { |
| 84 | 94 | return self.list.len - 1; |
| 85 | 95 | } |
| 86 | 96 | |
| 87 | | pub fn appendMem(self: &CBuf, m: []const u8) -> %void { |
| 97 | pub fn appendMem(self: &Buffer0, m: []const u8) -> %void { |
| 88 | 98 | const old_len = self.len(); |
| 89 | 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 | } |
| 92 | 102 | |
| 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 | 108 | self.appendMem(s[0...strlen(s)]) |
| 95 | 109 | } |
| 96 | 110 | |
| 97 | | pub fn appendChar(self: &CBuf, c: u8) -> %void { |
| 111 | pub fn appendByte(self: &Buffer0, byte: u8) -> %void { |
| 98 | 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 | } |
| 101 | 115 | |
| 102 | | pub fn eqlMem(self: &const CBuf, m: []const u8) -> bool { |
| 116 | pub fn eqlMem(self: &const Buffer0, m: []const u8) -> bool { |
| 103 | 117 | if (self.len() != m.len) return false; |
| 104 | 118 | return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal; |
| 105 | 119 | } |
| 106 | 120 | |
| 107 | | pub fn eqlCStr(self: &const CBuf, s: &const u8) -> bool { |
| 121 | pub fn eqlCStr(self: &const Buffer0, s: &const u8) -> bool { |
| 108 | 122 | self.eqlMem(s[0...strlen(s)]) |
| 109 | 123 | } |
| 110 | 124 | |
| 111 | | pub fn eqlCBuf(self: &const CBuf, other: &const CBuf) -> bool { |
| 125 | pub fn eqlOther(self: &const Buffer0, other: &const Buffer0) -> bool { |
| 112 | 126 | self.eqlMem(other.list.items[0...other.len()]) |
| 113 | 127 | } |
| 114 | 128 | |
| 115 | | pub fn startsWithMem(self: &const CBuf, m: []const u8) -> bool { |
| 129 | pub fn startsWithMem(self: &const Buffer0, m: []const u8) -> bool { |
| 116 | 130 | if (self.len() < m.len) return false; |
| 117 | 131 | return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal; |
| 118 | 132 | } |
| 119 | 133 | |
| 120 | | pub fn startsWithCBuf(self: &const CBuf, other: &const CBuf) -> bool { |
| 134 | pub fn startsWithOther(self: &const Buffer0, other: &const Buffer0) -> bool { |
| 121 | 135 | self.startsWithMem(other.list.items[0...other.len()]) |
| 122 | 136 | } |
| 123 | 137 | |
| 124 | | pub fn startsWithCStr(self: &const CBuf, s: &const u8) -> bool { |
| 138 | pub fn startsWithCStr(self: &const Buffer0, s: &const u8) -> bool { |
| 125 | 139 | self.startsWithMem(s[0...strlen(s)]) |
| 126 | 140 | } |
| 127 | 141 | }; |
| 128 | 142 | |
| 129 | | fn testSimpleCBuf() { |
| 143 | fn testSimpleBuffer0() { |
| 130 | 144 | @setFnTest(this); |
| 131 | 145 | |
| 132 | | var buf = %%CBuf.initEmpty(&debug.global_allocator); |
| 146 | var buf = %%Buffer0.initEmpty(&debug.global_allocator); |
| 133 | 147 | assert(buf.len() == 0); |
| 134 | 148 | %%buf.appendCStr(c"hello"); |
| 135 | | %%buf.appendChar(' '); |
| 149 | %%buf.appendByte(' '); |
| 136 | 150 | %%buf.appendMem("world"); |
| 137 | 151 | assert(buf.eqlCStr(c"hello world")); |
| 138 | 152 | assert(buf.eqlMem("hello world")); |
| 153 | assert(mem.eql(u8, buf.toSliceConst(), "hello world")); |
| 139 | 154 | |
| 140 | | var buf2 = %%CBuf.initFromCBuf(&buf); |
| 141 | | assert(buf.eqlCBuf(&buf2)); |
| 155 | var buf2 = %%Buffer0.initFromOther(&buf); |
| 156 | assert(buf.eqlOther(&buf2)); |
| 142 | 157 | |
| 143 | 158 | assert(buf.startsWithMem("hell")); |
| 144 | 159 | assert(buf.startsWithCStr(c"hell")); |
| 145 | 160 | |
| 146 | 161 | %%buf2.resize(4); |
| 147 | | assert(buf.startsWithCBuf(&buf2)); |
| 162 | assert(buf.startsWithOther(&buf2)); |
| 148 | 163 | } |
| 149 | 164 | |
| 150 | | // TODO do this without globals |
| 151 | | |
| 152 | | fn testCompileTimeStrCmp() { |
| 165 | fn testCStrFns() { |
| 153 | 166 | @setFnTest(this); |
| 154 | 167 | |
| 155 | | assert(test_compile_time_str_cmp_result); |
| 168 | comptime testCStrFnsImpl(); |
| 169 | testCStrFnsImpl(); |
| 156 | 170 | } |
| 157 | | const test_compile_time_str_cmp_result = (cmp(c"aoeu", c"aoez") == -1); |
| 158 | | |
| 159 | | fn testCompileTimeStrLen() { |
| 160 | | @setFnTest(this); |
| 161 | 171 | |
| 162 | | assert(test_comptime_str_len_result); |
| 172 | fn testCStrFnsImpl() { |
| 173 | assert(cmp(c"aoeu", c"aoez") == -1); |
| 174 | assert(len(c"123456789") == 9); |
| 163 | 175 | } |
| 164 | | const test_comptime_str_len_result = (len(c"123456789") == 9); |