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 3 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
6 changes: 3 additions & 3 deletions include/zenoh-pico/collections/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ _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_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
_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);
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
42 changes: 30 additions & 12 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 @@ -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 @@ -990,7 +1008,7 @@ 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;
}

Expand Down
8 changes: 4 additions & 4 deletions src/collections/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ _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_alias_str(const char *value) {
_z_string_t s;
s._slice = _z_slice_from_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);
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);
return s;
Expand Down Expand Up @@ -94,7 +94,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
36 changes: 18 additions & 18 deletions src/link/endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,36 +292,36 @@ int8_t _z_endpoint_config_from_string(_z_str_intmap_t *strint, _z_string_t *str,
// Call the right configuration parser depending on the protocol
_z_string_t cmp_str = _z_string_null();
#if Z_FEATURE_LINK_TCP == 1
cmp_str = _z_string_from_str(TCP_SCHEMA);
cmp_str = _z_string_alias_str(TCP_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_tcp_config_from_str(strint, p_start);
}
#endif
#if Z_FEATURE_LINK_UDP_UNICAST == 1 || Z_FEATURE_LINK_UDP_MULTICAST == 1
cmp_str = _z_string_from_str(UDP_SCHEMA);
cmp_str = _z_string_alias_str(UDP_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_udp_config_from_str(strint, p_start);
}
#endif
#if Z_FEATURE_LINK_BLUETOOTH == 1
cmp_str = _z_string_from_str(BT_SCHEMA);
cmp_str = _z_string_alias_str(BT_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_bt_config_from_str(strint, p_start);
}
#endif
#if Z_FEATURE_LINK_SERIAL == 1
cmp_str = _z_string_from_str(SERIAL_SCHEMA);
cmp_str = _z_string_alias_str(SERIAL_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_serial_config_from_str(strint, p_start);
}
#endif
#if Z_FEATURE_LINK_WS == 1
cmp_str = _z_string_from_str(WS_SCHEMA);
cmp_str = _z_string_alias_str(WS_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_ws_config_from_str(strint, p_start);
}
#endif
cmp_str = _z_string_from_str(RAWETH_SCHEMA);
cmp_str = _z_string_alias_str(RAWETH_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_raweth_config_from_str(strint, p_start);
}
Expand All @@ -333,36 +333,36 @@ size_t _z_endpoint_config_strlen(const _z_str_intmap_t *s, _z_string_t *proto) {
// Call the right configuration parser depending on the protocol
_z_string_t cmp_str = _z_string_null();
#if Z_FEATURE_LINK_TCP == 1
cmp_str = _z_string_from_str(TCP_SCHEMA);
cmp_str = _z_string_alias_str(TCP_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_tcp_config_strlen(s);
}
#endif
#if Z_FEATURE_LINK_UDP_UNICAST == 1 || Z_FEATURE_LINK_UDP_MULTICAST == 1
cmp_str = _z_string_from_str(UDP_SCHEMA);
cmp_str = _z_string_alias_str(UDP_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_udp_config_strlen(s);
}
#endif
#if Z_FEATURE_LINK_BLUETOOTH == 1
cmp_str = _z_string_from_str(BT_SCHEMA);
cmp_str = _z_string_alias_str(BT_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_bt_config_strlen(s);
}
#endif
#if Z_FEATURE_LINK_SERIAL == 1
cmp_str = _z_string_from_str(SERIAL_SCHEMA);
cmp_str = _z_string_alias_str(SERIAL_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_serial_config_strlen(s);
}
#endif
#if Z_FEATURE_LINK_WS == 1
cmp_str = _z_string_from_str(WS_SCHEMA);
cmp_str = _z_string_alias_str(WS_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_ws_config_strlen(s);
}
#endif
cmp_str = _z_string_from_str(RAWETH_SCHEMA);
cmp_str = _z_string_alias_str(RAWETH_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_raweth_config_strlen(s);
}
Expand All @@ -374,36 +374,36 @@ char *_z_endpoint_config_to_string(const _z_str_intmap_t *s, const _z_string_t *
_z_string_t cmp_str = _z_string_null();

#if Z_FEATURE_LINK_TCP == 1
cmp_str = _z_string_from_str(TCP_SCHEMA);
cmp_str = _z_string_alias_str(TCP_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_tcp_config_to_str(s);
}
#endif
#if Z_FEATURE_LINK_UDP_UNICAST == 1 || Z_FEATURE_LINK_UDP_MULTICAST == 1
cmp_str = _z_string_from_str(UDP_SCHEMA);
cmp_str = _z_string_alias_str(UDP_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_udp_config_to_str(s);
}
#endif
#if Z_FEATURE_LINK_BLUETOOTH == 1
cmp_str = _z_string_from_str(BT_SCHEMA);
cmp_str = _z_string_alias_str(BT_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_bt_config_to_str(s);
}
#endif
#if Z_FEATURE_LINK_SERIAL == 1
cmp_str = _z_string_from_str(SERIAL_SCHEMA);
cmp_str = _z_string_alias_str(SERIAL_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_serial_config_to_str(s);
}
#endif
#if Z_FEATURE_LINK_WS == 1
cmp_str = _z_string_from_str(WS_SCHEMA);
cmp_str = _z_string_alias_str(WS_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_ws_config_to_str(s);
}
#endif
cmp_str = _z_string_from_str(RAWETH_SCHEMA);
cmp_str = _z_string_alias_str(RAWETH_SCHEMA);
if (_z_string_equals(proto, &cmp_str)) {
return _z_raweth_config_to_str(s);
}
Expand Down
2 changes: 1 addition & 1 deletion src/link/multicast/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static char *__z_parse_address_segment_udp_multicast(_z_string_t *address) {
int8_t _z_endpoint_udp_multicast_valid(_z_endpoint_t *endpoint) {
int8_t ret = _Z_RES_OK;

_z_string_t udp_str = _z_string_from_str(UDP_SCHEMA);
_z_string_t udp_str = _z_string_alias_str(UDP_SCHEMA);
if (!_z_string_equals(&endpoint->_locator._protocol, &udp_str)) {
ret = _Z_ERR_CONFIG_LOCATOR_INVALID;
}
Expand Down
2 changes: 1 addition & 1 deletion src/link/unicast/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static char *__z_parse_address_segment_tcp(_z_string_t *address) {
int8_t _z_endpoint_tcp_valid(_z_endpoint_t *endpoint) {
int8_t ret = _Z_RES_OK;

_z_string_t tcp_str = _z_string_from_str(TCP_SCHEMA);
_z_string_t tcp_str = _z_string_alias_str(TCP_SCHEMA);
if (!_z_string_equals(&endpoint->_locator._protocol, &tcp_str)) {
ret = _Z_ERR_CONFIG_LOCATOR_INVALID;
}
Expand Down
2 changes: 1 addition & 1 deletion src/link/unicast/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static char *__z_parse_address_segment_udp_unicast(_z_string_t *address) {
int8_t _z_endpoint_udp_unicast_valid(_z_endpoint_t *endpoint) {
int8_t ret = _Z_RES_OK;

_z_string_t udp_str = _z_string_from_str(UDP_SCHEMA);
_z_string_t udp_str = _z_string_alias_str(UDP_SCHEMA);
if (!_z_string_equals(&endpoint->_locator._protocol, &udp_str)) {
ret = _Z_ERR_CONFIG_LOCATOR_INVALID;
}
Expand Down
Loading
Loading