authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-07 10:52:52-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-07 10:52:52-07:00
log01c46eef3a7e4fd5a96f364541c539746ae1ea3b
tree447b4bf1086aae9a01844abcb9e55156d3007c26
parent6f0f357ee43fc02ad2dc1eb44ab127c0d741282c

std: separate str and cstr


6 files changed, 32 insertions(+), 20 deletions(-)

CMakeLists.txt+1
......@@ -206,6 +206,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/io.zig" DESTINATION "${ZIG_STD_DEST}")
206206install(FILES "${CMAKE_SOURCE_DIR}/std/net.zig" DESTINATION "${ZIG_STD_DEST}")
207207install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}")
208208install(FILES "${CMAKE_SOURCE_DIR}/std/str.zig" DESTINATION "${ZIG_STD_DEST}")
209install(FILES "${CMAKE_SOURCE_DIR}/std/cstr.zig" DESTINATION "${ZIG_STD_DEST}")
209210install(FILES "${CMAKE_SOURCE_DIR}/std/linux.zig" DESTINATION "${ZIG_STD_DEST}")
210211install(FILES "${CMAKE_SOURCE_DIR}/std/errno.zig" DESTINATION "${ZIG_STD_DEST}")
211212install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}")
std/bootstrap.zig+2-2
......@@ -2,7 +2,7 @@
22
33const root = @import("@root");
44const linux = @import("linux.zig");
5const str = @import("str.zig");
5const cstr = @import("cstr.zig");
66
77const want_start_symbol = switch(@compile_var("os")) {
88 linux => true,
......@@ -34,7 +34,7 @@ fn call_main() -> %void {
3434 var args: [argc][]u8 = undefined;
3535 for (args) |arg, i| {
3636 const ptr = argv[i];
37 args[i] = ptr[0...str.len(ptr)];
37 args[i] = ptr[0...cstr.len(ptr)];
3838 }
3939 return root.main(args);
4040}
std/cstr.zig created+26
......@@ -0,0 +1,26 @@
1// TODO fix https://github.com/andrewrk/zig/issues/140
2// and then make this able to run at compile time
3#static_eval_enable(false)
4pub fn len(ptr: &const u8) -> isize {
5 var count: isize = 0;
6 while (ptr[count] != 0; count += 1) {}
7 return count;
8}
9
10// TODO fix https://github.com/andrewrk/zig/issues/140
11// and then make this able to run at compile time
12#static_eval_enable(false)
13pub fn cmp(a: &const u8, b: &const u8) -> i32 {
14 var index: isize = 0;
15 while (a[index] == b[index] && a[index] != 0; index += 1) {}
16 return a[index] - b[index];
17}
18
19pub fn to_slice_const(str: &const u8) -> []const u8 {
20 return str[0...len(str)];
21}
22
23pub fn to_slice(str: &u8) -> []u8 {
24 return str[0...len(str)];
25}
26
std/index.zig+1
......@@ -3,6 +3,7 @@ pub const io = @import("io.zig");
33pub const os = @import("os.zig");
44pub const math = @import("math.zig");
55pub const str = @import("str.zig");
6pub const cstr = @import("cstr.zig");
67pub const net = @import("net.zig");
78
89pub fn assert(b: bool) {
std/str.zig-17
......@@ -1,22 +1,5 @@
11const assert = @import("index.zig").assert;
22
3// fix https://github.com/andrewrk/zig/issues/140
4// and then make this able to run at compile time
5#static_eval_enable(false)
6pub fn len(ptr: &const u8) -> isize {
7 var count: isize = 0;
8 while (ptr[count] != 0; count += 1) {}
9 return count;
10}
11
12pub fn from_c_const(str: &const u8) -> []const u8 {
13 return str[0...len(str)];
14}
15
16pub fn from_c(str: &u8) -> []u8 {
17 return str[0...len(str)];
18}
19
203pub const eql = slice_eql(u8);
214
225pub fn slice_eql(T: type)(a: []const T, b: []const T) -> bool {
test/self_hosted.zig+2-1
......@@ -1,6 +1,7 @@
11const std = @import("std");
22const assert = std.assert;
33const str = std.str;
4const cstr = std.cstr;
45const other = @import("other.zig");
56
67#attribute("test")
......@@ -1557,7 +1558,7 @@ fn c_string_concatenation() {
15571558 const a = c"OK" ++ c" IT " ++ c"WORKED";
15581559 const b = c"OK IT WORKED";
15591560
1560 const len = str.len(b);
1561 const len = cstr.len(b);
15611562 const len_with_null = len + 1;
15621563 {var i: i32 = 0; while (i < len_with_null; i += 1) {
15631564 assert(a[i] == b[i]);