zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 01c46eef3a7e4fd5a96f364541c539746ae1ea3b (tree)
parent 6f0f357ee43fc02ad2dc1eb44ab127c0d741282c
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Sat,  7 May 2016 10:52:52 -0700

std: separate str and cstr

Diffstat:
MCMakeLists.txt | 1+
Mstd/bootstrap.zig | 4++--
Astd/cstr.zig | 26++++++++++++++++++++++++++
Mstd/index.zig | 1+
Mstd/str.zig | 17-----------------
Mtest/self_hosted.zig | 3++-
6 files changed, 32 insertions(+), 20 deletions(-)

diff --git 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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]);