From 22de2dee0d79e22443b2b8ccee4e13c70dffd66b Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 23 Aug 2024 11:45:28 +0200 Subject: [PATCH 1/7] feat: rename _z_string_from methods --- include/zenoh-pico/collections/string.h | 6 +-- src/api/api.c | 14 +++---- src/collections/string.c | 8 ++-- src/link/endpoint.c | 36 ++++++++--------- src/link/multicast/udp.c | 2 +- src/link/unicast/tcp.c | 2 +- src/link/unicast/udp.c | 2 +- src/link/unicast/ws.c | 2 +- src/net/encoding.c | 2 +- src/net/session.c | 2 +- src/protocol/keyexpr.c | 2 +- src/session/scout.c | 2 +- src/transport/raweth/link.c | 4 +- tests/z_client_test.c | 2 +- tests/z_endpoint_test.c | 52 ++++++++++++------------- tests/z_keyexpr_test.c | 2 +- tests/z_msgcodec_test.c | 2 +- 17 files changed, 71 insertions(+), 71 deletions(-) diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index 48d65e9ac..3e5427305 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -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); -_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); _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); diff --git a/src/api/api.c b/src/api/api.c index 009e8d5aa..3fba548c8 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -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; } @@ -314,7 +314,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; } @@ -558,13 +558,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)); } @@ -676,7 +676,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; } @@ -845,7 +845,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) { @@ -990,7 +990,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; } diff --git a/src/collections/string.c b/src/collections/string.c index 0a4b08314..4a984a231 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -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; @@ -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); } diff --git a/src/link/endpoint.c b/src/link/endpoint.c index 317c0b529..400f2d1e6 100644 --- a/src/link/endpoint.c +++ b/src/link/endpoint.c @@ -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); } @@ -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); } @@ -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); } diff --git a/src/link/multicast/udp.c b/src/link/multicast/udp.c index 675cc3792..7169d0ac3 100644 --- a/src/link/multicast/udp.c +++ b/src/link/multicast/udp.c @@ -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; } diff --git a/src/link/unicast/tcp.c b/src/link/unicast/tcp.c index 38254a178..1d1038711 100644 --- a/src/link/unicast/tcp.c +++ b/src/link/unicast/tcp.c @@ -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; } diff --git a/src/link/unicast/udp.c b/src/link/unicast/udp.c index fbb1520ed..8f38830bd 100644 --- a/src/link/unicast/udp.c +++ b/src/link/unicast/udp.c @@ -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; } diff --git a/src/link/unicast/ws.c b/src/link/unicast/ws.c index 630678d7c..970744051 100644 --- a/src/link/unicast/ws.c +++ b/src/link/unicast/ws.c @@ -76,7 +76,7 @@ char *__z_parse_address_segment_ws(_z_string_t *address) { int8_t _z_endpoint_ws_valid(_z_endpoint_t *endpoint) { int8_t ret = _Z_RES_OK; - _z_string_t str = _z_string_from_str(WS_SCHEMA); + _z_string_t str = _z_string_alias_str(WS_SCHEMA); if (_z_string_equals(&endpoint->_locator._protocol, &str)) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } diff --git a/src/net/encoding.c b/src/net/encoding.c index ae28bca39..84198505d 100644 --- a/src/net/encoding.c +++ b/src/net/encoding.c @@ -35,7 +35,7 @@ int8_t _z_encoding_make(_z_encoding_t *encoding, uint16_t id, const char *schema _z_encoding_t _z_encoding_wrap(uint16_t id, const char *schema) { return (_z_encoding_t){.id = id, - .schema = (schema == NULL) ? _z_string_null() : _z_string_from_str((char *)schema)}; + .schema = (schema == NULL) ? _z_string_null() : _z_string_alias_str((char *)schema)}; } _z_encoding_t _z_encoding_null(void) { return _z_encoding_wrap(_Z_ENCODING_ID_DEFAULT, NULL); } diff --git a/src/net/session.c b/src/net/session.c index 8a60bcb41..68df016fb 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -79,7 +79,7 @@ int8_t _z_open(_z_session_rc_t *zn, _z_config_t *config) { 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); opt_as_str = _z_config_get(config, Z_CONFIG_SCOUTING_TIMEOUT_KEY); if (opt_as_str == NULL) { diff --git a/src/protocol/keyexpr.c b/src/protocol/keyexpr.c index 2a255a0d9..280635e49 100644 --- a/src/protocol/keyexpr.c +++ b/src/protocol/keyexpr.c @@ -28,7 +28,7 @@ _z_keyexpr_t _z_rid_with_suffix(uint16_t rid, const char *suffix) { return (_z_keyexpr_t){ ._id = rid, ._mapping = _z_keyexpr_mapping(_Z_KEYEXPR_MAPPING_LOCAL), - ._suffix = (suffix == NULL) ? _z_string_null() : _z_string_from_str(suffix), + ._suffix = (suffix == NULL) ? _z_string_null() : _z_string_alias_str(suffix), }; } diff --git a/src/session/scout.c b/src/session/scout.c index 0430e4f07..7f11fb0de 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -34,7 +34,7 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, _z_string_t *locator, unsi err = _z_endpoint_from_string(&ep, locator); #if Z_FEATURE_SCOUTING_UDP == 1 - _z_string_t cmp_str = _z_string_from_str(UDP_SCHEMA); + _z_string_t cmp_str = _z_string_alias_str(UDP_SCHEMA); if ((err == _Z_RES_OK) && _z_string_equals(&ep._locator._protocol, &cmp_str)) { _z_endpoint_clear(&ep); } else diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index 056f44178..4c6a82b85 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -337,7 +337,7 @@ static _Bool _z_valid_address_raweth_inner(const _z_string_t *address) { } static _Bool _z_valid_address_raweth(const char *address) { - _z_string_t addr_str = _z_string_from_str(address); + _z_string_t addr_str = _z_string_alias_str(address); return _z_valid_address_raweth_inner(&addr_str); } @@ -459,7 +459,7 @@ int8_t _z_endpoint_raweth_valid(_z_endpoint_t *endpoint) { int8_t ret = _Z_RES_OK; // Check the root - _z_string_t str_cmp = _z_string_from_str(RAWETH_SCHEMA); + _z_string_t str_cmp = _z_string_alias_str(RAWETH_SCHEMA); if (!_z_string_equals(&endpoint->_locator._protocol, &str_cmp)) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } diff --git a/tests/z_client_test.c b/tests/z_client_test.c index a8a03cc0a..7fe4cf56c 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -100,7 +100,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { snprintf(res, 64, "%s%u", uri, *(unsigned int *)arg); printf(">> Received data: %s\t(%u/%u)\n", res, datas, total); - _z_string_t res_str = _z_string_from_str(res); + _z_string_t res_str = _z_string_alias_str(res); z_view_string_t k_str; z_keyexpr_as_view_string(z_sample_keyexpr(sample), &k_str); z_owned_slice_t value; diff --git a/tests/z_endpoint_test.c b/tests/z_endpoint_test.c index 41ecd677f..4252b0665 100644 --- a/tests/z_endpoint_test.c +++ b/tests/z_endpoint_test.c @@ -30,36 +30,36 @@ int main(void) { _z_locator_t lc; - _z_string_t str = _z_string_from_str("tcp/127.0.0.1:7447"); + _z_string_t str = _z_string_alias_str("tcp/127.0.0.1:7447"); assert(_z_locator_from_string(&lc, &str) == _Z_RES_OK); - str = _z_string_from_str("tcp"); + str = _z_string_alias_str("tcp"); assert(_z_string_equals(&lc._protocol, &str) == true); - str = _z_string_from_str("127.0.0.1:7447"); + str = _z_string_alias_str("127.0.0.1:7447"); assert(_z_string_equals(&lc._address, &str) == true); assert(_z_str_intmap_is_empty(&lc._metadata) == true); _z_locator_clear(&lc); - str = _z_string_from_str(""); + str = _z_string_alias_str(""); assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID); - str = _z_string_from_str("/"); + str = _z_string_alias_str("/"); assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID); - str = _z_string_from_str("tcp"); + str = _z_string_alias_str("tcp"); assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID); - str = _z_string_from_str("tcp/"); + str = _z_string_alias_str("tcp/"); assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID); - str = _z_string_from_str("127.0.0.1:7447"); + str = _z_string_alias_str("127.0.0.1:7447"); assert(_z_locator_from_string(&lc, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID); - str = _z_string_from_str("tcp/127.0.0.1:7447?"); + str = _z_string_alias_str("tcp/127.0.0.1:7447?"); assert(_z_locator_from_string(&lc, &str) == _Z_RES_OK); // No metadata defined so far... but this is a valid syntax in principle - str = _z_string_from_str("tcp/127.0.0.1:7447?invalid=ctrl"); + str = _z_string_alias_str("tcp/127.0.0.1:7447?invalid=ctrl"); assert(_z_locator_from_string(&lc, &str) == _Z_RES_OK); // Endpoint @@ -67,45 +67,45 @@ int main(void) { _z_endpoint_t ep; - str = _z_string_from_str("tcp/127.0.0.1:7447"); + str = _z_string_alias_str("tcp/127.0.0.1:7447"); assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK); - str = _z_string_from_str("tcp"); + str = _z_string_alias_str("tcp"); assert(_z_string_equals(&ep._locator._protocol, &str) == true); - str = _z_string_from_str("127.0.0.1:7447"); + str = _z_string_alias_str("127.0.0.1:7447"); assert(_z_string_equals(&ep._locator._address, &str) == true); assert(_z_str_intmap_is_empty(&ep._locator._metadata) == true); assert(_z_str_intmap_is_empty(&ep._config) == true); _z_endpoint_clear(&ep); - str = _z_string_from_str(""); + str = _z_string_alias_str(""); assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID); - str = _z_string_from_str("/"); + str = _z_string_alias_str("/"); assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID); - str = _z_string_from_str("tcp"); + str = _z_string_alias_str("tcp"); assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID); - str = _z_string_from_str("tcp/"); + str = _z_string_alias_str("tcp/"); assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID); - str = _z_string_from_str("127.0.0.1:7447"); + str = _z_string_alias_str("127.0.0.1:7447"); assert(_z_endpoint_from_string(&ep, &str) == _Z_ERR_CONFIG_LOCATOR_INVALID); - str = _z_string_from_str("tcp/127.0.0.1:7447?"); + str = _z_string_alias_str("tcp/127.0.0.1:7447?"); assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK); // No metadata defined so far... but this is a valid syntax in principle - str = _z_string_from_str("tcp/127.0.0.1:7447?invalid=ctrl"); + str = _z_string_alias_str("tcp/127.0.0.1:7447?invalid=ctrl"); assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK); - str = _z_string_from_str("udp/127.0.0.1:7447#iface=eth0"); + str = _z_string_alias_str("udp/127.0.0.1:7447#iface=eth0"); assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK); - str = _z_string_from_str("udp"); + str = _z_string_alias_str("udp"); assert(_z_string_equals(&ep._locator._protocol, &str) == true); - str = _z_string_from_str("127.0.0.1:7447"); + str = _z_string_alias_str("127.0.0.1:7447"); assert(_z_string_equals(&ep._locator._address, &str) == true); assert(_z_str_intmap_is_empty(&ep._locator._metadata) == true); assert(_z_str_intmap_len(&ep._config) == 1); @@ -114,13 +114,13 @@ int main(void) { (void)(p); _z_endpoint_clear(&ep); - str = _z_string_from_str("udp/127.0.0.1:7447#invalid=eth0"); + str = _z_string_alias_str("udp/127.0.0.1:7447#invalid=eth0"); assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK); - str = _z_string_from_str("udp/127.0.0.1:7447?invalid=ctrl#iface=eth0"); + str = _z_string_alias_str("udp/127.0.0.1:7447?invalid=ctrl#iface=eth0"); assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK); - str = _z_string_from_str("udp/127.0.0.1:7447?invalid=ctrl#invalid=eth0"); + str = _z_string_alias_str("udp/127.0.0.1:7447?invalid=ctrl#invalid=eth0"); assert(_z_endpoint_from_string(&ep, &str) == _Z_RES_OK); return 0; diff --git a/tests/z_keyexpr_test.c b/tests/z_keyexpr_test.c index 764f69941..ff5a01a86 100644 --- a/tests/z_keyexpr_test.c +++ b/tests/z_keyexpr_test.c @@ -344,7 +344,7 @@ void test_equals(void) { _Bool keyexpr_equals_string(const z_loaned_keyexpr_t *ke, const char *s) { z_view_string_t vs; z_keyexpr_as_view_string(ke, &vs); - _z_string_t str = _z_string_from_str(s); + _z_string_t str = _z_string_alias_str(s); return _z_string_equals(z_view_string_loan(&vs), &str); } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index a2ed5b990..fe2daba52 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -231,7 +231,7 @@ _z_string_svec_t gen_str_array(size_t size) { return sa; } -_z_string_t gen_string(size_t len) { return _z_string_from_str(gen_str(len)); } +_z_string_t gen_string(size_t len) { return _z_string_alias_str(gen_str(len)); } _z_locator_array_t gen_locator_array(size_t size) { _z_locator_array_t la = _z_locator_array_make(size); From 3d5b303234fc8f08f63da855d7b3ad676baf9e4b Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 23 Aug 2024 11:45:45 +0200 Subject: [PATCH 2/7] feat: add _z_keyexpr_from_substr --- include/zenoh-pico/protocol/keyexpr.h | 1 + src/protocol/keyexpr.c | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/include/zenoh-pico/protocol/keyexpr.h b/include/zenoh-pico/protocol/keyexpr.h index 89f87b560..4232a51ac 100644 --- a/include/zenoh-pico/protocol/keyexpr.h +++ b/include/zenoh-pico/protocol/keyexpr.h @@ -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); diff --git a/src/protocol/keyexpr.c b/src/protocol/keyexpr.c index 280635e49..6d87cbd73 100644 --- a/src/protocol/keyexpr.c +++ b/src/protocol/keyexpr.c @@ -40,6 +40,14 @@ _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) { + return (_z_keyexpr_t){ + ._id = rid, + ._mapping = _z_keyexpr_mapping(_Z_KEYEXPR_MAPPING_LOCAL), + ._suffix = (str != NULL) ? _z_string_alias_substr(str, len) : _z_string_null(), + }; +} + int8_t _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src) { *dst = _z_keyexpr_null(); dst->_id = src->_id; From 480dd6eac170631a50cbdb987a0adb5cf41a5dbf Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 23 Aug 2024 11:46:20 +0200 Subject: [PATCH 3/7] feat: add missing methods and add canon check --- include/zenoh-pico/api/primitives.h | 55 +++++++++++++++++++++++++---- src/api/api.c | 28 ++++++++++++--- 2 files changed, 71 insertions(+), 12 deletions(-) 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; From ad43a7f9c6121c71c139469c7b0207aa62abb395 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 27 Aug 2024 10:09:55 +0200 Subject: [PATCH 4/7] refactor: rename string and slice functions --- include/zenoh-pico/collections/slice.h | 4 ++-- include/zenoh-pico/collections/string.h | 6 +++--- src/api/api.c | 12 ++++++------ src/collections/bytes.c | 2 +- src/collections/slice.c | 8 ++++---- src/collections/string.c | 16 ++++++++-------- src/net/encoding.c | 2 +- src/net/primitives.c | 2 +- src/net/session.c | 4 ++-- src/protocol/codec.c | 6 +++--- src/protocol/codec/message.c | 8 ++++---- src/protocol/codec/transport.c | 2 +- src/transport/multicast.c | 2 +- src/transport/multicast/read.c | 2 +- src/transport/raweth/read.c | 2 +- src/transport/unicast.c | 2 +- tests/z_client_test.c | 2 +- tests/z_data_struct_test.c | 6 +++--- tests/z_msgcodec_test.c | 4 ++-- tests/z_peer_multicast_test.c | 4 ++-- 20 files changed, 48 insertions(+), 48 deletions(-) diff --git a/include/zenoh-pico/collections/slice.h b/include/zenoh-pico/collections/slice.h index 730b853f3..22e7345b5 100644 --- a/include/zenoh-pico/collections/slice.h +++ b/include/zenoh-pico/collections/slice.h @@ -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); diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index 3e5427305..c8755bf69 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -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_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); -_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); diff --git a/src/api/api.c b/src/api/api.c index 0062bd27e..0acf89f7a 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -379,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; } @@ -405,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; } @@ -542,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)); } @@ -999,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; } @@ -1015,7 +1015,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; } diff --git a/src/collections/bytes.c b/src/collections/bytes.c index b7b1290a3..5c1759242 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -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(); } diff --git a/src/collections/slice.c b/src/collections/slice.c index d2db78030..345a69b5c 100644 --- a/src/collections/slice.c +++ b/src/collections/slice.c @@ -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; @@ -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); } diff --git a/src/collections/string.c b/src/collections/string.c index 4a984a231..c7ea9e14e 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -27,13 +27,13 @@ _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); @@ -47,25 +47,25 @@ _z_string_t _z_string_n_make(const char *value, size_t len) { _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_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_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; @@ -134,7 +134,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; } diff --git a/src/net/encoding.c b/src/net/encoding.c index 84198505d..2b2a48b6d 100644 --- a/src/net/encoding.c +++ b/src/net/encoding.c @@ -23,7 +23,7 @@ int8_t _z_encoding_make(_z_encoding_t *encoding, uint16_t id, const char *schema encoding->id = id; // Clone schema if (schema != NULL) { - 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; } diff --git a/src/net/primitives.c b/src/net/primitives.c index ff5cb34d5..70e4ab6e7 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -464,7 +464,7 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, ret = _z_register_pending_query(zn, pq); // Add the pending query to the current session if (ret == _Z_RES_OK) { - _z_slice_t params = _z_slice_from_buf((uint8_t *)parameters, strlen(parameters)); + _z_slice_t params = _z_slice_alias_buf((uint8_t *)parameters, strlen(parameters)); _z_zenoh_message_t z_msg = _z_msg_make_query(&keyexpr, ¶ms, pq->_id, pq->_consolidation, &value, timeout_ms, attachment, cong_ctrl, priority, is_express); diff --git a/src/net/session.c b/src/net/session.c index 68df016fb..f2d675720 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -105,7 +105,7 @@ int8_t _z_open(_z_session_rc_t *zn, _z_config_t *config) { } } locators = _z_string_svec_make(1); - _z_string_t s = _z_string_make(_z_config_get(config, key)); + _z_string_t s = _z_string_copy_from_str(_z_config_get(config, key)); _z_string_svec_append(&locators, &s); } @@ -155,7 +155,7 @@ _z_config_t *_z_info(const _z_session_t *zn) { _z_config_t *ps = (_z_config_t *)z_malloc(sizeof(_z_config_t)); if (ps != NULL) { _z_config_init(ps); - _z_slice_t local_zid = _z_slice_from_buf(zn->_local_zid.id, _z_id_len(zn->_local_zid)); + _z_slice_t local_zid = _z_slice_alias_buf(zn->_local_zid.id, _z_id_len(zn->_local_zid)); // TODO(sasahcmc): is it zero terminated??? // rework it!!! _z_string_t s = _z_string_convert_bytes(&local_zid); diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 756be16cc..006490ae5 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -251,9 +251,9 @@ int8_t _z_slice_val_decode_na(_z_slice_t *bs, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; if (ret == _Z_RES_OK) { - if (_z_zbuf_len(zbf) >= bs->len) { // Check if we have enough bytes to read - *bs = _z_slice_from_buf(_z_zbuf_get_rptr(zbf), bs->len); // Decode without allocating - _z_zbuf_set_rpos(zbf, _z_zbuf_get_rpos(zbf) + bs->len); // Move the read position + if (_z_zbuf_len(zbf) >= bs->len) { // Check if we have enough bytes to read + *bs = _z_slice_alias_buf(_z_zbuf_get_rptr(zbf), bs->len); // Decode without allocating + _z_zbuf_set_rpos(zbf, _z_zbuf_get_rpos(zbf) + bs->len); // Move the read position } else { _Z_DEBUG("WARNING: Not enough bytes to read"); bs->len = 0; diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index cee9e8c27..960f54498 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -60,7 +60,7 @@ int8_t _z_id_encode_as_slice(_z_wbuf_t *wbf, const _z_id_t *id) { uint8_t len = _z_id_len(*id); if (len != 0) { - _z_slice_t buf = _z_slice_from_buf(id->id, len); + _z_slice_t buf = _z_slice_alias_buf(id->id, len); ret = _z_slice_encode(wbf, &buf); } else { _Z_DEBUG("Attempted to encode invalid ID 0"); @@ -225,7 +225,7 @@ int8_t _z_source_info_encode(_z_wbuf_t *wbf, const _z_source_info_t *info) { int8_t ret = 0; uint8_t zidlen = _z_id_len(info->_id); ret |= _z_uint8_encode(wbf, zidlen << 4); - _z_slice_t zid = _z_slice_from_buf(info->_id.id, zidlen); + _z_slice_t zid = _z_slice_alias_buf(info->_id.id, zidlen); ret |= _z_slice_val_encode(wbf, &zid); ret |= _z_zsize_encode(wbf, info->_entity_id); ret |= _z_zsize_encode(wbf, info->_source_sn); @@ -237,7 +237,7 @@ int8_t _z_source_info_encode_ext(_z_wbuf_t *wbf, const _z_source_info_t *info) { size_t ext_size = 1u + zidlen + _z_zint_len(info->_entity_id) + _z_zint_len(info->_source_sn); _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, ext_size)); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, zidlen << 4)); - _z_slice_t zid = _z_slice_from_buf(info->_id.id, zidlen); + _z_slice_t zid = _z_slice_alias_buf(info->_id.id, zidlen); _Z_RETURN_IF_ERR(_z_slice_val_encode(wbf, &zid)); _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, info->_entity_id)); _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, info->_source_sn)); @@ -637,7 +637,7 @@ int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *m cbyte |= _z_whatami_to_uint8(msg->_whatami); cbyte |= (uint8_t)(((zidlen - 1) & 0x0F) << 4); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, cbyte)) - _z_slice_t s = _z_slice_from_buf(msg->_zid.id, zidlen); + _z_slice_t s = _z_slice_alias_buf(msg->_zid.id, zidlen); _Z_RETURN_IF_ERR(_z_slice_val_encode(wbf, &s)); if (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true) { diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index 08b5dcd94..90b8efbba 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -409,7 +409,7 @@ int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t head ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x05); } - _z_slice_t slice = _z_slice_from_buf((uint8_t *)_z_zbuf_start(zbf), _z_zbuf_len(zbf)); + _z_slice_t slice = _z_slice_alias_buf((uint8_t *)_z_zbuf_start(zbf), _z_zbuf_len(zbf)); _z_slice_copy(&msg->_payload, &slice); zbf->_ios._r_pos = zbf->_ios._w_pos; diff --git a/src/transport/multicast.c b/src/transport/multicast.c index 278ca857f..42059f6dc 100644 --- a/src/transport/multicast.c +++ b/src/transport/multicast.c @@ -45,7 +45,7 @@ void _zp_multicast_info_session(const _z_transport_t *zt, _z_config_t *ps) { _z_transport_peer_entry_list_t *xs = zt->_transport._multicast._peers; while (xs != NULL) { _z_transport_peer_entry_t *peer = _z_transport_peer_entry_list_head(xs); - _z_slice_t remote_zid = _z_slice_from_buf(peer->_remote_zid.id, _z_id_len(peer->_remote_zid)); + _z_slice_t remote_zid = _z_slice_alias_buf(peer->_remote_zid.id, _z_id_len(peer->_remote_zid)); _z_string_t remote_zid_str = _z_string_convert_bytes(&remote_zid); _zp_config_insert(ps, Z_INFO_PEER_PID_KEY, _z_string_data(&remote_zid_str)); _z_string_clear(&remote_zid_str); diff --git a/src/transport/multicast/read.c b/src/transport/multicast/read.c index bc9c76f2c..34582418f 100644 --- a/src/transport/multicast/read.c +++ b/src/transport/multicast/read.c @@ -57,7 +57,7 @@ void *_zp_multicast_read_task(void *ztm_arg) { // Prepare the buffer _z_zbuf_reset(&ztm->_zbuf); - _z_slice_t addr = _z_slice_from_buf(NULL, 0); + _z_slice_t addr = _z_slice_alias_buf(NULL, 0); while (ztm->_read_task_running == true) { // Read bytes from socket to the main buffer size_t to_read = 0; diff --git a/src/transport/raweth/read.c b/src/transport/raweth/read.c index 5e85dc4a4..81cf0fd3d 100644 --- a/src/transport/raweth/read.c +++ b/src/transport/raweth/read.c @@ -52,7 +52,7 @@ int8_t _zp_raweth_read(_z_transport_multicast_t *ztm) { void *_zp_raweth_read_task(void *ztm_arg) { _z_transport_multicast_t *ztm = (_z_transport_multicast_t *)ztm_arg; _z_transport_message_t t_msg; - _z_slice_t addr = _z_slice_from_buf(NULL, 0); + _z_slice_t addr = _z_slice_alias_buf(NULL, 0); // Task loop while (ztm->_read_task_running == true) { diff --git a/src/transport/unicast.c b/src/transport/unicast.c index 796f09e66..dca9ae0ea 100644 --- a/src/transport/unicast.c +++ b/src/transport/unicast.c @@ -39,7 +39,7 @@ void _zp_unicast_fetch_zid(const _z_transport_t *zt, _z_closure_zid_t *callback) void _zp_unicast_info_session(const _z_transport_t *zt, _z_config_t *ps) { _z_id_t remote_zid = zt->_transport._unicast._remote_zid; - _z_slice_t remote_zid_bytes = _z_slice_from_buf(remote_zid.id, _z_id_len(remote_zid)); + _z_slice_t remote_zid_bytes = _z_slice_alias_buf(remote_zid.id, _z_id_len(remote_zid)); _z_string_t remote_zid_str = _z_string_convert_bytes(&remote_zid_bytes); _zp_config_insert(ps, Z_INFO_ROUTER_PID_KEY, _z_string_data(&remote_zid_str)); _z_string_clear(&remote_zid_str); diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 7fe4cf56c..8bdc10d16 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -115,7 +115,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { } _z_string_t format_id(const z_id_t *id) { - _z_slice_t id_as_bytes = _z_slice_from_buf(id->id, _z_id_len(*id)); + _z_slice_t id_as_bytes = _z_slice_alias_buf(id->id, _z_id_len(*id)); return _z_string_convert_bytes(&id_as_bytes); } diff --git a/tests/z_data_struct_test.c b/tests/z_data_struct_test.c index eea8acd5f..15394e2aa 100644 --- a/tests/z_data_struct_test.c +++ b/tests/z_data_struct_test.c @@ -151,10 +151,10 @@ void z_slice_custom_delete_test(void) { size_t counter = 0; uint8_t data[5] = {1, 2, 3, 4, 5}; _z_delete_context_t dc = (_z_delete_context_t){.deleter = _z_slice_custom_deleter, .context = &counter}; - _z_slice_t s1 = _z_slice_from_buf_custom_deleter(data, 5, dc); - _z_slice_t s2 = _z_slice_from_buf_custom_deleter(data, 5, dc); + _z_slice_t s1 = _z_slice_alias_buf_custom_deleter(data, 5, dc); + _z_slice_t s2 = _z_slice_alias_buf_custom_deleter(data, 5, dc); _z_slice_t s3 = _z_slice_copy_from_buf(data, 5); - _z_slice_t s4 = _z_slice_from_buf(data, 5); + _z_slice_t s4 = _z_slice_alias_buf(data, 5); assert(_z_slice_is_alloced(&s1)); assert(_z_slice_is_alloced(&s2)); assert(_z_slice_is_alloced(&s3)); diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index fe2daba52..fc7b15415 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -179,7 +179,7 @@ _z_slice_t gen_slice(size_t len) { for (_z_zint_t i = 0; i < len; i++) { ((uint8_t *)p)[i] = gen_uint8() & 0x7f; // 0b01111111 } - return _z_slice_from_buf_custom_deleter(p, len, _z_delete_context_default()); + return _z_slice_alias_buf_custom_deleter(p, len, _z_delete_context_default()); } _z_bytes_t gen_payload(size_t len) { @@ -224,7 +224,7 @@ char *gen_str(size_t size) { _z_string_svec_t gen_str_array(size_t size) { _z_string_svec_t sa = _z_string_svec_make(size); for (size_t i = 0; i < size; i++) { - _z_string_t s = _z_string_make(gen_str(16)); + _z_string_t s = _z_string_copy_from_str(gen_str(16)); _z_string_svec_append(&sa, &s); } diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 89efd3f49..25fa2d72b 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -77,7 +77,7 @@ int main(int argc, char **argv) { z_owned_session_t s1; assert(z_open(&s1, z_move(config)) == Z_OK); _z_slice_t id_as_bytes = - _z_slice_from_buf(_Z_RC_IN_VAL(z_loan(s1))->_local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s1))->_local_zid)); + _z_slice_alias_buf(_Z_RC_IN_VAL(z_loan(s1))->_local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s1))->_local_zid)); _z_string_t zid1 = _z_string_convert_bytes(&id_as_bytes); printf("Session 1 with PID: %s\n", z_string_data(&zid1)); _z_string_clear(&zid1); @@ -96,7 +96,7 @@ int main(int argc, char **argv) { assert(z_open(&s2, z_move(config)) == Z_OK); id_as_bytes = - _z_slice_from_buf(_Z_RC_IN_VAL(z_loan(s2))->_local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s2))->_local_zid)); + _z_slice_alias_buf(_Z_RC_IN_VAL(z_loan(s2))->_local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s2))->_local_zid)); _z_string_t zid2 = _z_string_convert_bytes(&id_as_bytes); printf("Session 2 with PID: %s\n", z_string_data(&zid2)); _z_string_clear(&zid2); From 77748acd054a5f8660cc896b940b398c97b63022 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 27 Aug 2024 10:10:47 +0200 Subject: [PATCH 5/7] fix: remove null terminated from string function --- src/collections/string.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/collections/string.c b/src/collections/string.c index c7ea9e14e..171739abe 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -35,14 +35,8 @@ _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 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_alias_str(const char *value) { From 2de87cfb181a3b38cbfc2c6f4ae5592f4d2d4fe0 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 27 Aug 2024 10:41:26 +0200 Subject: [PATCH 6/7] refactor: rename the custom_deleter functions --- include/zenoh-pico/collections/slice.h | 2 +- include/zenoh-pico/collections/string.h | 2 +- src/api/api.c | 8 ++++---- src/collections/slice.c | 4 ++-- src/collections/string.c | 6 +++--- tests/z_data_struct_test.c | 4 ++-- tests/z_msgcodec_test.c | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/zenoh-pico/collections/slice.h b/include/zenoh-pico/collections/slice.h index 22e7345b5..7edd952e8 100644 --- a/include/zenoh-pico/collections/slice.h +++ b/include/zenoh-pico/collections/slice.h @@ -50,7 +50,7 @@ inline static _Bool _z_slice_check(const _z_slice_t *slice) { return slice->star 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_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_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); _z_slice_t _z_slice_alias(const _z_slice_t *bs); diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index c8755bf69..4d6268bb6 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -73,7 +73,7 @@ _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); +_z_string_t _z_string_from_str_custom_deleter(char *value, _z_delete_context_t c); _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); diff --git a/src/api/api.c b/src/api/api.c index 0acf89f7a..8ab9be0f5 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -405,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_alias_buf_custom_deleter(data, len, _z_delete_context_create(deleter, context)); + slice->_val = _z_slice_from_buf_custom_deleter(data, len, _z_delete_context_create(deleter, context)); return _Z_RES_OK; } @@ -542,7 +542,7 @@ 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_alias_buf_custom_deleter(data, len, _z_delete_context_create(deleter, context)); + s._val = _z_slice_from_buf_custom_deleter(data, len, _z_delete_context_create(deleter, context)); return z_bytes_from_slice(bytes, z_slice_move(&s)); } @@ -576,7 +576,7 @@ 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_alias_str_custom_deleter(value, _z_delete_context_create(deleter, context)); + s._val = _z_string_from_str_custom_deleter(value, _z_delete_context_create(deleter, context)); return z_bytes_from_string(bytes, z_string_move(&s)); } @@ -1008,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_alias_str_custom_deleter(value, _z_delete_context_create(deleter, context)); + str->_val = _z_string_from_str_custom_deleter(value, _z_delete_context_create(deleter, context)); return _Z_RES_OK; } diff --git a/src/collections/slice.c b/src/collections/slice.c index 345a69b5c..730ce618c 100644 --- a/src/collections/slice.c +++ b/src/collections/slice.c @@ -74,7 +74,7 @@ _z_slice_t _z_slice_make(size_t capacity) { return bs; } -_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_from_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; @@ -88,7 +88,7 @@ _z_slice_t _z_slice_alias(const _z_slice_t *bs) { } _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()); + 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) { diff --git a/src/collections/string.c b/src/collections/string.c index 171739abe..88f9bbd91 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -51,9 +51,9 @@ _z_string_t _z_string_alias_substr(const char *value, size_t len) { return s; } -_z_string_t _z_string_alias_str_custom_deleter(char *value, _z_delete_context_t c) { +_z_string_t _z_string_from_str_custom_deleter(char *value, _z_delete_context_t c) { _z_string_t s; - s._slice = _z_slice_alias_buf_custom_deleter((const uint8_t *)(value), strlen(value), c); + s._slice = _z_slice_from_buf_custom_deleter((const uint8_t *)(value), strlen(value), c); return s; } @@ -128,7 +128,7 @@ _z_string_t _z_string_convert_bytes(const _z_slice_t *bs) { } else { len = 0; } - s._slice = _z_slice_alias_buf_custom_deleter((const uint8_t *)s_val, len, _z_delete_context_default()); + s._slice = _z_slice_from_buf_custom_deleter((const uint8_t *)s_val, len, _z_delete_context_default()); return s; } diff --git a/tests/z_data_struct_test.c b/tests/z_data_struct_test.c index 15394e2aa..2e18098dd 100644 --- a/tests/z_data_struct_test.c +++ b/tests/z_data_struct_test.c @@ -151,8 +151,8 @@ void z_slice_custom_delete_test(void) { size_t counter = 0; uint8_t data[5] = {1, 2, 3, 4, 5}; _z_delete_context_t dc = (_z_delete_context_t){.deleter = _z_slice_custom_deleter, .context = &counter}; - _z_slice_t s1 = _z_slice_alias_buf_custom_deleter(data, 5, dc); - _z_slice_t s2 = _z_slice_alias_buf_custom_deleter(data, 5, dc); + _z_slice_t s1 = _z_slice_from_buf_custom_deleter(data, 5, dc); + _z_slice_t s2 = _z_slice_from_buf_custom_deleter(data, 5, dc); _z_slice_t s3 = _z_slice_copy_from_buf(data, 5); _z_slice_t s4 = _z_slice_alias_buf(data, 5); assert(_z_slice_is_alloced(&s1)); diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index fc7b15415..810489dec 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -179,7 +179,7 @@ _z_slice_t gen_slice(size_t len) { for (_z_zint_t i = 0; i < len; i++) { ((uint8_t *)p)[i] = gen_uint8() & 0x7f; // 0b01111111 } - return _z_slice_alias_buf_custom_deleter(p, len, _z_delete_context_default()); + return _z_slice_from_buf_custom_deleter(p, len, _z_delete_context_default()); } _z_bytes_t gen_payload(size_t len) { From 86937ec9a07bd265d15a18311828dc0715d68a5c Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 27 Aug 2024 11:36:28 +0200 Subject: [PATCH 7/7] feat: call from_substr in from_str functions --- src/api/api.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 8ab9be0f5..a50228341 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -65,22 +65,6 @@ 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) { - 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_keyexpr_from_substr(0, name, name_len); - return _Z_RES_OK; -} - 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) { @@ -91,12 +75,22 @@ z_result_t z_view_keyexpr_from_substr(z_view_keyexpr_t *keyexpr, const char *nam return _Z_RES_OK; } +int8_t z_view_keyexpr_from_str(z_view_keyexpr_t *keyexpr, const char *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); + return z_view_keyexpr_from_substr_autocanonize(keyexpr, name, &name_len); +} + 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); }