mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
Add %s support for arrays and slices of bytes
This commit is contained in:
+16
-9
@@ -1599,13 +1599,18 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case runtime.Type_Info_Array:
|
case runtime.Type_Info_Array:
|
||||||
strings.write_byte(fi.buf, '[');
|
if verb == 's' && reflect.is_byte(info.elem) {
|
||||||
defer strings.write_byte(fi.buf, ']');
|
s := strings.string_from_ptr((^byte)(v.data), info.count);
|
||||||
for i in 0..<info.count {
|
fmt_string(fi, s, verb);
|
||||||
if i > 0 { strings.write_string(fi.buf, ", "); }
|
} else {
|
||||||
|
strings.write_byte(fi.buf, '[');
|
||||||
|
defer strings.write_byte(fi.buf, ']');
|
||||||
|
for i in 0..<info.count {
|
||||||
|
if i > 0 { strings.write_string(fi.buf, ", "); }
|
||||||
|
|
||||||
data := uintptr(v.data) + uintptr(i*info.elem_size);
|
data := uintptr(v.data) + uintptr(i*info.elem_size);
|
||||||
fmt_arg(fi, any{rawptr(data), info.elem.id}, verb);
|
fmt_arg(fi, any{rawptr(data), info.elem.id}, verb);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case runtime.Type_Info_Enumerated_Array:
|
case runtime.Type_Info_Enumerated_Array:
|
||||||
@@ -1658,13 +1663,15 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) {
|
|||||||
|
|
||||||
|
|
||||||
case runtime.Type_Info_Slice:
|
case runtime.Type_Info_Slice:
|
||||||
if verb == 'p' {
|
slice := cast(^mem.Raw_Slice)v.data;
|
||||||
slice := cast(^mem.Raw_Slice)v.data;
|
if verb == 's' && reflect.is_byte(info.elem) {
|
||||||
|
s := strings.string_from_ptr((^byte)(slice.data), slice.len);
|
||||||
|
fmt_string(fi, s, verb);
|
||||||
|
} else if verb == 'p' {
|
||||||
fmt_pointer(fi, slice.data, 'p');
|
fmt_pointer(fi, slice.data, 'p');
|
||||||
} else {
|
} else {
|
||||||
strings.write_byte(fi.buf, '[');
|
strings.write_byte(fi.buf, '[');
|
||||||
defer strings.write_byte(fi.buf, ']');
|
defer strings.write_byte(fi.buf, ']');
|
||||||
slice := cast(^mem.Raw_Slice)v.data;
|
|
||||||
for i in 0..<slice.len {
|
for i in 0..<slice.len {
|
||||||
if i > 0 { strings.write_string(fi.buf, ", "); }
|
if i > 0 { strings.write_string(fi.buf, ", "); }
|
||||||
|
|
||||||
|
|||||||
@@ -218,6 +218,14 @@ is_unsigned :: proc(info: ^Type_Info) -> bool {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
is_byte :: proc(info: ^Type_Info) -> bool {
|
||||||
|
if info == nil { return false; }
|
||||||
|
#partial switch i in type_info_base(info).variant {
|
||||||
|
case Type_Info_Integer: return info.size == 1;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
is_integer :: proc(info: ^Type_Info) -> bool {
|
is_integer :: proc(info: ^Type_Info) -> bool {
|
||||||
if info == nil { return false; }
|
if info == nil { return false; }
|
||||||
|
|||||||
@@ -160,6 +160,8 @@ delete :: proc{
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// The new built-in procedure allocates memory. The first argument is a type, not a value, and the value
|
||||||
|
// return is a pointer to a newly allocated value of that type using the specified allocator, default is context.allocator
|
||||||
@builtin
|
@builtin
|
||||||
new :: inline proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> ^T {
|
new :: inline proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> ^T {
|
||||||
ptr := (^T)(mem_alloc(size_of(T), align_of(T), allocator, loc));
|
ptr := (^T)(mem_alloc(size_of(T), align_of(T), allocator, loc));
|
||||||
@@ -222,6 +224,10 @@ make_map :: proc($T: typeid/map[$K]$E, auto_cast cap: int = 16, allocator := con
|
|||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The make built-in procedure allocates and initializes a value of type slice, dynamic array, or map (only)
|
||||||
|
// Similar to new, the first argument is a type, not a value. Unlike new, make's return type is the same as the
|
||||||
|
// type of its argument, not a pointer to it.
|
||||||
|
// Make uses the specified allocator, default is context.allocator, default is context.allocator
|
||||||
@builtin
|
@builtin
|
||||||
make :: proc{
|
make :: proc{
|
||||||
make_slice,
|
make_slice,
|
||||||
@@ -253,6 +259,8 @@ reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The delete_key built-in procedure deletes the element with the specified key (m[key]) from the map.
|
||||||
|
// If m is nil, or there is no such element, this procedure is a no-op
|
||||||
@builtin
|
@builtin
|
||||||
delete_key :: proc(m: ^$T/map[$K]$V, key: K) {
|
delete_key :: proc(m: ^$T/map[$K]$V, key: K) {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -286,6 +294,7 @@ append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) {
|
|||||||
a.len += arg_len;
|
a.len += arg_len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@builtin
|
@builtin
|
||||||
append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) {
|
append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) {
|
||||||
if array == nil {
|
if array == nil {
|
||||||
@@ -313,6 +322,8 @@ append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location)
|
|||||||
a.len += arg_len;
|
a.len += arg_len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The append_string built-in procedure appends a string to the end of a [dynamic]u8 like type
|
||||||
@builtin
|
@builtin
|
||||||
append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) {
|
append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) {
|
||||||
args := transmute([]E)arg;
|
args := transmute([]E)arg;
|
||||||
@@ -523,6 +534,7 @@ append_soa_elems :: proc(array: ^$T/#soa[dynamic]$E, args: ..E, loc := #caller_l
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The append_string built-in procedure appends multiple strings to the end of a [dynamic]u8 like type
|
||||||
@builtin
|
@builtin
|
||||||
append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_location) {
|
append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_location) {
|
||||||
for arg in args {
|
for arg in args {
|
||||||
@@ -530,8 +542,10 @@ append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The append built-in procedure appends elements to the end of a dynamic array
|
||||||
@builtin append :: proc{append_elem, append_elems, append_elem_string};
|
@builtin append :: proc{append_elem, append_elems, append_elem_string};
|
||||||
|
|
||||||
|
// The append_soa built-in procedure appends elements to the end of an #soa dynamic array
|
||||||
@builtin append_soa :: proc{append_soa_elem, append_soa_elems};
|
@builtin append_soa :: proc{append_soa_elem, append_soa_elems};
|
||||||
|
|
||||||
@builtin
|
@builtin
|
||||||
|
|||||||
Reference in New Issue
Block a user