Skip to content

Commit

Permalink
Remove wrap and make z_owned_string_t really owned
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Jul 3, 2024
1 parent c04a633 commit 3f051e6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
4 changes: 2 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions include/zenoh-pico/collections/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
14 changes: 5 additions & 9 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
20 changes: 20 additions & 0 deletions src/collections/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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; }

0 comments on commit 3f051e6

Please sign in to comment.