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/bootstrap.zig | 4 ++-- std/cstr.zig | 26 ++++++++++++++++++++++++++ std/index.zig | 1 + std/str.zig | 17 ----------------- 4 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 std/cstr.zig (limited to 'std') diff --git a/std/bootstrap.zig b/std/bootstrap.zig index f381311b00..7185c09a4c 100644 --- a/std/bootstrap.zig +++ b/std/bootstrap.zig @@ -2,7 +2,7 @@ const root = @import("@root"); const linux = @import("linux.zig"); -const str = @import("str.zig"); +const cstr = @import("cstr.zig"); const want_start_symbol = switch(@compile_var("os")) { linux => true, @@ -34,7 +34,7 @@ fn call_main() -> %void { var args: [argc][]u8 = undefined; for (args) |arg, i| { const ptr = argv[i]; - args[i] = ptr[0...str.len(ptr)]; + args[i] = ptr[0...cstr.len(ptr)]; } return root.main(args); } 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)]; +} + diff --git a/std/index.zig b/std/index.zig index b4a0de054e..16005faf14 100644 --- a/std/index.zig +++ b/std/index.zig @@ -3,6 +3,7 @@ pub const io = @import("io.zig"); pub const os = @import("os.zig"); pub const math = @import("math.zig"); pub const str = @import("str.zig"); +pub const cstr = @import("cstr.zig"); pub const net = @import("net.zig"); pub fn assert(b: bool) { diff --git a/std/str.zig b/std/str.zig index 060921d199..529c8ae7a0 100644 --- a/std/str.zig +++ b/std/str.zig @@ -1,22 +1,5 @@ const assert = @import("index.zig").assert; -// 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; -} - -pub fn from_c_const(str: &const u8) -> []const u8 { - return str[0...len(str)]; -} - -pub fn from_c(str: &u8) -> []u8 { - return str[0...len(str)]; -} - pub const eql = slice_eql(u8); pub fn slice_eql(T: type)(a: []const T, b: []const T) -> bool { -- cgit v1.2.3