From 01c46eef3a7e4fd5a96f364541c539746ae1ea3b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 7 May 2016 10:52:52 -0700 Subject: [PATCH] std: separate str and cstr --- CMakeLists.txt | 1 + std/bootstrap.zig | 4 ++-- std/cstr.zig | 26 ++++++++++++++++++++++++++ std/index.zig | 1 + std/str.zig | 17 ----------------- test/self_hosted.zig | 3 ++- 6 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 std/cstr.zig diff --git a/CMakeLists.txt b/CMakeLists.txt index 91cf201638..545725b384 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -206,6 +206,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/io.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/net.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/str.zig" DESTINATION "${ZIG_STD_DEST}") +install(FILES "${CMAKE_SOURCE_DIR}/std/cstr.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/linux.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/errno.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}") 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 { diff --git a/test/self_hosted.zig b/test/self_hosted.zig index 705dadca3d..8c6c9112e4 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -1,6 +1,7 @@ const std = @import("std"); const assert = std.assert; const str = std.str; +const cstr = std.cstr; const other = @import("other.zig"); #attribute("test") @@ -1557,7 +1558,7 @@ fn c_string_concatenation() { const a = c"OK" ++ c" IT " ++ c"WORKED"; const b = c"OK IT WORKED"; - const len = str.len(b); + const len = cstr.len(b); const len_with_null = len + 1; {var i: i32 = 0; while (i < len_with_null; i += 1) { assert(a[i] == b[i]);