authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-18 16:42:56-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-18 16:42:56-07:00
logf4c7e1bf4971503e2d33af80537aef27ed2e4fe1
tree1d99e8d2cd97d30870f95f46b2efcf872ccfcc49
parent5e33175517807c6e50fe66f8d6c65fc094c8e11a

rearrange standard library a bit


8 files changed, 67 insertions(+), 49 deletions(-)

CMakeLists.txt+1
...@@ -194,6 +194,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner_libc.zig" DESTINATION "${ZIG_...@@ -194,6 +194,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner_libc.zig" DESTINATION "${ZIG_
194install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner_nolibc.zig" DESTINATION "${ZIG_STD_DEST}")194install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner_nolibc.zig" DESTINATION "${ZIG_STD_DEST}")
195install(FILES "${CMAKE_SOURCE_DIR}/std/io.zig" DESTINATION "${ZIG_STD_DEST}")195install(FILES "${CMAKE_SOURCE_DIR}/std/io.zig" DESTINATION "${ZIG_STD_DEST}")
196install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}")196install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}")
197install(FILES "${CMAKE_SOURCE_DIR}/std/str.zig" DESTINATION "${ZIG_STD_DEST}")
197install(FILES "${CMAKE_SOURCE_DIR}/std/linux.zig" DESTINATION "${ZIG_STD_DEST}")198install(FILES "${CMAKE_SOURCE_DIR}/std/linux.zig" DESTINATION "${ZIG_STD_DEST}")
198install(FILES "${CMAKE_SOURCE_DIR}/std/errno.zig" DESTINATION "${ZIG_STD_DEST}")199install(FILES "${CMAKE_SOURCE_DIR}/std/errno.zig" DESTINATION "${ZIG_STD_DEST}")
199install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}")200install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}")
example/cat/main.zig+4-2
...@@ -1,4 +1,6 @@...@@ -1,4 +1,6 @@
1use @import("std");1const std = @import("std");
2const io = std.io;
3const str = std.str;
24
3// TODO var args printing5// TODO var args printing
46
...@@ -6,7 +8,7 @@ pub fn main(args: [][]u8) -> %void {...@@ -6,7 +8,7 @@ pub fn main(args: [][]u8) -> %void {
6 const exe = args[0];8 const exe = args[0];
7 var catted_anything = false;9 var catted_anything = false;
8 for (args[1...]) |arg| {10 for (args[1...]) |arg| {
9 if (str_eql(arg, "-")) {11 if (str.eql(arg, "-")) {
10 catted_anything = true;12 catted_anything = true;
11 cat_stream(io.stdin) %% |err| return err;13 cat_stream(io.stdin) %% |err| return err;
12 } else if (arg[0] == '-') {14 } else if (arg[0] == '-') {
std/bootstrap.zig+2-9
...@@ -2,6 +2,7 @@...@@ -2,6 +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");
56
6const want_start_symbol = switch(@compile_var("os")) {7const want_start_symbol = switch(@compile_var("os")) {
7 linux => true,8 linux => true,
...@@ -29,19 +30,11 @@ export fn _start() -> unreachable {...@@ -29,19 +30,11 @@ export fn _start() -> unreachable {
29 call_main_and_exit()30 call_main_and_exit()
30}31}
3132
32fn strlen(ptr: &const u8) -> isize {
33 var count: isize = 0;
34 while (ptr[count] != 0) {
35 count += 1;
36 }
37 return count;
38}
39
40fn call_main() -> %void {33fn call_main() -> %void {
41 var args: [argc][]u8 = undefined;34 var args: [argc][]u8 = undefined;
42 for (args) |arg, i| {35 for (args) |arg, i| {
43 const ptr = argv[i];36 const ptr = argv[i];
44 args[i] = ptr[0...strlen(ptr)];37 args[i] = ptr[0...str.len(ptr)];
45 }38 }
46 return root.main(args);39 return root.main(args);
47}40}
std/index.zig+1-16
...@@ -2,24 +2,9 @@ pub const Rand = @import("rand.zig").Rand;...@@ -2,24 +2,9 @@ pub const Rand = @import("rand.zig").Rand;
2pub const io = @import("io.zig");2pub 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");
56
6pub fn assert(b: bool) {7pub fn assert(b: bool) {
7 if (!b) unreachable{}8 if (!b) unreachable{}
8}9}
910
10pub const str_eql = slice_eql(u8);
11
12pub fn slice_eql(T: type)(a: []const T, b: []const T) -> bool {
13 if (a.len != b.len) return false;
14 for (a) |item, index| {
15 if (b[index] != item) return false;
16 }
17 return true;
18}
19
20#attribute("test")
21fn string_equality() {
22 assert(str_eql("abcd", "abcd"));
23 assert(!str_eql("abcdef", "abZdef"));
24 assert(!str_eql("abcdefg", "abcdef"));
25}
std/io.zig-7
...@@ -207,13 +207,6 @@ pub struct InStream {...@@ -207,13 +207,6 @@ pub struct InStream {
207 }207 }
208}208}
209209
210#attribute("cold")
211pub fn abort() -> unreachable {
212 linux.raise(linux.SIGABRT);
213 linux.raise(linux.SIGKILL);
214 while (true) {}
215}
216
217pub error InvalidChar;210pub error InvalidChar;
218pub error Overflow;211pub error Overflow;
219212
std/os.zig+8
...@@ -26,3 +26,11 @@ pub fn get_random_bytes(buf: []u8) -> %void {...@@ -26,3 +26,11 @@ pub fn get_random_bytes(buf: []u8) -> %void {
26 else => unreachable{},26 else => unreachable{},
27 }27 }
28}28}
29
30#attribute("cold")
31pub fn abort() -> unreachable {
32 linux.raise(linux.SIGABRT);
33 linux.raise(linux.SIGKILL);
34 while (true) {}
35}
36
std/str.zig created+34
...@@ -0,0 +1,34 @@
1const assert = @import("index.zig").assert;
2
3pub fn len(ptr: &const u8) -> isize {
4 var count: isize = 0;
5 while (ptr[count] != 0) {
6 count += 1;
7 }
8 return count;
9}
10
11pub fn from_c_const(str: &const u8) -> []const u8 {
12 return str[0...len(str)];
13}
14
15pub fn from_c(str: &u8) -> []u8 {
16 return str[0...len(str)];
17}
18
19pub const eql = slice_eql(u8);
20
21pub fn slice_eql(T: type)(a: []const T, b: []const T) -> bool {
22 if (a.len != b.len) return false;
23 for (a) |item, index| {
24 if (b[index] != item) return false;
25 }
26 return true;
27}
28
29#attribute("test")
30fn string_equality() {
31 assert(eql("abcd", "abcd"));
32 assert(!eql("abcdef", "abZdef"));
33 assert(!eql("abcdefg", "abcdef"));
34}
test/self_hosted.zig+17-15
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1// test std library1// test std library
2use @import("std");2const std = @import("std");
3const assert = std.assert;
4const str = std.str;
35
4#attribute("test")6#attribute("test")
5fn empty_function() {}7fn empty_function() {}
...@@ -701,8 +703,8 @@ three)AOEU";...@@ -701,8 +703,8 @@ three)AOEU";
701one703one
702two)704two)
703three)";705three)";
704 assert(str_eql(s1, s2));706 assert(str.eql(s1, s2));
705 assert(str_eql(s3, s2));707 assert(str.eql(s3, s2));
706}708}
707709
708710
...@@ -760,7 +762,7 @@ fn accepts_string(foo: []u8) { }...@@ -760,7 +762,7 @@ fn accepts_string(foo: []u8) { }
760762
761#attribute("test")763#attribute("test")
762fn hex_escape() {764fn hex_escape() {
763 assert(str_eql("\x68\x65\x6c\x6c\x6f", "hello"));765 assert(str.eql("\x68\x65\x6c\x6c\x6f", "hello"));
764}766}
765767
766768
...@@ -768,8 +770,8 @@ error AnError;...@@ -768,8 +770,8 @@ error AnError;
768error ALongerErrorName;770error ALongerErrorName;
769#attribute("test")771#attribute("test")
770fn error_name_string() {772fn error_name_string() {
771 assert(str_eql(@err_name(error.AnError), "AnError"));773 assert(str.eql(@err_name(error.AnError), "AnError"));
772 assert(str_eql(@err_name(error.ALongerErrorName), "ALongerErrorName"));774 assert(str.eql(@err_name(error.ALongerErrorName), "ALongerErrorName"));
773}775}
774776
775777
...@@ -1069,12 +1071,12 @@ fn overflow_intrinsics() {...@@ -1069,12 +1071,12 @@ fn overflow_intrinsics() {
1069#attribute("test")1071#attribute("test")
1070fn nested_arrays() {1072fn nested_arrays() {
1071 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};1073 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
1072 for (array_of_strings) |str, i| {1074 for (array_of_strings) |s, i| {
1073 if (i == 0) assert(str_eql(str, "hello"));1075 if (i == 0) assert(str.eql(s, "hello"));
1074 if (i == 1) assert(str_eql(str, "this"));1076 if (i == 1) assert(str.eql(s, "this"));
1075 if (i == 2) assert(str_eql(str, "is"));1077 if (i == 2) assert(str.eql(s, "is"));
1076 if (i == 3) assert(str_eql(str, "my"));1078 if (i == 3) assert(str.eql(s, "my"));
1077 if (i == 4) assert(str_eql(str, "thing"));1079 if (i == 4) assert(str.eql(s, "thing"));
1078 }1080 }
1079}1081}
10801082
...@@ -1088,7 +1090,7 @@ fn int_to_ptr_cast() {...@@ -1088,7 +1090,7 @@ fn int_to_ptr_cast() {
10881090
1089#attribute("test")1091#attribute("test")
1090fn string_concatenation() {1092fn string_concatenation() {
1091 assert(str_eql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));1093 assert(str.eql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
1092}1094}
10931095
1094#attribute("test")1096#attribute("test")
...@@ -1185,8 +1187,8 @@ fn test_pointer_to_void_return_type_2() -> &void {...@@ -1185,8 +1187,8 @@ fn test_pointer_to_void_return_type_2() -> &void {
11851187
1186#attribute("test")1188#attribute("test")
1187fn call_result_of_if_else_expression() {1189fn call_result_of_if_else_expression() {
1188 assert(str_eql(f2(true), "a"));1190 assert(str.eql(f2(true), "a"));
1189 assert(str_eql(f2(false), "b"));1191 assert(str.eql(f2(false), "b"));
1190}1192}
1191fn f2(x: bool) -> []u8 {1193fn f2(x: bool) -> []u8 {
1192 return (if (x) f_a else f_b)();1194 return (if (x) f_a else f_b)();