diff --git a/lib/fort.c b/lib/fort.c index 3e80136..171c7a9 100644 --- a/lib/fort.c +++ b/lib/fort.c @@ -6533,6 +6533,15 @@ f_string_buffer_t *copy_string_buffer(const f_string_buffer_t *buffer) } break; #endif /* FT_HAVE_WCHAR */ +#ifdef FT_HAVE_UTF8 + case UTF8_BUF: + if (FT_IS_ERROR( + fill_buffer_from_u8string(result, buffer->str.u8str))) { + destroy_string_buffer(result); + return NULL; + } + break; +#endif /* FT_HAVE_UTF8 */ default: destroy_string_buffer(result); return NULL; diff --git a/src/string_buffer.c b/src/string_buffer.c index cb2f6f1..f64b5ba 100644 --- a/src/string_buffer.c +++ b/src/string_buffer.c @@ -332,6 +332,15 @@ f_string_buffer_t *copy_string_buffer(const f_string_buffer_t *buffer) } break; #endif /* FT_HAVE_WCHAR */ +#ifdef FT_HAVE_UTF8 + case UTF8_BUF: + if (FT_IS_ERROR( + fill_buffer_from_u8string(result, buffer->str.u8str))) { + destroy_string_buffer(result); + return NULL; + } + break; +#endif /* FT_HAVE_UTF8 */ default: destroy_string_buffer(result); return NULL; diff --git a/tests/bb_tests/test_table_basic.c b/tests/bb_tests/test_table_basic.c index 8d1dc24..d9b84af 100644 --- a/tests/bb_tests/test_table_basic.c +++ b/tests/bb_tests/test_table_basic.c @@ -1907,6 +1907,69 @@ void test_table_copy(void) } } +#ifdef FT_HAVE_UTF8 +void test_table_copy_utf8(void) +{ + ft_table_t *table = NULL; + + WHEN("Test table copy utf8") { + table = ft_create_table(); + assert_true(table != NULL); + + assert_true(ft_set_cell_prop(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_CPROP_BOTTOM_PADDING, 1) == FT_SUCCESS); + assert_true(ft_set_cell_prop(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_CPROP_TOP_PADDING, 1) == FT_SUCCESS); + assert_true(ft_set_cell_prop(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_CPROP_LEFT_PADDING, 2) == FT_SUCCESS); + assert_true(ft_set_cell_prop(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_CPROP_RIGHT_PADDING, 2) == FT_SUCCESS); + + + ft_set_border_style(table, FT_DOUBLE2_STYLE); + + /* Set "header" type for the first row */ + ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER); + + ft_u8write_ln(table, "Movie title", "Director", "Year", "Rating ⭐"); + + ft_u8write_ln(table, "The Shawshank Redemption", "Frank Darabont", "1994", "9.5"); + ft_u8write_ln(table, "The Godfather", "Francis Ford Coppola", "1972", "9.2"); + ft_add_separator(table); + + ft_u8write_ln(table, "2001: A Space Odyssey", "Stanley Kubrick", "1968", "8.5"); + + /* Set center alignment for the 1st and 3rd columns */ + ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER); + ft_set_cell_prop(table, FT_ANY_ROW, 3, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER); + + + ft_table_t *table_copy = ft_copy_table(table); + + assert_true(table != NULL); + const char *table_str = ft_to_string(table_copy); + assert_true(table_str != NULL); + const char *table_str_etalon = + "╔════════════════════════════╤════════════════════════╤════════╤════════════╗\n" + "║ │ │ │ ║\n" + "║ Movie title │ Director │ Year │ Rating ⭐ ║\n" + "║ │ │ │ ║\n" + "╠════════════════════════════╪════════════════════════╪════════╪════════════╣\n" + "║ │ │ │ ║\n" + "║ The Shawshank Redemption │ Frank Darabont │ 1994 │ 9.5 ║\n" + "║ │ │ │ ║\n" + "╟────────────────────────────┼────────────────────────┼────────┼────────────╢\n" + "║ │ │ │ ║\n" + "║ The Godfather │ Francis Ford Coppola │ 1972 │ 9.2 ║\n" + "║ │ │ │ ║\n" + "╠════════════════════════════╪════════════════════════╪════════╪════════════╣\n" + "║ │ │ │ ║\n" + "║ 2001: A Space Odyssey │ Stanley Kubrick │ 1968 │ 8.5 ║\n" + "║ │ │ │ ║\n" + "╚════════════════════════════╧════════════════════════╧════════╧════════════╝\n"; + assert_str_equal(table_str, table_str_etalon); + ft_destroy_table(table); + ft_destroy_table(table_copy); + } +} +#endif + void test_table_changing_cell(void) diff --git a/tests/main_test.c b/tests/main_test.c index 5442d78..f4266a7 100644 --- a/tests/main_test.c +++ b/tests/main_test.c @@ -27,6 +27,7 @@ void test_table_tbl_properties(void); void test_memory_errors(void); void test_error_codes(void); #ifdef FT_HAVE_UTF8 +void test_table_copy_utf8(void); void test_utf8_table(void); #endif @@ -52,6 +53,7 @@ struct test_case bb_test_suite [] = { #endif #ifdef FT_HAVE_UTF8 {"test_utf8_table", test_utf8_table}, + {"test_table_copy_utf8", test_table_copy_utf8}, #endif {"test_table_write", test_table_write}, {"test_table_insert_strategy", test_table_insert_strategy},