2016-08-11 22:25:13 -07:00
|
|
|
const List = @import("list.zig").List;
|
|
|
|
const mem = @import("mem.zig");
|
|
|
|
const Allocator = mem.Allocator;
|
|
|
|
const debug = @import("debug.zig");
|
|
|
|
const assert = debug.assert;
|
|
|
|
|
|
|
|
const strlen = len;
|
|
|
|
|
2016-07-26 20:40:11 -07:00
|
|
|
pub fn len(ptr: &const u8) -> usize {
|
|
|
|
var count: usize = 0;
|
2016-05-07 10:52:52 -07:00
|
|
|
while (ptr[count] != 0; count += 1) {}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2016-09-26 19:33:33 -07:00
|
|
|
pub fn cmp(a: &const u8, b: &const u8) -> i8 {
|
2016-07-26 20:40:11 -07:00
|
|
|
var index: usize = 0;
|
2016-05-07 10:52:52 -07:00
|
|
|
while (a[index] == b[index] && a[index] != 0; index += 1) {}
|
2016-12-31 14:10:29 -08:00
|
|
|
if (a[index] > b[index]) {
|
|
|
|
return 1;
|
2016-09-26 19:33:33 -07:00
|
|
|
} else if (a[index] < b[index]) {
|
2016-12-31 14:10:29 -08:00
|
|
|
return -1;
|
2016-09-26 19:33:33 -07:00
|
|
|
} else {
|
2016-12-31 14:10:29 -08:00
|
|
|
return 0;
|
2016-09-26 19:33:33 -07:00
|
|
|
};
|
2016-05-07 10:52:52 -07:00
|
|
|
}
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
pub fn toSliceConst(str: &const u8) -> []const u8 {
|
2016-08-11 22:25:13 -07:00
|
|
|
return str[0...strlen(str)];
|
2016-05-07 10:52:52 -07:00
|
|
|
}
|
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
pub fn toSlice(str: &u8) -> []u8 {
|
2016-08-11 22:25:13 -07:00
|
|
|
return str[0...strlen(str)];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// A buffer that allocates memory and maintains a null byte at the end.
|
2017-02-28 00:07:11 -08:00
|
|
|
pub const Buffer0 = struct {
|
2016-08-11 22:25:13 -07:00
|
|
|
list: List(u8),
|
|
|
|
|
|
|
|
/// Must deinitialize with deinit.
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn initEmpty(allocator: &Allocator) -> %Buffer0 {
|
|
|
|
return initSize(allocator, 0);
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Must deinitialize with deinit.
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn initFromMem(allocator: &Allocator, m: []const u8) -> %Buffer0 {
|
|
|
|
var self = %return initSize(allocator, m.len);
|
2016-08-11 22:25:13 -07:00
|
|
|
mem.copy(u8, self.list.items, m);
|
2016-09-22 23:00:23 -07:00
|
|
|
return self;
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Must deinitialize with deinit.
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn initFromCStr(allocator: &Allocator, s: &const u8) -> %Buffer0 {
|
|
|
|
return Buffer0.initFromMem(allocator, s[0...strlen(s)]);
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Must deinitialize with deinit.
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn initFromOther(cbuf: &const Buffer0) -> %Buffer0 {
|
|
|
|
return Buffer0.initFromMem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()]);
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Must deinitialize with deinit.
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn initFromSlice(other: &const Buffer0, start: usize, end: usize) -> %Buffer0 {
|
|
|
|
return Buffer0.initFromMem(other.list.allocator, other.list.items[start...end]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Must deinitialize with deinit.
|
|
|
|
pub fn initSize(allocator: &Allocator, size: usize) -> %Buffer0 {
|
|
|
|
var self = Buffer0 {
|
|
|
|
.list = List(u8).init(allocator),
|
|
|
|
};
|
|
|
|
%return self.resize(size);
|
|
|
|
return self;
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn deinit(self: &Buffer0) {
|
2016-08-11 22:25:13 -07:00
|
|
|
self.list.deinit();
|
|
|
|
}
|
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn toSlice(self: &Buffer0) -> []u8 {
|
|
|
|
return self.list.toSlice()[0...self.len()];
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn toSliceConst(self: &const Buffer0) -> []const u8 {
|
|
|
|
return self.list.toSliceConst()[0...self.len()];
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resize(self: &Buffer0, new_len: usize) -> %void {
|
2016-08-11 22:25:13 -07:00
|
|
|
%return self.list.resize(new_len + 1);
|
|
|
|
self.list.items[self.len()] = 0;
|
|
|
|
}
|
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn len(self: &const Buffer0) -> usize {
|
2016-08-11 22:25:13 -07:00
|
|
|
return self.list.len - 1;
|
|
|
|
}
|
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn appendMem(self: &Buffer0, m: []const u8) -> %void {
|
2016-08-11 22:25:13 -07:00
|
|
|
const old_len = self.len();
|
|
|
|
%return self.resize(old_len + m.len);
|
2017-02-28 00:07:11 -08:00
|
|
|
mem.copy(u8, self.list.toSlice()[old_len...], m);
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn appendOther(self: &Buffer0, other: &const Buffer0) -> %void {
|
|
|
|
return self.appendMem(other.toSliceConst());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn appendCStr(self: &Buffer0, s: &const u8) -> %void {
|
2016-08-16 22:42:50 -07:00
|
|
|
self.appendMem(s[0...strlen(s)])
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn appendByte(self: &Buffer0, byte: u8) -> %void {
|
2016-08-11 22:25:13 -07:00
|
|
|
%return self.resize(self.len() + 1);
|
2017-02-28 00:07:11 -08:00
|
|
|
self.list.items[self.len() - 1] = byte;
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn eqlMem(self: &const Buffer0, m: []const u8) -> bool {
|
2016-08-11 22:25:13 -07:00
|
|
|
if (self.len() != m.len) return false;
|
|
|
|
return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal;
|
|
|
|
}
|
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn eqlCStr(self: &const Buffer0, s: &const u8) -> bool {
|
2016-08-16 22:42:50 -07:00
|
|
|
self.eqlMem(s[0...strlen(s)])
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn eqlOther(self: &const Buffer0, other: &const Buffer0) -> bool {
|
2016-08-16 22:42:50 -07:00
|
|
|
self.eqlMem(other.list.items[0...other.len()])
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn startsWithMem(self: &const Buffer0, m: []const u8) -> bool {
|
2016-08-11 22:25:13 -07:00
|
|
|
if (self.len() < m.len) return false;
|
|
|
|
return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal;
|
|
|
|
}
|
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn startsWithOther(self: &const Buffer0, other: &const Buffer0) -> bool {
|
2016-08-16 22:42:50 -07:00
|
|
|
self.startsWithMem(other.list.items[0...other.len()])
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
pub fn startsWithCStr(self: &const Buffer0, s: &const u8) -> bool {
|
2016-08-16 22:42:50 -07:00
|
|
|
self.startsWithMem(s[0...strlen(s)])
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
2016-12-18 14:24:52 -08:00
|
|
|
};
|
2016-05-07 10:52:52 -07:00
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
fn testSimpleBuffer0() {
|
2016-12-31 14:10:29 -08:00
|
|
|
@setFnTest(this);
|
2016-09-27 23:33:32 -07:00
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
var buf = %%Buffer0.initEmpty(&debug.global_allocator);
|
2016-08-11 22:25:13 -07:00
|
|
|
assert(buf.len() == 0);
|
2016-08-16 22:42:50 -07:00
|
|
|
%%buf.appendCStr(c"hello");
|
2017-02-28 00:07:11 -08:00
|
|
|
%%buf.appendByte(' ');
|
2016-08-16 22:42:50 -07:00
|
|
|
%%buf.appendMem("world");
|
|
|
|
assert(buf.eqlCStr(c"hello world"));
|
|
|
|
assert(buf.eqlMem("hello world"));
|
2017-02-28 00:07:11 -08:00
|
|
|
assert(mem.eql(u8, buf.toSliceConst(), "hello world"));
|
2016-08-11 22:25:13 -07:00
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
var buf2 = %%Buffer0.initFromOther(&buf);
|
|
|
|
assert(buf.eqlOther(&buf2));
|
2016-08-11 22:25:13 -07:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
assert(buf.startsWithMem("hell"));
|
|
|
|
assert(buf.startsWithCStr(c"hell"));
|
2016-08-11 22:25:13 -07:00
|
|
|
|
|
|
|
%%buf2.resize(4);
|
2017-02-28 00:07:11 -08:00
|
|
|
assert(buf.startsWithOther(&buf2));
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
2016-09-26 19:33:33 -07:00
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
fn testCStrFns() {
|
2016-12-31 14:10:29 -08:00
|
|
|
@setFnTest(this);
|
2016-09-27 23:33:32 -07:00
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
comptime testCStrFnsImpl();
|
|
|
|
testCStrFnsImpl();
|
2016-09-26 19:33:33 -07:00
|
|
|
}
|
2016-09-27 23:33:32 -07:00
|
|
|
|
2017-02-28 00:07:11 -08:00
|
|
|
fn testCStrFnsImpl() {
|
|
|
|
assert(cmp(c"aoeu", c"aoez") == -1);
|
|
|
|
assert(len(c"123456789") == 9);
|
2016-09-26 19:33:33 -07:00
|
|
|
}
|