Skip to content

Commit

Permalink
Add missing view_keyexpr methods (#605)
Browse files Browse the repository at this point in the history
* feat: rename _z_string_from methods

* feat: add _z_keyexpr_from_substr

* feat: add missing methods and add canon check

* refactor: rename string and slice functions

* fix: remove null terminated from string function

* refactor: rename the custom_deleter functions

* feat: call from_substr in from_str functions
  • Loading branch information
jean-roland authored Aug 27, 2024
1 parent 6861694 commit 9834914
Show file tree
Hide file tree
Showing 32 changed files with 184 additions and 128 deletions.
55 changes: 48 additions & 7 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int8_t z_view_string_from_str(z_view_string_t *str, const char *value);
/**
* Builds a :c:type:`z_keyexpr_t` from a null-terminated string.
* It is a loaned key expression that aliases ``name``.
* Unlike it's counterpart in zenoh-c, this function does not test passed expression to correctness.
* This function will fail if the string is not in canon form.
*
* Parameters:
* name: Pointer to string representation of the keyexpr as a null terminated string.
Expand All @@ -66,18 +66,15 @@ int8_t z_view_keyexpr_from_str(z_view_keyexpr_t *keyexpr, const char *name);
* Parameters:
* name: Pointer to string representation of the keyexpr as a null terminated string.
* keyexpr: Pointer to an uninitialized :c:type:`z_view_keyexpr_t`.
*
* Return:
* ``0`` if creation successful, ``negative value`` otherwise.
*/
int8_t z_view_keyexpr_from_str_unchecked(z_view_keyexpr_t *keyexpr, const char *name);
void z_view_keyexpr_from_str_unchecked(z_view_keyexpr_t *keyexpr, const char *name);

/**
* Builds a :c:type:`z_view_keyexpr_t` from a null-terminated string with auto canonization.
* It is a loaned key expression that aliases ``name``.
* The string is canonized in-place before being passed to keyexpr, possibly shortening it by modifying len.
* May SEGFAULT if `name` is NULL or lies in read-only memory (as values initialized with string litterals do).
* `name` must outlive the constucted key expression.
* May SEGFAULT if `name` is NULL or lies in read-only memory (as values initialized with string literals do).
* `name` must outlive the constructed key expression.
*
* Parameters:
* name: Pointer to string representation of the keyexpr as a null terminated string.
Expand All @@ -88,6 +85,50 @@ int8_t z_view_keyexpr_from_str_unchecked(z_view_keyexpr_t *keyexpr, const char *
*/
int8_t z_view_keyexpr_from_str_autocanonize(z_view_keyexpr_t *keyexpr, char *name);

/**
* Builds a :c:type:`z_keyexpr_t` by aliasing a substring.
* It is a loaned key expression that aliases ``name``.
* This function will fail if the string is not in canon form.
*
* Parameters:
* keyexpr: Pointer to an uninitialized :c:type:`z_view_keyexpr_t`.
* name: Pointer to string representation of the keyexpr.
* len: Size of the string.
*
* Return:
* ``0`` if creation successful, ``negative value`` otherwise.
*/
z_result_t z_view_keyexpr_from_substr(z_view_keyexpr_t *keyexpr, const char *name, size_t len);

/**
* Builds a :c:type:`z_view_keyexpr_t` from a substring with auto canonization.
* It is a loaned key expression that aliases ``name``.
* The string is canonized in-place before being passed to keyexpr, possibly shortening it by modifying len.
* May SEGFAULT if `name` is NULL or lies in read-only memory (as values initialized with string literals do).
* `name` must outlive the constructed key expression.
*
* Parameters:
* keyexpr: Pointer to an uninitialized :c:type:`z_view_keyexpr_t`.
* name: Pointer to string representation of the keyexpr.
* len: Pointer to the size of the string.
*
* Return:
* ``0`` if creation successful, ``negative value`` otherwise.
*/
z_result_t z_view_keyexpr_from_substr_autocanonize(z_view_keyexpr_t *keyexpr, char *name, size_t *len);

/**
* Builds a :c:type:`z_keyexpr_t` from a substring.
* It is a loaned key expression that aliases ``name``.
* Input key expression is not checked for correctness.
*
* Parameters:
* keyexpr: Pointer to an uninitialized :c:type:`z_view_keyexpr_t`.
* name: Pointer to string representation of the keyexpr.
* len: Size of the string.
*/
void z_view_keyexpr_from_substr_unchecked(z_view_keyexpr_t *keyexpr, const char *name, size_t len);

/**
* Gets a null-terminated string view from a :c:type:`z_keyexpr_t`.
*
Expand Down
2 changes: 1 addition & 1 deletion include/zenoh-pico/collections/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ _z_slice_t _z_slice_empty(void);
inline static _Bool _z_slice_check(const _z_slice_t *slice) { return slice->start != NULL; }
int8_t _z_slice_init(_z_slice_t *bs, size_t capacity);
_z_slice_t _z_slice_make(size_t capacity);
_z_slice_t _z_slice_from_buf(const uint8_t *bs, size_t len);
_z_slice_t _z_slice_alias_buf(const uint8_t *bs, size_t len);
_z_slice_t _z_slice_from_buf_custom_deleter(const uint8_t *p, size_t len, _z_delete_context_t dc);
_z_slice_t _z_slice_copy_from_buf(const uint8_t *bs, size_t len);
_z_slice_t _z_slice_steal(_z_slice_t *b);
Expand Down
10 changes: 5 additions & 5 deletions include/zenoh-pico/collections/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ 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_from_str(const char *value);
_z_string_t _z_string_from_substr(const char *value, size_t len);
_z_string_t _z_string_copy_from_str(const char *value);
_z_string_t _z_string_copy_from_substr(const char *value, size_t len);
_z_string_t *_z_string_copy_from_str_as_ptr(const char *value);
_z_string_t _z_string_alias_str(const char *value);
_z_string_t _z_string_alias_substr(const char *value, size_t len);
_z_string_t _z_string_from_str_custom_deleter(char *value, _z_delete_context_t c);
_z_string_t *_z_string_make_as_ptr(const char *value);
_Bool _z_string_is_empty(const _z_string_t *s);
const char *_z_string_rchr(_z_string_t *str, char filter);
char *_z_string_pbrk(_z_string_t *str, const char *filter);
Expand Down
1 change: 1 addition & 0 deletions include/zenoh-pico/protocol/keyexpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ _Bool _z_keyexpr_suffix_equals(const _z_keyexpr_t *left, const _z_keyexpr_t *rig

/*------------------ clone/Copy/Free helpers ------------------*/
_z_keyexpr_t _z_keyexpr_from_string(uint16_t rid, _z_string_t *str);
_z_keyexpr_t _z_keyexpr_from_substr(uint16_t rid, const char *str, size_t len);
int8_t _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src);
_z_keyexpr_t _z_keyexpr_duplicate(_z_keyexpr_t src);
_z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src);
Expand Down
46 changes: 29 additions & 17 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
/********* Data Types Handlers *********/

