| author | |
| committer | |
| log | f4c7e1bf4971503e2d33af80537aef27ed2e4fe1 |
| tree | 1d99e8d2cd97d30870f95f46b2efcf872ccfcc49 |
| parent | 5e33175517807c6e50fe66f8d6c65fc094c8e11a |
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 | 194 | install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner_nolibc.zig" DESTINATION "${ZIG_STD_DEST}") |
| 195 | 195 | install(FILES "${CMAKE_SOURCE_DIR}/std/io.zig" DESTINATION "${ZIG_STD_DEST}") |
| 196 | 196 | install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}") |
| 197 | install(FILES "${CMAKE_SOURCE_DIR}/std/str.zig" DESTINATION "${ZIG_STD_DEST}") | |
| 197 | 198 | install(FILES "${CMAKE_SOURCE_DIR}/std/linux.zig" DESTINATION "${ZIG_STD_DEST}") |
| 198 | 199 | install(FILES "${CMAKE_SOURCE_DIR}/std/errno.zig" DESTINATION "${ZIG_STD_DEST}") |
| 199 | 200 | install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}") |
example/cat/main.zig+4-2| ... | ... | @@ -1,4 +1,6 @@ |
| 1 | use @import("std"); | |
| 1 | const std = @import("std"); | |
| 2 | const io = std.io; | |
| 3 | const str = std.str; | |
| 2 | 4 | |
| 3 | 5 | // TODO var args printing |
| 4 | 6 | |
| ... | ... | @@ -6,7 +8,7 @@ pub fn main(args: [][]u8) -> %void { |
| 6 | 8 | const exe = args[0]; |
| 7 | 9 | var catted_anything = false; |
| 8 | 10 | for (args[1...]) |arg| { |
| 9 | if (str_eql(arg, "-")) { | |
| 11 | if (str.eql(arg, "-")) { | |
| 10 | 12 | catted_anything = true; |
| 11 | 13 | cat_stream(io.stdin) %% |err| return err; |
| 12 | 14 | } else if (arg[0] == '-') { |
std/bootstrap.zig+2-9| ... | ... | @@ -2,6 +2,7 @@ |
| 2 | 2 | |
| 3 | 3 | const root = @import("@root"); |
| 4 | 4 | const linux = @import("linux.zig"); |
| 5 | const str = @import("str.zig"); | |
| 5 | 6 | |
| 6 | 7 | const want_start_symbol = switch(@compile_var("os")) { |
| 7 | 8 | linux => true, |
| ... | ... | @@ -29,19 +30,11 @@ export fn _start() -> unreachable { |
| 29 | 30 | call_main_and_exit() |
| 30 | 31 | } |
| 31 | 32 | |
| 32 | fn strlen(ptr: &const u8) -> isize { | |
| 33 | var count: isize = 0; | |
| 34 | while (ptr[count] != 0) { | |
| 35 | count += 1; | |
| 36 | } | |
| 37 | return count; | |
| 38 | } | |
| 39 | ||
| 40 | 33 | fn call_main() -> %void { |
| 41 | 34 | var args: [argc][]u8 = undefined; |
| 42 | 35 | for (args) |arg, i| { |
| 43 | 36 | const ptr = argv[i]; |
| 44 | args[i] = ptr[0...strlen(ptr)]; | |
| 37 | args[i] = ptr[0...str.len(ptr)]; | |
| 45 | 38 | } |
| 46 | 39 | return root.main(args); |
| 47 | 40 | } |
std/index.zig+1-16| ... | ... | @@ -2,24 +2,9 @@ pub const Rand = @import("rand.zig").Rand; |
| 2 | 2 | pub const io = @import("io.zig"); |
| 3 | 3 | pub const os = @import("os.zig"); |
| 4 | 4 | pub const math = @import("math.zig"); |
| 5 | pub const str = @import("str.zig"); | |
| 5 | 6 | |
| 6 | 7 | pub fn assert(b: bool) { |
| 7 | 8 | if (!b) unreachable{} |
| 8 | 9 | } |
| 9 | 10 | |
| 10 | pub const str_eql = slice_eql(u8); | |
| 11 | ||
| 12 | pub 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") | |
| 21 | fn 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 | 207 | } |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | #attribute("cold") | |
| 211 | pub fn abort() -> unreachable { | |
| 212 | linux.raise(linux.SIGABRT); | |
| 213 | linux.raise(linux.SIGKILL); | |
| 214 | while (true) {} | |
| 215 | } | |
| 216 | ||
| 217 | 210 | pub error InvalidChar; |
| 218 | 211 | pub error Overflow; |
| 219 | 212 |
std/os.zig+8| ... | ... | @@ -26,3 +26,11 @@ pub fn get_random_bytes(buf: []u8) -> %void { |
| 26 | 26 | else => unreachable{}, |
| 27 | 27 | } |
| 28 | 28 | } |
| 29 | ||
| 30 | #attribute("cold") | |
| 31 | pub 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 @@ |
| 1 | const assert = @import("index.zig").assert; | |
| 2 | ||
| 3 | pub 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 | ||
| 11 | pub fn from_c_const(str: &const u8) -> []const u8 { | |
| 12 | return str[0...len(str)]; | |
| 13 | } | |
| 14 | ||
| 15 | pub fn from_c(str: &u8) -> []u8 { | |
| 16 | return str[0...len(str)]; | |
| 17 | } | |
| 18 | ||
| 19 | pub const eql = slice_eql(u8); | |
| 20 | ||
| 21 | pub 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") | |
| 30 | fn 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 | 1 | // test std library |
| 2 | use @import("std"); | |
| 2 | const std = @import("std"); | |
| 3 | const assert = std.assert; | |
| 4 | const str = std.str; | |
| 3 | 5 | |
| 4 | 6 | #attribute("test") |
| 5 | 7 | fn empty_function() {} |
| ... | ... | @@ -701,8 +703,8 @@ three)AOEU"; |
| 701 | 703 | one |
| 702 | 704 | two) |
| 703 | 705 | three)"; |
| 704 | assert(str_eql(s1, s2)); | |
| 705 | assert(str_eql(s3, s2)); | |
| 706 | assert(str.eql(s1, s2)); | |
| 707 | assert(str.eql(s3, s2)); | |
| 706 | 708 | } |
| 707 | 709 | |
| 708 | 710 | |
| ... | ... | @@ -760,7 +762,7 @@ fn accepts_string(foo: []u8) { } |
| 760 | 762 | |
| 761 | 763 | #attribute("test") |
| 762 | 764 | fn hex_escape() { |
| 763 | assert(str_eql("\x68\x65\x6c\x6c\x6f", "hello")); | |
| 765 | assert(str.eql("\x68\x65\x6c\x6c\x6f", "hello")); | |
| 764 | 766 | } |
| 765 | 767 | |
| 766 | 768 | |
| ... | ... | @@ -768,8 +770,8 @@ error AnError; |
| 768 | 770 | error ALongerErrorName; |
| 769 | 771 | #attribute("test") |
| 770 | 772 | fn 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")); | |
| 773 | 775 | } |
| 774 | 776 | |
| 775 | 777 | |
| ... | ... | @@ -1069,12 +1071,12 @@ fn overflow_intrinsics() { |
| 1069 | 1071 | #attribute("test") |
| 1070 | 1072 | fn nested_arrays() { |
| 1071 | 1073 | 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")); | |
| 1078 | 1080 | } |
| 1079 | 1081 | } |
| 1080 | 1082 | |
| ... | ... | @@ -1088,7 +1090,7 @@ fn int_to_ptr_cast() { |
| 1088 | 1090 | |
| 1089 | 1091 | #attribute("test") |
| 1090 | 1092 | fn string_concatenation() { |
| 1091 | assert(str_eql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED")); | |
| 1093 | assert(str.eql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED")); | |
| 1092 | 1094 | } |
| 1093 | 1095 | |
| 1094 | 1096 | #attribute("test") |
| ... | ... | @@ -1185,8 +1187,8 @@ fn test_pointer_to_void_return_type_2() -> &void { |
| 1185 | 1187 | |
| 1186 | 1188 | #attribute("test") |
| 1187 | 1189 | fn 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")); | |
| 1190 | 1192 | } |
| 1191 | 1193 | fn f2(x: bool) -> []u8 { |
| 1192 | 1194 | return (if (x) f_a else f_b)(); |