From e7b626d64ee00b478344f41ba16f9836f0ad2ab4 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 4 Sep 2024 17:44:48 +0200 Subject: [PATCH] Add z_keyexpr_canonize_null_terminated --- docs/api.rst | 1 + include/zenoh-pico/api/primitives.h | 13 +++++++++++++ src/api/api.c | 9 +++++++++ 3 files changed, 23 insertions(+) diff --git a/docs/api.rst b/docs/api.rst index 1fea537d1..561a34871 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -289,6 +289,7 @@ Primitives .. autocfunction:: primitives.h::z_keyexpr_as_view_string .. autocfunction:: primitives.h::z_keyexpr_is_canon .. autocfunction:: primitives.h::z_keyexpr_canonize +.. autocfunction:: primitives.h::z_keyexpr_canonize_null_terminated .. autocfunction:: primitives.h::z_keyexpr_includes .. autocfunction:: primitives.h::z_keyexpr_intersects .. autocfunction:: primitives.h::z_keyexpr_equals diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 97f50b63e..f9940af35 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -215,6 +215,19 @@ int8_t z_keyexpr_is_canon(const char *start, size_t len); */ int8_t z_keyexpr_canonize(char *start, size_t *len); +/** + * Canonizes of a given keyexpr in string representation. + * The canonization is performed over the passed string, possibly shortening it by setting null at the end. + * + * Parameters: + * start: Pointer to the keyexpr in its string representation as a null terminated string. + * + * Return: + * ``0`` if canonization successful, or a ``negative value`` otherwise. + * Error codes are defined in :c:enum:`zp_keyexpr_canon_status_t`. + */ +int8_t z_keyexpr_canonize_null_terminated(char *start); + /** * Checks if a given keyexpr contains another keyexpr in its set. * diff --git a/src/api/api.c b/src/api/api.c index 3c4bf37fa..2411d3f12 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -65,6 +65,15 @@ int8_t z_keyexpr_is_canon(const char *start, size_t len) { return _z_keyexpr_is_ int8_t z_keyexpr_canonize(char *start, size_t *len) { return _z_keyexpr_canonize(start, len); } +int8_t z_keyexpr_canonize_null_terminated(char *start) { + size_t len = (start != NULL) ? strlen(start) : 0; + int8_t res = _z_keyexpr_canonize(start, &len); + if (res == _Z_RES_OK) { + start[len] = '\0'; + } + return res; +} + void z_view_keyexpr_from_str_unchecked(z_view_keyexpr_t *keyexpr, const char *name) { keyexpr->_val = _z_rname(name); } z_result_t z_view_keyexpr_from_substr(z_view_keyexpr_t *keyexpr, const char *name, size_t len) {