int8_t z_view_string_from_str(z_view_string_t *str, const char *value) {
str->_val = _z_string_from_str((char *)value);
str->_val = _z_string_alias_str((char *)value);
return _Z_RES_OK;
}

Expand All @@ -65,22 +65,34 @@ 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); }

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) {
if (_z_keyexpr_is_canon(name, len) != Z_KEYEXPR_CANON_SUCCESS) {
return Z_EINVAL;
}
keyexpr->_val = _z_keyexpr_from_substr(0, name, len);
return _Z_RES_OK;
}

int8_t z_view_keyexpr_from_str(z_view_keyexpr_t *keyexpr, const char *name) {
keyexpr->_val = _z_rname(name);
size_t name_len = strlen(name);
return z_view_keyexpr_from_substr(keyexpr, name, name_len);
}

z_result_t z_view_keyexpr_from_substr_autocanonize(z_view_keyexpr_t *keyexpr, char *name, size_t *len) {
_Z_RETURN_IF_ERR(z_keyexpr_canonize(name, len));
keyexpr->_val = _z_keyexpr_from_substr(0, name, *len);
return _Z_RES_OK;
}

int8_t z_view_keyexpr_from_str_autocanonize(z_view_keyexpr_t *keyexpr, char *name) {
size_t name_len = strlen(name);
_Z_RETURN_IF_ERR(z_keyexpr_canonize(name, &name_len));
keyexpr->_val = _z_rname(NULL);
keyexpr->_val._suffix = _z_string_from_substr(name, name_len);
return _Z_RES_OK;
return z_view_keyexpr_from_substr_autocanonize(keyexpr, name, &name_len);
}

