len, cap, make; remove .count, .capacity, new_slice

This commit is contained in:
Ginger Bill
2017-04-02 18:16:45 +01:00
parent 2c8b99337b
commit 8ce58573df
16 changed files with 538 additions and 185 deletions
+66 -51
View File
@@ -277,17 +277,17 @@ default_allocator :: proc() -> Allocator {
__string_eq :: proc(a, b: string) -> bool {
if a.count != b.count {
if len(a) != len(b) {
return false;
}
if a.data == b.data {
if ^a[0] == ^b[0] {
return true;
}
return __string_cmp(a, b) == 0;
}
__string_cmp :: proc(a, b: string) -> int {
return __mem_compare(a.data, b.data, min(a.count, b.count));
return __mem_compare(^a[0], ^b[0], min(len(a), len(b)));
}
__string_ne :: proc(a, b: string) -> bool #inline { return !__string_eq(a, b); }
@@ -426,19 +426,19 @@ Raw_Any :: struct #ordered {
Raw_String :: struct #ordered {
data: ^byte,
count: int,
len: int,
};
Raw_Slice :: struct #ordered {
data: rawptr,
count: int,
capacity: int,
data: rawptr,
len: int,
cap: int,
};
Raw_Dynamic_Array :: struct #ordered {
data: rawptr,
count: int,
capacity: int,
len: int,
cap: int,
allocator: Allocator,
};
@@ -448,11 +448,22 @@ Raw_Dynamic_Map :: struct #ordered {
};
__dynamic_array_make :: proc(array_: rawptr, elem_size, elem_align: int, len, cap: int) {
array := cast(^Raw_Dynamic_Array)array_;
__check_context();
array.allocator = context.allocator;
assert(array.allocator.procedure != nil);
__dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, capacity: int) -> bool {
if cap > 0 {
__dynamic_array_reserve(array_, elem_size, elem_align, cap);
array.len = len;
}
}
__dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, cap: int) -> bool {
array := cast(^Raw_Dynamic_Array)array_;
if capacity <= array.capacity {
if cap <= array.cap {
return true;
}
@@ -462,8 +473,8 @@ __dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, capa
}
assert(array.allocator.procedure != nil);
old_size := array.capacity * elem_size;
new_size := capacity * elem_size;
old_size := array.cap * elem_size;
new_size := cap * elem_size;
allocator := array.allocator;
new_data := allocator.procedure(allocator.data, Allocator_Mode.RESIZE, new_size, elem_align, array.data, old_size, 0);
@@ -472,7 +483,7 @@ __dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, capa
}
array.data = new_data;
array.capacity = capacity;
array.cap = cap;
return true;
}
@@ -482,43 +493,43 @@ __dynamic_array_append :: proc(array_: rawptr, elem_size, elem_align: int,
array := cast(^Raw_Dynamic_Array)array_;
if item_count <= 0 || items == nil {
return array.count;
return array.len;
}
ok := true;
if array.capacity <= array.count+item_count {
capacity := 2 * array.capacity + max(8, item_count);
ok = __dynamic_array_reserve(array, elem_size, elem_align, capacity);
if array.cap <= array.len+item_count {
cap := 2 * array.cap + max(8, item_count);
ok = __dynamic_array_reserve(array, elem_size, elem_align, cap);
}
if !ok {
// TODO(bill): Better error handling for failed reservation
return array.count;
return array.len;
}
data := cast(^byte)array.data;
assert(data != nil);
__mem_copy(data + (elem_size*array.count), items, elem_size * item_count);
array.count += item_count;
return array.count;
__mem_copy(data + (elem_size*array.len), items, elem_size * item_count);
array.len += item_count;
return array.len;
}
__dynamic_array_append_nothing :: proc(array_: rawptr, elem_size, elem_align: int) -> int {
array := cast(^Raw_Dynamic_Array)array_;
ok := true;
if array.capacity <= array.count+1 {
capacity := 2 * array.capacity + max(8, 1);
ok = __dynamic_array_reserve(array, elem_size, elem_align, capacity);
if array.cap <= array.len+1 {
cap := 2 * array.cap + max(8, 1);
ok = __dynamic_array_reserve(array, elem_size, elem_align, cap);
}
if !ok {
// TODO(bill): Better error handling for failed reservation
return array.count;
return array.len;
}
data := cast(^byte)array.data;
assert(data != nil);
__mem_zero(data + (elem_size*array.count), elem_size);
array.count++;
return array.count;
__mem_zero(data + (elem_size*array.len), elem_size);
array.len++;
return array.len;
}
__slice_append :: proc(slice_: rawptr, elem_size, elem_align: int,
@@ -526,17 +537,17 @@ __slice_append :: proc(slice_: rawptr, elem_size, elem_align: int,
slice := cast(^Raw_Slice)slice_;
if item_count <= 0 || items == nil {
return slice.count;
return slice.len;
}
item_count = min(slice.capacity-slice.count, item_count);
item_count = min(slice.cap-slice.len, item_count);
if item_count > 0 {
data := cast(^byte)slice.data;
assert(data != nil);
__mem_copy(data + (elem_size*slice.count), items, elem_size * item_count);
slice.count += item_count;
__mem_copy(data + (elem_size*slice.len), items, elem_size * item_count);
slice.len += item_count;
}
return slice.count;
return slice.len;
}
@@ -583,9 +594,9 @@ __Map_Header :: struct #ordered {
value_offset: int,
}
__dynamic_map_reserve :: proc(using header: __Map_Header, capacity: int) -> bool {
h := __dynamic_array_reserve(^m.hashes, size_of(int), align_of(int), capacity);
e := __dynamic_array_reserve(^m.entries, entry_size, entry_align, capacity);
__dynamic_map_reserve :: proc(using header: __Map_Header, cap: int) -> bool {
h := __dynamic_array_reserve(^m.hashes, size_of(int), align_of(int), cap);
e := __dynamic_array_reserve(^m.entries, entry_size, entry_align, cap);
return h && e;
}
@@ -594,18 +605,22 @@ __dynamic_map_rehash :: proc(using header: __Map_Header, new_count: int) {
nm: Raw_Dynamic_Map;
new_header.m = ^nm;
header_hashes := cast(^Raw_Dynamic_Array)^header.m.hashes;
nm_hashes := cast(^Raw_Dynamic_Array)^nm.hashes;
reserve(nm.hashes, new_count);
nm.hashes.count = nm.hashes.capacity;
__dynamic_array_reserve(^nm.entries, entry_size, entry_align, m.entries.count);
nm_hashes.len = nm_hashes.cap;
__dynamic_array_reserve(^nm.entries, entry_size, entry_align, m.entries.len);
for _, i in nm.hashes {
nm.hashes[i] = -1;
}
for i := 0; i < nm.entries.count; i++ {
for i := 0; i < nm.entries.len; i++ {
entry_header := __dynamic_map_get_entry(new_header, i);
data := cast(^byte)entry_header;
if nm.hashes.count == 0 {
if len(nm.hashes) == 0 {
__dynamic_map_grow(new_header);
}
@@ -626,7 +641,7 @@ __dynamic_map_rehash :: proc(using header: __Map_Header, new_count: int) {
__dynamic_map_grow(new_header);
}
}
free_ptr_with_allocator(header.m.hashes.allocator, header.m.hashes.data);
free_ptr_with_allocator(header_hashes.allocator, header_hashes.data);
free_ptr_with_allocator(header.m.entries.allocator, header.m.entries.data);
header.m^ = nm;
}
@@ -644,7 +659,7 @@ __dynamic_map_get :: proc(h: __Map_Header, key: __Map_Key) -> rawptr {
__dynamic_map_set :: proc(using h: __Map_Header, key: __Map_Key, value: rawptr) {
index: int;
if m.hashes.count == 0 {
if len(m.hashes) == 0 {
__dynamic_map_grow(h);
}
fr := __dynamic_map_find(h, key);
@@ -672,12 +687,12 @@ __dynamic_map_set :: proc(using h: __Map_Header, key: __Map_Key, value: rawptr)
__dynamic_map_grow :: proc(using h: __Map_Header) {
new_count := 2*m.entries.count + 8;
new_count := 2*m.entries.len + 8;
__dynamic_map_rehash(h, new_count);
}
__dynamic_map_full :: proc(using h: __Map_Header) -> bool {
return cast(int)(0.75 * cast(f64)m.hashes.count) <= m.entries.count;
return cast(int)(0.75 * cast(f64)len(m.hashes)) <= m.entries.len;
}
@@ -693,8 +708,8 @@ __dynamic_map_hash_equal :: proc(h: __Map_Header, a, b: __Map_Key) -> bool {
__dynamic_map_find :: proc(using h: __Map_Header, key: __Map_Key) -> __Map_Find_Result {
fr := __Map_Find_Result{-1, -1, -1};
if m.hashes.count > 0 {
fr.hash_index = cast(int)(key.hash % cast(u64)m.hashes.count);
if len(m.hashes) > 0 {
fr.hash_index = cast(int)(key.hash % cast(u64)len(m.hashes));
fr.entry_index = m.hashes[fr.hash_index];
for fr.entry_index >= 0 {
entry := __dynamic_map_get_entry(h, fr.entry_index);
@@ -709,7 +724,7 @@ __dynamic_map_find :: proc(using h: __Map_Header, key: __Map_Key) -> __Map_Find_
}
__dynamic_map_add_entry :: proc(using h: __Map_Header, key: __Map_Key) -> int {
prev := m.entries.count;
prev := m.entries.len;
c := __dynamic_array_append_nothing(^m.entries, entry_size, entry_align);
if c != prev {
end := __dynamic_map_get_entry(h, c-1);
@@ -739,10 +754,10 @@ __dynamic_map_erase :: proc(using h: __Map_Header, fr: __Map_Find_Result) {
__dynamic_map_get_entry(h, fr.entry_prev).next = __dynamic_map_get_entry(h, fr.entry_index).next;
}
if fr.entry_index == m.entries.count-1 {
m.entries.count--;
if fr.entry_index == m.entries.len-1 {
m.entries.len--;
}
__mem_copy(__dynamic_map_get_entry(h, fr.entry_index), __dynamic_map_get_entry(h, m.entries.count-1), entry_size);
__mem_copy(__dynamic_map_get_entry(h, fr.entry_index), __dynamic_map_get_entry(h, m.entries.len-1), entry_size);
last := __dynamic_map_find(h, __dynamic_map_get_entry(h, fr.entry_index).key);
if last.entry_prev >= 0 {
__dynamic_map_get_entry(h, last.entry_prev).next = fr.entry_index;
+6 -6
View File
@@ -14,14 +14,14 @@ decimal_to_string :: proc(buf: []byte, a: ^Decimal) -> string {
for _, i in buf {
buf[i] = '0';
}
return buf.count;
return len(buf);
}
n := 10 + a.count + abs(a.decimal_point);
// TODO(bill): make this work with a buffer that's not big enough
assert(buf.count >= n);
assert(len(buf) >= n);
buf = buf[..n];
if a.count == 0 {
@@ -118,7 +118,7 @@ shift_right :: proc(a: ^Decimal, k: uint) {
for n > 0 {
dig := n>>k;
n &= mask;
if w < a.digits.count {
if w < len(a.digits) {
a.digits[w] = cast(byte)('0' + dig);
w++;
} else if dig > 0 {
@@ -144,7 +144,7 @@ shift_left :: proc(a: ^Decimal, k: uint) {
quo := n/10;
rem := n - 10*quo;
w--;
if w < a.digits.count {
if w < len(a.digits) {
a.digits[w] = cast(byte)('0' + rem);
} else if rem != 0 {
a.trunc = true;
@@ -156,7 +156,7 @@ shift_left :: proc(a: ^Decimal, k: uint) {
quo := n/10;
rem := n - 10*quo;
w--;
if w < a.digits.count {
if w < len(a.digits) {
a.digits[w] = cast(byte)('0' + rem);
} else if rem != 0 {
a.trunc = true;
@@ -165,7 +165,7 @@ shift_left :: proc(a: ^Decimal, k: uint) {
}
a.count += delta;
a.count = min(a.count, a.digits.count);
a.count = min(a.count, len(a.digits));
a.decimal_point += delta;
trim(a);
}
+37 -37
View File
@@ -48,23 +48,23 @@ fprint :: proc(fd: os.Handle, args: ..any) -> int {
data: [_BUFFER_SIZE]byte;
buf := data[..0];
bprint(^buf, ..args);
os.write(fd, buf[..buf.count]);
return buf.count;
os.write(fd, buf[..len(buf)]);
return len(buf);
}
fprintln :: proc(fd: os.Handle, args: ..any) -> int {
data: [_BUFFER_SIZE]byte;
buf := data[..0];
bprintln(^buf, ..args);
os.write(fd, buf[..buf.count]);
return buf.count;
os.write(fd, buf[..len(buf)]);
return len(buf);
}
fprintf :: proc(fd: os.Handle, fmt: string, args: ..any) -> int {
data: [_BUFFER_SIZE]byte;
buf := data[..0];
bprintf(^buf, fmt, ..args);
os.write(fd, buf[..buf.count]);
return buf.count;
os.write(fd, buf[..len(buf)]);
return len(buf);
}
@@ -83,7 +83,7 @@ fprint_type :: proc(fd: os.Handle, info: ^Type_Info) {
data: [_BUFFER_SIZE]byte;
buf := data[..0];
write_type(^buf, info);
os.write(fd, buf[..buf.count]);
os.write(fd, buf[..len(buf)]);
}
write_type :: proc(buf: ^[]byte, ti: ^Type_Info) {
@@ -147,14 +147,14 @@ write_type :: proc(buf: ^[]byte, ti: ^Type_Info) {
write_type(buf, info.results);
}
case Tuple:
count := info.names.count;
count := len(info.names);
if count != 1 { write_string(buf, "("); }
for name, i in info.names {
if i > 0 { write_string(buf, ", "); }
type := info.types[i];
if name.count > 0 {
if len(name) > 0 {
write_string(buf, name);
write_string(buf, ": ");
}
@@ -233,12 +233,12 @@ write_type :: proc(buf: ^[]byte, ti: ^Type_Info) {
variant_type := type_info_base(info.variant_types[i]);
variant := union_cast(^Struct)variant_type;
vc := variant.names.count-cf.names.count;
vc := len(variant.names)-len(cf.names);
for j in 0..vc {
if j > 0 {
write_string(buf, ", ");
}
index := j + cf.names.count;
index := j + len(cf.names);
write_string(buf, variant.names[index]);
write_string(buf, ": ");
write_type(buf, variant.types[index]);
@@ -279,14 +279,14 @@ bprint :: proc(buf: ^[]byte, args: ..any) -> int {
prev_string := false;
for arg, i in args {
is_string := arg.data != nil && types.is_string(arg.type_info);
is_string := arg != nil && types.is_string(arg.type_info);
if i > 0 && !is_string && !prev_string {
write_byte(buf, ' ');
}
fmt_value(^fi, args[i], 'v');
prev_string = is_string;
}
return buf.count;
return len(buf);
}
bprintln :: proc(buf: ^[]byte, args: ..any) -> int {
@@ -300,7 +300,7 @@ bprintln :: proc(buf: ^[]byte, args: ..any) -> int {
fmt_value(^fi, args[i], 'v');
}
write_byte(buf, '\n');
return buf.count;
return len(buf);
}
sprint :: proc(buf: []byte, args: ..any) -> string {
@@ -328,7 +328,7 @@ parse_int :: proc(s: string, offset: int) -> (result: int, offset: int, ok: bool
ok := true;
i := 0;
for o in offset..s.count {
for o in offset..len(s) {
c := cast(rune)s[offset+i];
if !is_digit(c) {
break;
@@ -349,11 +349,11 @@ _arg_number :: proc(fi: ^Fmt_Info,
arg_count: int,
) -> (index: int, offset: int, ok: bool) {
parse_arg_number :: proc(format: string) -> (int, int, bool) {
if format.count < 3 {
if len(format) < 3 {
return 0, 1, false;
}
for i in 1..format.count {
for i in 1..len(format) {
if format[i] == ']' {
width, new_index, ok := parse_int(format, 1);
if !ok || new_index != i {
@@ -367,7 +367,7 @@ _arg_number :: proc(fi: ^Fmt_Info,
}
if format.count <= offset || format[offset] != '[' {
if len(format) <= offset || format[offset] != '[' {
return arg_index, offset, false;
}
fi.reordered = true;
@@ -383,7 +383,7 @@ int_from_arg :: proc(args: []any, arg_index: int) -> (int, int, bool) {
num := 0;
new_arg_index := arg_index;
ok := true;
if arg_index < args.count {
if arg_index < len(args) {
arg := args[arg_index];
arg.type_info = type_info_base(arg.type_info);
match i in arg {
@@ -439,7 +439,7 @@ fmt_write_padding :: proc(fi: ^Fmt_Info, width: int) {
pad_byte = '0';
}
count := min(width, fi.buf.capacity-fi.buf.count);
count := min(width, cap(fi.buf)-len(fi.buf));
for _ in 0..count {
append(fi.buf, pad_byte);
}
@@ -585,7 +585,7 @@ fmt_float :: proc(fi: ^Fmt_Info, v: f64, bit_size: int, verb: rune) {
}
buf: [128]byte;
str := strconv.append_float(buf[1..1], v, 'f', prec, bit_size);
str = cast(string)buf[..str.count+1];
str = cast(string)buf[..len(str)+1];
if str[1] == '+' || str[1] == '-' {
str = str[1..];
} else {
@@ -602,9 +602,9 @@ fmt_float :: proc(fi: ^Fmt_Info, v: f64, bit_size: int, verb: rune) {
}
if fi.plus || str[0] != '+' {
if fi.zero && fi.width_set && fi.width > str.count {
if fi.zero && fi.width_set && fi.width > len(str) {
write_byte(fi.buf, str[0]);
fmt_write_padding(fi, fi.width - str.count);
fmt_write_padding(fi, fi.width - len(str));
write_string(fi.buf, str[1..]);
} else {
_pad(fi, str);
@@ -685,7 +685,7 @@ fmt_enum :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
break;
}
}
} else if e.values.count == 0 {
} else if len(e.values) == 0 {
write_string(fi.buf, "");
ok = true;
} else {
@@ -780,7 +780,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
write_byte(fi.buf, '[');
defer write_byte(fi.buf, ']');
array := cast(^Raw_Dynamic_Array)v.data;
for i in 0..array.count {
for i in 0..array.len {
if i > 0 {
write_string(fi.buf, ", ");
}
@@ -802,7 +802,7 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
entry_type := union_cast(^Struct)ed.elem;
entry_size := ed.elem_size;
for i in 0..entries.count {
for i in 0..entries.len {
if i > 0 {
write_string(fi.buf, ", ");
}
@@ -831,11 +831,11 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) {
write_byte(fi.buf, '[');
defer write_byte(fi.buf, ']');
slice := cast(^[]byte)v.data;
for i in 0..slice.count {
for i in 0..len(slice) {
if i > 0 {
write_string(fi.buf, ", ");
}
data := slice.data + i*info.elem_size;
data := ^slice[0] + i*info.elem_size;
fmt_arg(fi, any{info.elem, cast(rawptr)data}, 'v');
}
@@ -941,7 +941,7 @@ fmt_quaternion :: proc(fi: ^Fmt_Info, c: quaternion256, bits: int, verb: rune) {
}
fmt_arg :: proc(fi: ^Fmt_Info, arg: any, verb: rune) {
if arg.data == nil || arg.type_info == nil {
if arg == nil {
write_string(fi.buf, "<nil>");
return;
}
@@ -987,7 +987,7 @@ fmt_arg :: proc(fi: ^Fmt_Info, arg: any, verb: rune) {
bprintf :: proc(b: ^[]byte, fmt: string, args: ..any) -> int {
fi := Fmt_Info{};
end := fmt.count;
end := len(fmt);
arg_index := 0;
was_prev_index := false;
for i := 0; i < end; {
@@ -1026,7 +1026,7 @@ bprintf :: proc(b: ^[]byte, fmt: string, args: ..any) -> int {
}
}
arg_index, i, was_prev_index = _arg_number(^fi, arg_index, fmt, i, args.count);
arg_index, i, was_prev_index = _arg_number(^fi, arg_index, fmt, i, len(args));
// Width
if i < end && fmt[i] == '*' {
@@ -1056,7 +1056,7 @@ bprintf :: proc(b: ^[]byte, fmt: string, args: ..any) -> int {
fi.good_arg_index = false;
}
if i < end && fmt[i] == '*' {
arg_index, i, was_prev_index = _arg_number(^fi, arg_index, fmt, i, args.count);
arg_index, i, was_prev_index = _arg_number(^fi, arg_index, fmt, i, len(args));
i++;
fi.prec, arg_index, fi.prec_set = int_from_arg(args, arg_index);
if fi.prec < 0 {
@@ -1077,7 +1077,7 @@ bprintf :: proc(b: ^[]byte, fmt: string, args: ..any) -> int {
}
if !was_prev_index {
arg_index, i, was_prev_index = _arg_number(^fi, arg_index, fmt, i, args.count);
arg_index, i, was_prev_index = _arg_number(^fi, arg_index, fmt, i, len(args));
}
if i >= end {
@@ -1092,7 +1092,7 @@ bprintf :: proc(b: ^[]byte, fmt: string, args: ..any) -> int {
write_byte(b, '%');
} else if !fi.good_arg_index {
write_string(b, "%!(BAD ARGUMENT NUMBER)");
} else if arg_index >= args.count {
} else if arg_index >= len(args) {
write_string(b, "%!(MISSING ARGUMENT)");
} else {
fmt_arg(^fi, args[arg_index], verb);
@@ -1100,13 +1100,13 @@ bprintf :: proc(b: ^[]byte, fmt: string, args: ..any) -> int {
}
}
if !fi.reordered && arg_index < args.count {
if !fi.reordered && arg_index < len(args) {
write_string(b, "%!(EXTRA ");
for arg, index in args[arg_index..] {
if index > 0 {
write_string(b, ", ");
}
if arg.data == nil || arg.type_info == nil {
if arg == nil {
write_string(b, "<nil>");
} else {
fmt_arg(^fi, args[index], 'v');
@@ -1115,5 +1115,5 @@ bprintf :: proc(b: ^[]byte, fmt: string, args: ..any) -> int {
write_string(b, ")");
}
return b.count;
return len(b);
}
+7 -7
View File
@@ -50,8 +50,8 @@ murmur32 :: proc(data: []byte) -> u32 {
c2_32: u32 : 0x1b873593;
h1: u32 = 0;
nblocks := data.count/4;
p := data.data;
nblocks := len(data)/4;
p := ^data[0];
p1 := p + 4*nblocks;
for ; p < p1; p += 4 {
@@ -69,7 +69,7 @@ murmur32 :: proc(data: []byte) -> u32 {
tail := data[nblocks*4 ..];
k1: u32;
match tail.count&3 {
match len(tail)&3 {
case 3:
k1 ~= cast(u32)tail[2] << 16;
fallthrough;
@@ -84,7 +84,7 @@ murmur32 :: proc(data: []byte) -> u32 {
h1 ~= k1;
}
h1 ~= cast(u32)data.count;
h1 ~= cast(u32)len(data);
h1 ~= h1 >> 16;
h1 *= 0x85ebca6b;
@@ -137,11 +137,11 @@ murmur64 :: proc(data: []byte) -> u64 {
m :: 0x5bd1e995;
r :: 24;
h1: u32 = cast(u32)SEED ~ cast(u32)data.count;
h1: u32 = cast(u32)SEED ~ cast(u32)len(data);
h2: u32 = SEED >> 32;
data32 := slice_ptr(cast(^u32)^data[0], data.count/size_of(u32));
len := data.count;
data32 := slice_ptr(cast(^u32)^data[0], len(data)/size_of(u32));
len := len(data);
i := 0;
for len >= 8 {
+6 -6
View File
@@ -19,7 +19,7 @@ copy_non_overlapping :: proc(dst, src: rawptr, len: int) -> rawptr {
return __mem_copy_non_overlapping(dst, src, len);
}
compare :: proc(a, b: []byte) -> int {
return __mem_compare(a.data, b.data, min(a.count, b.count));
return __mem_compare(^a[0], ^b[0], min(len(a), len(b)));
}
@@ -102,7 +102,7 @@ init_arena_from_memory :: proc(using a: ^Arena, data: []byte) {
init_arena_from_context :: proc(using a: ^Arena, size: int) {
backing = context.allocator;
memory = new_slice(byte, size);
memory = make([]byte, size);
temp_count = 0;
}
@@ -133,7 +133,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
case ALLOC:
total_size := size + alignment;
if arena.offset + total_size > arena.memory.count {
if arena.offset + total_size > len(arena.memory) {
fmt.fprintln(os.stderr, "Arena out of memory");
return nil;
}
@@ -161,15 +161,15 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
begin_arena_temp_memory :: proc(a: ^Arena) -> Arena_Temp_Memory {
tmp: Arena_Temp_Memory;
tmp.arena = a;
tmp.original_count = a.memory.count;
tmp.original_count = len(a.memory);
a.temp_count++;
return tmp;
}
end_arena_temp_memory :: proc(using tmp: Arena_Temp_Memory) {
assert(arena.memory.count >= original_count);
assert(len(arena.memory) >= original_count);
assert(arena.temp_count > 0);
arena.memory.count = original_count;
arena.memory = arena.memory[..original_count];
arena.temp_count--;
}
+3 -3
View File
@@ -35,10 +35,10 @@ string_data :: proc(s: string) -> ^u8 #inline { return ^s[0]; }
_libgl := win32.LoadLibraryA(string_data("opengl32.dll\x00"));
GetProcAddress :: proc(name: string) -> proc() #cc_c {
assert(name[name.count-1] == 0);
res := wgl.GetProcAddress(name.data);
assert(name[len(name)-1] == 0);
res := wgl.GetProcAddress(^name[0]);
if res == nil {
res = win32.GetProcAddress(_libgl, name.data);
res = win32.GetProcAddress(_libgl, ^name[0]);
}
return res;
}
+8 -9
View File
@@ -53,7 +53,7 @@ args := _alloc_command_line_arguments();
open :: proc(path: string, mode: int, perm: u32) -> (Handle, Errno) {
if path.count == 0 {
if len(path) == 0 {
return INVALID_HANDLE, ERROR_FILE_NOT_FOUND;
}
@@ -110,7 +110,7 @@ close :: proc(fd: Handle) {
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
bytes_written: i32;
e := win32.WriteFile(cast(win32.Handle)fd, data.data, cast(i32)data.count, ^bytes_written, nil);
e := win32.WriteFile(cast(win32.Handle)fd, ^data[0], cast(i32)len(data), ^bytes_written, nil);
if e == win32.FALSE {
err := win32.GetLastError();
return 0, cast(Errno)err;
@@ -120,7 +120,7 @@ write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
bytes_read: i32;
e := win32.ReadFile(cast(win32.Handle)fd, data.data, cast(u32)data.count, ^bytes_read, nil);
e := win32.ReadFile(cast(win32.Handle)fd, ^data[0], cast(u32)len(data), ^bytes_read, nil);
if e == win32.FALSE {
err := win32.GetLastError();
return 0, cast(Errno)err;
@@ -180,7 +180,7 @@ last_write_time_by_name :: proc(name: string) -> File_Time {
data: win32.File_Attribute_Data;
buf: [1024]byte;
assert(buf.count > name.count);
assert(len(buf) > len(name));
copy(buf[..], cast([]byte)name);
@@ -213,8 +213,8 @@ read_entire_file :: proc(name: string) -> ([]byte, bool) {
return nil, false;
}
data := new_slice(u8, length);
if data.data == nil {
data := make([]byte, length);
if ^data[0] == nil {
return nil, false;
}
@@ -286,10 +286,9 @@ _alloc_command_line_arguments :: proc() -> []string {
wstr_len++;
}
len := 2*wstr_len-1;
buf := new_slice(byte, len+1);
buf := make([]byte, len+1);
str := slice_ptr(wstr, wstr_len+1);
i, j := 0, 0;
for str[j] != 0 {
match {
@@ -334,7 +333,7 @@ _alloc_command_line_arguments :: proc() -> []string {
arg_count: i32;
arg_list_ptr := win32.CommandLineToArgvW(win32.GetCommandLineW(), ^arg_count);
arg_list := new_slice(string, arg_count);
arg_list := make([]string, arg_count);
for _, i in arg_list {
arg_list[i] = alloc_ucs2_to_utf8((arg_list_ptr+i)^);
}
+8 -25
View File
@@ -131,50 +131,33 @@ generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, prec, bit_size: int) -> [
format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: Decimal_Slice, prec: int, fmt: byte) -> []byte {
match fmt {
case 'f', 'F':
add_bytes :: proc(dst: ^[]byte, w: ^int, bytes: ..byte) {
for b in bytes {
if dst.capacity <= w^ {
break;
}
dst.count++;
dst[w^] = b;
w^++;
}
}
dst := buf[..];
w := 0;
if neg {
add_bytes(^dst, ^w, '-');
} else {
add_bytes(^dst, ^w, '+');
}
append(buf, neg ? '-' : '+');
// integer, padded with zeros when needed
if digs.decimal_point > 0 {
m := min(digs.count, digs.decimal_point);
add_bytes(^dst, ^w, ..digs.digits[..m]);
append(buf, ..digs.digits[..m]);
for ; m < digs.decimal_point; m++ {
add_bytes(^dst, ^w, '0');
append(buf, '0');
}
} else {
add_bytes(^dst, ^w, '0');
append(buf, '0');
}
// fractional part
if prec > 0 {
add_bytes(^dst, ^w, '.');
append(buf, '.');
for i in 0..prec {
c: byte = '0';
if j := digs.decimal_point + i; 0 <= j && j < digs.count {
c = digs.digits[j];
}
add_bytes(^dst, ^w, c);
append(buf, c);
}
}
return buf[..w];
return buf;
case 'e', 'E':
panic("strconv: e/E float printing is not yet supported");
@@ -308,7 +291,7 @@ append_bits :: proc(buf: []byte, u: u64, base: int, is_signed: bool, bit_size: i
}
a: [65]byte;
i := a.count;
i := len(a);
neg: bool;
u, neg = is_integer_negative(u, is_signed, bit_size);
+7 -8
View File
@@ -1,15 +1,14 @@
new_c_string :: proc(s: string) -> ^byte {
c := new_slice(byte, s.count+1);
c := make([]byte, len(s)+1);
copy(c, cast([]byte)s);
c[s.count] = 0;
return c.data;
c[len(s)] = 0;
return ^c[0];
}
to_odin_string :: proc(c: ^byte) -> string {
s: string;
s.data = c;
for (c+s.count)^ != 0 {
s.count++;
len := 0;
for (c+len)^ != 0 {
len++;
}
return s;
return cast(string)slice_ptr(c, len);
}
+4 -4
View File
@@ -94,7 +94,7 @@ encode_rune :: proc(r: rune) -> ([4]byte, int) {
decode_rune :: proc(s: string) -> (rune, int) #inline { return decode_rune(cast([]byte)s); }
decode_rune :: proc(s: []byte) -> (rune, int) {
n := s.count;
n := len(s);
if n < 1 {
return RUNE_ERROR, 0;
}
@@ -138,7 +138,7 @@ decode_last_rune :: proc(s: []byte) -> (rune, int) {
size: int;
start, end, limit: int;
end = s.count;
end = len(s);
if end == 0 {
return RUNE_ERROR, 0;
}
@@ -183,7 +183,7 @@ valid_rune :: proc(r: rune) -> bool {
}
valid_string :: proc(s: string) -> bool {
n := s.count;
n := len(s);
for i := 0; i < n; {
si := s[i];
if si < RUNE_SELF { // ascii
@@ -220,7 +220,7 @@ rune_start :: proc(b: byte) -> bool #inline { return b&0xc0 != 0x80; }
rune_count :: proc(s: string) -> int #inline { return rune_count(cast([]byte)s); }
rune_count :: proc(s: []byte) -> int {
count := 0;
n := s.count;
n := len(s);
for i := 0; i < n; {
defer count++;