2016-08-11 22:25:13 -07:00
|
|
|
const debug = @import("debug.zig");
|
2017-10-13 06:31:03 -07:00
|
|
|
const mem = @import("mem.zig");
|
2016-08-11 22:25:13 -07:00
|
|
|
const assert = debug.assert;
|
|
|
|
|
2016-07-26 20:40:11 -07:00
|
|
|
pub fn len(ptr: &const u8) -> usize {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
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;
|
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 {
|
2017-05-19 07:39:59 -07:00
|
|
|
return str[0..len(str)];
|
2016-05-07 10:52:52 -07:00
|
|
|
}
|
|
|
|
|
2016-08-16 22:42:50 -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
|
|
|
|
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
|
|
|
}
|
2017-10-13 06:31:03 -07:00
|
|
|
|
|
|
|
/// Returns a mutable slice with exactly the same size which is guaranteed to
|
|
|
|
/// have a null byte after it.
|
|
|
|
/// Caller owns the returned memory.
|
|
|
|
pub fn addNullByte(allocator: &mem.Allocator, slice: []const u8) -> %[]u8 {
|
|
|
|
const result = %return allocator.alloc(u8, slice.len + 1);
|
|
|
|
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
|
|
|
}
|