mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
parent
b483db4868
commit
051ee8e626
40 changed files with 164 additions and 158 deletions
|
|
@ -133,7 +133,7 @@ FnCallExpression = "(" list(Expression, ",") ")"
|
||||||
|
|
||||||
ArrayAccessExpression = "[" Expression "]"
|
ArrayAccessExpression = "[" Expression "]"
|
||||||
|
|
||||||
SliceExpression = "[" Expression "..." option(Expression) "]"
|
SliceExpression = "[" Expression ".." option(Expression) "]"
|
||||||
|
|
||||||
ContainerInitExpression = "{" ContainerInitBody "}"
|
ContainerInitExpression = "{" ContainerInitBody "}"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ fn cat_stream(is: &io.InStream) -> %void {
|
||||||
var buf: [1024 * 4]u8 = undefined;
|
var buf: [1024 * 4]u8 = undefined;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const bytes_read = is.read(buf[0...]) %% |err| {
|
const bytes_read = is.read(buf[0..]) %% |err| {
|
||||||
%%io.stderr.printf("Unable to read from stream: {}\n", @errorName(err));
|
%%io.stderr.printf("Unable to read from stream: {}\n", @errorName(err));
|
||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
|
|
@ -49,7 +49,7 @@ fn cat_stream(is: &io.InStream) -> %void {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
io.stdout.write(buf[0...bytes_read]) %% |err| {
|
io.stdout.write(buf[0..bytes_read]) %% |err| {
|
||||||
%%io.stderr.printf("Unable to write to stdout: {}\n", @errorName(err));
|
%%io.stderr.printf("Unable to write to stdout: {}\n", @errorName(err));
|
||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ pub fn main() -> %void {
|
||||||
%%io.stdout.printf("Welcome to the Guess Number Game in Zig.\n");
|
%%io.stdout.printf("Welcome to the Guess Number Game in Zig.\n");
|
||||||
|
|
||||||
var seed_bytes: [@sizeOf(usize)]u8 = undefined;
|
var seed_bytes: [@sizeOf(usize)]u8 = undefined;
|
||||||
%%os.getRandomBytes(seed_bytes[0...]);
|
%%os.getRandomBytes(seed_bytes[0..]);
|
||||||
const seed = std.mem.readInt(seed_bytes, usize, true);
|
const seed = std.mem.readInt(seed_bytes, usize, true);
|
||||||
var rand = Rand.init(seed);
|
var rand = Rand.init(seed);
|
||||||
|
|
||||||
|
|
@ -18,12 +18,12 @@ pub fn main() -> %void {
|
||||||
%%io.stdout.printf("\nGuess a number between 1 and 100: ");
|
%%io.stdout.printf("\nGuess a number between 1 and 100: ");
|
||||||
var line_buf : [20]u8 = undefined;
|
var line_buf : [20]u8 = undefined;
|
||||||
|
|
||||||
const line_len = io.stdin.read(line_buf[0...]) %% |err| {
|
const line_len = io.stdin.read(line_buf[0..]) %% |err| {
|
||||||
%%io.stdout.printf("Unable to read from stdin: {}\n", @errorName(err));
|
%%io.stdout.printf("Unable to read from stdin: {}\n", @errorName(err));
|
||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
|
|
||||||
const guess = fmt.parseUnsigned(u8, line_buf[0...line_len - 1], 10) %% {
|
const guess = fmt.parseUnsigned(u8, line_buf[0..line_len - 1], 10) %% {
|
||||||
%%io.stdout.printf("Invalid number.\n");
|
%%io.stdout.printf("Invalid number.\n");
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
const base64 = @import("std").base64;
|
const base64 = @import("std").base64;
|
||||||
|
|
||||||
export fn decode_base_64(dest_ptr: &u8, dest_len: usize, source_ptr: &const u8, source_len: usize) -> usize {
|
export fn decode_base_64(dest_ptr: &u8, dest_len: usize, source_ptr: &const u8, source_len: usize) -> usize {
|
||||||
const src = source_ptr[0...source_len];
|
const src = source_ptr[0..source_len];
|
||||||
const dest = dest_ptr[0...dest_len];
|
const dest = dest_ptr[0..dest_len];
|
||||||
return base64.decode(dest, src).len;
|
return base64.decode(dest, src).len;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -284,7 +284,7 @@ static AstNode *ast_parse_param_decl(ParseContext *pc, size_t *token_index) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Token *ellipsis_tok = &pc->tokens->at(*token_index);
|
Token *ellipsis_tok = &pc->tokens->at(*token_index);
|
||||||
if (ellipsis_tok->id == TokenIdEllipsis) {
|
if (ellipsis_tok->id == TokenIdEllipsis3) {
|
||||||
*token_index += 1;
|
*token_index += 1;
|
||||||
node->data.param_decl.is_var_args = true;
|
node->data.param_decl.is_var_args = true;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -879,7 +879,7 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index,
|
||||||
|
|
||||||
Token *ellipsis_or_r_bracket = &pc->tokens->at(*token_index);
|
Token *ellipsis_or_r_bracket = &pc->tokens->at(*token_index);
|
||||||
|
|
||||||
if (ellipsis_or_r_bracket->id == TokenIdEllipsis) {
|
if (ellipsis_or_r_bracket->id == TokenIdEllipsis2) {
|
||||||
*token_index += 1;
|
*token_index += 1;
|
||||||
|
|
||||||
AstNode *node = ast_create_node(pc, NodeTypeSliceExpr, first_token);
|
AstNode *node = ast_create_node(pc, NodeTypeSliceExpr, first_token);
|
||||||
|
|
@ -1730,7 +1730,7 @@ static AstNode *ast_parse_for_expr(ParseContext *pc, size_t *token_index, bool m
|
||||||
/*
|
/*
|
||||||
SwitchExpression = "switch" "(" Expression ")" "{" many(SwitchProng) "}"
|
SwitchExpression = "switch" "(" Expression ")" "{" many(SwitchProng) "}"
|
||||||
SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" option("*") Symbol "|") Expression ","
|
SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" option("*") Symbol "|") Expression ","
|
||||||
SwitchItem : Expression | (Expression "..." Expression)
|
SwitchItem = Expression | (Expression "..." Expression)
|
||||||
*/
|
*/
|
||||||
static AstNode *ast_parse_switch_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
|
static AstNode *ast_parse_switch_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
|
||||||
Token *switch_token = &pc->tokens->at(*token_index);
|
Token *switch_token = &pc->tokens->at(*token_index);
|
||||||
|
|
@ -1767,7 +1767,7 @@ static AstNode *ast_parse_switch_expr(ParseContext *pc, size_t *token_index, boo
|
||||||
} else for (;;) {
|
} else for (;;) {
|
||||||
AstNode *expr1 = ast_parse_expression(pc, token_index, true);
|
AstNode *expr1 = ast_parse_expression(pc, token_index, true);
|
||||||
Token *ellipsis_tok = &pc->tokens->at(*token_index);
|
Token *ellipsis_tok = &pc->tokens->at(*token_index);
|
||||||
if (ellipsis_tok->id == TokenIdEllipsis) {
|
if (ellipsis_tok->id == TokenIdEllipsis3) {
|
||||||
*token_index += 1;
|
*token_index += 1;
|
||||||
|
|
||||||
AstNode *range_node = ast_create_node(pc, NodeTypeSwitchRange, ellipsis_tok);
|
AstNode *range_node = ast_create_node(pc, NodeTypeSwitchRange, ellipsis_tok);
|
||||||
|
|
|
||||||
|
|
@ -588,7 +588,7 @@ void tokenize(Buf *buf, Tokenization *out) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '.':
|
case '.':
|
||||||
t.state = TokenizeStateSawDotDot;
|
t.state = TokenizeStateSawDotDot;
|
||||||
set_token_id(&t, t.cur_tok, TokenIdEllipsis);
|
set_token_id(&t, t.cur_tok, TokenIdEllipsis2);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
t.pos -= 1;
|
t.pos -= 1;
|
||||||
|
|
@ -601,10 +601,14 @@ void tokenize(Buf *buf, Tokenization *out) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '.':
|
case '.':
|
||||||
t.state = TokenizeStateStart;
|
t.state = TokenizeStateStart;
|
||||||
|
set_token_id(&t, t.cur_tok, TokenIdEllipsis3);
|
||||||
end_token(&t);
|
end_token(&t);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
tokenize_error(&t, "invalid character: '%c'", c);
|
t.pos -= 1;
|
||||||
|
end_token(&t);
|
||||||
|
t.state = TokenizeStateStart;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TokenizeStateSawGreaterThan:
|
case TokenizeStateSawGreaterThan:
|
||||||
|
|
@ -1436,7 +1440,8 @@ const char * token_name(TokenId id) {
|
||||||
case TokenIdDivEq: return "/=";
|
case TokenIdDivEq: return "/=";
|
||||||
case TokenIdDot: return ".";
|
case TokenIdDot: return ".";
|
||||||
case TokenIdDoubleQuestion: return "??";
|
case TokenIdDoubleQuestion: return "??";
|
||||||
case TokenIdEllipsis: return "...";
|
case TokenIdEllipsis3: return "...";
|
||||||
|
case TokenIdEllipsis2: return "..";
|
||||||
case TokenIdEof: return "EOF";
|
case TokenIdEof: return "EOF";
|
||||||
case TokenIdEq: return "=";
|
case TokenIdEq: return "=";
|
||||||
case TokenIdFatArrow: return "=>";
|
case TokenIdFatArrow: return "=>";
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,8 @@ enum TokenId {
|
||||||
TokenIdDivEq,
|
TokenIdDivEq,
|
||||||
TokenIdDot,
|
TokenIdDot,
|
||||||
TokenIdDoubleQuestion,
|
TokenIdDoubleQuestion,
|
||||||
TokenIdEllipsis,
|
TokenIdEllipsis3,
|
||||||
|
TokenIdEllipsis2,
|
||||||
TokenIdEof,
|
TokenIdEof,
|
||||||
TokenIdEq,
|
TokenIdEq,
|
||||||
TokenIdFatArrow,
|
TokenIdFatArrow,
|
||||||
|
|
|
||||||
|
|
@ -27,11 +27,11 @@ pub fn ArrayList(comptime T: type) -> type{
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toSlice(l: &Self) -> []T {
|
pub fn toSlice(l: &Self) -> []T {
|
||||||
return l.items[0...l.len];
|
return l.items[0..l.len];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toSliceConst(l: &const Self) -> []const T {
|
pub fn toSliceConst(l: &const Self) -> []const T {
|
||||||
return l.items[0...l.len];
|
return l.items[0..l.len];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn append(l: &Self, item: &const T) -> %void {
|
pub fn append(l: &Self, item: &const T) -> %void {
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ pub fn encodeWithAlphabet(dest: []u8, source: []const u8, alphabet: []const u8)
|
||||||
out_index += 1;
|
out_index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return dest[0...out_index];
|
return dest[0..out_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decodeWithAlphabet(dest: []u8, source: []const u8, alphabet: []const u8) -> []u8 {
|
pub fn decodeWithAlphabet(dest: []u8, source: []const u8, alphabet: []const u8) -> []u8 {
|
||||||
|
|
@ -67,7 +67,7 @@ pub fn decodeWithAlphabet(dest: []u8, source: []const u8, alphabet: []const u8)
|
||||||
ascii6[c] = u8(i);
|
ascii6[c] = u8(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
return decodeWithAscii6BitMap(dest, source, ascii6[0...], alphabet[64]);
|
return decodeWithAscii6BitMap(dest, source, ascii6[0..], alphabet[64]);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decodeWithAscii6BitMap(dest: []u8, source: []const u8, ascii6: []const u8, pad_char: u8) -> []u8 {
|
pub fn decodeWithAscii6BitMap(dest: []u8, source: []const u8, ascii6: []const u8, pad_char: u8) -> []u8 {
|
||||||
|
|
@ -115,7 +115,7 @@ pub fn decodeWithAscii6BitMap(dest: []u8, source: []const u8, ascii6: []const u8
|
||||||
dest_index += 1;
|
dest_index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return dest[0...dest_index];
|
return dest[0..dest_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn calcEncodedSize(source_len: usize) -> usize {
|
pub fn calcEncodedSize(source_len: usize) -> usize {
|
||||||
|
|
@ -174,11 +174,11 @@ fn testBase64Case(expected_decoded: []const u8, expected_encoded: []const u8) {
|
||||||
|
|
||||||
var buf: [100]u8 = undefined;
|
var buf: [100]u8 = undefined;
|
||||||
|
|
||||||
const actual_decoded = decode(buf[0...], expected_encoded);
|
const actual_decoded = decode(buf[0..], expected_encoded);
|
||||||
assert(actual_decoded.len == expected_decoded.len);
|
assert(actual_decoded.len == expected_decoded.len);
|
||||||
assert(mem.eql(u8, expected_decoded, actual_decoded));
|
assert(mem.eql(u8, expected_decoded, actual_decoded));
|
||||||
|
|
||||||
const actual_encoded = encode(buf[0...], expected_decoded);
|
const actual_encoded = encode(buf[0..], expected_decoded);
|
||||||
assert(actual_encoded.len == expected_encoded.len);
|
assert(actual_encoded.len == expected_encoded.len);
|
||||||
assert(mem.eql(u8, expected_encoded, actual_encoded));
|
assert(mem.eql(u8, expected_encoded, actual_encoded));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ pub const BufMap = struct {
|
||||||
|
|
||||||
fn free(self: &BufMap, value: []const u8) {
|
fn free(self: &BufMap, value: []const u8) {
|
||||||
// remove the const
|
// remove the const
|
||||||
const mut_value = @ptrCast(&u8, value.ptr)[0...value.len];
|
const mut_value = @ptrCast(&u8, value.ptr)[0..value.len];
|
||||||
self.hash_map.allocator.free(mut_value);
|
self.hash_map.allocator.free(mut_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ pub const BufSet = struct {
|
||||||
|
|
||||||
fn free(self: &BufSet, value: []const u8) {
|
fn free(self: &BufSet, value: []const u8) {
|
||||||
// remove the const
|
// remove the const
|
||||||
const mut_value = @ptrCast(&u8, value.ptr)[0...value.len];
|
const mut_value = @ptrCast(&u8, value.ptr)[0..value.len];
|
||||||
self.hash_map.allocator.free(mut_value);
|
self.hash_map.allocator.free(mut_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,11 +43,11 @@ pub const Buffer = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toSlice(self: &Buffer) -> []u8 {
|
pub fn toSlice(self: &Buffer) -> []u8 {
|
||||||
return self.list.toSlice()[0...self.len()];
|
return self.list.toSlice()[0..self.len()];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toSliceConst(self: &const Buffer) -> []const u8 {
|
pub fn toSliceConst(self: &const Buffer) -> []const u8 {
|
||||||
return self.list.toSliceConst()[0...self.len()];
|
return self.list.toSliceConst()[0..self.len()];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resize(self: &Buffer, new_len: usize) -> %void {
|
pub fn resize(self: &Buffer, new_len: usize) -> %void {
|
||||||
|
|
@ -66,7 +66,7 @@ pub const Buffer = struct {
|
||||||
pub fn append(self: &Buffer, m: []const u8) -> %void {
|
pub fn append(self: &Buffer, m: []const u8) -> %void {
|
||||||
const old_len = self.len();
|
const old_len = self.len();
|
||||||
%return self.resize(old_len + m.len);
|
%return self.resize(old_len + m.len);
|
||||||
mem.copy(u8, self.list.toSlice()[old_len...], m);
|
mem.copy(u8, self.list.toSlice()[old_len..], m);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn appendByte(self: &Buffer, byte: u8) -> %void {
|
pub fn appendByte(self: &Buffer, byte: u8) -> %void {
|
||||||
|
|
@ -80,14 +80,14 @@ pub const Buffer = struct {
|
||||||
|
|
||||||
pub fn startsWith(self: &const Buffer, m: []const u8) -> bool {
|
pub fn startsWith(self: &const Buffer, m: []const u8) -> bool {
|
||||||
if (self.len() < m.len) return false;
|
if (self.len() < m.len) return false;
|
||||||
return mem.eql(u8, self.list.items[0...m.len], m);
|
return mem.eql(u8, self.list.items[0..m.len], m);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn endsWith(self: &const Buffer, m: []const u8) -> bool {
|
pub fn endsWith(self: &const Buffer, m: []const u8) -> bool {
|
||||||
const l = self.len();
|
const l = self.len();
|
||||||
if (l < m.len) return false;
|
if (l < m.len) return false;
|
||||||
const start = l - m.len;
|
const start = l - m.len;
|
||||||
return mem.eql(u8, self.list.items[start...], m);
|
return mem.eql(u8, self.list.items[start..], m);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn replaceContents(self: &const Buffer, m: []const u8) -> %void {
|
pub fn replaceContents(self: &const Buffer, m: []const u8) -> %void {
|
||||||
|
|
|
||||||
|
|
@ -335,7 +335,7 @@ pub const Builder = struct {
|
||||||
};
|
};
|
||||||
self.addRPath(rpath);
|
self.addRPath(rpath);
|
||||||
} else if (word.len > 2 and word[0] == '-' and word[1] == 'L') {
|
} else if (word.len > 2 and word[0] == '-' and word[1] == 'L') {
|
||||||
const lib_path = word[2...];
|
const lib_path = word[2..];
|
||||||
self.addLibPath(lib_path);
|
self.addLibPath(lib_path);
|
||||||
} else {
|
} else {
|
||||||
%%io.stderr.printf("Unrecognized C flag from NIX_LDFLAGS: {}\n", word);
|
%%io.stderr.printf("Unrecognized C flag from NIX_LDFLAGS: {}\n", word);
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,11 @@ pub fn cmp(a: &const u8, b: &const u8) -> i8 {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toSliceConst(str: &const u8) -> []const u8 {
|
pub fn toSliceConst(str: &const u8) -> []const u8 {
|
||||||
return str[0...len(str)];
|
return str[0..len(str)];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toSlice(str: &u8) -> []u8 {
|
pub fn toSlice(str: &u8) -> []u8 {
|
||||||
return str[0...len(str)];
|
return str[0..len(str)];
|
||||||
}
|
}
|
||||||
|
|
||||||
test "cstr fns" {
|
test "cstr fns" {
|
||||||
|
|
|
||||||
|
|
@ -149,8 +149,8 @@ fn printLineFromFile(allocator: &mem.Allocator, out_stream: &io.OutStream, line_
|
||||||
var column: usize = 1;
|
var column: usize = 1;
|
||||||
var abs_index: usize = 0;
|
var abs_index: usize = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
const amt_read = %return f.read(buf[0...]);
|
const amt_read = %return f.read(buf[0..]);
|
||||||
const slice = buf[0...amt_read];
|
const slice = buf[0..amt_read];
|
||||||
|
|
||||||
for (slice) |byte| {
|
for (slice) |byte| {
|
||||||
if (line == line_info.line) {
|
if (line == line_info.line) {
|
||||||
|
|
@ -939,7 +939,7 @@ var some_mem: [100 * 1024]u8 = undefined;
|
||||||
var some_mem_index: usize = 0;
|
var some_mem_index: usize = 0;
|
||||||
|
|
||||||
fn globalAlloc(self: &mem.Allocator, n: usize) -> %[]u8 {
|
fn globalAlloc(self: &mem.Allocator, n: usize) -> %[]u8 {
|
||||||
const result = some_mem[some_mem_index ... some_mem_index + n];
|
const result = some_mem[some_mem_index .. some_mem_index + n];
|
||||||
some_mem_index += n;
|
some_mem_index += n;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,7 @@ pub const Elf = struct {
|
||||||
elf.auto_close_stream = false;
|
elf.auto_close_stream = false;
|
||||||
|
|
||||||
var magic: [4]u8 = undefined;
|
var magic: [4]u8 = undefined;
|
||||||
%return elf.in_stream.readNoEof(magic[0...]);
|
%return elf.in_stream.readNoEof(magic[0..]);
|
||||||
if (!mem.eql(u8, magic, "\x7fELF")) return error.InvalidFormat;
|
if (!mem.eql(u8, magic, "\x7fELF")) return error.InvalidFormat;
|
||||||
|
|
||||||
elf.is_64 = switch (%return elf.in_stream.readByte()) {
|
elf.is_64 = switch (%return elf.in_stream.readByte()) {
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,6 @@ pub fn swapIf(is_be: bool, comptime T: type, x: T) -> T {
|
||||||
|
|
||||||
pub fn swap(comptime T: type, x: T) -> T {
|
pub fn swap(comptime T: type, x: T) -> T {
|
||||||
var buf: [@sizeOf(T)]u8 = undefined;
|
var buf: [@sizeOf(T)]u8 = undefined;
|
||||||
mem.writeInt(buf[0...], x, false);
|
mem.writeInt(buf[0..], x, false);
|
||||||
return mem.readInt(buf, T, true);
|
return mem.readInt(buf, T, true);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
38
std/fmt.zig
38
std/fmt.zig
|
|
@ -38,14 +38,14 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->bool,
|
||||||
'{' => {
|
'{' => {
|
||||||
// TODO if you make this an if statement with `and` then it breaks
|
// TODO if you make this an if statement with `and` then it breaks
|
||||||
if (start_index < i) {
|
if (start_index < i) {
|
||||||
if (!output(context, fmt[start_index...i]))
|
if (!output(context, fmt[start_index..i]))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
state = State.OpenBrace;
|
state = State.OpenBrace;
|
||||||
},
|
},
|
||||||
'}' => {
|
'}' => {
|
||||||
if (start_index < i) {
|
if (start_index < i) {
|
||||||
if (!output(context, fmt[start_index...i]))
|
if (!output(context, fmt[start_index..i]))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
state = State.CloseBrace;
|
state = State.CloseBrace;
|
||||||
|
|
@ -123,7 +123,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->bool,
|
||||||
},
|
},
|
||||||
State.IntegerWidth => switch (c) {
|
State.IntegerWidth => switch (c) {
|
||||||
'}' => {
|
'}' => {
|
||||||
width = comptime %%parseUnsigned(usize, fmt[width_start...i], 10);
|
width = comptime %%parseUnsigned(usize, fmt[width_start..i], 10);
|
||||||
if (!formatInt(args[next_arg], radix, uppercase, width, context, output))
|
if (!formatInt(args[next_arg], radix, uppercase, width, context, output))
|
||||||
return false;
|
return false;
|
||||||
next_arg += 1;
|
next_arg += 1;
|
||||||
|
|
@ -135,7 +135,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->bool,
|
||||||
},
|
},
|
||||||
State.BufWidth => switch (c) {
|
State.BufWidth => switch (c) {
|
||||||
'}' => {
|
'}' => {
|
||||||
width = comptime %%parseUnsigned(usize, fmt[width_start...i], 10);
|
width = comptime %%parseUnsigned(usize, fmt[width_start..i], 10);
|
||||||
if (!formatBuf(args[next_arg], width, context, output))
|
if (!formatBuf(args[next_arg], width, context, output))
|
||||||
return false;
|
return false;
|
||||||
next_arg += 1;
|
next_arg += 1;
|
||||||
|
|
@ -166,7 +166,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->bool,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (start_index < fmt.len) {
|
if (start_index < fmt.len) {
|
||||||
if (!output(context, fmt[start_index...]))
|
if (!output(context, fmt[start_index..]))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -198,7 +198,7 @@ pub fn formatValue(value: var, context: var, output: fn(@typeOf(context), []cons
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn formatAsciiChar(c: u8, context: var, output: fn(@typeOf(context), []const u8)->bool) -> bool {
|
pub fn formatAsciiChar(c: u8, context: var, output: fn(@typeOf(context), []const u8)->bool) -> bool {
|
||||||
return output(context, (&c)[0...1]);
|
return output(context, (&c)[0..1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn formatBuf(buf: []const u8, width: usize,
|
pub fn formatBuf(buf: []const u8, width: usize,
|
||||||
|
|
@ -210,7 +210,7 @@ pub fn formatBuf(buf: []const u8, width: usize,
|
||||||
var leftover_padding = if (width > buf.len) (width - buf.len) else return true;
|
var leftover_padding = if (width > buf.len) (width - buf.len) else return true;
|
||||||
const pad_byte: u8 = ' ';
|
const pad_byte: u8 = ' ';
|
||||||
while (leftover_padding > 0) : (leftover_padding -= 1) {
|
while (leftover_padding > 0) : (leftover_padding -= 1) {
|
||||||
if (!output(context, (&pad_byte)[0...1]))
|
if (!output(context, (&pad_byte)[0..1]))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -234,7 +234,7 @@ fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize,
|
||||||
const uint = @IntType(false, @typeOf(value).bit_count);
|
const uint = @IntType(false, @typeOf(value).bit_count);
|
||||||
if (value < 0) {
|
if (value < 0) {
|
||||||
const minus_sign: u8 = '-';
|
const minus_sign: u8 = '-';
|
||||||
if (!output(context, (&minus_sign)[0...1]))
|
if (!output(context, (&minus_sign)[0..1]))
|
||||||
return false;
|
return false;
|
||||||
const new_value = uint(-(value + 1)) + 1;
|
const new_value = uint(-(value + 1)) + 1;
|
||||||
const new_width = if (width == 0) 0 else (width - 1);
|
const new_width = if (width == 0) 0 else (width - 1);
|
||||||
|
|
@ -243,7 +243,7 @@ fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize,
|
||||||
return formatIntUnsigned(uint(value), base, uppercase, width, context, output);
|
return formatIntUnsigned(uint(value), base, uppercase, width, context, output);
|
||||||
} else {
|
} else {
|
||||||
const plus_sign: u8 = '+';
|
const plus_sign: u8 = '+';
|
||||||
if (!output(context, (&plus_sign)[0...1]))
|
if (!output(context, (&plus_sign)[0..1]))
|
||||||
return false;
|
return false;
|
||||||
const new_value = uint(value);
|
const new_value = uint(value);
|
||||||
const new_width = if (width == 0) 0 else (width - 1);
|
const new_width = if (width == 0) 0 else (width - 1);
|
||||||
|
|
@ -269,24 +269,24 @@ fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize,
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
const digits_buf = buf[index...];
|
const digits_buf = buf[index..];
|
||||||
const padding = if (width > digits_buf.len) (width - digits_buf.len) else 0;
|
const padding = if (width > digits_buf.len) (width - digits_buf.len) else 0;
|
||||||
|
|
||||||
if (padding > index) {
|
if (padding > index) {
|
||||||
const zero_byte: u8 = '0';
|
const zero_byte: u8 = '0';
|
||||||
var leftover_padding = padding - index;
|
var leftover_padding = padding - index;
|
||||||
while (true) {
|
while (true) {
|
||||||
if (!output(context, (&zero_byte)[0...1]))
|
if (!output(context, (&zero_byte)[0..1]))
|
||||||
return false;
|
return false;
|
||||||
leftover_padding -= 1;
|
leftover_padding -= 1;
|
||||||
if (leftover_padding == 0)
|
if (leftover_padding == 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
mem.set(u8, buf[0...index], '0');
|
mem.set(u8, buf[0..index], '0');
|
||||||
return output(context, buf);
|
return output(context, buf);
|
||||||
} else {
|
} else {
|
||||||
const padded_buf = buf[index - padding...];
|
const padded_buf = buf[index - padding..];
|
||||||
mem.set(u8, padded_buf[0...padding], '0');
|
mem.set(u8, padded_buf[0..padding], '0');
|
||||||
return output(context, padded_buf);
|
return output(context, padded_buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -304,7 +304,7 @@ const FormatIntBuf = struct {
|
||||||
index: usize,
|
index: usize,
|
||||||
};
|
};
|
||||||
fn formatIntCallback(context: &FormatIntBuf, bytes: []const u8) -> bool {
|
fn formatIntCallback(context: &FormatIntBuf, bytes: []const u8) -> bool {
|
||||||
mem.copy(u8, context.out_buf[context.index...], bytes);
|
mem.copy(u8, context.out_buf[context.index..], bytes);
|
||||||
context.index += bytes.len;
|
context.index += bytes.len;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -350,14 +350,14 @@ const BufPrintContext = struct {
|
||||||
|
|
||||||
fn bufPrintWrite(context: &BufPrintContext, bytes: []const u8) -> bool {
|
fn bufPrintWrite(context: &BufPrintContext, bytes: []const u8) -> bool {
|
||||||
mem.copy(u8, context.remaining, bytes);
|
mem.copy(u8, context.remaining, bytes);
|
||||||
context.remaining = context.remaining[bytes.len...];
|
context.remaining = context.remaining[bytes.len..];
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: ...) -> []u8 {
|
pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: ...) -> []u8 {
|
||||||
var context = BufPrintContext { .remaining = buf, };
|
var context = BufPrintContext { .remaining = buf, };
|
||||||
_ = format(&context, bufPrintWrite, fmt, args);
|
_ = format(&context, bufPrintWrite, fmt, args);
|
||||||
return buf[0...buf.len - context.remaining.len];
|
return buf[0..buf.len - context.remaining.len];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn allocPrint(allocator: &mem.Allocator, comptime fmt: []const u8, args: ...) -> %[]u8 {
|
pub fn allocPrint(allocator: &mem.Allocator, comptime fmt: []const u8, args: ...) -> %[]u8 {
|
||||||
|
|
@ -374,7 +374,7 @@ fn countSize(size: &usize, bytes: []const u8) -> bool {
|
||||||
|
|
||||||
test "buf print int" {
|
test "buf print int" {
|
||||||
var buffer: [max_int_digits]u8 = undefined;
|
var buffer: [max_int_digits]u8 = undefined;
|
||||||
const buf = buffer[0...];
|
const buf = buffer[0..];
|
||||||
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110"));
|
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110"));
|
||||||
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678"));
|
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678"));
|
||||||
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, false, 0), "-bc614e"));
|
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, false, 0), "-bc614e"));
|
||||||
|
|
@ -391,7 +391,7 @@ test "buf print int" {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, width: usize) -> []u8 {
|
fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, width: usize) -> []u8 {
|
||||||
return buf[0...formatIntBuf(buf, value, base, uppercase, width)];
|
return buf[0..formatIntBuf(buf, value, base, uppercase, width)];
|
||||||
}
|
}
|
||||||
|
|
||||||
test "parse u64 digit too big" {
|
test "parse u64 digit too big" {
|
||||||
|
|
|
||||||
14
std/io.zig
14
std/io.zig
|
|
@ -113,7 +113,7 @@ pub const OutStream = struct {
|
||||||
while (src_index < bytes.len) {
|
while (src_index < bytes.len) {
|
||||||
const dest_space_left = self.buffer.len - self.index;
|
const dest_space_left = self.buffer.len - self.index;
|
||||||
const copy_amt = math.min(dest_space_left, bytes.len - src_index);
|
const copy_amt = math.min(dest_space_left, bytes.len - src_index);
|
||||||
mem.copy(u8, self.buffer[self.index...], bytes[src_index...src_index + copy_amt]);
|
mem.copy(u8, self.buffer[self.index..], bytes[src_index..src_index + copy_amt]);
|
||||||
self.index += copy_amt;
|
self.index += copy_amt;
|
||||||
assert(self.index <= self.buffer.len);
|
assert(self.index <= self.buffer.len);
|
||||||
if (self.index == self.buffer.len) {
|
if (self.index == self.buffer.len) {
|
||||||
|
|
@ -152,7 +152,7 @@ pub const OutStream = struct {
|
||||||
|
|
||||||
pub fn flush(self: &OutStream) -> %void {
|
pub fn flush(self: &OutStream) -> %void {
|
||||||
if (self.index != 0) {
|
if (self.index != 0) {
|
||||||
%return os.posixWrite(self.fd, self.buffer[0...self.index]);
|
%return os.posixWrite(self.fd, self.buffer[0..self.index]);
|
||||||
self.index = 0;
|
self.index = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -236,13 +236,13 @@ pub const InStream = struct {
|
||||||
|
|
||||||
pub fn readByte(is: &InStream) -> %u8 {
|
pub fn readByte(is: &InStream) -> %u8 {
|
||||||
var result: [1]u8 = undefined;
|
var result: [1]u8 = undefined;
|
||||||
%return is.readNoEof(result[0...]);
|
%return is.readNoEof(result[0..]);
|
||||||
return result[0];
|
return result[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn readByteSigned(is: &InStream) -> %i8 {
|
pub fn readByteSigned(is: &InStream) -> %i8 {
|
||||||
var result: [1]i8 = undefined;
|
var result: [1]i8 = undefined;
|
||||||
%return is.readNoEof(([]u8)(result[0...]));
|
%return is.readNoEof(([]u8)(result[0..]));
|
||||||
return result[0];
|
return result[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -256,7 +256,7 @@ pub const InStream = struct {
|
||||||
|
|
||||||
pub fn readInt(is: &InStream, is_be: bool, comptime T: type) -> %T {
|
pub fn readInt(is: &InStream, is_be: bool, comptime T: type) -> %T {
|
||||||
var bytes: [@sizeOf(T)]u8 = undefined;
|
var bytes: [@sizeOf(T)]u8 = undefined;
|
||||||
%return is.readNoEof(bytes[0...]);
|
%return is.readNoEof(bytes[0..]);
|
||||||
return mem.readInt(bytes, T, is_be);
|
return mem.readInt(bytes, T, is_be);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -264,7 +264,7 @@ pub const InStream = struct {
|
||||||
assert(size <= @sizeOf(T));
|
assert(size <= @sizeOf(T));
|
||||||
assert(size <= 8);
|
assert(size <= 8);
|
||||||
var input_buf: [8]u8 = undefined;
|
var input_buf: [8]u8 = undefined;
|
||||||
const input_slice = input_buf[0...size];
|
const input_slice = input_buf[0..size];
|
||||||
%return is.readNoEof(input_slice);
|
%return is.readNoEof(input_slice);
|
||||||
return mem.readInt(input_slice, T, is_be);
|
return mem.readInt(input_slice, T, is_be);
|
||||||
}
|
}
|
||||||
|
|
@ -349,7 +349,7 @@ pub const InStream = struct {
|
||||||
|
|
||||||
var actual_buf_len: usize = 0;
|
var actual_buf_len: usize = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
const dest_slice = buf.toSlice()[actual_buf_len...];
|
const dest_slice = buf.toSlice()[actual_buf_len..];
|
||||||
const bytes_read = %return is.read(dest_slice);
|
const bytes_read = %return is.read(dest_slice);
|
||||||
actual_buf_len += bytes_read;
|
actual_buf_len += bytes_read;
|
||||||
|
|
||||||
|
|
|
||||||
22
std/mem.zig
22
std/mem.zig
|
|
@ -31,7 +31,7 @@ pub const Allocator = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn destroy(self: &Allocator, ptr: var) {
|
fn destroy(self: &Allocator, ptr: var) {
|
||||||
self.free(ptr[0...1]);
|
self.free(ptr[0..1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn alloc(self: &Allocator, comptime T: type, n: usize) -> %[]T {
|
fn alloc(self: &Allocator, comptime T: type, n: usize) -> %[]T {
|
||||||
|
|
@ -69,7 +69,7 @@ pub const IncrementingAllocator = struct {
|
||||||
.reallocFn = realloc,
|
.reallocFn = realloc,
|
||||||
.freeFn = free,
|
.freeFn = free,
|
||||||
},
|
},
|
||||||
.bytes = @intToPtr(&u8, addr)[0...capacity],
|
.bytes = @intToPtr(&u8, addr)[0..capacity],
|
||||||
.end_index = 0,
|
.end_index = 0,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
@ -87,7 +87,7 @@ pub const IncrementingAllocator = struct {
|
||||||
if (new_end_index > self.bytes.len) {
|
if (new_end_index > self.bytes.len) {
|
||||||
return error.NoMem;
|
return error.NoMem;
|
||||||
}
|
}
|
||||||
const result = self.bytes[self.end_index...new_end_index];
|
const result = self.bytes[self.end_index..new_end_index];
|
||||||
self.end_index = new_end_index;
|
self.end_index = new_end_index;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -165,7 +165,7 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) -> ?usi
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
const end = haystack.len - needle.len;
|
const end = haystack.len - needle.len;
|
||||||
while (i <= end) : (i += 1) {
|
while (i <= end) : (i += 1) {
|
||||||
if (eql(T, haystack[i...i + needle.len], needle))
|
if (eql(T, haystack[i .. i + needle.len], needle))
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -253,7 +253,7 @@ test "mem.split" {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) -> bool {
|
pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) -> bool {
|
||||||
return if (needle.len > haystack.len) false else eql(T, haystack[0...needle.len], needle);
|
return if (needle.len > haystack.len) false else eql(T, haystack[0 .. needle.len], needle);
|
||||||
}
|
}
|
||||||
|
|
||||||
const SplitIterator = struct {
|
const SplitIterator = struct {
|
||||||
|
|
@ -273,7 +273,7 @@ const SplitIterator = struct {
|
||||||
while (self.index < self.s.len and self.s[self.index] != self.c) : (self.index += 1) {}
|
while (self.index < self.s.len and self.s[self.index] != self.c) : (self.index += 1) {}
|
||||||
const end = self.index;
|
const end = self.index;
|
||||||
|
|
||||||
return self.s[start...end];
|
return self.s[start..end];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a slice of the remaining bytes. Does not affect iterator state.
|
/// Returns a slice of the remaining bytes. Does not affect iterator state.
|
||||||
|
|
@ -281,7 +281,7 @@ const SplitIterator = struct {
|
||||||
// move to beginning of token
|
// move to beginning of token
|
||||||
var index: usize = self.index;
|
var index: usize = self.index;
|
||||||
while (index < self.s.len and self.s[index] == self.c) : (index += 1) {}
|
while (index < self.s.len and self.s[index] == self.c) : (index += 1) {}
|
||||||
return self.s[index...];
|
return self.s[index..];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -320,16 +320,16 @@ test "testWriteInt" {
|
||||||
fn testWriteIntImpl() {
|
fn testWriteIntImpl() {
|
||||||
var bytes: [4]u8 = undefined;
|
var bytes: [4]u8 = undefined;
|
||||||
|
|
||||||
writeInt(bytes[0...], u32(0x12345678), true);
|
writeInt(bytes[0..], u32(0x12345678), true);
|
||||||
assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
|
assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
|
||||||
|
|
||||||
writeInt(bytes[0...], u32(0x78563412), false);
|
writeInt(bytes[0..], u32(0x78563412), false);
|
||||||
assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
|
assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
|
||||||
|
|
||||||
writeInt(bytes[0...], u16(0x1234), true);
|
writeInt(bytes[0..], u16(0x1234), true);
|
||||||
assert(eql(u8, bytes, []u8{ 0x00, 0x00, 0x12, 0x34 }));
|
assert(eql(u8, bytes, []u8{ 0x00, 0x00, 0x12, 0x34 }));
|
||||||
|
|
||||||
writeInt(bytes[0...], u16(0x1234), false);
|
writeInt(bytes[0..], u16(0x1234), false);
|
||||||
assert(eql(u8, bytes, []u8{ 0x34, 0x12, 0x00, 0x00 }));
|
assert(eql(u8, bytes, []u8{ 0x34, 0x12, 0x00, 0x00 }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
10
std/net.zig
10
std/net.zig
|
|
@ -34,7 +34,7 @@ const Connection = struct {
|
||||||
const recv_ret = linux.recvfrom(c.socket_fd, buf.ptr, buf.len, 0, null, null);
|
const recv_ret = linux.recvfrom(c.socket_fd, buf.ptr, buf.len, 0, null, null);
|
||||||
const recv_err = linux.getErrno(recv_ret);
|
const recv_err = linux.getErrno(recv_ret);
|
||||||
switch (recv_err) {
|
switch (recv_err) {
|
||||||
0 => return buf[0...recv_ret],
|
0 => return buf[0..recv_ret],
|
||||||
errno.EINVAL => unreachable,
|
errno.EINVAL => unreachable,
|
||||||
errno.EFAULT => unreachable,
|
errno.EFAULT => unreachable,
|
||||||
errno.ENOTSOCK => return error.NotSocket,
|
errno.ENOTSOCK => return error.NotSocket,
|
||||||
|
|
@ -81,7 +81,7 @@ pub fn lookup(hostname: []const u8, out_addrs: []Address) -> %[]Address {
|
||||||
//switch (parseIpLiteral(hostname)) {
|
//switch (parseIpLiteral(hostname)) {
|
||||||
// Ok => |addr| {
|
// Ok => |addr| {
|
||||||
// out_addrs[0] = addr;
|
// out_addrs[0] = addr;
|
||||||
// return out_addrs[0...1];
|
// return out_addrs[0..1];
|
||||||
// },
|
// },
|
||||||
// else => {},
|
// else => {},
|
||||||
//};
|
//};
|
||||||
|
|
@ -134,7 +134,7 @@ pub fn connectAddr(addr: &Address, port: u16) -> %Connection {
|
||||||
|
|
||||||
pub fn connect(hostname: []const u8, port: u16) -> %Connection {
|
pub fn connect(hostname: []const u8, port: u16) -> %Connection {
|
||||||
var addrs_buf: [1]Address = undefined;
|
var addrs_buf: [1]Address = undefined;
|
||||||
const addrs_slice = %return lookup(hostname, addrs_buf[0...]);
|
const addrs_slice = %return lookup(hostname, addrs_buf[0..]);
|
||||||
const main_addr = &addrs_slice[0];
|
const main_addr = &addrs_slice[0];
|
||||||
|
|
||||||
return connectAddr(main_addr, port);
|
return connectAddr(main_addr, port);
|
||||||
|
|
@ -186,7 +186,7 @@ fn parseIp6(buf: []const u8) -> %Address {
|
||||||
var result: Address = undefined;
|
var result: Address = undefined;
|
||||||
result.family = linux.AF_INET6;
|
result.family = linux.AF_INET6;
|
||||||
result.scope_id = 0;
|
result.scope_id = 0;
|
||||||
const ip_slice = result.addr[0...];
|
const ip_slice = result.addr[0..];
|
||||||
|
|
||||||
var x: u16 = 0;
|
var x: u16 = 0;
|
||||||
var saw_any_digits = false;
|
var saw_any_digits = false;
|
||||||
|
|
@ -280,7 +280,7 @@ fn parseIp6(buf: []const u8) -> %Address {
|
||||||
|
|
||||||
fn parseIp4(buf: []const u8) -> %u32 {
|
fn parseIp4(buf: []const u8) -> %u32 {
|
||||||
var result: u32 = undefined;
|
var result: u32 = undefined;
|
||||||
const out_ptr = ([]u8)((&result)[0...1]);
|
const out_ptr = ([]u8)((&result)[0..1]);
|
||||||
|
|
||||||
var x: u8 = 0;
|
var x: u8 = 0;
|
||||||
var index: u8 = 0;
|
var index: u8 = 0;
|
||||||
|
|
|
||||||
|
|
@ -225,7 +225,7 @@ fn forkChildErrReport(fd: i32, err: error) -> noreturn {
|
||||||
const ErrInt = @IntType(false, @sizeOf(error) * 8);
|
const ErrInt = @IntType(false, @sizeOf(error) * 8);
|
||||||
fn writeIntFd(fd: i32, value: ErrInt) -> %void {
|
fn writeIntFd(fd: i32, value: ErrInt) -> %void {
|
||||||
var bytes: [@sizeOf(ErrInt)]u8 = undefined;
|
var bytes: [@sizeOf(ErrInt)]u8 = undefined;
|
||||||
mem.writeInt(bytes[0...], value, true);
|
mem.writeInt(bytes[0..], value, true);
|
||||||
|
|
||||||
var index: usize = 0;
|
var index: usize = 0;
|
||||||
while (index < bytes.len) {
|
while (index < bytes.len) {
|
||||||
|
|
@ -259,6 +259,6 @@ fn readIntFd(fd: i32) -> %ErrInt {
|
||||||
index += amt_written;
|
index += amt_written;
|
||||||
}
|
}
|
||||||
|
|
||||||
return mem.readInt(bytes[0...], ErrInt, true);
|
return mem.readInt(bytes[0..], ErrInt, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -172,7 +172,7 @@ pub fn posixOpen(file_path: []const u8, flags: usize, perm: usize, allocator: ?&
|
||||||
var need_free = false;
|
var need_free = false;
|
||||||
|
|
||||||
if (file_path.len < stack_buf.len) {
|
if (file_path.len < stack_buf.len) {
|
||||||
path0 = stack_buf[0...file_path.len + 1];
|
path0 = stack_buf[0..file_path.len + 1];
|
||||||
} else if (allocator) |a| {
|
} else if (allocator) |a| {
|
||||||
path0 = %return a.alloc(u8, file_path.len + 1);
|
path0 = %return a.alloc(u8, file_path.len + 1);
|
||||||
need_free = true;
|
need_free = true;
|
||||||
|
|
@ -311,7 +311,7 @@ pub fn posixExecve(exe_path: []const u8, argv: []const []const u8, env_map: &con
|
||||||
while (it.next()) |search_path| {
|
while (it.next()) |search_path| {
|
||||||
mem.copy(u8, path_buf, search_path);
|
mem.copy(u8, path_buf, search_path);
|
||||||
path_buf[search_path.len] = '/';
|
path_buf[search_path.len] = '/';
|
||||||
mem.copy(u8, path_buf[search_path.len + 1 ...], exe_path);
|
mem.copy(u8, path_buf[search_path.len + 1 ..], exe_path);
|
||||||
path_buf[search_path.len + exe_path.len + 1] = 0;
|
path_buf[search_path.len + exe_path.len + 1] = 0;
|
||||||
err = posix.getErrno(posix.execve(path_buf.ptr, argv_buf.ptr, envp_buf.ptr));
|
err = posix.getErrno(posix.execve(path_buf.ptr, argv_buf.ptr, envp_buf.ptr));
|
||||||
assert(err > 0);
|
assert(err > 0);
|
||||||
|
|
@ -352,11 +352,11 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap {
|
||||||
for (environ_raw) |ptr| {
|
for (environ_raw) |ptr| {
|
||||||
var line_i: usize = 0;
|
var line_i: usize = 0;
|
||||||
while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {}
|
while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {}
|
||||||
const key = ptr[0...line_i];
|
const key = ptr[0..line_i];
|
||||||
|
|
||||||
var end_i: usize = line_i;
|
var end_i: usize = line_i;
|
||||||
while (ptr[end_i] != 0) : (end_i += 1) {}
|
while (ptr[end_i] != 0) : (end_i += 1) {}
|
||||||
const value = ptr[line_i + 1...end_i];
|
const value = ptr[line_i + 1..end_i];
|
||||||
|
|
||||||
%return result.set(key, value);
|
%return result.set(key, value);
|
||||||
}
|
}
|
||||||
|
|
@ -367,13 +367,13 @@ pub fn getEnv(key: []const u8) -> ?[]const u8 {
|
||||||
for (environ_raw) |ptr| {
|
for (environ_raw) |ptr| {
|
||||||
var line_i: usize = 0;
|
var line_i: usize = 0;
|
||||||
while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {}
|
while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {}
|
||||||
const this_key = ptr[0...line_i];
|
const this_key = ptr[0..line_i];
|
||||||
if (!mem.eql(u8, key, this_key))
|
if (!mem.eql(u8, key, this_key))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var end_i: usize = line_i;
|
var end_i: usize = line_i;
|
||||||
while (ptr[end_i] != 0) : (end_i += 1) {}
|
while (ptr[end_i] != 0) : (end_i += 1) {}
|
||||||
const this_value = ptr[line_i + 1...end_i];
|
const this_value = ptr[line_i + 1..end_i];
|
||||||
|
|
||||||
return this_value;
|
return this_value;
|
||||||
}
|
}
|
||||||
|
|
@ -417,7 +417,7 @@ pub fn symLink(allocator: &Allocator, existing_path: []const u8, new_path: []con
|
||||||
mem.copy(u8, existing_buf, existing_path);
|
mem.copy(u8, existing_buf, existing_path);
|
||||||
existing_buf[existing_path.len] = 0;
|
existing_buf[existing_path.len] = 0;
|
||||||
|
|
||||||
const new_buf = full_buf[existing_path.len + 1...];
|
const new_buf = full_buf[existing_path.len + 1..];
|
||||||
mem.copy(u8, new_buf, new_path);
|
mem.copy(u8, new_buf, new_path);
|
||||||
new_buf[new_path.len] = 0;
|
new_buf[new_path.len] = 0;
|
||||||
|
|
||||||
|
|
@ -456,10 +456,10 @@ pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path:
|
||||||
var rand_buf: [12]u8 = undefined;
|
var rand_buf: [12]u8 = undefined;
|
||||||
const tmp_path = %return allocator.alloc(u8, new_path.len + base64.calcEncodedSize(rand_buf.len));
|
const tmp_path = %return allocator.alloc(u8, new_path.len + base64.calcEncodedSize(rand_buf.len));
|
||||||
defer allocator.free(tmp_path);
|
defer allocator.free(tmp_path);
|
||||||
mem.copy(u8, tmp_path[0...], new_path);
|
mem.copy(u8, tmp_path[0..], new_path);
|
||||||
while (true) {
|
while (true) {
|
||||||
%return getRandomBytes(rand_buf[0...]);
|
%return getRandomBytes(rand_buf[0..]);
|
||||||
_ = base64.encodeWithAlphabet(tmp_path[new_path.len...], rand_buf, b64_fs_alphabet);
|
_ = base64.encodeWithAlphabet(tmp_path[new_path.len..], rand_buf, b64_fs_alphabet);
|
||||||
if (symLink(allocator, existing_path, tmp_path)) {
|
if (symLink(allocator, existing_path, tmp_path)) {
|
||||||
return rename(allocator, tmp_path, new_path);
|
return rename(allocator, tmp_path, new_path);
|
||||||
} else |err| {
|
} else |err| {
|
||||||
|
|
@ -510,9 +510,9 @@ pub fn copyFileMode(allocator: &Allocator, source_path: []const u8, dest_path: [
|
||||||
var rand_buf: [12]u8 = undefined;
|
var rand_buf: [12]u8 = undefined;
|
||||||
const tmp_path = %return allocator.alloc(u8, dest_path.len + base64.calcEncodedSize(rand_buf.len));
|
const tmp_path = %return allocator.alloc(u8, dest_path.len + base64.calcEncodedSize(rand_buf.len));
|
||||||
defer allocator.free(tmp_path);
|
defer allocator.free(tmp_path);
|
||||||
mem.copy(u8, tmp_path[0...], dest_path);
|
mem.copy(u8, tmp_path[0..], dest_path);
|
||||||
%return getRandomBytes(rand_buf[0...]);
|
%return getRandomBytes(rand_buf[0..]);
|
||||||
_ = base64.encodeWithAlphabet(tmp_path[dest_path.len...], rand_buf, b64_fs_alphabet);
|
_ = base64.encodeWithAlphabet(tmp_path[dest_path.len..], rand_buf, b64_fs_alphabet);
|
||||||
|
|
||||||
var out_stream = %return io.OutStream.openMode(tmp_path, mode, allocator);
|
var out_stream = %return io.OutStream.openMode(tmp_path, mode, allocator);
|
||||||
defer out_stream.close();
|
defer out_stream.close();
|
||||||
|
|
@ -521,7 +521,7 @@ pub fn copyFileMode(allocator: &Allocator, source_path: []const u8, dest_path: [
|
||||||
var in_stream = %return io.InStream.open(source_path, allocator);
|
var in_stream = %return io.InStream.open(source_path, allocator);
|
||||||
defer in_stream.close();
|
defer in_stream.close();
|
||||||
|
|
||||||
const buf = out_stream.buffer[0...];
|
const buf = out_stream.buffer[0..];
|
||||||
while (true) {
|
while (true) {
|
||||||
const amt = %return in_stream.read(buf);
|
const amt = %return in_stream.read(buf);
|
||||||
out_stream.index = amt;
|
out_stream.index = amt;
|
||||||
|
|
@ -539,7 +539,7 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8)
|
||||||
mem.copy(u8, old_buf, old_path);
|
mem.copy(u8, old_buf, old_path);
|
||||||
old_buf[old_path.len] = 0;
|
old_buf[old_path.len] = 0;
|
||||||
|
|
||||||
const new_buf = full_buf[old_path.len + 1...];
|
const new_buf = full_buf[old_path.len + 1..];
|
||||||
mem.copy(u8, new_buf, new_path);
|
mem.copy(u8, new_buf, new_path);
|
||||||
new_buf[new_path.len] = 0;
|
new_buf[new_path.len] = 0;
|
||||||
|
|
||||||
|
|
@ -601,7 +601,7 @@ pub fn makePath(allocator: &Allocator, full_path: []const u8) -> %void {
|
||||||
|
|
||||||
var end_index: usize = resolved_path.len;
|
var end_index: usize = resolved_path.len;
|
||||||
while (true) {
|
while (true) {
|
||||||
makeDir(allocator, resolved_path[0...end_index]) %% |err| {
|
makeDir(allocator, resolved_path[0..end_index]) %% |err| {
|
||||||
if (err == error.PathAlreadyExists) {
|
if (err == error.PathAlreadyExists) {
|
||||||
// TODO stat the file and return an error if it's not a directory
|
// TODO stat the file and return an error if it's not a directory
|
||||||
// this is important because otherwise a dangling symlink
|
// this is important because otherwise a dangling symlink
|
||||||
|
|
@ -691,7 +691,7 @@ start_over:
|
||||||
const full_entry_path = full_entry_buf.toSlice();
|
const full_entry_path = full_entry_buf.toSlice();
|
||||||
mem.copy(u8, full_entry_path, full_path);
|
mem.copy(u8, full_entry_path, full_path);
|
||||||
full_entry_path[full_path.len] = '/';
|
full_entry_path[full_path.len] = '/';
|
||||||
mem.copy(u8, full_entry_path[full_path.len + 1...], entry.name);
|
mem.copy(u8, full_entry_path[full_path.len + 1..], entry.name);
|
||||||
|
|
||||||
%return deleteTree(allocator, full_entry_path);
|
%return deleteTree(allocator, full_entry_path);
|
||||||
}
|
}
|
||||||
|
|
@ -856,6 +856,6 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
|
||||||
result_buf = %return allocator.realloc(u8, result_buf, result_buf.len * 2);
|
result_buf = %return allocator.realloc(u8, result_buf, result_buf.len * 2);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
return result_buf[0...ret_val];
|
return result_buf[0..ret_val];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ pub fn join(allocator: &Allocator, paths: ...) -> %[]u8 {
|
||||||
inline while (true) {
|
inline while (true) {
|
||||||
const arg = ([]const u8)(paths[path_i]);
|
const arg = ([]const u8)(paths[path_i]);
|
||||||
path_i += 1;
|
path_i += 1;
|
||||||
mem.copy(u8, buf[buf_index...], arg);
|
mem.copy(u8, buf[buf_index..], arg);
|
||||||
buf_index += arg.len;
|
buf_index += arg.len;
|
||||||
if (path_i >= paths.len) break;
|
if (path_i >= paths.len) break;
|
||||||
if (buf[buf_index - 1] != sep) {
|
if (buf[buf_index - 1] != sep) {
|
||||||
|
|
@ -48,7 +48,7 @@ pub fn join(allocator: &Allocator, paths: ...) -> %[]u8 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf[0...buf_index];
|
return buf[0..buf_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
test "os.path.join" {
|
test "os.path.join" {
|
||||||
|
|
@ -110,7 +110,7 @@ pub fn resolveSlice(allocator: &Allocator, paths: []const []const u8) -> %[]u8 {
|
||||||
}
|
}
|
||||||
%defer allocator.free(result);
|
%defer allocator.free(result);
|
||||||
|
|
||||||
for (paths[first_index...]) |p, i| {
|
for (paths[first_index..]) |p, i| {
|
||||||
var it = mem.split(p, '/');
|
var it = mem.split(p, '/');
|
||||||
while (it.next()) |component| {
|
while (it.next()) |component| {
|
||||||
if (mem.eql(u8, component, ".")) {
|
if (mem.eql(u8, component, ".")) {
|
||||||
|
|
@ -126,7 +126,7 @@ pub fn resolveSlice(allocator: &Allocator, paths: []const []const u8) -> %[]u8 {
|
||||||
} else {
|
} else {
|
||||||
result[result_index] = '/';
|
result[result_index] = '/';
|
||||||
result_index += 1;
|
result_index += 1;
|
||||||
mem.copy(u8, result[result_index...], component);
|
mem.copy(u8, result[result_index..], component);
|
||||||
result_index += component.len;
|
result_index += component.len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -137,7 +137,7 @@ pub fn resolveSlice(allocator: &Allocator, paths: []const []const u8) -> %[]u8 {
|
||||||
result_index += 1;
|
result_index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result[0...result_index];
|
return result[0..result_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
test "os.path.resolve" {
|
test "os.path.resolve" {
|
||||||
|
|
@ -153,24 +153,24 @@ fn testResolve(args: ...) -> []u8 {
|
||||||
|
|
||||||
pub fn dirname(path: []const u8) -> []const u8 {
|
pub fn dirname(path: []const u8) -> []const u8 {
|
||||||
if (path.len == 0)
|
if (path.len == 0)
|
||||||
return path[0...0];
|
return path[0..0];
|
||||||
var end_index: usize = path.len - 1;
|
var end_index: usize = path.len - 1;
|
||||||
while (path[end_index] == '/') {
|
while (path[end_index] == '/') {
|
||||||
if (end_index == 0)
|
if (end_index == 0)
|
||||||
return path[0...1];
|
return path[0..1];
|
||||||
end_index -= 1;
|
end_index -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (path[end_index] != '/') {
|
while (path[end_index] != '/') {
|
||||||
if (end_index == 0)
|
if (end_index == 0)
|
||||||
return path[0...0];
|
return path[0..0];
|
||||||
end_index -= 1;
|
end_index -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (end_index == 0 and path[end_index] == '/')
|
if (end_index == 0 and path[end_index] == '/')
|
||||||
return path[0...1];
|
return path[0..1];
|
||||||
|
|
||||||
return path[0...end_index];
|
return path[0..end_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
test "os.path.dirname" {
|
test "os.path.dirname" {
|
||||||
|
|
@ -202,11 +202,11 @@ pub fn basename(path: []const u8) -> []const u8 {
|
||||||
end_index += 1;
|
end_index += 1;
|
||||||
while (path[start_index] != '/') {
|
while (path[start_index] != '/') {
|
||||||
if (start_index == 0)
|
if (start_index == 0)
|
||||||
return path[0...end_index];
|
return path[0..end_index];
|
||||||
start_index -= 1;
|
start_index -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return path[start_index + 1...end_index];
|
return path[start_index + 1..end_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
test "os.path.basename" {
|
test "os.path.basename" {
|
||||||
|
|
@ -265,10 +265,10 @@ pub fn relative(allocator: &Allocator, from: []const u8, to: []const u8) -> %[]u
|
||||||
}
|
}
|
||||||
if (to_rest.len == 0) {
|
if (to_rest.len == 0) {
|
||||||
// shave off the trailing slash
|
// shave off the trailing slash
|
||||||
return result[0...result_index - 1];
|
return result[0..result_index - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
mem.copy(u8, result[result_index...], to_rest);
|
mem.copy(u8, result[result_index..], to_rest);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -303,7 +303,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
|
||||||
defer os.posixClose(fd);
|
defer os.posixClose(fd);
|
||||||
|
|
||||||
var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined;
|
var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined;
|
||||||
const proc_path = fmt.bufPrint(buf[0...], "/proc/self/fd/{}", fd);
|
const proc_path = fmt.bufPrint(buf[0..], "/proc/self/fd/{}", fd);
|
||||||
|
|
||||||
return os.readLink(allocator, proc_path);
|
return os.readLink(allocator, proc_path);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ pub const Rand = struct {
|
||||||
return (r.rng.get() & 0b1) == 0;
|
return (r.rng.get() & 0b1) == 0;
|
||||||
} else {
|
} else {
|
||||||
var result: [@sizeOf(T)]u8 = undefined;
|
var result: [@sizeOf(T)]u8 = undefined;
|
||||||
r.fillBytes(result[0...]);
|
r.fillBytes(result[0..]);
|
||||||
return mem.readInt(result, T, false);
|
return mem.readInt(result, T, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -48,12 +48,12 @@ pub const Rand = struct {
|
||||||
pub fn fillBytes(r: &Rand, buf: []u8) {
|
pub fn fillBytes(r: &Rand, buf: []u8) {
|
||||||
var bytes_left = buf.len;
|
var bytes_left = buf.len;
|
||||||
while (bytes_left >= @sizeOf(usize)) {
|
while (bytes_left >= @sizeOf(usize)) {
|
||||||
mem.writeInt(buf[buf.len - bytes_left...], r.rng.get(), false);
|
mem.writeInt(buf[buf.len - bytes_left..], r.rng.get(), false);
|
||||||
bytes_left -= @sizeOf(usize);
|
bytes_left -= @sizeOf(usize);
|
||||||
}
|
}
|
||||||
if (bytes_left > 0) {
|
if (bytes_left > 0) {
|
||||||
var rand_val_array: [@sizeOf(usize)]u8 = undefined;
|
var rand_val_array: [@sizeOf(usize)]u8 = undefined;
|
||||||
mem.writeInt(rand_val_array[0...], r.rng.get(), false);
|
mem.writeInt(rand_val_array[0..], r.rng.get(), false);
|
||||||
while (bytes_left > 0) {
|
while (bytes_left > 0) {
|
||||||
buf[buf.len - bytes_left] = rand_val_array[@sizeOf(usize) - bytes_left];
|
buf[buf.len - bytes_left] = rand_val_array[@sizeOf(usize) - bytes_left];
|
||||||
bytes_left -= 1;
|
bytes_left -= 1;
|
||||||
|
|
@ -71,7 +71,7 @@ pub const Rand = struct {
|
||||||
var rand_val_array: [@sizeOf(T)]u8 = undefined;
|
var rand_val_array: [@sizeOf(T)]u8 = undefined;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
r.fillBytes(rand_val_array[0...]);
|
r.fillBytes(rand_val_array[0..]);
|
||||||
const rand_val = mem.readInt(rand_val_array, T, false);
|
const rand_val = mem.readInt(rand_val_array, T, false);
|
||||||
if (rand_val < upper_bound) {
|
if (rand_val < upper_bound) {
|
||||||
return start + (rand_val % range);
|
return start + (rand_val % range);
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ test "testSort" {
|
||||||
|
|
||||||
for (u8cases) |case| {
|
for (u8cases) |case| {
|
||||||
var buf: [8]u8 = undefined;
|
var buf: [8]u8 = undefined;
|
||||||
const slice = buf[0...case[0].len];
|
const slice = buf[0..case[0].len];
|
||||||
mem.copy(u8, slice, case[0]);
|
mem.copy(u8, slice, case[0]);
|
||||||
sort(u8, slice, u8asc);
|
sort(u8, slice, u8asc);
|
||||||
assert(mem.eql(u8, slice, case[1]));
|
assert(mem.eql(u8, slice, case[1]));
|
||||||
|
|
@ -87,7 +87,7 @@ test "testSort" {
|
||||||
|
|
||||||
for (i32cases) |case| {
|
for (i32cases) |case| {
|
||||||
var buf: [8]i32 = undefined;
|
var buf: [8]i32 = undefined;
|
||||||
const slice = buf[0...case[0].len];
|
const slice = buf[0..case[0].len];
|
||||||
mem.copy(i32, slice, case[0]);
|
mem.copy(i32, slice, case[0]);
|
||||||
sort(i32, slice, i32asc);
|
sort(i32, slice, i32asc);
|
||||||
assert(mem.eql(i32, slice, case[1]));
|
assert(mem.eql(i32, slice, case[1]));
|
||||||
|
|
@ -106,7 +106,7 @@ test "testSortDesc" {
|
||||||
|
|
||||||
for (rev_cases) |case| {
|
for (rev_cases) |case| {
|
||||||
var buf: [8]i32 = undefined;
|
var buf: [8]i32 = undefined;
|
||||||
const slice = buf[0...case[0].len];
|
const slice = buf[0..case[0].len];
|
||||||
mem.copy(i32, slice, case[0]);
|
mem.copy(i32, slice, case[0]);
|
||||||
sort(i32, slice, i32desc);
|
sort(i32, slice, i32desc);
|
||||||
assert(mem.eql(i32, slice, case[1]));
|
assert(mem.eql(i32, slice, case[1]));
|
||||||
|
|
|
||||||
|
|
@ -39,11 +39,11 @@ fn callMainAndExit() -> noreturn {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void {
|
fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void {
|
||||||
std.os.args.raw = argv[0...argc];
|
std.os.args.raw = argv[0..argc];
|
||||||
|
|
||||||
var env_count: usize = 0;
|
var env_count: usize = 0;
|
||||||
while (envp[env_count] != null) : (env_count += 1) {}
|
while (envp[env_count] != null) : (env_count += 1) {}
|
||||||
std.os.environ_raw = @ptrCast(&&u8, envp)[0...env_count];
|
std.os.environ_raw = @ptrCast(&&u8, envp)[0..env_count];
|
||||||
|
|
||||||
std.debug.user_main_fn = root.main;
|
std.debug.user_main_fn = root.main;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,14 +58,14 @@ pub fn main() -> %void {
|
||||||
while (arg_i < os.args.count()) : (arg_i += 1) {
|
while (arg_i < os.args.count()) : (arg_i += 1) {
|
||||||
const arg = os.args.at(arg_i);
|
const arg = os.args.at(arg_i);
|
||||||
if (mem.startsWith(u8, arg, "-D")) {
|
if (mem.startsWith(u8, arg, "-D")) {
|
||||||
const option_contents = arg[2...];
|
const option_contents = arg[2..];
|
||||||
if (option_contents.len == 0) {
|
if (option_contents.len == 0) {
|
||||||
%%io.stderr.printf("Expected option name after '-D'\n\n");
|
%%io.stderr.printf("Expected option name after '-D'\n\n");
|
||||||
return usage(&builder, false, &io.stderr);
|
return usage(&builder, false, &io.stderr);
|
||||||
}
|
}
|
||||||
if (mem.indexOfScalar(u8, option_contents, '=')) |name_end| {
|
if (mem.indexOfScalar(u8, option_contents, '=')) |name_end| {
|
||||||
const option_name = option_contents[0...name_end];
|
const option_name = option_contents[0..name_end];
|
||||||
const option_value = option_contents[name_end + 1...];
|
const option_value = option_contents[name_end + 1..];
|
||||||
if (builder.addUserInputOption(option_name, option_value))
|
if (builder.addUserInputOption(option_name, option_value))
|
||||||
return usage(&builder, false, &io.stderr);
|
return usage(&builder, false, &io.stderr);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,10 @@ export coldcc fn __zig_panic(message_ptr: &const u8, message_len: usize) -> nore
|
||||||
@setDebugSafety(this, false);
|
@setDebugSafety(this, false);
|
||||||
|
|
||||||
if (builtin.__zig_panic_implementation_provided) {
|
if (builtin.__zig_panic_implementation_provided) {
|
||||||
@import("@root").panic(message_ptr[0...message_len]);
|
@import("@root").panic(message_ptr[0..message_len]);
|
||||||
} else if (builtin.os == builtin.Os.freestanding) {
|
} else if (builtin.os == builtin.Os.freestanding) {
|
||||||
while (true) {}
|
while (true) {}
|
||||||
} else {
|
} else {
|
||||||
@import("std").debug.panic("{}", message_ptr[0...message_len]);
|
@import("std").debug.panic("{}", message_ptr[0..message_len]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ const Str = struct {
|
||||||
a: []Sub,
|
a: []Sub,
|
||||||
};
|
};
|
||||||
test "setGlobalVarArrayViaSliceEmbeddedInStruct" {
|
test "setGlobalVarArrayViaSliceEmbeddedInStruct" {
|
||||||
var s = Str { .a = s_array[0...]};
|
var s = Str { .a = s_array[0..]};
|
||||||
|
|
||||||
s.a[0].b = 1;
|
s.a[0].b = 1;
|
||||||
s.a[1].b = 2;
|
s.a[1].b = 2;
|
||||||
|
|
|
||||||
|
|
@ -148,7 +148,7 @@ fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) -> []const u8 {
|
||||||
return []const u8 {};
|
return []const u8 {};
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice[0...1];
|
return slice[0..1];
|
||||||
}
|
}
|
||||||
|
|
||||||
test "implicitly cast from [N]T to ?[]const T" {
|
test "implicitly cast from [N]T to ?[]const T" {
|
||||||
|
|
@ -177,13 +177,13 @@ fn gimmeErrOrSlice() -> %[]u8 {
|
||||||
test "peer type resolution: [0]u8, []const u8, and %[]u8" {
|
test "peer type resolution: [0]u8, []const u8, and %[]u8" {
|
||||||
{
|
{
|
||||||
var data = "hi";
|
var data = "hi";
|
||||||
const slice = data[0...];
|
const slice = data[0..];
|
||||||
assert((%%peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
|
assert((%%peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
|
||||||
assert((%%peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
|
assert((%%peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
|
||||||
}
|
}
|
||||||
comptime {
|
comptime {
|
||||||
var data = "hi";
|
var data = "hi";
|
||||||
const slice = data[0...];
|
const slice = data[0..];
|
||||||
assert((%%peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
|
assert((%%peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0);
|
||||||
assert((%%peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
|
assert((%%peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1);
|
||||||
}
|
}
|
||||||
|
|
@ -193,7 +193,7 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) -> %[]u8 {
|
||||||
return []u8{};
|
return []u8{};
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice[0...1];
|
return slice[0..1];
|
||||||
}
|
}
|
||||||
|
|
||||||
test "resolve undefined with integer" {
|
test "resolve undefined with integer" {
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ fn bar(argc: usize) {
|
||||||
const args = %%debug.global_allocator.alloc([]const u8, argc);
|
const args = %%debug.global_allocator.alloc([]const u8, argc);
|
||||||
for (args) |_, i| {
|
for (args) |_, i| {
|
||||||
const ptr = argv[i];
|
const ptr = argv[i];
|
||||||
args[i] = ptr[0...strlen(ptr)];
|
args[i] = ptr[0..strlen(ptr)];
|
||||||
}
|
}
|
||||||
foo(args);
|
foo(args);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,9 @@ test "enumWithMembers" {
|
||||||
const b = ET.UINT { 42 };
|
const b = ET.UINT { 42 };
|
||||||
var buf: [20]u8 = undefined;
|
var buf: [20]u8 = undefined;
|
||||||
|
|
||||||
assert(%%a.print(buf[0...]) == 3);
|
assert(%%a.print(buf[0..]) == 3);
|
||||||
assert(mem.eql(u8, buf[0...3], "-42"));
|
assert(mem.eql(u8, buf[0..3], "-42"));
|
||||||
|
|
||||||
assert(%%b.print(buf[0...]) == 2);
|
assert(%%b.print(buf[0..]) == 2);
|
||||||
assert(mem.eql(u8, buf[0...2], "42"));
|
assert(mem.eql(u8, buf[0..2], "42"));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -145,7 +145,7 @@ test "constSlice" {
|
||||||
comptime {
|
comptime {
|
||||||
const a = "1234567890";
|
const a = "1234567890";
|
||||||
assert(a.len == 10);
|
assert(a.len == 10);
|
||||||
const b = a[1...2];
|
const b = a[1..2];
|
||||||
assert(b.len == 1);
|
assert(b.len == 1);
|
||||||
assert(b[0] == '2');
|
assert(b[0] == '2');
|
||||||
}
|
}
|
||||||
|
|
@ -255,7 +255,7 @@ test "callMethodOnBoundFnReferringToVarInstance" {
|
||||||
test "ptrToLocalArrayArgumentAtComptime" {
|
test "ptrToLocalArrayArgumentAtComptime" {
|
||||||
comptime {
|
comptime {
|
||||||
var bytes: [10]u8 = undefined;
|
var bytes: [10]u8 = undefined;
|
||||||
modifySomeBytes(bytes[0...]);
|
modifySomeBytes(bytes[0..]);
|
||||||
assert(bytes[0] == 'a');
|
assert(bytes[0] == 'a');
|
||||||
assert(bytes[9] == 'b');
|
assert(bytes[9] == 'b');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,8 @@ test "continueInForLoop" {
|
||||||
test "forLoopWithPointerElemVar" {
|
test "forLoopWithPointerElemVar" {
|
||||||
const source = "abcdefg";
|
const source = "abcdefg";
|
||||||
var target: [source.len]u8 = undefined;
|
var target: [source.len]u8 = undefined;
|
||||||
mem.copy(u8, target[0...], source);
|
mem.copy(u8, target[0..], source);
|
||||||
mangleString(target[0...]);
|
mangleString(target[0..]);
|
||||||
assert(mem.eql(u8, target, "bcdefgh"));
|
assert(mem.eql(u8, target, "bcdefgh"));
|
||||||
}
|
}
|
||||||
fn mangleString(s: []u8) {
|
fn mangleString(s: []u8) {
|
||||||
|
|
@ -53,5 +53,5 @@ test "basicForLoop" {
|
||||||
buf_index += 1;
|
buf_index += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(mem.eql(u8, buffer[0...buf_index], expected_result));
|
assert(mem.eql(u8, buffer[0..buf_index], expected_result));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -173,14 +173,14 @@ test "slicing" {
|
||||||
|
|
||||||
array[5] = 1234;
|
array[5] = 1234;
|
||||||
|
|
||||||
var slice = array[5...10];
|
var slice = array[5..10];
|
||||||
|
|
||||||
if (slice.len != 5) unreachable;
|
if (slice.len != 5) unreachable;
|
||||||
|
|
||||||
const ptr = &slice[0];
|
const ptr = &slice[0];
|
||||||
if (ptr[0] != 1234) unreachable;
|
if (ptr[0] != 1234) unreachable;
|
||||||
|
|
||||||
var slice_rest = array[10...];
|
var slice_rest = array[10..];
|
||||||
if (slice_rest.len != 10) unreachable;
|
if (slice_rest.len != 10) unreachable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -260,7 +260,7 @@ test "generic malloc free" {
|
||||||
}
|
}
|
||||||
const some_mem : [100]u8 = undefined;
|
const some_mem : [100]u8 = undefined;
|
||||||
fn memAlloc(comptime T: type, n: usize) -> %[]T {
|
fn memAlloc(comptime T: type, n: usize) -> %[]T {
|
||||||
return @ptrCast(&T, &some_mem[0])[0...n];
|
return @ptrCast(&T, &some_mem[0])[0..n];
|
||||||
}
|
}
|
||||||
fn memFree(comptime T: type, memory: []T) { }
|
fn memFree(comptime T: type, memory: []T) { }
|
||||||
|
|
||||||
|
|
@ -396,7 +396,7 @@ test "C string concatenation" {
|
||||||
test "cast slice to u8 slice" {
|
test "cast slice to u8 slice" {
|
||||||
assert(@sizeOf(i32) == 4);
|
assert(@sizeOf(i32) == 4);
|
||||||
var big_thing_array = []i32{1, 2, 3, 4};
|
var big_thing_array = []i32{1, 2, 3, 4};
|
||||||
const big_thing_slice: []i32 = big_thing_array[0...];
|
const big_thing_slice: []i32 = big_thing_array[0..];
|
||||||
const bytes = ([]u8)(big_thing_slice);
|
const bytes = ([]u8)(big_thing_slice);
|
||||||
assert(bytes.len == 4 * 4);
|
assert(bytes.len == 4 * 4);
|
||||||
bytes[4] = 0;
|
bytes[4] = 0;
|
||||||
|
|
@ -509,9 +509,9 @@ test "volatile load and store" {
|
||||||
|
|
||||||
test "slice string literal has type []const u8" {
|
test "slice string literal has type []const u8" {
|
||||||
comptime {
|
comptime {
|
||||||
assert(@typeOf("aoeu"[0...]) == []const u8);
|
assert(@typeOf("aoeu"[0..]) == []const u8);
|
||||||
const array = []i32{1, 2, 3, 4};
|
const array = []i32{1, 2, 3, 4};
|
||||||
assert(@typeOf(array[0...]) == []const i32);
|
assert(@typeOf(array[0..]) == []const i32);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
const assert = @import("std").debug.assert;
|
const assert = @import("std").debug.assert;
|
||||||
|
|
||||||
const x = @intToPtr(&i32, 0x1000)[0...0x500];
|
const x = @intToPtr(&i32, 0x1000)[0..0x500];
|
||||||
const y = x[0x100...];
|
const y = x[0x100..];
|
||||||
test "compile time slice of pointer to hard coded address" {
|
test "compile time slice of pointer to hard coded address" {
|
||||||
assert(usize(x.ptr) == 0x1000);
|
assert(usize(x.ptr) == 0x1000);
|
||||||
assert(x.len == 0x500);
|
assert(x.len == 0x500);
|
||||||
|
|
|
||||||
|
|
@ -305,7 +305,7 @@ test "packedArray24Bits" {
|
||||||
|
|
||||||
var bytes = []u8{0} ** (@sizeOf(FooArray24Bits) + 1);
|
var bytes = []u8{0} ** (@sizeOf(FooArray24Bits) + 1);
|
||||||
bytes[bytes.len - 1] = 0xaa;
|
bytes[bytes.len - 1] = 0xaa;
|
||||||
const ptr = &([]FooArray24Bits)(bytes[0...bytes.len - 1])[0];
|
const ptr = &([]FooArray24Bits)(bytes[0..bytes.len - 1])[0];
|
||||||
assert(ptr.a == 0);
|
assert(ptr.a == 0);
|
||||||
assert(ptr.b[0].field == 0);
|
assert(ptr.b[0].field == 0);
|
||||||
assert(ptr.b[1].field == 0);
|
assert(ptr.b[1].field == 0);
|
||||||
|
|
@ -354,7 +354,7 @@ test "alignedArrayOfPackedStruct" {
|
||||||
}
|
}
|
||||||
|
|
||||||
var bytes = []u8{0xbb} ** @sizeOf(FooArrayOfAligned);
|
var bytes = []u8{0xbb} ** @sizeOf(FooArrayOfAligned);
|
||||||
const ptr = &([]FooArrayOfAligned)(bytes[0...bytes.len])[0];
|
const ptr = &([]FooArrayOfAligned)(bytes[0..bytes.len])[0];
|
||||||
|
|
||||||
assert(ptr.a[0].a == 0xbb);
|
assert(ptr.a[0].a == 0xbb);
|
||||||
assert(ptr.a[0].b == 0xbb);
|
assert(ptr.a[0].b == 0xbb);
|
||||||
|
|
|
||||||
|
|
@ -27,12 +27,12 @@ test "struct contains slice of itself" {
|
||||||
},
|
},
|
||||||
Node {
|
Node {
|
||||||
.payload = 3,
|
.payload = 3,
|
||||||
.children = other_nodes[0...],
|
.children = other_nodes[0..],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const root = Node {
|
const root = Node {
|
||||||
.payload = 1234,
|
.payload = 1234,
|
||||||
.children = nodes[0...],
|
.children = nodes[0..],
|
||||||
};
|
};
|
||||||
assert(root.payload == 1234);
|
assert(root.payload == 1234);
|
||||||
assert(root.children[0].payload == 1);
|
assert(root.children[0].payload == 1);
|
||||||
|
|
|
||||||
|
|
@ -1112,7 +1112,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
|
||||||
cases.add("bogus method call on slice",
|
cases.add("bogus method call on slice",
|
||||||
\\var self = "aoeu";
|
\\var self = "aoeu";
|
||||||
\\fn f(m: []const u8) {
|
\\fn f(m: []const u8) {
|
||||||
\\ m.copy(u8, self[0...], m);
|
\\ m.copy(u8, self[0..], m);
|
||||||
\\}
|
\\}
|
||||||
\\export fn entry() -> usize { @sizeOf(@typeOf(f)) }
|
\\export fn entry() -> usize { @sizeOf(@typeOf(f)) }
|
||||||
, ".tmp_source.zig:3:6: error: no member named 'copy' in '[]const u8'");
|
, ".tmp_source.zig:3:6: error: no member named 'copy' in '[]const u8'");
|
||||||
|
|
@ -1467,7 +1467,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
|
||||||
\\pub fn pass(in: []u8) -> []u8 {
|
\\pub fn pass(in: []u8) -> []u8 {
|
||||||
\\ var out = &s_buffer;
|
\\ var out = &s_buffer;
|
||||||
\\ *out[0] = in[0];
|
\\ *out[0] = in[0];
|
||||||
\\ return (*out)[0...1];
|
\\ return (*out)[0..1];
|
||||||
\\}
|
\\}
|
||||||
\\
|
\\
|
||||||
\\export fn entry() -> usize { @sizeOf(@typeOf(pass)) }
|
\\export fn entry() -> usize { @sizeOf(@typeOf(pass)) }
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue