Reduce number of range and slice operators #239

Replace .. and ... with : and ..
This commit is contained in:
gingerBill
2018-08-01 21:34:59 +01:00
parent a6fe656f21
commit 0718f14774
31 changed files with 204 additions and 219 deletions
+9 -9
View File
@@ -20,29 +20,29 @@ decimal_to_string :: proc(buf: []byte, a: ^Decimal) -> string {
// TODO(bill): make this work with a buffer that's not big enough
assert(len(buf) >= n);
buf = buf[0..n];
buf = buf[0:n];
if a.count == 0 {
buf[0] = '0';
return string(buf[0..1]);
return string(buf[0:1]);
}
w := 0;
if a.decimal_point <= 0 {
buf[w] = '0'; w += 1;
buf[w] = '.'; w += 1;
w += digit_zero(buf[w .. w-a.decimal_point]);
w += copy(buf[w..], a.digits[0..a.count]);
w += digit_zero(buf[w : w-a.decimal_point]);
w += copy(buf[w:], a.digits[0:a.count]);
} else if a.decimal_point < a.count {
w += copy(buf[w..], a.digits[0..a.decimal_point]);
w += copy(buf[w:], a.digits[0:a.decimal_point]);
buf[w] = '.'; w += 1;
w += copy(buf[w..], a.digits[a.decimal_point .. a.count]);
w += copy(buf[w:], a.digits[a.decimal_point : a.count]);
} else {
w += copy(buf[w..], a.digits[0..a.count]);
w += digit_zero(buf[w .. w+a.decimal_point-a.count]);
w += copy(buf[w:], a.digits[0:a.count]);
w += digit_zero(buf[w : w+a.decimal_point-a.count]);
}
return string(buf[0..w]);
return string(buf[0:w]);
}
// trim trailing zeros
+59 -59
View File
@@ -45,7 +45,7 @@ string_buffer_from_slice :: proc(backing: []byte) -> String_Buffer {
to_string :: proc(buf: String_Buffer) -> string {
return string(buf[..]);
return string(buf[:]);
}
@@ -53,7 +53,7 @@ write_string :: proc(buf: ^String_Buffer, s: string) {
append_string(buf, s);
}
write_bytes :: proc(buf: ^String_Buffer, data: []byte) {
append(buf, ...data);
append(buf, ..data);
}
write_byte :: proc(buf: ^String_Buffer, data: byte) {
append(buf, data);
@@ -65,79 +65,79 @@ write_rune :: proc(buf: ^String_Buffer, r: rune) {
}
b, n := utf8.encode_rune(r);
write_bytes(buf, b[..n]);
write_bytes(buf, b[:n]);
}
write_i64 :: proc(buf: ^String_Buffer, i: i64, base: int) {
b: [129]byte;
s := strconv.append_bits(b[..], u64(i), base, true, 64, strconv.digits, 0);
s := strconv.append_bits(b[:], u64(i), base, true, 64, strconv.digits, 0);
write_string(buf, s);
}
fprint :: proc(fd: os.Handle, args: ...any) -> int {
fprint :: proc(fd: os.Handle, args: ..any) -> int {
data: [_BUFFER_SIZE]byte;
buf := string_buffer_from_slice(data[..]);
res := sbprint(&buf, ...args);
buf := string_buffer_from_slice(data[:]);
res := sbprint(&buf, ..args);
os.write_string(fd, res);
return len(res);
}
fprintln :: proc(fd: os.Handle, args: ...any) -> int {
fprintln :: proc(fd: os.Handle, args: ..any) -> int {
data: [_BUFFER_SIZE]byte;
buf := string_buffer_from_slice(data[..]);
res := sbprintln(&buf, ...args);
buf := string_buffer_from_slice(data[:]);
res := sbprintln(&buf, ..args);
os.write_string(fd, res);
return len(res);
}
fprintf :: proc(fd: os.Handle, fmt: string, args: ...any) -> int {
fprintf :: proc(fd: os.Handle, fmt: string, args: ..any) -> int {
data: [_BUFFER_SIZE]byte;
buf := string_buffer_from_slice(data[..]);
res := sbprintf(&buf, fmt, ...args);
buf := string_buffer_from_slice(data[:]);
res := sbprintf(&buf, fmt, ..args);
os.write_string(fd, res);
return len(res);
}
// print* procedures return the number of bytes written
print :: proc(args: ...any) -> int { return fprint(os.stdout, ...args); }
print_err :: proc(args: ...any) -> int { return fprint(os.stderr, ...args); }
println :: proc(args: ...any) -> int { return fprintln(os.stdout, ...args); }
println_err :: proc(args: ...any) -> int { return fprintln(os.stderr, ...args); }
printf :: proc(fmt: string, args: ...any) -> int { return fprintf(os.stdout, fmt, ...args); }
printf_err :: proc(fmt: string, args: ...any) -> int { return fprintf(os.stderr, fmt, ...args); }
print :: proc(args: ..any) -> int { return fprint(os.stdout, ..args); }
print_err :: proc(args: ..any) -> int { return fprint(os.stderr, ..args); }
println :: proc(args: ..any) -> int { return fprintln(os.stdout, ..args); }
println_err :: proc(args: ..any) -> int { return fprintln(os.stderr, ..args); }
printf :: proc(fmt: string, args: ..any) -> int { return fprintf(os.stdout, fmt, ..args); }
printf_err :: proc(fmt: string, args: ..any) -> int { return fprintf(os.stderr, fmt, ..args); }
// aprint* procedures return a string that was allocated with the current context
// They must be freed accordingly
aprint :: proc(args: ...any) -> string {
aprint :: proc(args: ..any) -> string {
buf := String_Buffer(make([dynamic]byte));
sbprint(&buf, ...args);
sbprint(&buf, ..args);
return to_string(buf);
}
aprintln :: proc(args: ...any) -> string {
aprintln :: proc(args: ..any) -> string {
buf := String_Buffer(make([dynamic]byte));
sbprintln(&buf, ...args);
sbprintln(&buf, ..args);
return to_string(buf);
}
aprintf :: proc(fmt: string, args: ...any) -> string {
aprintf :: proc(fmt: string, args: ..any) -> string {
buf := String_Buffer(make([dynamic]byte));
sbprintf(&buf, fmt, ...args);
sbprintf(&buf, fmt, ..args);
return to_string(buf);
}
// bprint* procedures return a string using a buffer from an array
bprint :: proc(buf: []byte, args: ...any) -> string {
sb := string_buffer_from_slice(buf[0..len(buf)]);
return sbprint(&sb, ...args);
bprint :: proc(buf: []byte, args: ..any) -> string {
sb := string_buffer_from_slice(buf[0:len(buf)]);
return sbprint(&sb, ..args);
}
bprintln :: proc(buf: []byte, args: ...any) -> string {
sb := string_buffer_from_slice(buf[0..len(buf)]);
return sbprintln(&sb, ...args);
bprintln :: proc(buf: []byte, args: ..any) -> string {
sb := string_buffer_from_slice(buf[0:len(buf)]);
return sbprintln(&sb, ..args);
}
bprintf :: proc(buf: []byte, fmt: string, args: ...any) -> string {
sb := string_buffer_from_slice(buf[0..len(buf)]);
return sbprintf(&sb, fmt, ...args);
bprintf :: proc(buf: []byte, fmt: string, args: ..any) -> string {
sb := string_buffer_from_slice(buf[0:len(buf)]);
return sbprintf(&sb, fmt, ..args);
}
@@ -147,9 +147,9 @@ bprintf :: proc(buf: []byte, fmt: string, args: ...any) -> string {
fprint_type :: proc(fd: os.Handle, info: ^runtime.Type_Info) {
data: [_BUFFER_SIZE]byte;
buf := string_buffer_from_slice(data[..]);
buf := string_buffer_from_slice(data[:]);
write_type(&buf, info);
os.write(fd, buf[..]);
os.write(fd, buf[:]);
}
write_typeid :: proc(buf: ^String_Buffer, id: typeid) {
@@ -336,7 +336,7 @@ _arg_number :: proc(fi: ^Fmt_Info, arg_index: int, format: string, offset, arg_c
parse_arg_number :: proc(format: string) -> (int, int, bool) {
if len(format) < 3 do return 0, 1, false;
for i in 1...len(format) {
for i in 1..len(format)-1 {
if format[i] == ']' {
width, new_index, ok := _parse_int(format, 1);
if !ok || new_index != i {
@@ -356,7 +356,7 @@ _arg_number :: proc(fi: ^Fmt_Info, arg_index: int, format: string, offset, arg_c
fi.reordered = true;
width: int;
index, width, ok = parse_arg_number(format[offset..]);
index, width, ok = parse_arg_number(format[offset:]);
if ok && 0 <= index && index < arg_count {
return index, offset+width, true;
}
@@ -421,7 +421,7 @@ fmt_write_padding :: proc(fi: ^Fmt_Info, width: int) {
pad_byte: byte = '0';
if fi.space do pad_byte = ' ';
for _ in 0..width {
for _ in 0..width-1 {
write_byte(fi.buf, pad_byte);
}
}
@@ -470,7 +470,7 @@ _fmt_int :: proc(fi: ^Fmt_Info, u: u64, base: int, is_signed: bool, bit_size: in
if fi.hash && !fi.zero do flags |= strconv.Int_Flag.Prefix;
if fi.plus do flags |= strconv.Int_Flag.Plus;
if fi.space do flags |= strconv.Int_Flag.Space;
s := strconv.append_bits(buf[start..], u, base, is_signed, bit_size, digits, flags);
s := strconv.append_bits(buf[start:], u, base, is_signed, bit_size, digits, flags);
if fi.hash && fi.zero {
c: byte = 0;
@@ -557,10 +557,10 @@ fmt_float :: proc(fi: ^Fmt_Info, v: f64, bit_size: int, verb: rune) {
if fi.prec_set do prec = fi.prec;
buf: [386]byte;
str := strconv.append_float(buf[1..], v, 'f', prec, bit_size);
str = string(buf[...len(str)]);
str := strconv.append_float(buf[1:], v, 'f', prec, bit_size);
str = string(buf[:len(str)+1]);
if str[1] == '+' || str[1] == '-' {
str = str[1..];
str = str[1:];
} else {
str[0] = '+';
}
@@ -578,12 +578,12 @@ fmt_float :: proc(fi: ^Fmt_Info, v: f64, bit_size: int, verb: rune) {
if fi.zero && fi.width_set && fi.width > len(str) {
write_byte(fi.buf, str[0]);
fmt_write_padding(fi, fi.width - len(str));
write_string(fi.buf, str[1..]);
write_string(fi.buf, str[1:]);
} else {
_pad(fi, str);
}
} else {
_pad(fi, str[1..]);
_pad(fi, str[1:]);
}
case:
@@ -600,7 +600,7 @@ fmt_string :: proc(fi: ^Fmt_Info, s: string, verb: rune) {
fi.space = false;
defer fi.space = space;
for i in 0..len(s) {
for i in 0..len(s)-1 {
if i > 0 && space do write_byte(fi.buf, ' ');
char_set := __DIGITS_UPPER;
if verb == 'x' do char_set = __DIGITS_LOWER;
@@ -747,7 +747,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
for _, i in b.names {
if !hash && i > 0 do write_string(fi.buf, ", ");
if hash do for in 0..fi.indent do write_byte(fi.buf, '\t');
if hash do for in 0..fi.indent-1 do write_byte(fi.buf, '\t');
write_string(fi.buf, b.names[i]);
write_string(fi.buf, " = ");
@@ -762,7 +762,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
if hash do write_string(fi.buf, ",\n");
}
if hash do for in 0..indent do write_byte(fi.buf, '\t');
if hash do for in 0..indent-1 do write_byte(fi.buf, '\t');
write_byte(fi.buf, '}');
case:
@@ -786,7 +786,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
case runtime.Type_Info_Array:
write_byte(fi.buf, '[');
defer write_byte(fi.buf, ']');
for i in 0..info.count {
for i in 0..info.count-1 {
if i > 0 do write_string(fi.buf, ", ");
data := uintptr(v.data) + uintptr(i*info.elem_size);
@@ -797,7 +797,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
write_byte(fi.buf, '[');
defer write_byte(fi.buf, ']');
array := cast(^mem.Raw_Dynamic_Array)v.data;
for i in 0..array.len {
for i in 0..array.len-1 {
if i > 0 do write_string(fi.buf, ", ");
data := uintptr(array.data) + uintptr(i*info.elem_size);
@@ -808,7 +808,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
write_byte(fi.buf, '[');
defer write_byte(fi.buf, ']');
slice := cast(^mem.Raw_Slice)v.data;
for i in 0..slice.len {
for i in 0..slice.len-1 {
if i > 0 do write_string(fi.buf, ", ");
data := uintptr(slice.data) + uintptr(i*info.elem_size);
@@ -833,7 +833,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
entry_type := ed.elem.variant.(runtime.Type_Info_Struct);
entry_size := ed.elem_size;
for i in 0..entries.len {
for i in 0..entries.len-1 {
if i > 0 do write_string(fi.buf, ", ");
data := uintptr(entries.data) + uintptr(i*entry_size);
@@ -872,7 +872,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
for _, i in info.names {
if !hash && i > 0 do write_string(fi.buf, ", ");
if hash {
for in 0..fi.indent {
for in 0..fi.indent-1 {
write_byte(fi.buf, '\t');
}
}
@@ -1007,7 +1007,7 @@ fmt_arg :: proc(fi: ^Fmt_Info, arg: any, verb: rune) {
sbprint :: proc(buf: ^String_Buffer, args: ...any) -> string {
sbprint :: proc(buf: ^String_Buffer, args: ..any) -> string {
fi: Fmt_Info;
prev_string := false;
@@ -1024,7 +1024,7 @@ sbprint :: proc(buf: ^String_Buffer, args: ...any) -> string {
return to_string(buf^);
}
sbprintln :: proc(buf: ^String_Buffer, args: ...any) -> string {
sbprintln :: proc(buf: ^String_Buffer, args: ..any) -> string {
fi: Fmt_Info;
fi.buf = buf;
@@ -1037,7 +1037,7 @@ sbprintln :: proc(buf: ^String_Buffer, args: ...any) -> string {
return to_string(buf^);
}
sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ...any) -> string {
sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ..any) -> string {
fi: Fmt_Info;
arg_index: int = 0;
end := len(fmt);
@@ -1052,7 +1052,7 @@ sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ...any) -> string {
i += 1;
}
if i > prev_i {
write_string(b, fmt[prev_i..i]);
write_string(b, fmt[prev_i:i]);
}
if i >= end {
break loop;
@@ -1138,7 +1138,7 @@ sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ...any) -> string {
break loop;
}
verb, w := utf8.decode_rune_from_string(fmt[i..]);
verb, w := utf8.decode_rune_from_string(fmt[i:]);
i += w;
switch {
@@ -1156,7 +1156,7 @@ sbprintf :: proc(b: ^String_Buffer, fmt: string, args: ...any) -> string {
if !fi.reordered && arg_index < len(args) {
write_string(b, "%!(EXTRA ");
for arg, index in args[arg_index..] {
for arg, index in args[arg_index:] {
if index > 0 do write_string(b, ", ");
if arg == nil do write_string(b, "<nil>");
+2 -2
View File
@@ -80,7 +80,7 @@ murmur32 :: proc(data: []byte) -> u32 {
h1 = h1*5 + 0xe6546b64;
}
tail := data[nblocks*4 ..];
tail := data[nblocks*4:];
k1: u32;
switch len(tail)&3 {
case 3:
@@ -187,7 +187,7 @@ murmur64 :: proc(data: []byte) -> u64 {
}
// TODO(bill): Fix this
#no_bounds_check data8 := mem.slice_to_bytes(data32[i..])[..3];
#no_bounds_check data8 := mem.slice_to_bytes(data32[i:])[:3];
switch len {
case 3:
h2 ~= u32(data8[2]) << 16;
+8 -8
View File
@@ -164,7 +164,7 @@ cross :: proc[cross2, cross3];
vec_dot :: proc(a, b: $T/[$N]$E) -> E {
res: E;
for i in 0..N {
for i in 0..N-1 {
res += a[i] * b[i];
}
return res;
@@ -194,13 +194,13 @@ norm0 :: proc(v: $T/[$N]$E) -> T {
identity :: proc(T: type/[$N][N]$E) -> T {
m: T;
for i in 0..N do m[i][i] = E(1);
for i in 0..N-1 do m[i][i] = E(1);
return m;
}
transpose :: proc(m: $M/[$N][N]f32) -> M {
for j in 0..N {
for i in 0..N {
for j in 0..N-1 {
for i in 0..N-1 {
m[i][j], m[j][i] = m[j][i], m[i][j];
}
}
@@ -209,8 +209,8 @@ transpose :: proc(m: $M/[$N][N]f32) -> M {
mat3_mul :: proc(a, b: Mat3) -> Mat3 {
c: Mat3;
for j in 0..3 {
for i in 0..3 {
for j in 0..2 {
for i in 0..2 {
c[j][i] = a[0][i]*b[j][0] +
a[1][i]*b[j][1] +
a[2][i]*b[j][2];
@@ -221,8 +221,8 @@ mat3_mul :: proc(a, b: Mat3) -> Mat3 {
mat4_mul :: proc(a, b: Mat4) -> Mat4 {
c: Mat4;
for j in 0..4 {
for i in 0..4 {
for j in 0..3 {
for i in 0..3 {
c[j][i] = a[0][i]*b[j][0] +
a[1][i]*b[j][1] +
a[2][i]*b[j][2] +
+2 -2
View File
@@ -62,7 +62,7 @@ compare :: proc "contextless" (a, b: []byte) -> int {
}
compare_byte_ptrs :: proc "contextless" (a, b: ^byte, n: int) -> int {
pa :: ptr_offset;
for i in 0..n do switch {
for i in 0..n-1 do switch {
case pa(a, i)^ < pa(b, i)^: return -1;
case pa(a, i)^ > pa(b, i)^: return +1;
}
@@ -147,7 +147,7 @@ allocation_header_fill :: proc(header: ^AllocationHeader, data: rawptr, size: in
ptr := cast(^uint)(ptr_offset(header, 1));
n := ptr_sub(cast(^uint)data, ptr);
for i in 0..n {
for i in 0..n-1 {
ptr_offset(ptr, i)^ = ~uint(0);
}
}
+1 -1
View File
@@ -46,7 +46,7 @@ _libgl := win32.load_library_a("opengl32.dll");
get_gl_proc_address :: proc(name: string) -> rawptr {
if name[len(name)-1] == 0 {
name = name[..len(name)-1];
name = name[:len(name)-1];
}
// NOTE(bill): null terminated
assert(mem.ptr_offset(&name[0], cast(uintptr)len(name))^ == 0);
+1 -1
View File
@@ -37,7 +37,7 @@ read_entire_file :: proc(name: string) -> (data: []byte, success: bool) {
delete(data);
return nil, false;
}
return data[0..bytes_read], true;
return data[0:bytes_read], true;
}
write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) {
+1 -1
View File
@@ -49,7 +49,7 @@ OS_Node_Information :: struct {
ntype: OS_Node_Type,
size: i64,
// Our additions...
// Our additions..
position: i64,
}
+1 -1
View File
@@ -74,7 +74,7 @@ Stat :: struct {
_reserve1,
_reserve2,
_reserve3: i64,
serial_numbe: u64, // File serial number...? Maybe.
serial_numbe: u64, // File serial number..? Maybe.
_reserve4: i64,
};
+1 -1
View File
@@ -73,7 +73,7 @@ Stat :: struct {
blocks: i64, // Number of blocks allocated for the file
block_size: i32, // Optimal blocksize for I/O
flags: u32, // User-defined flags for the file
gen_num: u32, // File generation number ...?
gen_num: u32, // File generation number ..?
_spare: i32, // RESERVED
_reserve1,
_reserve2: i64, // RESERVED
+3 -3
View File
@@ -100,7 +100,7 @@ open :: proc(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errn
}
buf: [300]byte;
copy(buf[..], cast([]byte)path);
copy(buf[:], cast([]byte)path);
handle := Handle(win32.create_file_a(cstring(&buf[0]), access, share_mode, sa, create_mode, win32.FILE_ATTRIBUTE_NORMAL, nil));
if handle != INVALID_HANDLE do return handle, ERROR_NONE;
@@ -221,7 +221,7 @@ last_write_time_by_name :: proc(name: string) -> File_Time {
assert(len(buf) > len(name));
copy(buf[..], cast([]byte)name);
copy(buf[:], cast([]byte)name);
if win32.get_file_attributes_ex_a(cstring(&buf[0]), win32.GetFileExInfoStandard, &data) {
last_write_time = data.last_write_time;
@@ -279,7 +279,7 @@ _alloc_command_line_arguments :: proc() -> []string {
if n > 0 {
n -= 1;
}
arg_list[i] = string(buf[..n]);
arg_list[i] = string(buf[:n]);
}
return arg_list;
+4 -4
View File
@@ -350,7 +350,7 @@ delete_key :: proc(m: ^$T/map[$K]$V, key: K) {
@(builtin)
append :: proc(array: ^$T/[dynamic]$E, args: ...E, loc := #caller_location) -> int {
append :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) -> int {
if array == nil do return 0;
arg_len := len(args);
@@ -373,7 +373,7 @@ append :: proc(array: ^$T/[dynamic]$E, args: ...E, loc := #caller_location) -> i
}
@(builtin)
append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ...string, loc := #caller_location) -> int {
append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_location) -> int {
for arg in args {
append(array = array, args = ([]E)(arg), loc = loc);
}
@@ -616,9 +616,9 @@ __dynamic_map_rehash :: proc(using header: Map_Header, new_count: int, loc := #c
__dynamic_array_resize(nm_hashes, size_of(int), align_of(int), new_count, loc);
__dynamic_array_reserve(&nm.entries, entry_size, entry_align, m.entries.len, loc);
for i in 0..new_count do nm.hashes[i] = -1;
for i in 0..new_count-1 do nm.hashes[i] = -1;
for i in 0..m.entries.len {
for i in 0..m.entries.len-1 {
if len(nm.hashes) == 0 do __dynamic_map_grow(new_header, loc);
entry_header := __dynamic_map_get_entry(header, i);
+7 -7
View File
@@ -17,7 +17,7 @@ __print_u64 :: proc(fd: os.Handle, u: u64) {
}
i -= 1; a[i] = digits[u % b];
os.write(fd, a[i..]);
os.write(fd, a[i:]);
}
__print_i64 :: proc(fd: os.Handle, u: i64) {
@@ -38,7 +38,7 @@ __print_i64 :: proc(fd: os.Handle, u: i64) {
i -= 1; a[i] = '-';
}
os.write(fd, a[i..]);
os.write(fd, a[i:]);
}
__print_caller_location :: proc(fd: os.Handle, using loc: Source_Code_Location) {
@@ -256,7 +256,7 @@ bounds_check_error :: proc "contextless" (file: string, line, column: int, index
__print_caller_location(fd, Source_Code_Location{file, line, column, ""});
os.write_string(fd, " Index ");
__print_i64(fd, i64(index));
os.write_string(fd, " is out of bounds range 0..");
os.write_string(fd, " is out of bounds range 0:");
__print_i64(fd, i64(count));
os.write_byte(fd, '\n');
debug_trap();
@@ -270,9 +270,9 @@ slice_expr_error :: proc "contextless" (file: string, line, column: int, lo, hi:
__print_caller_location(fd, Source_Code_Location{file, line, column, ""});
os.write_string(fd, " Invalid slice indices: ");
__print_i64(fd, i64(lo));
os.write_string(fd, "..");
os.write_string(fd, ":");
__print_i64(fd, i64(hi));
os.write_string(fd, "..");
os.write_string(fd, ":");
__print_i64(fd, i64(len));
os.write_byte(fd, '\n');
debug_trap();
@@ -285,9 +285,9 @@ dynamic_array_expr_error :: proc "contextless" (file: string, line, column: int,
__print_caller_location(fd, Source_Code_Location{file, line, column, ""});
os.write_string(fd, " Invalid dynamic array values: ");
__print_i64(fd, i64(low));
os.write_string(fd, "..");
os.write_string(fd, ":");
__print_i64(fd, i64(high));
os.write_string(fd, "..");
os.write_string(fd, ":");
__print_i64(fd, i64(max));
os.write_byte(fd, '\n');
debug_trap();
+18 -18
View File
@@ -11,7 +11,7 @@ bubble_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
for {
init_swap, prev_swap := -1, -1;
for j in init_j..last_j {
for j in init_j..last_j-1 {
if f(array[j], array[j+1]) > 0 {
array[j], array[j+1] = array[j+1], array[j];
prev_swap = j;
@@ -34,7 +34,7 @@ bubble_sort :: proc(array: $A/[]$T) {
for {
init_swap, prev_swap := -1, -1;
for j in init_j..last_j {
for j in init_j..last_j-1 {
if array[j] > array[j+1] {
array[j], array[j+1] = array[j+1], array[j];
prev_swap = j;
@@ -69,8 +69,8 @@ quick_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
j -= 1;
}
quick_sort_proc(a[0..i], f);
quick_sort_proc(a[i..n], f);
quick_sort_proc(a[0:i], f);
quick_sort_proc(a[i:n], f);
}
quick_sort :: proc(array: $A/[]$T) {
@@ -92,8 +92,8 @@ quick_sort :: proc(array: $A/[]$T) {
j -= 1;
}
quick_sort(a[0..i]);
quick_sort(a[i..n]);
quick_sort(a[0:i]);
quick_sort(a[i:n]);
}
_log2 :: proc(n: int) -> int {
@@ -106,7 +106,7 @@ merge_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
merge_slices :: proc(arr1, arr2, out: A, f: proc(T, T) -> int) {
N1, N2 := len(arr1), len(arr2);
i, j := 0, 0;
for k in 0..N1+N2 {
for k in 0..N1+N2-1 {
if j == N2 || i < N1 && j < N2 && f(arr1[i], arr2[j]) < 0 {
out[k] = arr1[i];
i += 1;
@@ -126,16 +126,16 @@ merge_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
a, b, m, M := N/2, N, 1, _log2(N);
for i in 0..M+1 {
for j in 0..a {
for i in 0..M {
for j in 0..a-1 {
k := 2*j*m;
merge_slices(arr1[k..k+m], arr1[k+m..k+m+m], arr2[k..], f);
merge_slices(arr1[k:k+m], arr1[k+m:k+m+m], arr2[k:], f);
}
if N-b > m {
k := 2*a*m;
merge_slices(arr1[k..k+m], arr1[k+m..k+m+(N-b)&(m-1)], arr2[k..], f);
merge_slices(arr1[k:k+m], arr1[k+m : k+m+(N-b)&(m-1)], arr2[k:], f);
} else {
copy(arr2[b..N], arr1[b..N]);
copy(arr2[b:N], arr1[b:N]);
}
arr1, arr2 = arr2, arr1;
m <<= 1;
@@ -150,7 +150,7 @@ merge_sort :: proc(array: $A/[]$T) {
merge_slices :: proc(arr1, arr2, out: A) {
N1, N2 := len(arr1), len(arr2);
i, j := 0, 0;
for k in 0..N1+N2 {
for k in 0..N1+N2-1 {
if j == N2 || i < N1 && j < N2 && arr1[i] < arr2[j] {
out[k] = arr1[i];
i += 1;
@@ -168,16 +168,16 @@ merge_sort :: proc(array: $A/[]$T) {
a, b, m, M := N/2, N, 1, _log2(N);
for i in 0..M+1 {
for j in 0..a {
for i in 0..M {
for j in 0..a-1 {
k := 2*j*m;
merge_slices(arr1[k..k+m], arr1[k+m..k+m+m], arr2[k..]);
merge_slices(arr1[k:k+m], arr1[k+m:k+m+m], arr2[k:]);
}
if N-b > m {
k := 2*a*m;
merge_slices(arr1[k..k+m], arr1[k+m..k+m+(N-b)&(m-1)], arr2[k..]);
merge_slices(arr1[k:k+m], arr1[k+m : k+m+(N-b)&(m-1)], arr2[k:]);
} else {
copy(arr2[b..N], arr1[b..N]);
copy(arr2[b:N], arr1[b:N]);
}
arr1, arr2 = arr2, arr1;
m <<= 1;
+28 -28
View File
@@ -23,9 +23,9 @@ _digit_value :: proc(r: rune) -> int {
ri := int(r);
v: int = 16;
switch r {
case '0'...'9': v = ri-'0';
case 'a'...'z': v = ri-'a'+10;
case 'A'...'Z': v = ri-'A'+10;
case '0'..'9': v = ri-'0';
case 'a'..'z': v = ri-'a'+10;
case 'A'..'Z': v = ri-'A'+10;
}
return v;
}
@@ -36,9 +36,9 @@ parse_i64 :: proc(s: string) -> i64 {
switch s[0] {
case '-':
neg = true;
s = s[1..];
s = s[1:];
case '+':
s = s[1..];
s = s[1:];
}
}
@@ -46,11 +46,11 @@ parse_i64 :: proc(s: string) -> i64 {
base: i64 = 10;
if len(s) > 2 && s[0] == '0' {
switch s[1] {
case 'b': base = 2; s = s[2..];
case 'o': base = 8; s = s[2..];
case 'd': base = 10; s = s[2..];
case 'z': base = 12; s = s[2..];
case 'x': base = 16; s = s[2..];
case 'b': base = 2; s = s[2:];
case 'o': base = 8; s = s[2:];
case 'd': base = 10; s = s[2:];
case 'z': base = 12; s = s[2:];
case 'x': base = 16; s = s[2:];
}
}
@@ -76,18 +76,18 @@ parse_i64 :: proc(s: string) -> i64 {
parse_u64 :: proc(s: string) -> u64 {
neg := false;
if len(s) > 1 && s[0] == '+' {
s = s[1..];
s = s[1:];
}
base := u64(10);
if len(s) > 2 && s[0] == '0' {
switch s[1] {
case 'b': base = 2; s = s[2..];
case 'o': base = 8; s = s[2..];
case 'd': base = 10; s = s[2..];
case 'z': base = 12; s = s[2..];
case 'x': base = 16; s = s[2..];
case 'b': base = 2; s = s[2:];
case 'o': base = 8; s = s[2:];
case 'd': base = 10; s = s[2:];
case 'z': base = 12; s = s[2:];
case 'x': base = 16; s = s[2:];
}
}
@@ -194,7 +194,7 @@ append_bool :: proc(buf: []byte, b: bool) -> string {
n := 0;
if b do n = copy(buf, cast([]byte)"true");
else do n = copy(buf, cast([]byte)"false");
return string(buf[..n]);
return string(buf[:n]);
}
append_uint :: proc(buf: []byte, u: u64, base: int) -> string {
@@ -260,7 +260,7 @@ generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, prec, bit_size: int) -> [
s = "+Inf";
}
n := copy(buf, cast([]byte)s);
return buf[..n];
return buf[:n];
case 0: // denormalized
exp += 1;
@@ -279,7 +279,7 @@ generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, prec, bit_size: int) -> [
shortest := prec < 0;
if shortest {
round_shortest(d, mant, exp, flt);
digs = DecimalSlice{digits = d.digits[..], count = d.count, decimal_point = d.decimal_point};
digs = DecimalSlice{digits = d.digits[:], count = d.count, decimal_point = d.decimal_point};
switch fmt {
case 'e', 'E': prec = digs.count-1;
case 'f', 'F': prec = max(digs.count-digs.decimal_point, 0);
@@ -296,7 +296,7 @@ generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, prec, bit_size: int) -> [
round(d, prec);
}
digs = DecimalSlice{digits = d.digits[..], count = d.count, decimal_point = d.decimal_point};
digs = DecimalSlice{digits = d.digits[:], count = d.count, decimal_point = d.decimal_point};
}
return format_digits(buf, shortest, neg, digs, prec, fmt);
}
@@ -309,9 +309,9 @@ format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: DecimalSlice
n: int,
}
to_bytes :: proc(b: Buffer) -> []byte do return b.b[..b.n];
add_bytes :: proc(buf: ^Buffer, bytes: ...byte) {
buf.n += copy(buf.b[buf.n..], bytes);
to_bytes :: proc(b: Buffer) -> []byte do return b.b[:b.n];
add_bytes :: proc(buf: ^Buffer, bytes: ..byte) {
buf.n += copy(buf.b[buf.n:], bytes);
}
b := Buffer{b = buf};
@@ -323,7 +323,7 @@ format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: DecimalSlice
// integer, padded with zeros when needed
if digs.decimal_point > 0 {
m := min(digs.count, digs.decimal_point);
add_bytes(&b, ...digs.digits[0..m]);
add_bytes(&b, ..digs.digits[0:m]);
for ; m < digs.decimal_point; m += 1 {
add_bytes(&b, '0');
}
@@ -335,7 +335,7 @@ format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: DecimalSlice
// fractional part
if prec > 0 {
add_bytes(&b, '.');
for i in 0..prec {
for i in 0..prec-1 {
c: byte = '0';
if j := digs.decimal_point + i; 0 <= j && j < digs.count {
c = digs.digits[j];
@@ -398,7 +398,7 @@ round_shortest :: proc(d: ^Decimal, mant: u64, exp: int, flt: ^FloatInfo) {
inclusive := mant%2 == 0;
for i in 0..d.count {
for i in 0..d.count-1 {
l: byte = '0'; // lower digit
if i < lower.count {
l = lower.digits[i];
@@ -498,8 +498,8 @@ append_bits :: proc(buf: []byte, u: u64, base: int, is_signed: bool, bit_size: i
i-=1; a[i] = ' ';
}
out := a[i..];
out := a[i:];
copy(buf, out);
return string(buf[0..len(out)]);
return string(buf[0:len(out)]);
}
+1 -1
View File
@@ -6,7 +6,7 @@ new_string :: proc(s: string) -> string {
c := make([]byte, len(s)+1);
copy(c, cast([]byte)s);
c[len(s)] = 0;
return string(c[..len(s)]);
return string(c[:len(s)]);
}
new_cstring :: proc(s: string) -> cstring {
+2 -2
View File
@@ -33,7 +33,7 @@ encode :: proc(d: []u16, s: []rune) -> int {
n, m := 0, len(d);
loop: for r in s {
switch r {
case 0.._surr1, _surr3 .. _surr_self:
case 0.._surr1-1, _surr3 .. _surr_self-1:
if m+1 < n do break loop;
d[n] = u16(r);
n += 1;
@@ -59,7 +59,7 @@ encode_string :: proc(d: []u16, s: string) -> int {
n, m := 0, len(d);
loop: for r in s {
switch r {
case 0.._surr1, _surr3 .. _surr_self:
case 0.._surr1-1, _surr3 .. _surr_self-1:
if m+1 < n do break loop;
d[n] = u16(r);
n += 1;
+1 -1
View File
@@ -158,7 +158,7 @@ decode_last_rune :: proc(s: []u8) -> (rune, int) {
}
start = max(start, 0);
r, size = decode_rune(s[start..end]);
r, size = decode_rune(s[start:end]);
if start+size != end {
return RUNE_ERROR, 1;
}