diff --git a/ChangeLog.md b/ChangeLog.md index f8beb74..a4eca33 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,3 +1,9 @@ +## v0.4.3 + +### Bug fixes + +- Fix `ft_printf_ln` when it is used for position which have empty cells before it. + ## v0.4.2 ### Internal diff --git a/lib/fort.c b/lib/fort.c index 6c5e3d6..0e25e07 100644 --- a/lib/fort.c +++ b/lib/fort.c @@ -5485,6 +5485,11 @@ f_status swap_row(f_row_t *cur_row, f_row_t *ins_row, size_t pos) return FT_SUCCESS; } + // Append empty cells to `cur_row` if needed. + while (vector_size(cur_row->cells) < pos) { + create_cell_in_position(cur_row, vector_size(cur_row->cells)); + } + return vector_swap(cur_row->cells, ins_row->cells, pos); } diff --git a/src/row.c b/src/row.c index efae3f6..62a2422 100644 --- a/src/row.c +++ b/src/row.c @@ -213,6 +213,11 @@ f_status swap_row(f_row_t *cur_row, f_row_t *ins_row, size_t pos) return FT_SUCCESS; } + // Append empty cells to `cur_row` if needed. + while (vector_size(cur_row->cells) < pos) { + create_cell_in_position(cur_row, vector_size(cur_row->cells)); + } + return vector_swap(cur_row->cells, ins_row->cells, pos); } diff --git a/tests/bb_tests/test_table_basic.c b/tests/bb_tests/test_table_basic.c index 8988693..8d1dc24 100644 --- a/tests/bb_tests/test_table_basic.c +++ b/tests/bb_tests/test_table_basic.c @@ -296,6 +296,43 @@ void test_bug_fixes(void) assert_str_equal(table_str, table_str_etalon); ft_destroy_table(table); } + + SCENARIO("Issue 58 - https://github.com/seleznevae/libfort/issues/58") { + { + ft_table_t *table = ft_create_table(); + ft_printf_ln(table, "00|01"); + ft_printf_ln(table, "10|11"); + ft_set_cur_cell(table, 0, 5); + ft_printf_ln(table, "05"); + + const char *table_str = ft_to_string(table); + assert_true(table_str != NULL); + const char *table_str_etalon = + "+----+----+--+--+--+----+\n" + "| 00 | 01 | | | | 05 |\n" + "| 10 | 11 | | | | |\n" + "+----+----+--+--+--+----+\n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + } + { + ft_table_t *table = ft_create_table(); + ft_printf_ln(table, "00|01|02|03|04|05"); + ft_printf_ln(table, "10|11|12|13|14|15"); + ft_set_cur_cell(table, 0, 4); + ft_printf_ln(table, "24|25|26|27|28|29|30"); + + const char *table_str = ft_to_string(table); + assert_true(table_str != NULL); + const char *table_str_etalon = + "+----+----+----+----+----+----+----+----+----+----+----+\n" + "| 00 | 01 | 02 | 03 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |\n" + "| 10 | 11 | 12 | 13 | 14 | 15 | | | | | |\n" + "+----+----+----+----+----+----+----+----+----+----+----+\n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + } + } } void test_table_basic(void)