Remove usage of do in core library

This commit is contained in:
gingerBill
2020-09-23 17:17:14 +01:00
parent 4844dd4d96
commit fc4fdd588e
45 changed files with 960 additions and 520 deletions

View File

@@ -92,7 +92,9 @@ _encode :: proc(out, data: []byte, ENC_TBL := ENC_TABLE, allocator := context.al
}
decode :: proc(data: string, DEC_TBL := DEC_TABLE, allocator := context.allocator) -> []byte #no_bounds_check{
if len(data) == 0 do return []byte{};
if len(data) == 0 {
return nil;
}
outi := 0;
olen := len(data);
@@ -113,7 +115,9 @@ decode :: proc(data: string, DEC_TBL := DEC_TABLE, allocator := context.allocato
data = data[1:];
if input == byte(PADDING) && j >= 2 && len(data) < 8 {
assert(!(len(data) + j < 8 - 1), "Corrupted input");
for k := 0; k < 8-1-j; k +=1 do assert(len(data) < k || data[k] == byte(PADDING), "Corrupted input");
for k := 0; k < 8-1-j; k +=1 {
assert(len(data) < k || data[k] == byte(PADDING), "Corrupted input");
}
dlen, end = j, true;
assert(dlen != 1 && dlen != 3 && dlen != 6, "Corrupted input");
break;

View File

@@ -2,46 +2,48 @@ package base64
// @note(zh): Encoding utility for Base64
// A secondary param can be used to supply a custom alphabet to
// @link(encode) and a matching decoding table to @link(decode).
// @link(encode) and a matching decoding table to @link(decode).
// If none is supplied it just uses the standard Base64 alphabet.
// Incase your specific version does not use padding, you may
// truncate it from the encoded output.
ENC_TABLE := [64]byte {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
PADDING :: '=';
DEC_TABLE := [128]int {
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59,
60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59,
60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, -1, -1, -1, -1, -1
};
encode :: proc(data: []byte, ENC_TBL := ENC_TABLE, allocator := context.allocator) -> string #no_bounds_check {
length := len(data);
if length == 0 do return "";
if length == 0 {
return "";
}
out_length := ((4 * length / 3) + 3) &~ 3;
out := make([]byte, out_length, allocator);
@@ -51,8 +53,8 @@ encode :: proc(data: []byte, ENC_TBL := ENC_TABLE, allocator := context.allocato
for i, d := 0, 0; i < length; i, d = i + 3, d + 4 {
c0, c1, c2 = int(data[i]), -1, -1;
if i + 1 < length do c1 = int(data[i + 1]);
if i + 2 < length do c2 = int(data[i + 2]);
if i + 1 < length { c1 = int(data[i + 1]); }
if i + 2 < length { c2 = int(data[i + 2]); }
block = (c0 << 16) | (max(c1, 0) << 8) | max(c2, 0);
@@ -66,7 +68,9 @@ encode :: proc(data: []byte, ENC_TBL := ENC_TABLE, allocator := context.allocato
decode :: proc(data: string, DEC_TBL := DEC_TABLE, allocator := context.allocator) -> []byte #no_bounds_check {
length := len(data);
if length == 0 do return []byte{};
if length == 0 {
return nil;
}
pad_count := data[length - 1] == PADDING ? (data[length - 2] == PADDING ? 2 : 1) : 0;
out_length := ((length * 6) >> 3) - pad_count;

View File

@@ -32,7 +32,9 @@ Parser :: struct {
print_value :: proc(value: Value, pretty := true, indent := 0) {
print_indent :: proc(indent: int) {
for _ in 0..<indent do fmt.print("\t");
for _ in 0..<indent {
fmt.print("\t");
}
}
switch v in value {
@@ -42,22 +44,22 @@ print_value :: proc(value: Value, pretty := true, indent := 0) {
case string: fmt.print(v);
case Array:
fmt.print("[");
if pretty do fmt.println();
if pretty { fmt.println(); }
for e, i in v {
if pretty {
print_indent(indent+1);
print_value(e, pretty, indent+1);
fmt.println(",");
} else {
if i > 0 do fmt.print(", ");
if i > 0 { fmt.print(", "); }
print_value(e);
}
}
if pretty do print_indent(indent);
if pretty { print_indent(indent); }
fmt.print("]");
case Dict:
fmt.print("{");
if pretty do fmt.println();
if pretty { fmt.println(); }
i := 0;
for name, val in v {
@@ -67,14 +69,14 @@ print_value :: proc(value: Value, pretty := true, indent := 0) {
print_value(val, pretty, indent+1);
fmt.println(",");
} else {
if i > 0 do fmt.print(", ");
if i > 0 { fmt.print(", "); }
fmt.printf("%s = ", name);
print_value(val, pretty, indent+1);
i += 1;
}
}
if pretty do print_indent(indent);
if pretty { print_indent(indent); }
fmt.print("}");
case:
fmt.print("nil");
@@ -149,17 +151,23 @@ destroy :: proc(p: ^Parser) {
destroy_value :: proc(value: Value) {
#partial switch v in value {
case Array:
for elem in v do destroy_value(elem);
for elem in v {
destroy_value(elem);
}
delete(v);
case Dict:
for _, dv in v do destroy_value(dv);
for _, dv in v {
destroy_value(dv);
}
delete(v);
}
}
delete(p.tokens);
for s in p.allocated_strings do delete(s);
for s in p.allocated_strings {
delete(s);
}
delete(p.allocated_strings);
delete(p.dict_stack);
@@ -348,7 +356,9 @@ expect_token :: proc(p: ^Parser, kind: Kind) -> Token {
prev := p.curr_token;
if prev.kind != kind {
got := prev.lit;
if got == "\n" do got = ";";
if got == "\n" {
got = ";";
}
error(p, prev.pos, "Expected %s, got %s", kind_to_string[kind], got);
}
next_token(p);
@@ -411,7 +421,7 @@ parse_operand :: proc(p: ^Parser) -> (Value, Pos) {
case .Ident:
next_token(p);
v, ok := lookup_value(p, tok.lit);
if !ok do error(p, tok.pos, "Undeclared identifier %s", tok.lit);
if !ok { error(p, tok.pos, "Undeclared identifier %s", tok.lit); }
return v, tok.pos;
case .True:
@@ -438,7 +448,7 @@ parse_operand :: proc(p: ^Parser) -> (Value, Pos) {
case .String:
next_token(p);
str, ok := unquote_string(p, tok);
if !ok do error(p, tok.pos, "Unable to unquote string");
if !ok { error(p, tok.pos, "Unable to unquote string"); }
return string(str), tok.pos;
case .Open_Paren:
@@ -480,7 +490,7 @@ parse_operand :: proc(p: ^Parser) -> (Value, Pos) {
}
name, ok := unquote_string(p, name_tok);
if !ok do error(p, tok.pos, "Unable to unquote string");
if !ok { error(p, tok.pos, "Unable to unquote string"); }
expect_token(p, .Assign);
elem, _ := parse_expr(p);
@@ -520,7 +530,7 @@ parse_atom_expr :: proc(p: ^Parser, operand: Value, pos: Pos) -> (Value, Pos) {
continue;
}
name, usok := unquote_string(p, tok);
if !usok do error(p, tok.pos, "Unable to unquote string");
if !usok { error(p, tok.pos, "Unable to unquote string"); }
val, found := d[name];
if !found {
error(p, tok.pos, "Field %s not found in dictionary", name);
@@ -594,7 +604,7 @@ parse_unary_expr :: proc(p: ^Parser) -> (Value, Pos) {
next_token(p);
tok := expect_token(p, .String);
v, ok := lookup_value(p, tok.lit);
if !ok do error(p, tok.pos, "Undeclared identifier %s", tok.lit);
if !ok { error(p, tok.pos, "Undeclared identifier %s", tok.lit); }
return parse_atom_expr(p, v, tok.pos);
case .Add, .Sub:
@@ -603,8 +613,8 @@ parse_unary_expr :: proc(p: ^Parser) -> (Value, Pos) {
expr, pos := parse_unary_expr(p);
#partial switch e in expr {
case i64: if op.kind == .Sub do return -e, pos;
case f64: if op.kind == .Sub do return -e, pos;
case i64: if op.kind == .Sub { return -e, pos; }
case f64: if op.kind == .Sub { return -e, pos; }
case:
error(p, op.pos, "Unary operator %s can only be used on integers or floats", op.lit);
return nil, op.pos;
@@ -678,7 +688,7 @@ calculate_binary_value :: proc(p: ^Parser, op: Kind, a, b: Value) -> (Value, boo
case bool:
b, ok := y.(bool);
if !ok do return nil, false;
if !ok { return nil, false; }
#partial switch op {
case .Eq: return a == b, true;
case .NotEq: return a != b, true;
@@ -688,7 +698,7 @@ calculate_binary_value :: proc(p: ^Parser, op: Kind, a, b: Value) -> (Value, boo
case i64:
b, ok := y.(i64);
if !ok do return nil, false;
if !ok { return nil, false; }
#partial switch op {
case .Add: return a + b, true;
case .Sub: return a - b, true;
@@ -705,7 +715,7 @@ calculate_binary_value :: proc(p: ^Parser, op: Kind, a, b: Value) -> (Value, boo
case f64:
b, ok := y.(f64);
if !ok do return nil, false;
if !ok { return nil, false; }
#partial switch op {
case .Add: return a + b, true;
@@ -722,7 +732,7 @@ calculate_binary_value :: proc(p: ^Parser, op: Kind, a, b: Value) -> (Value, boo
case string:
b, ok := y.(string);
if !ok do return nil, false;
if !ok { return nil, false; }
#partial switch op {
case .Add:
@@ -825,7 +835,7 @@ parse_assignment :: proc(p: ^Parser) -> bool {
if allow_token(p, .Ident) || allow_token(p, .String) {
expect_token(p, .Assign);
name, ok := unquote_string(p, tok);
if !ok do error(p, tok.pos, "Unable to unquote string");
if !ok { error(p, tok.pos, "Unable to unquote string"); }
expr, _ := parse_expr(p);
d := top_dict(p);
if _, ok2 := d[name]; ok2 {

View File

@@ -162,9 +162,9 @@ token_lookup :: proc(ident: string) -> Kind {
return Ident;
}
is_literal :: proc(tok: Kind) -> bool do return _literal_start < tok && tok < _literal_end;
is_operator :: proc(tok: Kind) -> bool do return _operator_start < tok && tok < _operator_end;
is_keyword :: proc(tok: Kind) -> bool do return _keyword_start < tok && tok < _keyword_end;
is_literal :: proc(tok: Kind) -> bool { return _literal_start < tok && tok < _literal_end; }
is_operator :: proc(tok: Kind) -> bool { return _operator_start < tok && tok < _operator_end; }
is_keyword :: proc(tok: Kind) -> bool { return _keyword_start < tok && tok < _keyword_end; }
tokenizer_init :: proc(t: ^Tokenizer, src: []byte, file := "") {

View File

@@ -161,7 +161,7 @@ marshal_arg :: proc(b: ^strings.Builder, v: any) -> Marshal_Error {
case Type_Info_Array:
write_byte(b, '[');
for i in 0..<info.count {
if i > 0 do write_string(b, ", ");
if i > 0 { write_string(b, ", "); }
data := uintptr(v.data) + uintptr(i*info.elem_size);
marshal_arg(b, any{rawptr(data), info.elem.id});
@@ -172,7 +172,7 @@ marshal_arg :: proc(b: ^strings.Builder, v: any) -> Marshal_Error {
write_byte(b, '[');
array := cast(^mem.Raw_Dynamic_Array)v.data;
for i in 0..<array.len {
if i > 0 do write_string(b, ", ");
if i > 0 { write_string(b, ", "); }
data := uintptr(array.data) + uintptr(i*info.elem_size);
marshal_arg(b, any{rawptr(data), info.elem.id});
@@ -183,7 +183,7 @@ marshal_arg :: proc(b: ^strings.Builder, v: any) -> Marshal_Error {
write_byte(b, '[');
slice := cast(^mem.Raw_Slice)v.data;
for i in 0..<slice.len {
if i > 0 do write_string(b, ", ");
if i > 0 { write_string(b, ", "); }
data := uintptr(slice.data) + uintptr(i*info.elem_size);
marshal_arg(b, any{rawptr(data), info.elem.id});
@@ -205,7 +205,7 @@ marshal_arg :: proc(b: ^strings.Builder, v: any) -> Marshal_Error {
entry_size := ed.elem_size;
for i in 0..<entries.len {
if i > 0 do write_string(b, ", ");
if i > 0 { write_string(b, ", "); }
data := uintptr(entries.data) + uintptr(i*entry_size);
header := cast(^Map_Entry_Header)data;
@@ -223,7 +223,7 @@ marshal_arg :: proc(b: ^strings.Builder, v: any) -> Marshal_Error {
case Type_Info_Struct:
write_byte(b, '{');
for name, i in info.names {
if i > 0 do write_string(b, ", ");
if i > 0 { write_string(b, ", "); }
write_quoted_string(b, name);
write_string(b, ": ");
@@ -271,7 +271,7 @@ marshal_arg :: proc(b: ^strings.Builder, v: any) -> Marshal_Error {
write_byte(b, '{');
for name, i in info.names {
if i > 0 do write_string(b, ", ");
if i > 0 { write_string(b, ", "); }
bits := u64(info.bits[i]);
offset := u64(info.offsets[i]);
@@ -317,15 +317,21 @@ marshal_arg :: proc(b: ^strings.Builder, v: any) -> Marshal_Error {
bit_data = u64(x);
case 16:
x := (^u16)(v.data)^;
if do_byte_swap do x = bits.byte_swap(x);
if do_byte_swap {
x = bits.byte_swap(x);
}
bit_data = u64(x);
case 32:
x := (^u32)(v.data)^;
if do_byte_swap do x = bits.byte_swap(x);
if do_byte_swap {
x = bits.byte_swap(x);
}
bit_data = u64(x);
case 64:
x := (^u64)(v.data)^;
if do_byte_swap do x = bits.byte_swap(x);
if do_byte_swap {
x = bits.byte_swap(x);
}
bit_data = u64(x);
case: panic("unknown bit_size size");
}

View File

@@ -183,9 +183,11 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) {
case "false": token.kind = .False;
case "true": token.kind = .True;
case:
if t.spec == .JSON5 do switch str {
case "Infinity": token.kind = .Infinity;
case "NaN": token.kind = .NaN;
if t.spec == .JSON5 {
switch str {
case "Infinity": token.kind = .Infinity;
case "NaN": token.kind = .NaN;
}
}
}
@@ -361,7 +363,9 @@ is_valid_number :: proc(str: string, spec: Specification) -> bool {
s = s[1:];
case '1'..'9':
s = s[1:];
for len(s) > 0 && '0' <= s[0] && s[0] <= '9' do s = s[1:];
for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
s = s[1:];
}
case '.':
if spec == .JSON5 { // Allow leading decimal point
s = s[1:];
@@ -380,7 +384,9 @@ is_valid_number :: proc(str: string, spec: Specification) -> bool {
if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' {
s = s[2:];
for len(s) > 0 && '0' <= s[0] && s[0] <= '9' do s = s[1:];
for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
s = s[1:];
}
}
if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') {
@@ -392,7 +398,9 @@ is_valid_number :: proc(str: string, spec: Specification) -> bool {
return false;
}
}
for len(s) > 0 && '0' <= s[0] && s[0] <= '9' do s = s[1:];
for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
s = s[1:];
}
}
// The string should be empty now to be valid

View File

@@ -65,7 +65,9 @@ destroy_value :: proc(value: Value) {
}
delete(v);
case Array:
for elem in v do destroy_value(elem);
for elem in v {
destroy_value(elem);
}
delete(v);
case String:
delete(v);