Skip to content

Commit

Permalink
feat: add missing methods and add canon check
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Aug 26, 2024
1 parent 3d5b303 commit 480dd6e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 12 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
28 changes: 23 additions & 5 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 480dd6e

Please sign in to comment.