Skip to content

Commit

Permalink
Add 'cowl_to_ustring' and 'cowl_to_debug_ustring'
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanoBilenchi committed Mar 16, 2024
1 parent 1ae5840 commit 3d7929f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
25 changes: 25 additions & 0 deletions include/cowl_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ COWL_API
COWL_RETAINED
CowlString *cowl_to_string(CowlAny *object);

/**
* Returns the string representation of the specified object.
*
* @param object The object.
* @return String representation, or @val{#ustring_null} on error.
*
* @destructor{ustring_deinit}
*/
COWL_API
UString cowl_to_ustring(CowlAny *object);

/**
* Returns a debug string representation of the specified object.
*
Expand All @@ -223,6 +234,20 @@ COWL_API
COWL_RETAINED
CowlString *cowl_to_debug_string(CowlAny *object);

/**
* Returns a debug string representation of the specified object.
*
* The debug string includes internal details such as the object's address,
* type and reference count.
*
* @param object The object.
* @return String representation, or @val{#ustring_null} on error.
*
* @destructor{ustring_deinit}
*/
COWL_API
UString cowl_to_debug_ustring(CowlAny *object);

/**
* Equality function.
*
Expand Down
21 changes: 14 additions & 7 deletions src/cowl_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,27 +245,34 @@ static cowl_ret cowl_write_debug_impl(UOStream *stream, CowlAny *object) {
return cowl_ret_from_ustream(cowl_write_debug(stream, object));
}

static CowlString *cowl_to_string_impl(CowlAny *object, cowl_ret (*fun)(UOStream *, CowlAny *)) {
static UString cowl_to_ustring_impl(CowlAny *object, cowl_ret (*fun)(UOStream *, CowlAny *)) {
UOStream stream;
UStrBuf buf = ustrbuf();
if (uostream_to_strbuf(&stream, &buf) || fun(&stream, object)) goto err;

CowlString *string = cowl_string(ustrbuf_to_ustring(&buf));
UString ret = ustrbuf_to_ustring(&buf);
uostream_deinit(&stream);
return string;
return ret;

err:
uostream_deinit(&stream);
ustrbuf_deinit(&buf);
return NULL;
return ustring_null;
}

CowlString *cowl_to_string(CowlAny *object) {
return cowl_to_string_impl(object, cowl_write);
return cowl_string(cowl_to_ustring(object));
}

UString cowl_to_ustring(CowlAny *object) {
return cowl_to_ustring_impl(object, cowl_write);
}

CowlString *cowl_to_debug_string(CowlAny *object) {
return cowl_to_string_impl(object, cowl_write_debug_impl);
return cowl_string(cowl_to_debug_ustring(object));
}

UString cowl_to_debug_ustring(CowlAny *object) {
return cowl_to_ustring_impl(object, cowl_write_debug_impl);
}

bool cowl_equals(CowlAny *lhs, CowlAny *rhs) {
Expand Down
18 changes: 9 additions & 9 deletions test/tests/cowl_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ uvec_decl(CowlError);
utest_assert_wrap( \
cowl_equals(LHS, RHS), \
{ \
CowlString *T##_lhs_str = cowl_to_string(LHS); \
CowlString *T##_rhs_str = cowl_to_string(RHS); \
printf(" must be equal to \"%s\", found \"%s\".", \
cowl_string_get_cstring(T##_rhs_str), cowl_string_get_cstring(T##_lhs_str)); \
cowl_release(T##_lhs_str); \
cowl_release(T##_rhs_str); \
UString T##_lhs_str = cowl_to_ustring(LHS); \
UString T##_rhs_str = cowl_to_ustring(RHS); \
printf(" must be equal to \"%s\", found \"%s\".", ustring_data(T##_rhs_str), \
ustring_data(T##_lhs_str)); \
ustring_deinit(&T##_lhs_str); \
ustring_deinit(&T##_rhs_str); \
}, \
#LHS)

#define cowl_assert_not_equal(T, LHS, RHS) \
utest_assert_wrap( \
!cowl_equals(LHS, RHS), \
{ \
CowlString *T##_rhs_str = cowl_to_string(RHS); \
printf(" must not be equal to \"%s\".", cowl_string_get_cstring(T##_rhs_str)); \
cowl_release(T##_rhs_str); \
UString T##_rhs_str = cowl_to_ustring(RHS); \
printf(" must not be equal to \"%s\".", ustring_data(T##_rhs_str)); \
ustring_deinit(&T##_rhs_str); \
}, \
#LHS)

Expand Down

0 comments on commit 3d7929f

Please sign in to comment.