Skip to content

Commit

Permalink
Add 'cowl_iri_to_string' and 'cowl_iri_to_ustring'
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanoBilenchi committed Aug 30, 2024
1 parent 61692ed commit abe0fea
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
26 changes: 26 additions & 0 deletions include/cowl_iri.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,32 @@ COWL_API
COWL_PURE
bool cowl_iri_is_reserved(CowlIRI *iri);

/**
* Returns the string representation of the specified IRI.
*
* @param iri The IRI.
* @return String representation, or NULL on error.
*
* @note The IRI is represented as the concatenation of the namespace and the remainder,
* without any additional markup (e.g. angle brackets).
*/
COWL_API
COWL_RETAINED
CowlString *cowl_iri_to_string(CowlIRI *iri);

/**
* Returns the string representation of the specified IRI.
*
* @param iri The IRI.
* @return String representation, or @val{#ustring_null} on error.
*
* @destructor{ustring_deinit}
* @note The IRI is represented as the concatenation of the namespace and the remainder,
* without any additional markup (e.g. angle brackets).
*/
COWL_API
UString cowl_iri_to_ustring(CowlIRI *iri);

/// @}

COWL_END_DECLS
Expand Down
3 changes: 3 additions & 0 deletions include/cowl_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ ustream_ret cowl_write_string(UOStream *stream, CowlString *string);
* @param stream Output stream.
* @param iri IRI.
* @return Return code.
*
* @note The IRI is written as the concatenation of the namespace and the remainder,
* without any additional markup (e.g. angle brackets).
*/
COWL_API
ustream_ret cowl_write_iri(UOStream *stream, CowlIRI *iri);
Expand Down
13 changes: 13 additions & 0 deletions src/cowl_iri.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "cowl_string_private.h"
#include "cowl_table.h" // IWYU pragma: keep, needed for UHash(CowlObjectTable)
#include "cowl_vocab.h"
#include "cowl_writer.h"
#include "cowl_xml_utils.h"
#include "ulib.h"
#include <stddef.h>
Expand Down Expand Up @@ -169,3 +170,15 @@ CowlString *cowl_iri_get_rem(CowlIRI *iri) {
bool cowl_iri_is_reserved(CowlIRI *iri) {
return cowl_vocab_is_reserved_ns(iri->ns);
}

CowlString *cowl_iri_to_string(CowlIRI *iri) {
return cowl_string(cowl_iri_to_ustring(iri));
}

static cowl_ret write_iri(UOStream *stream, CowlAny *iri) {
return cowl_ret_from_ustream(cowl_write_iri(stream, iri));
}

UString cowl_iri_to_ustring(CowlIRI *iri) {
return cowl_object_to_ustring_impl(iri, write_iri);
}
16 changes: 8 additions & 8 deletions src/cowl_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,10 @@ CowlVector *cowl_get_annot(CowlAny *object) {
return NULL;
}

static cowl_ret cowl_write_debug_impl(UOStream *stream, CowlAny *object) {
return cowl_ret_from_ustream(cowl_write_debug(stream, object));
}

static UString cowl_to_ustring_impl(CowlAny *object, cowl_ret (*fun)(UOStream *, CowlAny *)) {
UString cowl_object_to_ustring_impl(CowlAny *object, cowl_ret (*writer)(UOStream *, CowlAny *)) {
UOStream stream;
UStrBuf buf = ustrbuf();
if (uostream_to_strbuf(&stream, &buf) || fun(&stream, object)) goto err;
if (uostream_to_strbuf(&stream, &buf) || writer(&stream, object)) goto err;
UString ret = ustrbuf_to_ustring(&buf);
uostream_deinit(&stream);
return ret;
Expand All @@ -302,15 +298,19 @@ CowlString *cowl_to_string(CowlAny *object) {
}

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

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

static cowl_ret write_debug(UOStream *stream, CowlAny *object) {
return cowl_ret_from_ustream(cowl_write_debug(stream, object));
}

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

bool cowl_equals(CowlAny *lhs, CowlAny *rhs) {
Expand Down
3 changes: 3 additions & 0 deletions src/cowl_object_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "cowl_attrs.h"
#include "cowl_object.h" // IWYU pragma: export
#include "cowl_object_flags.h"
#include "cowl_ret.h"
#include "ulib.h"

COWL_BEGIN_DECLS
Expand Down Expand Up @@ -66,6 +67,8 @@ void cowl_object_bit_unset(CowlAny *o) {
cowl_object_flags_unset_bit(((CowlObject *)o)->flags);
}

UString cowl_object_to_ustring_impl(CowlAny *object, cowl_ret (*writer)(UOStream *, CowlAny *));

COWL_END_DECLS

#endif // COWL_OBJECT_PRIVATE_H
8 changes: 4 additions & 4 deletions src/writer/functional/cowl_func_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ static ustream_ret cowl_func_write_short_iri(UOStream *stream, CowlString *pfx,
}

static ustream_ret cowl_func_write_iri(UOStream *stream, CowlIRI *iri, CowlSymTable *st) {
if (!st) return cowl_write_iri(stream, iri);
CowlString *prefix;

CowlString *pfx;
if (cowl_iri_has_rem(iri) && (pfx = cowl_sym_table_get_prefix(st, cowl_iri_get_ns(iri)))) {
return cowl_func_write_short_iri(stream, pfx, cowl_iri_get_rem(iri));
if (st && cowl_iri_has_rem(iri) &&
(prefix = cowl_sym_table_get_prefix(st, cowl_iri_get_ns(iri)))) {
return cowl_func_write_short_iri(stream, prefix, cowl_iri_get_rem(iri));
}

return cowl_func_write_full_iri(stream, iri);
Expand Down

0 comments on commit abe0fea

Please sign in to comment.