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}")...@@ -206,6 +206,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/io.zig" DESTINATION "${ZIG_STD_DEST}")
206install(FILES "${CMAKE_SOURCE_DIR}/std/net.zig" DESTINATION "${ZIG_STD_DEST}")206install(FILES "${CMAKE_SOURCE_DIR}/std/net.zig" DESTINATION "${ZIG_STD_DEST}")
207install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}")207install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}")
208install(FILES "${CMAKE_SOURCE_DIR}/std/str.zig" DESTINATION "${ZIG_STD_DEST}")208install(FILES "${CMAKE_SOURCE_DIR}/std/str.zig" DESTINATION "${ZIG_STD_DEST}")
209install(FILES "${CMAKE_SOURCE_DIR}/std/cstr.zig" DESTINATION "${ZIG_STD_DEST}")
209install(FILES "${CMAKE_SOURCE_DIR}/std/linux.zig" DESTINATION "${ZIG_STD_DEST}")210install(FILES "${CMAKE_SOURCE_DIR}/std/linux.zig" DESTINATION "${ZIG_STD_DEST}")
210install(FILES "${CMAKE_SOURCE_DIR}/std/errno.zig" DESTINATION "${ZIG_STD_DEST}")211install(FILES "${CMAKE_SOURCE_DIR}/std/errno.zig" DESTINATION "${ZIG_STD_DEST}")
211install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}")212install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}")
std/bootstrap.zig+2-2
...@@ -2,7 +2,7 @@...@@ -2,7 +2,7 @@
22
3const root = @import("@root");3const root = @import("@root");
4const linux = @import("linux.zig");4const linux = @import("linux.zig");
5const str = @import("str.zig");5const cstr = @import("cstr.zig");
66
7const want_start_symbol = switch(@compile_var("os")) {7const want_start_symbol = switch(@compile_var("os")) {
8 linux => true,8 linux => true,
...@@ -34,7 +34,7 @@ fn call_main() -> %void {...@@ -34,7 +34,7 @@ fn call_main() -> %void {
34 var args: [argc][]u8 = undefined;34 var args: [argc][]u8 = undefined;
35 for (args) |arg, i| {35 for (args) |arg, i| {
36 const ptr = argv[i];36 const ptr = argv[i];
37 args[i] = ptr[0...str.len(ptr)];37 args[i] = ptr[0...cstr.len(ptr)];
38 }38 }
39 return root.main(args);39 return root.main(args);
40}40}
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");...@@ -3,6 +3,7 @@ pub const io = @import("io.zig");
3pub const os = @import("os.zig");3pub const os = @import("os.zig");
4pub const math = @import("math.zig");4pub const math = @import("math.zig");
5pub const str = @import("str.zig");5pub const str = @import("str.zig");
6pub const cstr = @import("cstr.zig");
6pub const net = @import("net.zig");7pub const net = @import("net.zig");
78
8pub fn assert(b: bool) {9pub fn assert(b: bool) {
std/str.zig-17
...@@ -1,22 +1,5 @@...@@ -1,22 +1,5 @@
1const assert = @import("index.zig").assert;1const 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
20pub const eql = slice_eql(u8);3pub const eql = slice_eql(u8);
214
22pub fn slice_eql(T: type)(a: []const T, b: []const T) -> bool {5pub fn slice_eql(T: type)(a: []const T, b: []const T) -> bool {
test/self_hosted.zig+2-1
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const assert = std.assert;2const assert = std.assert;
3const str = std.str;3const str = std.str;
4const cstr = std.cstr;
4const other = @import("other.zig");5const other = @import("other.zig");
56
6#attribute("test")7#attribute("test")
...@@ -1557,7 +1558,7 @@ fn c_string_concatenation() {...@@ -1557,7 +1558,7 @@ fn c_string_concatenation() {
1557 const a = c"OK" ++ c" IT " ++ c"WORKED";1558 const a = c"OK" ++ c" IT " ++ c"WORKED";
1558 const b = c"OK IT WORKED";1559 const b = c"OK IT WORKED";
15591560
1560 const len = str.len(b);1561 const len = cstr.len(b);
1561 const len_with_null = len + 1;1562 const len_with_null = len + 1;
1562 {var i: i32 = 0; while (i < len_with_null; i += 1) {1563 {var i: i32 = 0; while (i < len_with_null; i += 1) {
1563 assert(a[i] == b[i]);1564 assert(a[i] == b[i]);