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_
194194install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner_nolibc.zig" DESTINATION "${ZIG_STD_DEST}")
195195install(FILES "${CMAKE_SOURCE_DIR}/std/io.zig" DESTINATION "${ZIG_STD_DEST}")
196196install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}")
197install(FILES "${CMAKE_SOURCE_DIR}/std/str.zig" DESTINATION "${ZIG_STD_DEST}")
197198install(FILES "${CMAKE_SOURCE_DIR}/std/linux.zig" DESTINATION "${ZIG_STD_DEST}")
198199install(FILES "${CMAKE_SOURCE_DIR}/std/errno.zig" DESTINATION "${ZIG_STD_DEST}")
199200install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}")
example/cat/main.zig+4-2
......@@ -1,4 +1,6 @@
1use @import("std");
1const std = @import("std");
2const io = std.io;
3const str = std.str;
24
35// TODO var args printing
46
......@@ -6,7 +8,7 @@ pub fn main(args: [][]u8) -> %void {
68 const exe = args[0];
79 var catted_anything = false;
810 for (args[1...]) |arg| {
9 if (str_eql(arg, "-")) {
11 if (str.eql(arg, "-")) {
1012 catted_anything = true;
1113 cat_stream(io.stdin) %% |err| return err;
1214 } else if (arg[0] == '-') {
std/bootstrap.zig+2-9
......@@ -2,6 +2,7 @@
22
33const root = @import("@root");
44const linux = @import("linux.zig");
5const str = @import("str.zig");
56
67const want_start_symbol = switch(@compile_var("os")) {
78 linux => true,
......@@ -29,19 +30,11 @@ export fn _start() -> unreachable {
2930 call_main_and_exit()
3031}
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
4033fn call_main() -> %void {
4134 var args: [argc][]u8 = undefined;
4235 for (args) |arg, i| {
4336 const ptr = argv[i];
44 args[i] = ptr[0...strlen(ptr)];
37 args[i] = ptr[0...str.len(ptr)];
4538 }
4639 return root.main(args);
4740}
std/index.zig+1-16
......@@ -2,24 +2,9 @@ pub const Rand = @import("rand.zig").Rand;
22pub const io = @import("io.zig");
33pub const os = @import("os.zig");
44pub const math = @import("math.zig");
5pub const str = @import("str.zig");
56
67pub fn assert(b: bool) {
78 if (!b) unreachable{}
89}
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 {
207207 }
208208}
209209
210#attribute("cold")
211pub fn abort() -> unreachable {
212 linux.raise(linux.SIGABRT);
213 linux.raise(linux.SIGKILL);
214 while (true) {}
215}
216
217210pub error InvalidChar;
218211pub error Overflow;
219212
std/os.zig+8
......@@ -26,3 +26,11 @@ pub fn get_random_bytes(buf: []u8) -> %void {
2626 else => unreachable{},
2727 }
2828}
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 @@
11// test std library
2use @import("std");
2const std = @import("std");
3const assert = std.assert;
4const str = std.str;
35
46#attribute("test")
57fn empty_function() {}
......@@ -701,8 +703,8 @@ three)AOEU";
701703one
702704two)
703705three)";
704 assert(str_eql(s1, s2));
705 assert(str_eql(s3, s2));
706 assert(str.eql(s1, s2));
707 assert(str.eql(s3, s2));
706708}
707709
708710
......@@ -760,7 +762,7 @@ fn accepts_string(foo: []u8) { }
760762
761763#attribute("test")
762764fn hex_escape() {
763 assert(str_eql("\x68\x65\x6c\x6c\x6f", "hello"));
765 assert(str.eql("\x68\x65\x6c\x6c\x6f", "hello"));
764766}
765767
766768
......@@ -768,8 +770,8 @@ error AnError;
768770error ALongerErrorName;
769771#attribute("test")
770772fn error_name_string() {
771 assert(str_eql(@err_name(error.AnError), "AnError"));
772 assert(str_eql(@err_name(error.ALongerErrorName), "ALongerErrorName"));
773 assert(str.eql(@err_name(error.AnError), "AnError"));
774 assert(str.eql(@err_name(error.ALongerErrorName), "ALongerErrorName"));
773775}
774776
775777
......@@ -1069,12 +1071,12 @@ fn overflow_intrinsics() {
10691071#attribute("test")
10701072fn nested_arrays() {
10711073 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
1072 for (array_of_strings) |str, i| {
1073 if (i == 0) assert(str_eql(str, "hello"));
1074 if (i == 1) assert(str_eql(str, "this"));
1075 if (i == 2) assert(str_eql(str, "is"));
1076 if (i == 3) assert(str_eql(str, "my"));
1077 if (i == 4) assert(str_eql(str, "thing"));
1074 for (array_of_strings) |s, i| {
1075 if (i == 0) assert(str.eql(s, "hello"));
1076 if (i == 1) assert(str.eql(s, "this"));
1077 if (i == 2) assert(str.eql(s, "is"));
1078 if (i == 3) assert(str.eql(s, "my"));
1079 if (i == 4) assert(str.eql(s, "thing"));
10781080 }
10791081}
10801082
......@@ -1088,7 +1090,7 @@ fn int_to_ptr_cast() {
10881090
10891091#attribute("test")
10901092fn string_concatenation() {
1091 assert(str_eql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
1093 assert(str.eql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
10921094}
10931095
10941096#attribute("test")
......@@ -1185,8 +1187,8 @@ fn test_pointer_to_void_return_type_2() -> &void {
11851187
11861188#attribute("test")
11871189fn call_result_of_if_else_expression() {
1188 assert(str_eql(f2(true), "a"));
1189 assert(str_eql(f2(false), "b"));
1190 assert(str.eql(f2(true), "a"));
1191 assert(str.eql(f2(false), "b"));
11901192}
11911193fn f2(x: bool) -> []u8 {
11921194 return (if (x) f_a else f_b)();