2016-08-12 13:25:13 +08:00
|
|
|
const debug = @import("debug.zig");
|
|
|
|
const assert = debug.assert;
|
|
|
|
|
2016-07-27 11:40:11 +08:00
|
|
|
pub fn len(ptr: &const u8) -> usize {
|
|
|
|
var count: usize = 0;
|
2016-05-08 01:52:52 +08:00
|
|
|
while (ptr[count] != 0; count += 1) {}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2016-09-27 10:33:33 +08:00
|
|
|
pub fn cmp(a: &const u8, b: &const u8) -> i8 {
|
2016-07-27 11:40:11 +08:00
|
|
|
var index: usize = 0;
|
2017-03-26 17:21:28 +08:00
|
|
|
while (a[index] == b[index] and a[index] != 0; index += 1) {}
|
2017-01-01 06:10:29 +08:00
|
|
|
if (a[index] > b[index]) {
|
|
|
|
return 1;
|
2016-09-27 10:33:33 +08:00
|
|
|
} else if (a[index] < b[index]) {
|
2017-01-01 06:10:29 +08:00
|
|
|
return -1;
|
2016-09-27 10:33:33 +08:00
|
|
|
} else {
|
2017-01-01 06:10:29 +08:00
|
|
|
return 0;
|
2016-09-27 10:33:33 +08:00
|
|
|
};
|
2016-05-08 01:52:52 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 13:42:50 +08:00
|
|
|
pub fn toSliceConst(str: &const u8) -> []const u8 {
|
2017-04-21 13:56:12 +08:00
|
|
|
return str[0...len(str)];
|
2016-05-08 01:52:52 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 13:42:50 +08:00
|
|
|
pub fn toSlice(str: &u8) -> []u8 {
|
2017-04-21 13:56:12 +08:00
|
|
|
return str[0...len(str)];
|
2016-08-12 13:25:13 +08:00
|
|
|
}
|
2016-09-27 10:33:33 +08:00
|
|
|
|
2017-03-17 04:02:35 +08:00
|
|
|
test "cstr fns" {
|
2017-02-28 16:07:11 +08:00
|
|
|
comptime testCStrFnsImpl();
|
|
|
|
testCStrFnsImpl();
|
2016-09-27 10:33:33 +08:00
|
|
|
}
|
2016-09-28 14:33:32 +08:00
|
|
|
|
2017-02-28 16:07:11 +08:00
|
|
|
fn testCStrFnsImpl() {
|
|
|
|
assert(cmp(c"aoeu", c"aoez") == -1);
|
|
|
|
assert(len(c"123456789") == 9);
|
2016-09-27 10:33:33 +08:00
|
|
|
}
|