zig/std/cstr.zig
Andrew Kelley fb492d19eb zig build system supports building a library
See #329

Supporting work:
 * move std.cstr.Buffer0 to std.buffer.Buffer
 * add build.zig to example/shared_library/ and add an automated test
   for it
 * add std.list.List.resizeDown
 * improve std.os.makePath
   - no longer recursive
   - takes into account . and ..
 * add std.os.path.isAbsolute
 * add std.os.path.resolve
 * reimplement std.os.path.dirname
   - no longer requires an allocator
   - handles edge cases correctly
2017-04-21 01:56:12 -04:00

39 lines
811 B
Zig

const debug = @import("debug.zig");
const assert = debug.assert;
pub fn len(ptr: &const u8) -> usize {
var count: usize = 0;
while (ptr[count] != 0; count += 1) {}
return count;
}
pub fn cmp(a: &const u8, b: &const u8) -> i8 {
var index: usize = 0;
while (a[index] == b[index] and a[index] != 0; index += 1) {}
if (a[index] > b[index]) {
return 1;
} else if (a[index] < b[index]) {
return -1;
} else {
return 0;
};
}
pub fn toSliceConst(str: &const u8) -> []const u8 {
return str[0...len(str)];
}
pub fn toSlice(str: &u8) -> []u8 {
return str[0...len(str)];
}
test "cstr fns" {
comptime testCStrFnsImpl();
testCStrFnsImpl();
}
fn testCStrFnsImpl() {
assert(cmp(c"aoeu", c"aoez") == -1);
assert(len(c"123456789") == 9);
}