Working on format string

This commit is contained in:
Krzosa Karol
2022-10-04 20:32:17 +02:00
parent ed0b32eb3e
commit 6f04eb86b2

View File

@@ -13,7 +13,7 @@ AnyArguments :: (values: []Any)
* Written by Lukás Chmela * Written by Lukás Chmela
* Released under GPLv3. * Released under GPLv3.
*/ */
Itoa :: (value: S64, result: *U8, base: S64): *U8 IntegerToString :: (value: S64, result: *U8, base: S64): *U8
// check that the base if valid // check that the base if valid
if (base < 2) || (base > 36) if (base < 2) || (base > 36)
*result = 0 // ' *result = 0 // '
@@ -27,9 +27,6 @@ Itoa :: (value: S64, result: *U8, base: S64): *U8
for value != 0 for value != 0
tmp_value = value tmp_value = value
value /= base value /= base
// @todo: FIX FIX FIX String is untyped
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)] *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)]
// Apply negative sign // Apply negative sign
@@ -43,7 +40,10 @@ Itoa :: (value: S64, result: *U8, base: S64): *U8
return result return result
FormatString :: (buffer: *U8, buffer_len: U64, string: String, args: ..) FormatString :: (buffer: *U8, buffer_len: U64, string: String, args: ..)
// @todo(krzosa): Add consideration of buffer SIZE! Add some function to handle this OutStr or something
arg_counter := 0 arg_counter := 0
out_buffer_len := 0
for i := 0, i < Len(string), i+=1 for i := 0, i < Len(string), i+=1
if string[i] == '%' if string[i] == '%'
Assert(arg_counter < Len(args), "Passed too many [%] to the string formating function") Assert(arg_counter < Len(args), "Passed too many [%] to the string formating function")
@@ -52,12 +52,13 @@ FormatString :: (buffer: *U8, buffer_len: U64, string: String, args: ..)
if arg.type == S64 if arg.type == S64
value := *(arg.data->*S64) value := *(arg.data->*S64)
itoa_buff: [64]U8 itoa_buff: [64]U8
p := Itoa(value, &itoa_buff[0], 10) p := IntegerToString(value, &itoa_buff[0], 10)
for *p != 0 for *p != 0
buffer[buffer_len++] = *p++ buffer[out_buffer_len++] = *p++
else;; Assert(false) else;; Assert(false)
else else
buffer[buffer_len++] = string[i] buffer[out_buffer_len++] = string[i]
main :: (): int main :: (): int
@@ -65,8 +66,6 @@ main :: (): int
b := 20 b := 20
values := []Any{a, b} values := []Any{a, b}
// printf("Test %d", {a})
for values for values
Assert(it.type == S64) Assert(it.type == S64)
AnyArguments({a,b}) AnyArguments({a,b})
@@ -78,4 +77,7 @@ main :: (): int
Assert(*(values[0].data->*S64) == 10) Assert(*(values[0].data->*S64) == 10)
Assert(*(values[1].data->*S64) == 20) Assert(*(values[1].data->*S64) == 20)
buf: [128]U8
FormatString(&buf[0], Len(buf), "Test % %", {32->S64, 156->S64})
return 0 return 0