From c04a63385ca6c3d92801612a3816996787c2f432 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Tue, 2 Jul 2024 19:01:11 +0200 Subject: [PATCH 1/3] Add missed z_string_* methods --- docs/api.rst | 4 +++ include/zenoh-pico/api/primitives.h | 43 +++++++++++++++++++++++++++++ src/api/api.c | 18 ++++++++++++ 3 files changed, 65 insertions(+) diff --git a/docs/api.rst b/docs/api.rst index 374de79fc..df9704e77 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -379,6 +379,10 @@ Primitives .. autocfunction:: primitives.h::z_sample_loan .. autocfunction:: primitives.h::z_string_data .. autocfunction:: primitives.h::z_string_len +.. autocfunction:: primitives.h::z_string_wrap +.. autocfunction:: primitives.h::z_string_from_substring +.. autocfunction:: primitives.h::z_string_empty +.. autocfunction:: primitives.h::z_string_is_empty .. autocfunction:: primitives.h::z_scout .. autocfunction:: primitives.h::z_open .. autocfunction:: primitives.h::z_close diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index d5fc63d68..a00ffd2de 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -1133,6 +1133,49 @@ const char *z_string_data(const z_loaned_string_t *str); */ size_t z_string_len(const z_loaned_string_t *str); +/** + * Builds a :c:type:`z_string_t` by wrapping a ``const char *`` string. + * + * Parameters: + * str: Pointer to an uninitialized :c:type:`z_string_t`. + * value: Pointer to a null terminated string. + * + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. + */ +int8_t z_string_wrap(z_owned_string_t *str, const char *value); + +/** + * Builds an empty :c:type:`z_string_t`. + * + * Parameters: + * str: Pointer to an uninitialized :c:type:`z_string_t`. + */ +void z_string_empty(z_owned_string_t *str); + +/** + * Builds a :c:type:`z_string_t` by wrapping a substring specified by ``const char *`` and length `len`. + * + * Parameters: + * value: Pointer to a string. + * len: String size. + * str: Pointer to an uninitialized :c:type:`z_string_t`. + * + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. + */ +int8_t z_string_from_substring(z_owned_string_t *str, const char *value, size_t len); + +/** + * Checks if string is empty + * + * Parameters: + * str: Pointer to a :c:type:`z_loaned_string_t` to check. + * Return: + * ``true`` if conainer is empty, ``false`` otherwise. + */ +bool z_string_is_empty(const z_loaned_string_t *str); + /************* Primitives **************/ /** * Scouts for other Zenoh entities like routers and/or peers. diff --git a/src/api/api.c b/src/api/api.c index 24ed53625..bbe8d88ea 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -837,6 +837,24 @@ const z_loaned_encoding_t *z_reply_err_encoding(const z_loaned_reply_err_t *repl const char *z_string_data(const z_loaned_string_t *str) { return str->val; } size_t z_string_len(const z_loaned_string_t *str) { return str->len; } +int8_t z_string_wrap(z_owned_string_t *str, const char *value) { + str->_val = _z_string_wrap((char *)value); + return _Z_RES_OK; +} + +void z_string_empty(z_owned_string_t *str) { + str->_val.val = NULL; + str->_val.len = 0; +} + +int8_t z_string_from_substring(z_owned_string_t *str, const char *value, size_t len) { + str->_val.val = (char *)value; + str->_val.len = len; + return _Z_RES_OK; +} + +bool z_string_is_empty(const z_loaned_string_t *str) { return str->val == NULL; } + #if Z_FEATURE_PUBLICATION == 1 int8_t _z_undeclare_and_clear_publisher(_z_publisher_t *pub) { int8_t ret = _Z_RES_OK; From 3f051e69b4e50e311ec3b44910040e5f72b61622 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 3 Jul 2024 15:35:46 +0200 Subject: [PATCH 2/3] Remove wrap and make z_owned_string_t really owned --- docs/api.rst | 4 ++-- include/zenoh-pico/api/primitives.h | 4 ++-- include/zenoh-pico/collections/string.h | 2 ++ src/api/api.c | 14 +++++--------- src/collections/string.c | 20 ++++++++++++++++++++ 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index df9704e77..def5fa04e 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -379,8 +379,8 @@ Primitives .. autocfunction:: primitives.h::z_sample_loan .. autocfunction:: primitives.h::z_string_data .. autocfunction:: primitives.h::z_string_len -.. autocfunction:: primitives.h::z_string_wrap -.. autocfunction:: primitives.h::z_string_from_substring +.. autocfunction:: primitives.h::z_string_from_str +.. autocfunction:: primitives.h::z_string_from_substr .. autocfunction:: primitives.h::z_string_empty .. autocfunction:: primitives.h::z_string_is_empty .. autocfunction:: primitives.h::z_scout diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index a00ffd2de..cfa0286e2 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -1143,7 +1143,7 @@ size_t z_string_len(const z_loaned_string_t *str); * Return: * ``0`` if creation successful, ``negative value`` otherwise. */ -int8_t z_string_wrap(z_owned_string_t *str, const char *value); +int8_t z_string_from_str(z_owned_string_t *str, const char *value); /** * Builds an empty :c:type:`z_string_t`. @@ -1164,7 +1164,7 @@ void z_string_empty(z_owned_string_t *str); * Return: * ``0`` if creation successful, ``negative value`` otherwise. */ -int8_t z_string_from_substring(z_owned_string_t *str, const char *value, size_t len); +int8_t z_string_from_substr(z_owned_string_t *str, const char *value, size_t len); /** * Checks if string is empty diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index e2e423a59..f5e20ba7a 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -25,6 +25,7 @@ typedef char *_z_str_t; char *_z_str_clone(const char *src); +char *_z_str_n_clone(const char *src, size_t len); void _z_str_clear(char *src); void _z_str_free(char **src); _Bool _z_str_eq(const char *left, const char *right); @@ -72,6 +73,7 @@ typedef struct { _z_string_t _z_string_null(void); _Bool _z_string_check(const _z_string_t *value); _z_string_t _z_string_make(const char *value); +_z_string_t _z_string_n_make(const char *value, size_t len); _z_string_t _z_string_wrap(char *value); _z_string_t *_z_string_make_as_ptr(const char *value); diff --git a/src/api/api.c b/src/api/api.c index bbe8d88ea..1446daa62 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -837,19 +837,15 @@ const z_loaned_encoding_t *z_reply_err_encoding(const z_loaned_reply_err_t *repl const char *z_string_data(const z_loaned_string_t *str) { return str->val; } size_t z_string_len(const z_loaned_string_t *str) { return str->len; } -int8_t z_string_wrap(z_owned_string_t *str, const char *value) { - str->_val = _z_string_wrap((char *)value); +int8_t z_string_from_str(z_owned_string_t *str, const char *value) { + str->_val = _z_string_make(value); return _Z_RES_OK; } -void z_string_empty(z_owned_string_t *str) { - str->_val.val = NULL; - str->_val.len = 0; -} +void z_string_empty(z_owned_string_t *str) { str->_val = _z_string_null(); } -int8_t z_string_from_substring(z_owned_string_t *str, const char *value, size_t len) { - str->_val.val = (char *)value; - str->_val.len = len; +int8_t z_string_from_substr(z_owned_string_t *str, const char *value, size_t len) { + str->_val = _z_string_n_make(value, len); return _Z_RES_OK; } diff --git a/src/collections/string.c b/src/collections/string.c index a805ed0e1..7ba11aedc 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -36,6 +36,17 @@ _z_string_t _z_string_make(const char *value) { return s; } +_z_string_t _z_string_n_make(const char *value, size_t len) { + _z_string_t s; + s.val = _z_str_n_clone(value, len); + if (s.val == NULL) { + s.len = 0; + } else { + s.len = len; + } + return s; +} + _z_string_t _z_string_wrap(char *value) { _z_string_t s; s.val = value; @@ -190,4 +201,13 @@ char *_z_str_clone(const char *src) { return dst; } +char *_z_str_n_clone(const char *src, size_t len) { + char *dst = (char *)z_malloc(len + 1); + if (dst != NULL) { + _z_str_n_copy(dst, src, len + 1); + } + + return dst; +} + _Bool _z_str_eq(const char *left, const char *right) { return strcmp(left, right) == 0; } From 68e43615a5216b3e5fabd01dfbfe4880ca4524a9 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 3 Jul 2024 17:39:36 +0200 Subject: [PATCH 3/3] Fix _z_str_n_copy --- src/collections/string.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/collections/string.c b/src/collections/string.c index 7ba11aedc..dbe93869a 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -182,12 +182,12 @@ void _z_str_free(char **src) { void _z_str_copy(char *dst, const char *src) { size_t size = _z_str_size(src); - strncpy(dst, src, size); + strncpy(dst, src, size - 1); dst[size - 1] = '\0'; // No matter what, strings are always null-terminated upon copy } void _z_str_n_copy(char *dst, const char *src, size_t size) { - strncpy(dst, src, size); + strncpy(dst, src, size - 1); dst[size - 1] = '\0'; // No matter what, strings are always null-terminated upon copy }