From abe0feacc19b86314e0bb1677175b299f66c9049 Mon Sep 17 00:00:00 2001 From: Ivano Bilenchi Date: Fri, 30 Aug 2024 10:32:47 +0200 Subject: [PATCH] Add 'cowl_iri_to_string' and 'cowl_iri_to_ustring' --- include/cowl_iri.h | 26 ++++++++++++++++++++++++ include/cowl_writer.h | 3 +++ src/cowl_iri.c | 13 ++++++++++++ src/cowl_object.c | 16 +++++++-------- src/cowl_object_private.h | 3 +++ src/writer/functional/cowl_func_writer.c | 8 ++++---- 6 files changed, 57 insertions(+), 12 deletions(-) diff --git a/include/cowl_iri.h b/include/cowl_iri.h index f88eca1..66f50fd 100644 --- a/include/cowl_iri.h +++ b/include/cowl_iri.h @@ -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 diff --git a/include/cowl_writer.h b/include/cowl_writer.h index 7620d61..daf1cfb 100644 --- a/include/cowl_writer.h +++ b/include/cowl_writer.h @@ -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); diff --git a/src/cowl_iri.c b/src/cowl_iri.c index a80a2c6..db81f6e 100644 --- a/src/cowl_iri.c +++ b/src/cowl_iri.c @@ -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 @@ -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); +} diff --git a/src/cowl_object.c b/src/cowl_object.c index 406b407..83ebcbd 100644 --- a/src/cowl_object.c +++ b/src/cowl_object.c @@ -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; @@ -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) { diff --git a/src/cowl_object_private.h b/src/cowl_object_private.h index 0280c46..2977b41 100644 --- a/src/cowl_object_private.h +++ b/src/cowl_object_private.h @@ -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 @@ -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 diff --git a/src/writer/functional/cowl_func_writer.c b/src/writer/functional/cowl_func_writer.c index ce28b96..a15a80d 100644 --- a/src/writer/functional/cowl_func_writer.c +++ b/src/writer/functional/cowl_func_writer.c @@ -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);