std: separate str and cstr

master
Andrew Kelley 2016-05-07 10:52:52 -07:00
parent 6f0f357ee4
commit 01c46eef3a
6 changed files with 32 additions and 20 deletions

View File

@ -206,6 +206,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/io.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/net.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/str.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/cstr.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/linux.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/errno.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}")

View File

@ -2,7 +2,7 @@
const root = @import("@root");
const linux = @import("linux.zig");
const str = @import("str.zig");
const cstr = @import("cstr.zig");
const want_start_symbol = switch(@compile_var("os")) {
linux => true,
@ -34,7 +34,7 @@ fn call_main() -> %void {
var args: [argc][]u8 = undefined;
for (args) |arg, i| {
const ptr = argv[i];
args[i] = ptr[0...str.len(ptr)];
args[i] = ptr[0...cstr.len(ptr)];
}
return root.main(args);
}

26
std/cstr.zig Normal file
View File

@ -0,0 +1,26 @@
// TODO fix https://github.com/andrewrk/zig/issues/140
// and then make this able to run at compile time
#static_eval_enable(false)
pub fn len(ptr: &const u8) -> isize {
var count: isize = 0;
while (ptr[count] != 0; count += 1) {}
return count;
}
// TODO fix https://github.com/andrewrk/zig/issues/140
// and then make this able to run at compile time
#static_eval_enable(false)
pub fn cmp(a: &const u8, b: &const u8) -> i32 {
var index: isize = 0;
while (a[index] == b[index] && a[index] != 0; index += 1) {}
return a[index] - b[index];
}
pub fn to_slice_const(str: &const u8) -> []const u8 {
return str[0...len(str)];
}
pub fn to_slice(str: &u8) -> []u8 {
return str[0...len(str)];
}

View File

@ -3,6 +3,7 @@ pub const io = @import("io.zig");
pub const os = @import("os.zig");
pub const math = @import("math.zig");
pub const str = @import("str.zig");
pub const cstr = @import("cstr.zig");
pub const net = @import("net.zig");
pub fn assert(b: bool) {

View File

@ -1,22 +1,5 @@
const assert = @import("index.zig").assert;
// fix https://github.com/andrewrk/zig/issues/140
// and then make this able to run at compile time
#static_eval_enable(false)
pub fn len(ptr: &const u8) -> isize {
var count: isize = 0;
while (ptr[count] != 0; count += 1) {}
return count;
}
pub fn from_c_const(str: &const u8) -> []const u8 {
return str[0...len(str)];
}
pub fn from_c(str: &u8) -> []u8 {
return str[0...len(str)];
}
pub const eql = slice_eql(u8);
pub fn slice_eql(T: type)(a: []const T, b: []const T) -> bool {

View File

@ -1,6 +1,7 @@
const std = @import("std");
const assert = std.assert;
const str = std.str;
const cstr = std.cstr;
const other = @import("other.zig");
#attribute("test")
@ -1557,7 +1558,7 @@ fn c_string_concatenation() {
const a = c"OK" ++ c" IT " ++ c"WORKED";
const b = c"OK IT WORKED";
const len = str.len(b);
const len = cstr.len(b);
const len_with_null = len + 1;
{var i: i32 = 0; while (i < len_with_null; i += 1) {
assert(a[i] == b[i]);