2017-12-23 19:08:53 -08:00
|
|
|
const std = @import("index.zig");
|
2018-02-10 17:55:13 -08:00
|
|
|
const builtin = @import("builtin");
|
2017-12-23 19:08:53 -08:00
|
|
|
const debug = std.debug;
|
|
|
|
const mem = std.mem;
|
2016-08-11 22:25:13 -07:00
|
|
|
const assert = debug.assert;
|
|
|
|
|
2018-02-10 17:55:13 -08:00
|
|
|
pub const line_sep = switch (builtin.os) {
|
|
|
|
builtin.Os.windows => "\r\n",
|
|
|
|
else => "\n",
|
|
|
|
};
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn len(ptr: *const u8) usize {
|
2016-07-26 20:40:11 -07:00
|
|
|
var count: usize = 0;
|
2017-05-03 15:12:07 -07:00
|
|
|
while (ptr[count] != 0) : (count += 1) {}
|
2016-05-07 10:52:52 -07:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn cmp(a: *const u8, b: *const u8) i8 {
|
2016-07-26 20:40:11 -07:00
|
|
|
var index: usize = 0;
|
2017-05-03 15:12:07 -07:00
|
|
|
while (a[index] == b[index] and 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;
|
2017-12-21 21:50:30 -08:00
|
|
|
}
|
2016-05-07 10:52:52 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn toSliceConst(str: *const u8) []const u8 {
|
2017-05-19 07:39:59 -07:00
|
|
|
return str[0..len(str)];
|
2016-05-07 10:52:52 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn toSlice(str: *u8) []u8 {
|
2017-05-19 07:39:59 -07:00
|
|
|
return str[0..len(str)];
|
2016-08-11 22:25:13 -07:00
|
|
|
}
|
2016-09-26 19:33:33 -07:00
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "cstr fns" {
|
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
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testCStrFnsImpl() void {
|
2017-02-28 00:07:11 -08:00
|
|
|
assert(cmp(c"aoeu", c"aoez") == -1);
|
|
|
|
assert(len(c"123456789") == 9);
|
2016-09-26 19:33:33 -07:00
|
|
|
}
|
2017-10-13 06:31:03 -07:00
|
|
|
|
2018-02-09 15:27:50 -08:00
|
|
|
/// Returns a mutable slice with 1 more byte of length which is a null byte.
|
2017-10-13 06:31:03 -07:00
|
|
|
/// Caller owns the returned memory.
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn addNullByte(allocator: *mem.Allocator, slice: []const u8) ![]u8 {
|
2018-01-07 13:51:46 -08:00
|
|
|
const result = try allocator.alloc(u8, slice.len + 1);
|
2017-10-13 06:31:03 -07:00
|
|
|
mem.copy(u8, result, slice);
|
|
|
|
result[slice.len] = 0;
|
2017-10-14 14:39:44 -07:00
|
|
|
return result;
|
2017-10-13 06:31:03 -07:00
|
|
|
}
|
2017-12-26 16:44:08 -08:00
|
|
|
|
|
|
|
pub const NullTerminated2DArray = struct {
|
2018-05-31 07:56:59 -07:00
|
|
|
allocator: *mem.Allocator,
|
2017-12-26 16:44:08 -08:00
|
|
|
byte_count: usize,
|
2018-05-31 07:56:59 -07:00
|
|
|
ptr: ?*?*u8,
|
2017-12-26 16:44:08 -08:00
|
|
|
|
|
|
|
/// Takes N lists of strings, concatenates the lists together, and adds a null terminator
|
|
|
|
/// Caller must deinit result
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn fromSlices(allocator: *mem.Allocator, slices: []const []const []const u8) !NullTerminated2DArray {
|
2017-12-26 16:44:08 -08:00
|
|
|
var new_len: usize = 1; // 1 for the list null
|
|
|
|
var byte_count: usize = 0;
|
|
|
|
for (slices) |slice| {
|
|
|
|
new_len += slice.len;
|
|
|
|
for (slice) |inner| {
|
|
|
|
byte_count += inner.len;
|
|
|
|
}
|
|
|
|
byte_count += slice.len; // for the null terminators of inner
|
|
|
|
}
|
|
|
|
|
|
|
|
const index_size = @sizeOf(usize) * new_len; // size of the ptrs
|
|
|
|
byte_count += index_size;
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
const buf = try allocator.alignedAlloc(u8, @alignOf(?*u8), byte_count);
|
2018-01-23 20:08:09 -08:00
|
|
|
errdefer allocator.free(buf);
|
2017-12-26 16:44:08 -08:00
|
|
|
|
|
|
|
var write_index = index_size;
|
2018-05-31 07:56:59 -07:00
|
|
|
const index_buf = ([]?*u8)(buf);
|
2017-12-26 16:44:08 -08:00
|
|
|
|
|
|
|
var i: usize = 0;
|
|
|
|
for (slices) |slice| {
|
|
|
|
for (slice) |inner| {
|
|
|
|
index_buf[i] = &buf[write_index];
|
|
|
|
i += 1;
|
|
|
|
mem.copy(u8, buf[write_index..], inner);
|
|
|
|
write_index += inner.len;
|
|
|
|
buf[write_index] = 0;
|
|
|
|
write_index += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
index_buf[i] = null;
|
|
|
|
|
2018-05-26 15:16:39 -07:00
|
|
|
return NullTerminated2DArray{
|
2017-12-26 16:44:08 -08:00
|
|
|
.allocator = allocator,
|
|
|
|
.byte_count = byte_count,
|
2018-05-31 07:56:59 -07:00
|
|
|
.ptr = @ptrCast(?*?*u8, buf.ptr),
|
2017-12-26 16:44:08 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn deinit(self: *NullTerminated2DArray) void {
|
|
|
|
const buf = @ptrCast(*u8, self.ptr);
|
2017-12-26 16:44:08 -08:00
|
|
|
self.allocator.free(buf[0..self.byte_count]);
|
|
|
|
}
|
|
|
|
};
|