diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index fed282823..9938149d6 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -477,6 +477,9 @@ _Z_OWNED_TYPE_VALUE(_z_reply_t, reply) * * Operations over :c:type:`z_loaned_string_array_t` must be done using the provided functions: * + * - :c:func:`z_string_array_new` + * - :c:func:`z_string_array_push_by_alias` + * - :c:func:`z_string_array_push_by_copy` * - :c:func:`z_string_array_get` * - :c:func:`z_string_array_len` * - :c:func:`z_str_array_array_is_empty` @@ -484,6 +487,9 @@ _Z_OWNED_TYPE_VALUE(_z_reply_t, reply) _Z_OWNED_TYPE_VALUE(_z_string_svec_t, string_array) _Z_VIEW_TYPE(_z_string_svec_t, string_array) +void z_string_array_new(z_owned_string_array_t *a); +size_t z_string_array_push_by_alias(z_loaned_string_array_t *a, const z_loaned_string_t *value); +size_t z_string_array_push_by_copy(z_loaned_string_array_t *a, const z_loaned_string_t *value); const z_loaned_string_t *z_string_array_get(const z_loaned_string_array_t *a, size_t k); size_t z_string_array_len(const z_loaned_string_array_t *a); _Bool z_string_array_is_empty(const z_loaned_string_array_t *a); diff --git a/src/api/api.c b/src/api/api.c index 83916d78f..0633178d4 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -23,6 +23,7 @@ #include "zenoh-pico/api/primitives.h" #include "zenoh-pico/api/types.h" #include "zenoh-pico/collections/slice.h" +#include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" #include "zenoh-pico/net/config.h" #include "zenoh-pico/net/filtering.h" @@ -58,6 +59,25 @@ int8_t z_view_string_from_substr(z_view_string_t *str, const char *value, size_t return _Z_RES_OK; } +_z_string_svec_t _z_string_array_null(void) { return _z_string_svec_make(0); } + +void z_string_array_new(z_owned_string_array_t *a) { a->_val = _z_string_array_null(); } + +size_t z_string_array_push_by_alias(z_loaned_string_array_t *a, const z_loaned_string_t *value) { + _z_string_t str = _z_string_alias(value); + _z_string_svec_append(a, &str); + + return _z_string_svec_len(a); +} + +size_t z_string_array_push_by_copy(z_loaned_string_array_t *a, const z_loaned_string_t *value) { + _z_string_t str; + _z_string_copy(&str, value); + _z_string_svec_append(a, &str); + + return _z_string_svec_len(a); +} + const z_loaned_string_t *z_string_array_get(const z_loaned_string_array_t *a, size_t k) { return _z_string_svec_get(a, k); } @@ -799,7 +819,6 @@ int8_t _z_string_array_copy(_z_string_svec_t *dst, const _z_string_svec_t *src) _z_string_svec_copy(dst, src); return dst->_len == src->_len ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY; } -_z_string_svec_t _z_string_array_null(void) { return _z_string_svec_make(0); } _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_string_svec_t, string_array, _z_string_array_check, _z_string_array_null, _z_string_array_copy, _z_string_svec_clear) _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_slice_t, slice, _z_slice_check, _z_slice_empty, _z_slice_copy, _z_slice_clear) diff --git a/tests/z_data_struct_test.c b/tests/z_data_struct_test.c index 2e18098dd..0b7cfa702 100644 --- a/tests/z_data_struct_test.c +++ b/tests/z_data_struct_test.c @@ -16,9 +16,10 @@ #include #include +#include "zenoh-pico/api/primitives.h" +#include "zenoh-pico/api/types.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/protocol/core.h" -#include "zenoh-pico/system/platform.h" #include "zenoh-pico/transport/transport.h" #undef NDEBUG @@ -167,10 +168,76 @@ void z_slice_custom_delete_test(void) { assert(counter == 2); } +void z_string_array_test(void) { + // create new array + z_owned_string_array_t a; + z_string_array_new(&a); + + char s1[] = "string1"; + char s2[] = "string2"; + char s3[] = "string3"; + char s4[] = "string4"; + + // add by copy + z_view_string_t vs1; + z_view_string_from_str(&vs1, s1); + assert(z_string_array_push_by_copy(z_string_array_loan_mut(&a), z_view_string_loan(&vs1)) == 1); + + z_view_string_t vs2; + z_view_string_from_str(&vs2, s2); + assert(z_string_array_push_by_copy(z_string_array_loan_mut(&a), z_view_string_loan(&vs2)) == 2); + + // add by alias + z_view_string_t vs3; + z_view_string_from_str(&vs3, s3); + assert(z_string_array_push_by_alias(z_string_array_loan_mut(&a), z_view_string_loan(&vs3)) == 3); + + z_view_string_t vs4; + z_view_string_from_str(&vs4, s4); + assert(z_string_array_push_by_alias(z_string_array_loan_mut(&a), z_view_string_loan(&vs4)) == 4); + + // check values + const z_loaned_string_t *ls1 = z_string_array_get(z_string_array_loan(&a), 0); + assert(strncmp(z_string_data(ls1), s1, z_string_len(ls1)) == 0); + + const z_loaned_string_t *ls2 = z_string_array_get(z_string_array_loan(&a), 1); + assert(strncmp(z_string_data(ls2), s2, z_string_len(ls2)) == 0); + + const z_loaned_string_t *ls3 = z_string_array_get(z_string_array_loan(&a), 2); + assert(strncmp(z_string_data(ls3), s3, z_string_len(ls3)) == 0); + + const z_loaned_string_t *ls4 = z_string_array_get(z_string_array_loan(&a), 3); + assert(strncmp(z_string_data(ls4), s4, z_string_len(ls4)) == 0); + + // modify original strings values + s1[0] = 'X'; + s2[0] = 'X'; + s3[0] = 'X'; + s4[0] = 'X'; + + // values passed by copy should NOT be changed + ls1 = z_string_array_get(z_string_array_loan(&a), 0); + assert(strncmp(z_string_data(ls1), "string1", z_string_len(ls1)) == 0); + + ls2 = z_string_array_get(z_string_array_loan(&a), 1); + assert(strncmp(z_string_data(ls2), "string2", z_string_len(ls2)) == 0); + + // values passed by alias should be changed + ls3 = z_string_array_get(z_string_array_loan(&a), 2); + assert(strncmp(z_string_data(ls3), s3, z_string_len(ls3)) == 0); + + ls4 = z_string_array_get(z_string_array_loan(&a), 3); + assert(strncmp(z_string_data(ls4), s4, z_string_len(ls4)) == 0); + + // cleanup + z_string_array_drop(z_string_array_move(&a)); +} + int main(void) { entry_list_test(); str_vec_list_intmap_test(); z_slice_custom_delete_test(); + z_string_array_test(); return 0; }