From: Brendan Hansen Date: Thu, 1 Jul 2021 16:29:19 +0000 (-0500) Subject: initial version of new and better str_format X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=78fd11eef1d5480830ee982c52d0bd5c07d7d839;p=onyx.git initial version of new and better str_format --- diff --git a/bin/onyx b/bin/onyx index 61def406..018c527e 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/alloc/logging.onyx b/core/alloc/logging.onyx index 14f5144e..33579f3c 100644 --- a/core/alloc/logging.onyx +++ b/core/alloc/logging.onyx @@ -14,7 +14,7 @@ logging_allocator_proc :: (data: rawptr, aa: AllocationAction, size: u32, align: res := allocator.func(allocator.data, aa, size, align, oldptr); use package core { printf } - printf("%s with size %i, align %i, oldptr %p returns %p\n", + printf("{} with size {}, align {}, oldptr {} returns {}\n", Allocation_Action_Strings[cast(u32) aa], size, align, oldptr, res); return res; diff --git a/core/conv.onyx b/core/conv.onyx index fe93b1f6..cd020b69 100644 --- a/core/conv.onyx +++ b/core/conv.onyx @@ -157,6 +157,7 @@ i64_to_str :: (n: i64, base: u64, buf: [] u8, min_length := 0) -> str { return str.{ data = c + 1, count = len }; } +#if false { @Hack @Cleanup // This is a big hack but it will work for now f64_to_str :: (f: f64, buf: [] u8) -> str { f *= 10000.0; @@ -181,7 +182,281 @@ f64_to_str :: (f: f64, buf: [] u8) -> str { return str.{ buf.data, len }; } +} + +// This is better than the above version, but still relies on converting the integer +// part of the float to an integer, which could overflow. +f64_to_str :: (f: f64, buf: [] u8) -> str { + math :: package core.math + + len := 0; + + if f < 0 { + f = -f; + buf[0] = #char "-"; + len += 1; + } + + dec_part := f - math.trunc(f); + int_part := f - dec_part; + dec_part = math.abs(dec_part); + + s1 := i64_to_str(~~int_part, 10, buf); + for i: 0 .. s1.count do buf.data[i + len] = s1.data[i]; + buf.data[s1.count + len] = #char "."; + len += s1.count + 1; + + digits := "0123456789"; + + decimals :: 4; + for i: decimals { + dec_part *= 10; + v := math.trunc(dec_part); + dec_part -= v; + + buf.data[len + i] = digits[cast(i32) v]; + } + len += decimals; + + return str.{ buf.data, len }; +} + +str_format :: (format: str, buffer: [] u8, va: ..any) -> str { + return str_format_va(format, buffer, ~~va); +} + +str_format_va :: (format: str, buffer: [] u8, va: [] any) -> str { + @Cleanup + Output :: struct { + data: ^u8; + count: u32; + capacity: u32; + + write :: #match { + (use output: ^Output, c: u8) { + if count >= capacity do return; + + data[count] = c; + count += 1; + }, + + (use output: ^Output, s: str) { + for c: s do output->write(c); + } + } + } + + output := Output.{ buffer.data, 0, buffer.count }; + + Format :: struct { + digits_after_decimal := cast(u32) 4; + } + formatting := Format.{}; + + vararg_index := 0; + + while i := 0; i < format.count { + defer i += 1; + + ch := format[i]; + + if ch == #char "{" { + if format[i + 1] == #char "{" { + output->write(#char "{"); + i += 1; + continue; + } + + while true { + i += 1; + ch = format[i]; + + switch ch { + case #char "." { + i += 1; + + digits := 0; + while format[i] >= #char "0" && format[i] <= #char "9" { + digits *= 10; + digits += ~~(format[i] - #char "0"); + i += 1; + } + + formatting.digits_after_decimal = digits; + } + case #default do break break; + } + } + } + + if ch == #char "}" { + if format[i + 1] == #char "}" { + output->write(#char "}"); + i += 1; + continue; + } + + arg := va[vararg_index]; + vararg_index += 1; + print_any(^output, arg); + + continue; + } + + output->write(ch); + } + + return .{ output.data, output.count }; + + print_any :: (output: ^Output, v: any) { + use package builtin.type_info + array :: package core.array + + switch v.type { + case bool { + value := *(cast(^bool) v.data); + if value do output->write("true"); + else do output->write("false"); + } + + case i32, u32 { + value := *(cast(^i32) v.data); + + ibuf : [128] u8; + istr := i64_to_str(~~value, 10, ~~ibuf); + output->write(istr); + } + + case i64, u64 { + value := *(cast(^i64) v.data); + + ibuf : [128] u8; + istr := i64_to_str(~~value, 10, ~~ibuf); + output->write(istr); + } + + case f32 { + value := *(cast(^f32) v.data); + + fbuf : [128] u8; + fstr := f64_to_str(~~value, ~~fbuf); + output->write(fstr); + } + + case f64 { + value := *(cast(^f64) v.data); + + fbuf : [128] u8; + fstr := f64_to_str(~~value, ~~fbuf); + output->write(fstr); + } + + case str do output->write(*(cast(^str) v.data)); + + case rawptr { + value := *(cast(^rawptr) v.data); + + ibuf : [128] u8; + istr := i64_to_str(~~value, 16, ~~ibuf); + output->write(istr); + } + + case #default { + info := get_type_info(v.type); + + if info.kind == .Struct { + s := cast(^Type_Info_Struct) info; + + if s.name.count > 0 do output->write(s.name); + output->write(" { "); + + for ^member: s.members { + if member != s.members.data do output->write(", "); + + output->write(member.name); + output->write(" = "); + + print_any(output, .{ ~~(cast(^u8) v.data + member.offset), member.type }); + } + + output->write(" }"); + } + + if info.kind == .Function do output->write(""); + + if info.kind == .Pointer { + value := *(cast(^rawptr) v.data); + + ibuf : [128] u8; + istr := i64_to_str(~~value, 16, ~~ibuf); + output->write(istr); + } + + // This assumes that the following type_info kinds are basically the same. + if info.kind == .Dynamic_Array || info.kind == .Slice || info.kind == .Variadic_Argument { + output->write("[ "); + + a := cast(^Type_Info_Dynamic_Array) info; + arr := cast(^array.Untyped_Array) v.data; + data := arr.data; + count := arr.count; + + for i: count { + if i != 0 do output->write(", "); + + print_any(output, .{ ~~(cast(^u8) data + get_type_info(a.of).size * i), a.of }); + } + + output->write(" ]"); + } + + if info.kind == .Array { + output->write("[ "); + + a := cast(^Type_Info_Array) info; + data := v.data; + + for i: a.count { + if i != 0 do output->write(", "); + + print_any(output, .{ ~~(cast(^u8) data + get_type_info(a.of).size * i), a.of }); + } + + output->write(" ]"); + } + + if info.kind == .Enum { + e := cast(^Type_Info_Enum) info; + + value: u64; + switch e.backing_type { + case i8, u8 do value = cast(u64) *(cast(^u8) v.data); + case i16, u16 do value = cast(u64) *(cast(^u16) v.data); + case i32, u32 do value = cast(u64) *(cast(^u32) v.data); + case i64, u64 do value = cast(u64) *(cast(^u64) v.data); + case #default do assert(false, "Bad enum backing type"); + } + + { + for ^member: e.members { + if value == member.value { + output->write(member.name); + break break; + } + } + + output->write("UNKNOWN"); + } + } + } + } + } +} + + +// Old % style print formatting +#if false { str_format :: (format: str, buffer: [] u8, va: ...) -> str { return str_format_va(format, buffer, va); } @@ -306,3 +581,5 @@ str_format_va :: (format: str, buffer: [] u8, va: vararg) -> str { return str.{ ~~buffer.data, len }; } +} + diff --git a/core/io/writer.onyx b/core/io/writer.onyx index 0bb40c2b..da730eae 100644 --- a/core/io/writer.onyx +++ b/core/io/writer.onyx @@ -75,10 +75,10 @@ write_range :: (use writer: ^Writer, r: range, sep := " ") { } } -write_format :: (use writer: ^Writer, format: str, va: ...) { +write_format :: (use writer: ^Writer, format: str, va: ..any) { // POTENTIAL BUG: this buffer will need to be bigger (or dynamic). buffer: [2048] u8; - write_str(writer, conv.str_format_va(format, ~~buffer, va)); + write_str(writer, conv.str_format_va(format, ~~buffer, ~~va)); } write_escaped_str :: (use writer: ^Writer, s: str) { diff --git a/core/std.onyx b/core/std.onyx index 0d12896c..139e13c6 100644 --- a/core/std.onyx +++ b/core/std.onyx @@ -33,6 +33,8 @@ package core #load "./runtime/build_opts" #load "./runtime/common" +#load "./type_info/helper" + #private_file runtime :: package runtime #if runtime.Runtime == runtime.Runtime_Wasi { #load "./runtime/wasi" @@ -40,8 +42,6 @@ package core #load "./wasi/env" #load "./wasi/clock" #load "./io/file" - - #load "./type_info/helper" } #if runtime.Runtime == runtime.Runtime_Js { diff --git a/core/stdio.onyx b/core/stdio.onyx index 90d4680e..51c9adea 100644 --- a/core/stdio.onyx +++ b/core/stdio.onyx @@ -29,9 +29,9 @@ println :: (x: $T) { print("\n"); } -printf :: (format: str, va: ...) { +printf :: (format: str, va: ..any) { buffer: [2048] u8; - print(conv.str_format_va(format, buffer[0 .. 2048], va)); + print(conv.str_format_va(format, buffer[0 .. 2048], ~~va)); } // This works on both slices and arrays diff --git a/core/type_info/helper.onyx b/core/type_info/helper.onyx index d85a476d..faa0704b 100644 --- a/core/type_info/helper.onyx +++ b/core/type_info/helper.onyx @@ -49,7 +49,7 @@ write_type_name :: (writer: ^io.Writer, t: type_expr) { case .Array { arr := cast(^Type_Info_Array) info; - io.write_format(writer, "[%i] ", arr.count); + io.write_format(writer, "[{}] ", arr.count); write_type_name(writer, arr.of); } diff --git a/modules/immediate_mode/gl_utils.onyx b/modules/immediate_mode/gl_utils.onyx index fbf8c1e1..9abbcb49 100644 --- a/modules/immediate_mode/gl_utils.onyx +++ b/modules/immediate_mode/gl_utils.onyx @@ -52,7 +52,7 @@ Shader :: struct { success := true; if gl.getShaderParameter(shader, gl.COMPILE_STATUS) == 0 { - printf("Error compiling shader."); + println("Error compiling shader."); gl.printShaderInfoLog(shader); success = false; } @@ -68,7 +68,7 @@ Shader :: struct { success := true; if gl.getProgramParameter(program, gl.LINK_STATUS) == 0 { - printf("Error linking program."); + println("Error linking program."); gl.printProgramInfoLog(program); success = false; } diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 47849049..60d5a3c8 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -3089,6 +3089,13 @@ static void emit_string_literal(OnyxWasmModule* mod, AstStrLit* strlit) { i8* strdata = bh_alloc_array(global_heap_allocator, i8, strlit->token->length + 1); i32 length = string_process_escape_seqs(strdata, strlit->token->text, strlit->token->length); + // Warning for having '%' in a string literal (because that probably is being used for a old print format) + /* + if (charset_contains((const char *) strdata, '%')) { + onyx_report_warning(strlit->token->pos, "Found string literal with '%%'"); + } + */ + if (bh_table_has(StrLitInfo, mod->string_literals, (char *) strdata)) { StrLitInfo sti = bh_table_get(StrLitInfo, mod->string_literals, (char *) strdata); strlit->addr = sti.addr; diff --git a/tests/aoc-2020/day1.onyx b/tests/aoc-2020/day1.onyx index d4728cae..eecdcddc 100644 --- a/tests/aoc-2020/day1.onyx +++ b/tests/aoc-2020/day1.onyx @@ -17,7 +17,7 @@ main :: (args: [] cstr) { for i: nums do for j: nums do for k: nums { if j + i + k == 2020 { - printf("Answer: %i\n", i * j * k); + printf("Answer: {}\n", i * j * k); // Break out of all of the loops break break break; diff --git a/tests/aoc-2020/day10.onyx b/tests/aoc-2020/day10.onyx index 61f942b6..81fa2746 100644 --- a/tests/aoc-2020/day10.onyx +++ b/tests/aoc-2020/day10.onyx @@ -47,6 +47,6 @@ main :: (args: [] cstr) { for i: 1 .. nums.count do diffs[nums[i] - nums[i - 1] - 1] += 1; diffs[2] += 1; - printf("Diff prod: %i\n", diffs[0] * diffs[2]); - printf("Arrangements: %l\n", count_ending_paths(nums)); + printf("Diff prod: {}\n", diffs[0] * diffs[2]); + printf("Arrangements: {}\n", count_ending_paths(nums)); } diff --git a/tests/aoc-2020/day11.onyx b/tests/aoc-2020/day11.onyx index af756dc5..40a5df5a 100644 --- a/tests/aoc-2020/day11.onyx +++ b/tests/aoc-2020/day11.onyx @@ -96,6 +96,6 @@ main :: (args: [] cstr) { occupied := 0; for s: gos.seats do if s == SeatState.Occupied do occupied += 1; - printf("Occupied: %i\n", occupied); + printf("Occupied: {}\n", occupied); } diff --git a/tests/aoc-2020/day12.onyx b/tests/aoc-2020/day12.onyx index 0dd6ff55..6269127d 100644 --- a/tests/aoc-2020/day12.onyx +++ b/tests/aoc-2020/day12.onyx @@ -67,5 +67,5 @@ main :: (args: [] cstr) { } } - printf("Ship distance: %i\n", math.abs(ship.x) + math.abs(ship.y)); + printf("Ship distance: {}\n", math.abs(ship.x) + math.abs(ship.y)); } diff --git a/tests/aoc-2020/day13.onyx b/tests/aoc-2020/day13.onyx index 1cfd7f59..97fe1603 100644 --- a/tests/aoc-2020/day13.onyx +++ b/tests/aoc-2020/day13.onyx @@ -82,11 +82,11 @@ main :: (args: [] cstr) { // } // result := take_bus * (depart - est); - // printf("Bus: %i\n", take_bus); - // printf("Depart at: %i\n", depart); - // printf("Result: %i\n", result); + // printf("Bus: {}\n", take_bus); + // printf("Depart at: {}\n", depart); + // printf("Result: {}\n", result); // Part 2 result := chinese_remainder_theorem(buses, rems); - printf("Result: %l\n", result); + printf("Result: {}\n", result); } diff --git a/tests/aoc-2020/day14.onyx b/tests/aoc-2020/day14.onyx index 4fff2311..78a16dc7 100644 --- a/tests/aoc-2020/day14.onyx +++ b/tests/aoc-2020/day14.onyx @@ -125,5 +125,5 @@ main :: (args: [] cstr) { s: u64 = 0; for e: mem.entries do s += e.value; - printf("Sum: %l\n", s); + printf("Sum: {}\n", s); } diff --git a/tests/aoc-2020/day15.onyx b/tests/aoc-2020/day15.onyx index 168ced9c..c5417d9b 100644 --- a/tests/aoc-2020/day15.onyx +++ b/tests/aoc-2020/day15.onyx @@ -40,5 +40,5 @@ main :: (args: [] cstr) { turn += 1; } - printf("2020th: %i\n", last_num); + printf("2020th: {}\n", last_num); } diff --git a/tests/aoc-2020/day16.onyx b/tests/aoc-2020/day16.onyx index 5228289d..1742a204 100644 --- a/tests/aoc-2020/day16.onyx +++ b/tests/aoc-2020/day16.onyx @@ -92,7 +92,7 @@ main :: (args: [] cstr) { } } - printf("Scanning error: %i\n", total_scanning_error); + printf("Scanning error: {}\n", total_scanning_error); cols_to_assign := array.make(u32, fields.count); defer array.free(^cols_to_assign); @@ -134,5 +134,5 @@ main :: (args: [] cstr) { if string.starts_with(field.name, "departure") do prod *= ~~my_ticket[field.column]; } - printf("Departure multiply: %l\n", prod); + printf("Departure multiply: {}\n", prod); } diff --git a/tests/aoc-2020/day17.onyx b/tests/aoc-2020/day17.onyx index 9839aa0c..6c6825f2 100644 --- a/tests/aoc-2020/day17.onyx +++ b/tests/aoc-2020/day17.onyx @@ -100,5 +100,5 @@ main :: (args: [] cstr) { active_count := 0; for ^cube_entry: cubes.entries do if cube_entry.value.alive do active_count += 1; - printf("Active count: %i\n", active_count); + printf("Active count: {}\n", active_count); } diff --git a/tests/aoc-2020/day18.onyx b/tests/aoc-2020/day18.onyx index 64512186..1d798105 100644 --- a/tests/aoc-2020/day18.onyx +++ b/tests/aoc-2020/day18.onyx @@ -73,5 +73,5 @@ main :: (args: [] cstr) { total += parse_expression_mul(^file); } - printf("Total: %l\n", total); + printf("Total: {}\n", total); } diff --git a/tests/aoc-2020/day19.onyx b/tests/aoc-2020/day19.onyx index 928e1d72..3327b85f 100644 --- a/tests/aoc-2020/day19.onyx +++ b/tests/aoc-2020/day19.onyx @@ -171,5 +171,5 @@ main :: (args: [] cstr) { if cyk_algorithm(^grammar, line) do valid_count += 1; } - printf("Valid count: %i\n", valid_count); + printf("Valid count: {}\n", valid_count); } diff --git a/tests/aoc-2020/day2.onyx b/tests/aoc-2020/day2.onyx index 1731a912..fca7b94f 100644 --- a/tests/aoc-2020/day2.onyx +++ b/tests/aoc-2020/day2.onyx @@ -35,5 +35,5 @@ main :: (args: [] cstr) { if count == 1 do valid += 1; } - printf("Number valid: %i\n", valid); + printf("Number valid: {}\n", valid); } diff --git a/tests/aoc-2020/day20.onyx b/tests/aoc-2020/day20.onyx index dafa71cc..c616f275 100644 --- a/tests/aoc-2020/day20.onyx +++ b/tests/aoc-2020/day20.onyx @@ -313,7 +313,7 @@ main :: (args: [] cstr) { } } - printf("Corner product: %l\n", prod); + printf("Corner product: {}\n", prod); grid : [12 * 12] u32; memory.set(^grid, 0, sizeof [12 * 12] u32); @@ -369,5 +369,5 @@ main :: (args: [] cstr) { safe_count := 0; for c: forest do if c == #char "#" do safe_count += 1; - printf("Safe count: %i\n", safe_count); + printf("Safe count: {}\n", safe_count); } diff --git a/tests/aoc-2020/day21.onyx b/tests/aoc-2020/day21.onyx index 118eb6ce..f10729d8 100644 --- a/tests/aoc-2020/day21.onyx +++ b/tests/aoc-2020/day21.onyx @@ -129,7 +129,7 @@ main :: (args: [] cstr) { map.delete(^ingredient_map, safe); } - printf("Total safe: %i\n", total_safe); + printf("Total safe: {}\n", total_safe); matched_ingredients := array.make(Ingredient); defer array.free(^matched_ingredients); @@ -166,8 +166,8 @@ main :: (args: [] cstr) { return string.compare(i1.allergen, i2.allergen); }); - for ^mi: matched_ingredients do printf("%s -> %s\n", mi.name, mi.allergen); - for ^mi: matched_ingredients do printf("%s,", mi.name); + for ^mi: matched_ingredients do printf("{} -> {}\n", mi.name, mi.allergen); + for ^mi: matched_ingredients do printf("{},", mi.name); println("\n(Don't copy the last ','!)"); } diff --git a/tests/aoc-2020/day22.onyx b/tests/aoc-2020/day22.onyx index c1d99d90..616d62f4 100644 --- a/tests/aoc-2020/day22.onyx +++ b/tests/aoc-2020/day22.onyx @@ -134,7 +134,7 @@ main :: (args: [] cstr) { defer array.free(^player2_copy); combat_score := combat(^player1_copy, ^player2_copy); - printf("Answer: %i\n", combat_score); + printf("Answer: {}\n", combat_score); } { @@ -152,6 +152,6 @@ main :: (args: [] cstr) { if winner == 1 do score = score_player(^player1_copy); if winner == 2 do score = score_player(^player2_copy); - printf("Recursive answer: %i\n", score); + printf("Recursive answer: {}\n", score); } } diff --git a/tests/aoc-2020/day23.onyx b/tests/aoc-2020/day23.onyx index c40b3bb1..7f64f0cc 100644 --- a/tests/aoc-2020/day23.onyx +++ b/tests/aoc-2020/day23.onyx @@ -76,12 +76,12 @@ main :: (args: [] cstr) { // Part 1 // one_idx := get_idx(array.to_slice(^cups), 1); // for i: 1 .. 9 { - // printf("%i", cast(i32) cups[w(one_idx + i)]); + // printf("{}", cast(i32) cups[w(one_idx + i)]); // } // printf("\n"); // Part 2 one_idx := get_idx(array.to_slice(^cups), 1); prod: i64 = cast(i64) (cups[w(one_idx + 1)]) * cast(i64) (cups[w(one_idx + 2)]); - printf("Cup product: %l\n", prod); + printf("Cup product: {}\n", prod); } diff --git a/tests/aoc-2020/day24.onyx b/tests/aoc-2020/day24.onyx index 4979884a..0e52d1dd 100644 --- a/tests/aoc-2020/day24.onyx +++ b/tests/aoc-2020/day24.onyx @@ -74,7 +74,7 @@ main :: (args: [] cstr) { for ^cell: grid.entries { if cell.value.alive do black_count += 1; } - printf("Black count: %i\n", black_count); + printf("Black count: {}\n", black_count); // Part 2 cells_to_consider := array.make(Vec2); @@ -118,7 +118,7 @@ main :: (args: [] cstr) { for ^cell: grid.entries { if cell.value.alive do black_count += 1; } - printf("GOL black count: %i\n", black_count); + printf("GOL black count: {}\n", black_count); } get_neighbor_count :: (grid: ^map.Map(Vec2, Cell), pos: Vec2) -> u32 { diff --git a/tests/aoc-2020/day25.onyx b/tests/aoc-2020/day25.onyx index db11f1ec..7806bede 100644 --- a/tests/aoc-2020/day25.onyx +++ b/tests/aoc-2020/day25.onyx @@ -67,5 +67,5 @@ main :: (args: [] cstr) { encryption_key_0 := transform_subject(door_pub_key, card_loop_secret); encryption_key_1 := transform_subject(card_pub_key, door_loop_secret); - printf("%i == %i\n", encryption_key_0, encryption_key_1); + printf("{} == {}\n", encryption_key_0, encryption_key_1); } diff --git a/tests/aoc-2020/day3.onyx b/tests/aoc-2020/day3.onyx index 63a7553d..f494fe65 100644 --- a/tests/aoc-2020/day3.onyx +++ b/tests/aoc-2020/day3.onyx @@ -58,5 +58,5 @@ main :: (args: [] cstr) { tree_prod *= tree_count; } - printf("Tree product: %l\n", tree_prod); + printf("Tree product: {}\n", tree_prod); } diff --git a/tests/aoc-2020/day4.onyx b/tests/aoc-2020/day4.onyx index 94f6c3ed..6b70058a 100644 --- a/tests/aoc-2020/day4.onyx +++ b/tests/aoc-2020/day4.onyx @@ -40,5 +40,5 @@ main :: (args: [] cstr) { if field_count == 7 do valid_passports += 1; } - printf("Valid passports: %i\n", valid_passports); + printf("Valid passports: {}\n", valid_passports); } diff --git a/tests/aoc-2020/day5.onyx b/tests/aoc-2020/day5.onyx index 15be6298..0fa91620 100644 --- a/tests/aoc-2020/day5.onyx +++ b/tests/aoc-2020/day5.onyx @@ -31,8 +31,8 @@ main :: (args: [] cstr) { if vals[i + 1] - vals[i] != 1 do missing = vals[i] + 1; } - printf("Max val: %i\n", max_val); - printf("Your seat: %i\n", missing); + printf("Max val: {}\n", max_val); + printf("Your seat: {}\n", missing); } cmp_asc :: (a: $T, b: T) -> i32 do return ~~(a - b); diff --git a/tests/aoc-2020/day6.onyx b/tests/aoc-2020/day6.onyx index 1d731702..a034bbca 100644 --- a/tests/aoc-2020/day6.onyx +++ b/tests/aoc-2020/day6.onyx @@ -52,5 +52,5 @@ main :: (args: [] cstr) { unique_sum += unique; } - printf("Unique sum: %i\n", unique_sum); + printf("Unique sum: {}\n", unique_sum); } diff --git a/tests/aoc-2020/day7.onyx b/tests/aoc-2020/day7.onyx index 8c06460b..2fb4ea3c 100644 --- a/tests/aoc-2020/day7.onyx +++ b/tests/aoc-2020/day7.onyx @@ -106,13 +106,13 @@ main :: (args: [] cstr) { // // for container: bag.contained_in { // if !array.contains(^processed_bags, container.bag) && !array.contains(^to_process_bags, container.bag) { - // // printf("Adding %s to process.\n", container.bag.color); + // // printf("Adding {} to process.\n", container.bag.color); // array.push(^to_process_bags, container.bag); // } // } // } - // printf("Count: %i\n", processed_bags.count - 1); + // printf("Count: {}\n", processed_bags.count - 1); // Part 2 to_process := array.make(#type ^BagNode); @@ -132,5 +132,5 @@ main :: (args: [] cstr) { } } - printf("Count: %i\n", count - 1); + printf("Count: {}\n", count - 1); } diff --git a/tests/aoc-2020/day8.onyx b/tests/aoc-2020/day8.onyx index 61728050..d121d690 100644 --- a/tests/aoc-2020/day8.onyx +++ b/tests/aoc-2020/day8.onyx @@ -81,5 +81,5 @@ main :: (args: [] cstr) { elseif instr.opcode == OpCode.Jmp do instr.opcode = OpCode.Nop; } - printf("Accumulator value: %i\n", acc); + printf("Accumulator value: {}\n", acc); } diff --git a/tests/aoc-2020/day9.onyx b/tests/aoc-2020/day9.onyx index 8df60e34..9bcd661f 100644 --- a/tests/aoc-2020/day9.onyx +++ b/tests/aoc-2020/day9.onyx @@ -60,12 +60,12 @@ main :: (args: [] cstr) { } } - printf("Invalid number: %l\n", invalid); + printf("Invalid number: {}\n", invalid); start, end := find_contiguous_subarray_with_sum(nums, invalid); max := array.fold(nums.data[start .. end + 1], cast(u64) 0, math.max_poly); min := array.fold(nums.data[start .. end + 1], cast(u64) max, math.min_poly); - printf("Extrema sum: %l\n", min + max); + printf("Extrema sum: {}\n", min + max); } diff --git a/tests/array_struct_robustness.onyx b/tests/array_struct_robustness.onyx index 1668b9cf..39d93bc0 100644 --- a/tests/array_struct_robustness.onyx +++ b/tests/array_struct_robustness.onyx @@ -6,7 +6,7 @@ Vec2 :: struct { x: i32; y: i32; } // Overload print() to print Vec2's. #add_match io.write, (use writer: ^io.Writer, use v: Vec2) { - io.write_format(writer, "Vec2(%i, %i)", x, y); + io.write_format(writer, "Vec2({}, {})", x, y); } EntityStore :: struct { diff --git a/tests/baked_parameters.onyx b/tests/baked_parameters.onyx index 461466a6..237afbc3 100644 --- a/tests/baked_parameters.onyx +++ b/tests/baked_parameters.onyx @@ -3,7 +3,7 @@ use package core count_to :: ($N: i32) { - for i: 0 .. N do printf("%i ", i); + for i: 0 .. N do printf("{} ", i); print("\n"); } @@ -40,7 +40,7 @@ main :: (args: [] cstr) { print_age :: (ages: ^map.Map(str, u32), name: str) { age := map.get(ages, name); - printf("%s's age is %i.\n", name, age); + printf("{}'s age is {}.\n", name, age); } print_age(^ages, "Dwight"); diff --git a/tests/better_field_accesses.onyx b/tests/better_field_accesses.onyx index 3485f315..df0fe17f 100644 --- a/tests/better_field_accesses.onyx +++ b/tests/better_field_accesses.onyx @@ -17,7 +17,7 @@ get_foo :: () -> Foo { } main :: (args: [] cstr) { - printf("%i\n", "Hello World!".count); + printf("{}\n", "Hello World!".count); println(get_foo().name); println(get_foo().x); diff --git a/tests/defer_with_continue.onyx b/tests/defer_with_continue.onyx index 1c076ae7..6594a51a 100644 --- a/tests/defer_with_continue.onyx +++ b/tests/defer_with_continue.onyx @@ -7,31 +7,31 @@ main :: (args: [] cstr) { i := 0; while i < 10 { defer i += 1; - defer printf("Index is %i\n", i); + defer printf("Index is {}\n", i); if i == 3 { - printf("Skipping %i!!\n", i); + printf("Skipping {}!!\n", i); continue; } - printf("Doing something with "); + print("Doing something with "); } - printf("i is %i\n", i); + printf("i is {}\n", i); println("\n\n==================================="); i = 0; while i < 10 { defer i += 1; - defer printf("Index is %i\n", i); + defer printf("Index is {}\n", i); if i == 3 { - printf("Skipping %i!!\n", i); + printf("Skipping {}!!\n", i); break; } - printf("Doing something with "); + print("Doing something with "); } - printf("i is %i\n", i); + printf("i is {}\n", i); println("\n\n==================================="); switch i { diff --git a/tests/float_parsing b/tests/float_parsing index 4d983702..f84a3c91 100644 --- a/tests/float_parsing +++ b/tests/float_parsing @@ -1,7 +1,7 @@ 12.0000 12.0000 8.0000 -12.3400 +12.3399 0.3399 2.0000 1.0000 diff --git a/tests/i32map.onyx b/tests/i32map.onyx index 2a55f776..05ab5d3a 100644 --- a/tests/i32map.onyx +++ b/tests/i32map.onyx @@ -17,7 +17,7 @@ main :: (args: [] cstr) { println(map.has(^imap, 50)); println(map.has(^imap, 51)); - printf("%s%s\n", map.get(^imap, 50), map.get(^imap, 1234)); + printf("{}{}\n", map.get(^imap, 50), map.get(^imap, 1234)); map.delete(^imap, 50); println(map.has(^imap, 50)); diff --git a/tests/lazy_iterators.onyx b/tests/lazy_iterators.onyx index c069b3ab..b0f1abe4 100644 --- a/tests/lazy_iterators.onyx +++ b/tests/lazy_iterators.onyx @@ -67,6 +67,6 @@ main :: (args: [] cstr) { |> iter.zip(iter.const(42.0f)); for value: zipped_iterator { - printf("%i %f\n", value.first, value.second); + printf("{} {}\n", value.first, value.second); } } diff --git a/tests/multiple_returns_robustness.onyx b/tests/multiple_returns_robustness.onyx index 0021d167..79c3a10d 100644 --- a/tests/multiple_returns_robustness.onyx +++ b/tests/multiple_returns_robustness.onyx @@ -41,7 +41,7 @@ main :: (args: [] cstr) { least, greatest := get_extrema(i32.[ -5, 20, 5, -10, 200, 8 ]); - printf("%i to %i\n", least, greatest); + printf("{} to {}\n", least, greatest); iarr, farr := return_multiple_arrays(); array_print(iarr); diff --git a/tests/named_arguments_test.onyx b/tests/named_arguments_test.onyx index 4f993236..7b1bcc26 100644 --- a/tests/named_arguments_test.onyx +++ b/tests/named_arguments_test.onyx @@ -4,8 +4,8 @@ use package core main :: (args: [] cstr) { foo :: (x: i32, y: f32) { - printf("x is %i\n", x); - printf("y is %f\n", y); + printf("x is {}\n", x); + printf("y is {}\n", y); } foo(10, 20); @@ -42,8 +42,8 @@ main :: (args: [] cstr) { println(x); println(y); - for elem: z do printf("%f ", elem); - printf("\n"); + for elem: z do printf("{} ", elem); + print("\n"); } overload_with_varargs(10, 20, 30, 40, 50); @@ -61,7 +61,7 @@ main :: (args: [] cstr) { options_f := false, options_g := true, ) { - printf("%b %b %b %b %b %b %b\n", + printf("{} {} {} {} {} {} {}\n", options_a, options_b, options_c, options_d, options_e, options_f, diff --git a/tests/new_printf b/tests/new_printf new file mode 100644 index 00000000..3442358f --- /dev/null +++ b/tests/new_printf @@ -0,0 +1 @@ +123 Test { } false 0x4E8 diff --git a/tests/new_printf.onyx b/tests/new_printf.onyx new file mode 100644 index 00000000..c53077ad --- /dev/null +++ b/tests/new_printf.onyx @@ -0,0 +1,7 @@ +#load "core/std" + +use package core + +main :: (args: [] cstr) { + printf("{} {} {{ }} {} {}\n", 123, "Test", false, ^stdio.print_writer); +} diff --git a/tests/new_struct_behaviour.onyx b/tests/new_struct_behaviour.onyx index 4f114fb1..c3094a2b 100644 --- a/tests/new_struct_behaviour.onyx +++ b/tests/new_struct_behaviour.onyx @@ -66,7 +66,7 @@ main :: (args: [] cstr) { println(foo.some_constant); person := Foo2.SubStruct.{ name = "Joe", age = 30 }; - printf("%s is %i.\n", person.name, person.age); + printf("{} is {}.\n", person.name, person.age); { BasicallyANamespace.function_3(); @@ -80,7 +80,7 @@ main :: (args: [] cstr) { v := Vec3.{ 4, 9, 16 }; - printf("V3(%f, %f, %f)\n", v.x, v.y, v.z); + printf("V3({}, {}, {})\n", v.x, v.y, v.z); } BasicallyANamespace :: struct { diff --git a/tests/operator_overload.onyx b/tests/operator_overload.onyx index d3c0eefb..0fa1aaf0 100644 --- a/tests/operator_overload.onyx +++ b/tests/operator_overload.onyx @@ -68,24 +68,24 @@ main :: (args: [] cstr) { b := C(0, 1); c := a + b; - printf("(%f, %f)\n", c.re, c.im); + printf("({}, {})\n", c.re, c.im); c = a - b; - printf("(%f, %f)\n", c.re, c.im); + printf("({}, {})\n", c.re, c.im); c = a * b; - printf("(%f, %f)\n", c.re, c.im); + printf("({}, {})\n", c.re, c.im); } { a := make_vec(f32.[10, 20]); b := make_vec(f32.[3, 4]); c := a + b; - printf("(%f, %f)\n", c.data[0], c.data[1]); + printf("({}, {})\n", c.data[0], c.data[1]); d := make_vec(f32.[2, 3, 5, 7]); e := join(d, c); - for v: e.data do printf("%f ", v); + for v: e.data do printf("{} ", v); print("\n"); } @@ -93,7 +93,7 @@ main :: (args: [] cstr) { a := make_vec(Complex.[C(2, 3), C(0, 1)]); b := make_vec(Complex.[C(1, 0), C(3, 7)]); c := a * b; - printf("(%f, %f)\n", c.re, c.im); + printf("({}, {})\n", c.re, c.im); } println(test_overload("World!", "Hello!")); diff --git a/tests/polymorphic_array_lengths.onyx b/tests/polymorphic_array_lengths.onyx index 7f88e2b3..8ec8c4a3 100644 --- a/tests/polymorphic_array_lengths.onyx +++ b/tests/polymorphic_array_lengths.onyx @@ -4,7 +4,7 @@ use package core main :: (args: [] cstr) { arr := u32.[ 1, 2, 3, 4, 5 ]; - for elem: array_to_slice(arr) do printf("%i ", elem); + for elem: array_to_slice(arr) do printf("{} ", elem); roots : [20] f32; compute_roots(roots); diff --git a/tests/struct_robustness.onyx b/tests/struct_robustness.onyx index f84f7c88..55d5f974 100644 --- a/tests/struct_robustness.onyx +++ b/tests/struct_robustness.onyx @@ -27,7 +27,7 @@ main :: (args: [] cstr) { ss: SimpleStruct = SimpleStruct.{ 41, 67, "Steve" }; use ss; - printf("SimpleStruct<%i, %i>(%i, %i, %s)\n", + printf("SimpleStruct<{}, {}>({}, {}, {})\n", sizeof SimpleStruct, alignof SimpleStruct, cast(u32) age, height, name); @@ -44,7 +44,7 @@ main :: (args: [] cstr) { u : SimpleUnion; u.float_val = 0.5; - printf("%p == 0x3F000000\n", u.int_val); + printf("{} == 0x3F000000\n", cast(rawptr) u.int_val); } test_default_values :: () { @@ -64,7 +64,7 @@ main :: (args: [] cstr) { print_defaulted(ds2); print_defaulted :: (use ds: DefaultedStruct) { - printf("DefaultedStruct(%i, %f, %l, %d)\n", i, f, l, d); + printf("DefaultedStruct({}, {}, {}, {})\n", i, f, l, d); } } @@ -96,7 +96,7 @@ main :: (args: [] cstr) { // swu3 := StructWithUse.{ 1234, 1, 2, 5678 }; print_swu :: (use swu: StructWithUse) { - printf("StructWithUse(%i, (%f, %f), %i)\n", + printf("StructWithUse({}, ({}, {}), {})\n", first_member, x, y, last_member); } @@ -116,11 +116,11 @@ main :: (args: [] cstr) { ps1.t_data = 1234; ps1.r_data = 5678; - printf("PolyStruct(%i, %f)\n", ps1.t_data, ps1.r_data); + printf("PolyStruct({}, {})\n", ps1.t_data, ps1.r_data); // Currently, this is how you have to do this. ps2 := .{ 1234, 5678 }; - printf("PolyStruct(%f, %i)\n", ps2.t_data, ps2.r_data); + printf("PolyStruct({}, {})\n", ps2.t_data, ps2.r_data); } test_polymorphic_with_defaults :: () { @@ -134,7 +134,7 @@ main :: (args: [] cstr) { PolyStructTyped :: ; ps := PolyStructTyped.{}; - printf("PolyStruct(%i, %f)\n", ps.t_data, ps.r_data); + printf("PolyStruct({}, {})\n", ps.t_data, ps.r_data); } test_polymorphic_union_with_use :: () { @@ -147,12 +147,12 @@ main :: (args: [] cstr) { }; } - printf("%i == 16\n", sizeof PolyStruct(i32, [] u32)); + printf("{} == 16\n", sizeof PolyStruct(i32, [] u32)); ps : PolyStruct(i32, f64); ps.t_data = 1234; ps.r_data = 5678; - printf("%i, %d\n", ps.t_data, ps.r_data); + printf("{}, {}\n", ps.t_data, ps.r_data); } test_union_with_use :: () { @@ -171,10 +171,10 @@ main :: (args: [] cstr) { u : Union; u.x = 10; u.y = 20; - printf("Union(%i, %i)\n", u.x, u.y); + printf("Union({}, {})\n", u.x, u.y); u.name = "Joe"; - printf("Union(%s)\n", u.name); + printf("Union({})\n", u.name); } test_polymorphic_union :: () { @@ -187,9 +187,9 @@ main :: (args: [] cstr) { pu : PolyUnion(str, i32); pu.t_data = "Hello World."; - printf("PolyUnion(%s)\n", pu.t_data); + printf("PolyUnion({})\n", pu.t_data); pu.r_data = 0x4D2; - printf("PolyUnion(%i)\n", pu.r_data); + printf("PolyUnion({})\n", pu.r_data); } } diff --git a/tests/vararg_test.onyx b/tests/vararg_test.onyx index 7751a254..899930b5 100644 --- a/tests/vararg_test.onyx +++ b/tests/vararg_test.onyx @@ -23,5 +23,5 @@ main :: (args: [] cstr) { new_va_test("foo", 1, 2, 3.0f, 5.0f); old_va_test("bar", 1); - printf("Hello, %i, %s!\n", 1234, "World"); + printf("Hello, {}, {}!\n", 1234, "World"); }