| ... | @@ -34,131 +34,142 @@ pub fn toSlice(str: &u8) -> []u8 { | ... | @@ -34,131 +34,142 @@ pub fn toSlice(str: &u8) -> []u8 { |
| 34 | | 34 | |
| 35 | | 35 | |
| 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. |
| 37 | pub const CBuf = struct { | 37 | pub const Buffer0 = struct { |
| 38 | list: List(u8), | 38 | list: List(u8), |
| 39 | | 39 | |
| 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 | } |
| 48 | | 44 | |
| 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 | } |
| 58 | | 51 | |
| 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 | } |
| 63 | | 56 | |
| 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 | } |
| 68 | | 61 | |
| 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 | } |
| 73 | | 75 | |
| 74 | pub fn deinit(self: &CBuf) { | 76 | pub fn deinit(self: &Buffer0) { |
| 75 | self.list.deinit(); | 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 | %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 | } |
| 82 | | 92 | |
| 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 | } |
| 86 | | 96 | |
| 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 | } |
| 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 | self.appendMem(s[0...strlen(s)]) | 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 | %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 | } |
| 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 | 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 | } |
| 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 | self.eqlMem(s[0...strlen(s)]) | 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 | self.eqlMem(other.list.items[0...other.len()]) | 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 | 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 | } |
| 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 | self.startsWithMem(other.list.items[0...other.len()]) | 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 | self.startsWithMem(s[0...strlen(s)]) | 139 | self.startsWithMem(s[0...strlen(s)]) |
| 126 | } | 140 | } |
| 127 | }; | 141 | }; |
| 128 | | 142 | |
| 129 | fn testSimpleCBuf() { | 143 | fn testSimpleBuffer0() { |
| 130 | @setFnTest(this); | 144 | @setFnTest(this); |
| 131 | | 145 | |
| 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")); |
| 139 | | 154 | |
| 140 | var buf2 = %%CBuf.initFromCBuf(&buf); | 155 | var buf2 = %%Buffer0.initFromOther(&buf); |
| 141 | assert(buf.eqlCBuf(&buf2)); | 156 | assert(buf.eqlOther(&buf2)); |
| 142 | | 157 | |
| 143 | assert(buf.startsWithMem("hell")); | 158 | assert(buf.startsWithMem("hell")); |
| 144 | assert(buf.startsWithCStr(c"hell")); | 159 | assert(buf.startsWithCStr(c"hell")); |
| 145 | | 160 | |
| 146 | %%buf2.resize(4); | 161 | %%buf2.resize(4); |
| 147 | assert(buf.startsWithCBuf(&buf2)); | 162 | assert(buf.startsWithOther(&buf2)); |
| 148 | } | 163 | } |
| 149 | | 164 | |
| 150 | // TODO do this without globals | 165 | fn testCStrFns() { |
| 151 | | | |
| 152 | fn testCompileTimeStrCmp() { | | |
| 153 | @setFnTest(this); | 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); | | |