Skip to content

Commit

Permalink
Add missed z_string_* methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Jul 2, 2024
1 parent 26b5bc1 commit c04a633
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c04a633

Please sign in to comment.