From 01c46eef3a7e4fd5a96f364541c539746ae1ea3b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 7 May 2016 10:52:52 -0700 Subject: std: separate str and cstr --- std/cstr.zig | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 std/cstr.zig (limited to 'std/cstr.zig') diff --git a/std/cstr.zig b/std/cstr.zig new file mode 100644 index 0000000000..cdc0f02eed --- /dev/null +++ b/std/cstr.zig @@ -0,0 +1,26 @@ +// TODO fix https://github.com/andrewrk/zig/issues/140 +// and then make this able to run at compile time +#static_eval_enable(false) +pub fn len(ptr: &const u8) -> isize { + var count: isize = 0; + while (ptr[count] != 0; count += 1) {} + return count; +} + +// TODO fix https://github.com/andrewrk/zig/issues/140 +// and then make this able to run at compile time +#static_eval_enable(false) +pub fn cmp(a: &const u8, b: &const u8) -> i32 { + var index: isize = 0; + while (a[index] == b[index] && a[index] != 0; index += 1) {} + return a[index] - b[index]; +} + +pub fn to_slice_const(str: &const u8) -> []const u8 { + return str[0...len(str)]; +} + +pub fn to_slice(str: &u8) -> []u8 { + return str[0...len(str)]; +} + -- cgit v1.2.3