diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 93dfa50ec..f0a5c1c19 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -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. @@ -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. @@ -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`. * diff --git a/src/api/api.c b/src/api/api.c index 3fba548c8..0062bd27e 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -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) { - 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) { 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;