int8_t z_view_keyexpr_from_str_unchecked(z_view_keyexpr_t *keyexpr, const char *name) {
keyexpr->_val = _z_rname(name);
return _Z_RES_OK;
void z_view_keyexpr_from_substr_unchecked(z_view_keyexpr_t *keyexpr, const char *name, size_t len) {
keyexpr->_val = _z_keyexpr_from_substr(0, name, len);
}

int8_t z_keyexpr_as_view_string(const z_loaned_keyexpr_t *keyexpr, z_view_string_t *s) {
Expand Down Expand Up @@ -314,7 +326,7 @@ static int8_t _z_encoding_convert_into_string(const z_loaned_encoding_t *encodin
(void)strncat(value, _z_string_data(&encoding->schema), _z_string_len(&encoding->schema));
}
// Fill container
s->_val = _z_string_from_str(value);
s->_val = _z_string_alias_str(value);
return _Z_RES_OK;
}

Expand Down Expand Up @@ -361,7 +373,7 @@ int8_t z_encoding_set_schema_from_substr(z_loaned_encoding_t *encoding, const ch
if (schema == NULL && len > 0) {
return _Z_ERR_INVALID;
}
encoding->schema = _z_string_n_make(schema, len);
encoding->schema = _z_string_copy_from_substr(schema, len);
if (_z_string_len(&encoding->schema) != len) {
return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
}
Expand Down Expand Up @@ -530,7 +542,7 @@ int8_t z_bytes_from_buf(z_owned_bytes_t *bytes, uint8_t *data, size_t len, void

int8_t z_bytes_from_static_buf(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) {
z_owned_slice_t s;
s._val = _z_slice_from_buf(data, len);
s._val = _z_slice_alias_buf(data, len);
return z_bytes_from_slice(bytes, z_slice_move(&s));
}

Expand Down Expand Up @@ -564,7 +576,7 @@ int8_t z_bytes_from_str(z_owned_bytes_t *bytes, char *value, void (*deleter)(voi

int8_t z_bytes_from_static_str(z_owned_bytes_t *bytes, const char *value) {
z_owned_string_t s;
s._val = _z_string_from_str(value);
s._val = _z_string_alias_str(value);
return z_bytes_from_string(bytes, z_string_move(&s));
}

Expand Down Expand Up @@ -676,7 +688,7 @@ z_query_consolidation_t z_query_consolidation_none(void) {
z_query_consolidation_t z_query_consolidation_default(void) { return z_query_consolidation_auto(); }

void z_query_parameters(const z_loaned_query_t *query, z_view_string_t *parameters) {
parameters->_val = _z_string_from_str(_Z_RC_IN_VAL(query)->_parameters);
parameters->_val = _z_string_alias_str(_Z_RC_IN_VAL(query)->_parameters);
}

const z_loaned_bytes_t *z_query_attachment(const z_loaned_query_t *query) { return &_Z_RC_IN_VAL(query)->attachment; }
Expand Down Expand Up @@ -845,7 +857,7 @@ int8_t z_scout(z_moved_config_t *config, z_moved_closure_hello_t *callback, cons
if (opt_as_str == NULL) {
opt_as_str = (char *)Z_CONFIG_MULTICAST_LOCATOR_DEFAULT;
}
_z_string_t mcast_locator = _z_string_from_str(opt_as_str);
_z_string_t mcast_locator = _z_string_alias_str(opt_as_str);

uint32_t timeout;
if (options != NULL) {
Expand Down Expand Up @@ -981,7 +993,7 @@ const char *z_string_data(const z_loaned_string_t *str) { return _z_string_data(
size_t z_string_len(const z_loaned_string_t *str) { return _z_string_len(str); }

int8_t z_string_copy_from_str(z_owned_string_t *str, const char *value) {
str->_val = _z_string_make(value);
str->_val = _z_string_copy_from_str(value);
if (str->_val._slice.start == NULL && value != NULL) {
return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
}
Expand All @@ -997,7 +1009,7 @@ int8_t z_string_from_str(z_owned_string_t *str, char *value, void (*deleter)(voi
void z_string_empty(z_owned_string_t *str) { str->_val = _z_string_null(); }

int8_t z_string_copy_from_substr(z_owned_string_t *str, const char *value, size_t len) {
str->_val = _z_string_n_make(value, len);
str->_val = _z_string_copy_from_substr(value, len);
return _Z_RES_OK;
}

Expand Down
2 changes: 1 addition & 1 deletion src/collections/bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ int8_t _z_bytes_from_double(_z_bytes_t *b, double val) { return _z_bytes_from_bu
_z_slice_t _z_bytes_try_get_contiguous(const _z_bytes_t *bs) {
if (_z_bytes_num_slices(bs) == 1) {
_z_arc_slice_t *arc_s = _z_bytes_get_slice(bs, 0);
return _z_slice_from_buf(_z_arc_slice_data(arc_s), _z_arc_slice_len(arc_s));
return _z_slice_alias_buf(_z_arc_slice_data(arc_s), _z_arc_slice_len(arc_s));
}
return _z_slice_empty();
}
Expand Down
4 changes: 2 additions & 2 deletions src/collections/slice.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ _z_slice_t _z_slice_alias(const _z_slice_t *bs) {
return alias;
}

_z_slice_t _z_slice_from_buf(const uint8_t *p, size_t len) {
_z_slice_t _z_slice_alias_buf(const uint8_t *p, size_t len) {
return _z_slice_from_buf_custom_deleter(p, len, _z_delete_context_null());
}

_z_slice_t _z_slice_copy_from_buf(const uint8_t *p, size_t len) {
_z_slice_t bs = _z_slice_from_buf(p, len);
_z_slice_t bs = _z_slice_alias_buf(p, len);
return _z_slice_duplicate(&bs);
}

Expand Down
28 changes: 11 additions & 17 deletions src/collections/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,27 @@ _z_string_t _z_string_null(void) {

_Bool _z_string_check(const _z_string_t *value) { return !_z_slice_is_empty(&value->_slice); }

_z_string_t _z_string_make(const char *value) {
_z_string_t _z_string_copy_from_str(const char *value) {
_z_string_t s;
s._slice = _z_slice_copy_from_buf((uint8_t *)value, strlen(value));
return s;
}

_z_string_t _z_string_n_make(const char *value, size_t len) {
_z_string_t _z_string_copy_from_substr(const char *value, size_t len) {
_z_string_t s;
char *c = _z_str_n_clone(value, len);

if (c == NULL) {
return _z_string_null();
} else {
s._slice = _z_slice_from_buf_custom_deleter((const uint8_t *)c, len, _z_delete_context_default());
return s;
}
s._slice = _z_slice_copy_from_buf((uint8_t *)value, len);
return s;
}

_z_string_t _z_string_from_str(const char *value) {
_z_string_t _z_string_alias_str(const char *value) {
_z_string_t s;
s._slice = _z_slice_from_buf((const uint8_t *)(value), strlen(value));
s._slice = _z_slice_alias_buf((const uint8_t *)(value), strlen(value));
return s;
}

_z_string_t _z_string_from_substr(const char *value, size_t len) {
_z_string_t _z_string_alias_substr(const char *value, size_t len) {
_z_string_t s;
s._slice = _z_slice_from_buf((const uint8_t *)(value), len);
s._slice = _z_slice_alias_buf((const uint8_t *)(value), len);
return s;
}

Expand All @@ -63,9 +57,9 @@ _z_string_t _z_string_from_str_custom_deleter(char *value, _z_delete_context_t c
return s;
}

_z_string_t *_z_string_make_as_ptr(const char *value) {
_z_string_t *_z_string_copy_from_str_as_ptr(const char *value) {
_z_string_t *s = (_z_string_t *)z_malloc(sizeof(_z_string_t));
*s = _z_string_make(value);
*s = _z_string_copy_from_str(value);
if (_z_slice_is_empty(&s->_slice) && value != NULL) {
z_free(s);
return NULL;
Expand Down Expand Up @@ -94,7 +88,7 @@ _z_string_t _z_string_alias(const _z_string_t *str) {
return alias;
}

void _z_string_move_str(_z_string_t *dst, char *src) { *dst = _z_string_from_str(src); }
void _z_string_move_str(_z_string_t *dst, char *src) { *dst = _z_string_alias_str(src); }

void _z_string_reset(_z_string_t *str) { _z_slice_reset(&str->_slice); }

Expand Down
Loading

0 comments on commit 9834914

Please sign in to comment.