Basic type safe print
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
#import "os_windows.kl"
|
||||
|
||||
main :: (argc: int, argv: **char): int
|
||||
test_any()
|
||||
@@ -31,7 +32,9 @@ test_arrays :: ()
|
||||
for array2;; *it = 0
|
||||
for i := 0, i < length_of(array1), i+=1;; assert(0 == array1[i])
|
||||
|
||||
test_array_any(10, 20, 30)
|
||||
print("Thing % %", {30, 20})
|
||||
test_array_any(10, {20, 30})
|
||||
// test_array_any(10, 20, 30) // @fix
|
||||
|
||||
Some_Struct :: struct
|
||||
thing: int
|
||||
|
||||
@@ -45,7 +45,7 @@ release :: (m: *Memory)
|
||||
m.commit = 0
|
||||
m.reserve = 0
|
||||
|
||||
print :: (string: String16)
|
||||
write_console :: (string: String16)
|
||||
handle := GetStdHandle(STD_OUTPUT_HANDLE)
|
||||
WriteConsoleW(handle, string.str->*void, string.len->DWORD, 0, 0)
|
||||
|
||||
@@ -61,3 +61,55 @@ time :: (): F64
|
||||
assert(err != 0)
|
||||
result := query->F64 / performance_frequency
|
||||
return result
|
||||
|
||||
/**
|
||||
* C++ version 0.4 char* style "itoa":
|
||||
* Written by Lukás Chmela
|
||||
* Released under GPLv3.
|
||||
*/
|
||||
itoa :: (value: S64, result: *U8, base: S64): *U8
|
||||
// check that the base if valid
|
||||
if (base < 2) || (base > 36)
|
||||
*result = 0 // '
|
||||
return result
|
||||
|
||||
ptr := result
|
||||
ptr1 := result
|
||||
tmp_char: U8
|
||||
tmp_value: S64
|
||||
|
||||
for value != 0
|
||||
tmp_value = value
|
||||
value /= base
|
||||
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]
|
||||
|
||||
// Apply negative sign
|
||||
if tmp_value < 0
|
||||
*ptr++ = '- // '
|
||||
*ptr-- = 0
|
||||
for ptr1 < ptr
|
||||
tmp_char = *ptr
|
||||
*ptr-- = *ptr1
|
||||
*ptr1++ = tmp_char
|
||||
return result
|
||||
|
||||
|
||||
print :: (string: String, args: ..)
|
||||
buffer: [1024]U8
|
||||
buffer_len: S64
|
||||
|
||||
arg_counter := 0
|
||||
for i := 0, i < length_of(string), i+=1
|
||||
if string[i] == '% // '
|
||||
assert(arg_counter < length_of(args), "Passing too many [%] to a print lambda")
|
||||
arg := args[arg_counter++]
|
||||
|
||||
if arg.type == S64
|
||||
value := *(arg.data->*S64)
|
||||
itoa_buff: [64]U8
|
||||
p := itoa(value, &itoa_buff[0], 10)
|
||||
for *p != 0
|
||||
buffer[buffer_len++] = *p++
|
||||
else;; assert(false)
|
||||
else
|
||||
buffer[buffer_len++] = string[i]
|
||||
Reference in New Issue
Block a user