Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing view_keyexpr methods #605

Merged
merged 7 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions include/zenoh-pico/collections/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ _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_from_buf_custom_deleter(const uint8_t *p, size_t len, _z_delete_context_t dc);
_z_slice_t _z_slice_alias_buf(const uint8_t *bs, size_t len);
_z_slice_t _z_slice_alias_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);
_z_slice_t _z_slice_alias(const _z_slice_t *bs);
Expand Down
12 changes: 6 additions & 6 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);
jean-roland marked this conversation as resolved.
Show resolved Hide resolved
_z_string_t _z_string_from_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);
_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_alias_str_custom_deleter(char *value, _z_delete_context_t c);
jean-roland marked this conversation as resolved.
Show resolved Hide resolved
_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
54 changes: 36 additions & 18 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 @@ -66,23 +66,41 @@ 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_view_keyexpr_from_str(z_view_keyexpr_t *keyexpr, const char *name) {
jean-roland marked this conversation as resolved.
Show resolved Hide resolved
keyexpr->_val = _z_rname(name);
size_t name_len = strlen(name);
if (_z_keyexpr_is_canon(name, name_len) != Z_KEYEXPR_CANON_SUCCESS) {
return Z_EINVAL;
}
keyexpr->_val = _z_keyexpr_from_substr(0, name, name_len);
return _Z_RES_OK;
}

int8_t z_view_keyexpr_from_str_autocanonize(z_view_keyexpr_t *keyexpr, char *name) {
jean-roland marked this conversation as resolved.
Show resolved Hide resolved
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);
keyexpr->_val = _z_keyexpr_from_substr(0, name, name_len);
return _Z_RES_OK;
}

int8_t z_view_keyexpr_from_str_unchecked(z_view_keyexpr_t *keyexpr, const char *name) {
keyexpr->_val = _z_rname(name);
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;
}

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;
}

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) {
s->_val = _z_string_alias(&keyexpr->_suffix);
return _Z_RES_OK;
Expand Down Expand Up @@ -314,7 +332,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 +379,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 All @@ -387,7 +405,7 @@ int8_t z_slice_copy_from_buf(z_owned_slice_t *slice, const uint8_t *data, size_t

int8_t z_slice_from_buf(z_owned_slice_t *slice, uint8_t *data, size_t len, void (*deleter)(void *data, void *context),
void *context) {
slice->_val = _z_slice_from_buf_custom_deleter(data, len, _z_delete_context_create(deleter, context));
slice->_val = _z_slice_alias_buf_custom_deleter(data, len, _z_delete_context_create(deleter, context));
return _Z_RES_OK;
}

Expand Down Expand Up @@ -524,13 +542,13 @@ int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const z_loaned_slice
int8_t z_bytes_from_buf(z_owned_bytes_t *bytes, uint8_t *data, size_t len, void (*deleter)(void *data, void *context),
void *context) {
z_owned_slice_t s;
s._val = _z_slice_from_buf_custom_deleter(data, len, _z_delete_context_create(deleter, context));
s._val = _z_slice_alias_buf_custom_deleter(data, len, _z_delete_context_create(deleter, context));
return z_bytes_from_slice(bytes, z_slice_move(&s));
}

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 @@ -558,13 +576,13 @@ int8_t z_bytes_serialize_from_string(z_owned_bytes_t *bytes, const z_loaned_stri
int8_t z_bytes_from_str(z_owned_bytes_t *bytes, char *value, void (*deleter)(void *value, void *context),
void *context) {
z_owned_string_t s;
s._val = _z_string_from_str_custom_deleter(value, _z_delete_context_create(deleter, context));
s._val = _z_string_alias_str_custom_deleter(value, _z_delete_context_create(deleter, context));
return z_bytes_from_string(bytes, z_string_move(&s));
}

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 +694,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 +863,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 +999,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 @@ -990,14 +1008,14 @@ int8_t z_string_copy_from_str(z_owned_string_t *str, const char *value) {

int8_t z_string_from_str(z_owned_string_t *str, char *value, void (*deleter)(void *value, void *context),
void *context) {
str->_val = _z_string_from_str_custom_deleter(value, _z_delete_context_create(deleter, context));
str->_val = _z_string_alias_str_custom_deleter(value, _z_delete_context_create(deleter, context));
return _Z_RES_OK;
}

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
8 changes: 4 additions & 4 deletions src/collections/slice.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ _z_slice_t _z_slice_make(size_t capacity) {
return bs;
}

_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_alias_buf_custom_deleter(const uint8_t *p, size_t len, _z_delete_context_t dc) {
_z_slice_t bs;
bs.start = p;
bs.len = len;
Expand All @@ -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) {
return _z_slice_from_buf_custom_deleter(p, len, _z_delete_context_null());
_z_slice_t _z_slice_alias_buf(const uint8_t *p, size_t len) {
return _z_slice_alias_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
34 changes: 14 additions & 20 deletions src/collections/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,45 +27,39 @@ _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;
}

_z_string_t _z_string_from_str_custom_deleter(char *value, _z_delete_context_t c) {
_z_string_t _z_string_alias_str_custom_deleter(char *value, _z_delete_context_t c) {
_z_string_t s;
s._slice = _z_slice_from_buf_custom_deleter((const uint8_t *)(value), strlen(value), c);
s._slice = _z_slice_alias_buf_custom_deleter((const uint8_t *)(value), strlen(value), 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 Expand Up @@ -134,7 +128,7 @@ _z_string_t _z_string_convert_bytes(const _z_slice_t *bs) {
} else {
len = 0;
}
s._slice = _z_slice_from_buf_custom_deleter((const uint8_t *)s_val, len, _z_delete_context_default());
s._slice = _z_slice_alias_buf_custom_deleter((const uint8_t *)s_val, len, _z_delete_context_default());

return s;
}
Expand Down
Loading
Loading