From a83be07b8139971548973b97a23e7c3b23d70843 Mon Sep 17 00:00:00 2001 From: DenisBiryukov91 <155981813+DenisBiryukov91@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:05:02 +0200 Subject: [PATCH] Keyexpr alignment with zenoh-c (#516) * rename z_keyexpr_to_string into z_keyexpr_as_view_string * added missing keyexpr constructors; updated tests; * added z_keyexpr_join and z_keyexpr_concat * added z_keyexpr_relation_to * c99 fixes * c99 fixes * typo fix * fix z_keyexpr_from_substr_autocanonize api * address review commens; swap arguments of z_undeclare_keyexpr; * .pc update * if format --- docs/api.rst | 8 +- examples/arduino/z_get.ino | 7 +- examples/arduino/z_pull.ino | 7 +- examples/arduino/z_queryable.ino | 8 +- examples/arduino/z_sub.ino | 7 +- examples/espidf/z_get.c | 5 +- examples/espidf/z_pull.c | 5 +- examples/espidf/z_queryable.c | 5 +- examples/espidf/z_sub.c | 7 +- examples/freertos_plus_tcp/z_get.c | 5 +- examples/freertos_plus_tcp/z_pull.c | 5 +- examples/freertos_plus_tcp/z_put.c | 2 +- examples/freertos_plus_tcp/z_queryable.c | 5 +- examples/freertos_plus_tcp/z_sub.c | 5 +- examples/freertos_plus_tcp/z_sub_st.c | 5 +- examples/mbed/z_get.cpp | 7 +- examples/mbed/z_pull.cpp | 7 +- examples/mbed/z_queryable.cpp | 7 +- examples/mbed/z_sub.cpp | 7 +- examples/unix/c11/z_get.c | 5 +- examples/unix/c11/z_get_attachment.c | 5 +- examples/unix/c11/z_get_channel.c | 5 +- examples/unix/c11/z_pull.c | 5 +- examples/unix/c11/z_put.c | 2 +- examples/unix/c11/z_queryable.c | 5 +- examples/unix/c11/z_queryable_attachment.c | 5 +- examples/unix/c11/z_queryable_channel.c | 5 +- examples/unix/c11/z_sub.c | 5 +- examples/unix/c11/z_sub_attachment.c | 5 +- examples/unix/c11/z_sub_channel.c | 5 +- examples/unix/c11/z_sub_st.c | 5 +- examples/unix/c99/z_get.c | 7 +- examples/unix/c99/z_pull.c | 7 +- examples/unix/c99/z_put.c | 2 +- examples/unix/c99/z_queryable.c | 7 +- examples/unix/c99/z_sub.c | 7 +- examples/unix/c99/z_sub_st.c | 7 +- examples/windows/z_get.c | 5 +- examples/windows/z_pull.c | 5 +- examples/windows/z_put.c | 2 +- examples/windows/z_queryable.c | 6 +- examples/windows/z_sub.c | 5 +- examples/windows/z_sub_st.c | 5 +- examples/zephyr/z_get.c | 5 +- examples/zephyr/z_pull.c | 5 +- examples/zephyr/z_queryable.c | 5 +- examples/zephyr/z_sub.c | 5 +- include/zenoh-pico/api/constants.h | 16 +++ include/zenoh-pico/api/macros.h | 5 +- include/zenoh-pico/api/olv_macros.h | 4 +- include/zenoh-pico/api/primitives.h | 104 ++++++++++++++-- include/zenoh-pico/api/types.h | 2 +- src/api/api.c | 132 +++++++++++++++++---- tests/z_api_alignment_test.c | 21 ++-- tests/z_client_test.c | 19 ++- tests/z_keyexpr_test.c | 104 +++++++++++++++- tests/z_peer_multicast_test.c | 5 +- tests/z_test_fragment_rx.c | 5 +- zenohpico.pc | 2 +- 59 files changed, 459 insertions(+), 216 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index e57e20615..b3a6f279b 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -299,7 +299,7 @@ Primitives .. autocfunction:: primitives.h::z_view_keyexpr_from_str .. autocfunction:: primitives.h::z_view_keyexpr_from_str_unchecked .. autocfunction:: primitives.h::z_view_keyexpr_from_str_autocanonize -.. autocfunction:: primitives.h::z_keyexpr_to_string +.. autocfunction:: primitives.h::z_keyexpr_as_view_string .. autocfunction:: primitives.h::z_keyexpr_is_initialized .. autocfunction:: primitives.h::z_keyexpr_is_canon .. autocfunction:: primitives.h::zp_keyexpr_is_canon_null_terminated @@ -311,6 +311,9 @@ Primitives .. autocfunction:: primitives.h::zp_keyexpr_intersect_null_terminated .. autocfunction:: primitives.h::z_keyexpr_equals .. autocfunction:: primitives.h::zp_keyexpr_equals_null_terminated +.. autocfunction:: primitives.h::z_keyexpr_relation_to +.. autocfunction:: primitives.h::z_keyexpr_concat +.. autocfunction:: primitives.h::z_keyexpr_join .. autocfunction:: primitives.h::z_config_new .. autocfunction:: primitives.h::z_config_default .. autocfunction:: primitives.h::zp_config_get @@ -422,6 +425,9 @@ Primitives .. autocfunction:: primitives.h::z_query_reply_options_default .. autocfunction:: primitives.h::z_query_reply .. autocfunction:: primitives.h::z_keyexpr_from_str +.. autocfunction:: primitives.h::z_keyexpr_from_substr +.. autocfunction:: primitives.h::z_keyexpr_from_str_autocanonize +.. autocfunction:: primitives.h::z_keyexpr_from_substr_autocanonize .. autocfunction:: primitives.h::z_declare_keyexpr .. autocfunction:: primitives.h::z_undeclare_keyexpr .. autocfunction:: primitives.h::z_subscriber_options_default diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index 1053c7adf..6c790880a 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -46,18 +46,17 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { (void)(ctx); if (z_reply_is_ok(oreply)) { const z_loaned_sample_t *sample = z_reply_ok(oreply); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t replystr; z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr); Serial.print(" >> [Get listener] Received ("); - Serial.print(z_string_data(z_string_loan(&keystr))); + Serial.print(z_string_data(z_view_string_loan(&keystr))); Serial.print(", "); Serial.print(z_string_data(z_string_loan(&replystr))); Serial.println(")"); - z_string_drop(z_string_move(&keystr)); z_string_drop(z_string_move(&replystr)); } else { Serial.println(" >> Received an error"); diff --git a/examples/arduino/z_pull.ino b/examples/arduino/z_pull.ino index da698f373..cc53a3adf 100644 --- a/examples/arduino/z_pull.ino +++ b/examples/arduino/z_pull.ino @@ -39,17 +39,16 @@ // @TODO // void data_handler(const z_loaned_sample_t *sample, void *arg) { -// z_owned_string_t keystr; -// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// z_view_string_t keystr; +// z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); // std::string val((const char *)sample->payload.start, sample->payload.len); // Serial.print(" >> [Subscription listener] Received ("); -// Serial.print(z_string_data(z_string_loan(&keystr))); +// Serial.print(z_string_data(z_view_string_loan(&keystr))); // Serial.print(", "); // Serial.print(val.c_str()); // Serial.println(")"); -// z_string_drop(z_string_move(&keystr)); // } void setup() { diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index 593db36e7..79cd1f689 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -36,10 +36,10 @@ #define VALUE "[ARDUINO]{ESP32} Queryable from Zenoh-Pico!" void query_handler(const z_loaned_query_t *query, void *arg) { - z_owned_string_t keystr; - z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); Serial.print(" >> [Queryable handler] Received Query '"); - Serial.print(z_string_data(z_string_loan(&keystr))); + Serial.print(z_string_data(z_view_string_loan(&keystr))); Serial.println("'"); // Process value @@ -60,8 +60,6 @@ void query_handler(const z_loaned_query_t *query, void *arg) { z_bytes_serialize_from_str(&reply_payload, VALUE); z_query_reply(query, z_view_keyexpr_loan(&ke), z_bytes_move(&reply_payload), NULL); - - z_string_drop(z_string_move(&keystr)); } void setup() { diff --git a/examples/arduino/z_sub.ino b/examples/arduino/z_sub.ino index a2c3bb0b0..631b48c60 100644 --- a/examples/arduino/z_sub.ino +++ b/examples/arduino/z_sub.ino @@ -35,18 +35,17 @@ #define KEYEXPR "demo/example/**" void data_handler(const z_loaned_sample_t *sample, void *arg) { - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); Serial.print(" >> [Subscription listener] Received ("); - Serial.print(z_string_data(z_string_loan(&keystr))); + Serial.print(z_string_data(z_view_string_loan(&keystr))); Serial.print(", "); Serial.print(z_string_data(z_string_loan(&value))); Serial.println(")"); - z_string_drop(z_string_move(&keystr)); z_string_drop(z_string_move(&value)); } diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index fd4f7d30c..d33e3d2c9 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -106,13 +106,12 @@ void reply_dropper(void *ctx) { printf(" >> Received query final notification\n" void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { if (z_reply_is_ok(oreply)) { const z_loaned_sample_t *sample = z_reply_ok(oreply); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t replystr; z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr); printf(" >> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr))); - z_drop(z_move(keystr)); z_drop(z_move(replystr)); } else { printf(" >> Received an error\n"); diff --git a/examples/espidf/z_pull.c b/examples/espidf/z_pull.c index 9e30e428b..999dffdf0 100644 --- a/examples/espidf/z_pull.c +++ b/examples/espidf/z_pull.c @@ -102,12 +102,11 @@ void wifi_init_sta(void) { // @TODO // void data_handler(const z_loaned_sample_t* sample, void* arg) { -// z_owned_string_t keystr; -// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// z_view_string_t keystr; +// z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); // printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), // (int)sample->payload.len, // sample->payload.start); -// z_drop(z_move(keystr)); // } void app_main() { diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index 22660b831..1c9d79068 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -103,8 +103,8 @@ void wifi_init_sta(void) { void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t params; z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, @@ -125,7 +125,6 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_bytes_serialize_from_str(&reply_payload, VALUE); z_query_reply(query, z_loan(ke), z_move(reply_payload), NULL); - z_drop(z_move(keystr)); } void app_main() { diff --git a/examples/espidf/z_sub.c b/examples/espidf/z_sub.c index b80d4d828..2fbec348a 100644 --- a/examples/espidf/z_sub.c +++ b/examples/espidf/z_sub.c @@ -101,13 +101,12 @@ void wifi_init_sta(void) { } void data_handler(const z_loaned_sample_t* sample, void* arg) { - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); - printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)), + printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_view_string_loan(&keystr)), z_string_data(z_string_loan(&value))); - z_string_drop(z_string_move(&keystr)); z_string_drop(z_string_move(&value)); } diff --git a/examples/freertos_plus_tcp/z_get.c b/examples/freertos_plus_tcp/z_get.c index c4a52d20b..7ea823074 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -40,13 +40,12 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { (void)(ctx); if (z_reply_is_ok(reply)) { const z_loaned_sample_t *sample = z_reply_ok(reply); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t replystr; z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr); printf(">> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr))); - z_drop(z_move(keystr)); z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); diff --git a/examples/freertos_plus_tcp/z_pull.c b/examples/freertos_plus_tcp/z_pull.c index 9a0b3f1a5..f43bdb685 100644 --- a/examples/freertos_plus_tcp/z_pull.c +++ b/examples/freertos_plus_tcp/z_pull.c @@ -31,11 +31,10 @@ // @TODO // void data_handler(const z_loaned_sample_t *sample, void *ctx) { // (void)(ctx); -// z_owned_string_t keystr; -// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// z_view_string_t keystr; +// z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); // printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)sample->payload.len, // sample->payload.start); -// z_drop(z_move(keystr)); // } void app_main(void) { diff --git a/examples/freertos_plus_tcp/z_put.c b/examples/freertos_plus_tcp/z_put.c index ef0d1c579..459077ae9 100644 --- a/examples/freertos_plus_tcp/z_put.c +++ b/examples/freertos_plus_tcp/z_put.c @@ -80,7 +80,7 @@ void app_main(void) { } // Clean up - z_undeclare_keyexpr(z_loan(s), z_move(ke)); + z_undeclare_keyexpr(z_move(ke), z_loan(s)); zp_stop_read_task(z_loan_mut(s)); zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index 68511f9f3..06fb4e89f 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -31,8 +31,8 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t params; z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, @@ -52,7 +52,6 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_bytes_serialize_from_str(&reply_payload, VALUE); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); - z_drop(z_move(keystr)); } void app_main(void) { diff --git a/examples/freertos_plus_tcp/z_sub.c b/examples/freertos_plus_tcp/z_sub.c index 649d973aa..b4a212258 100644 --- a/examples/freertos_plus_tcp/z_sub.c +++ b/examples/freertos_plus_tcp/z_sub.c @@ -30,12 +30,11 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); - z_drop(z_move(keystr)); z_drop(z_move(value)); } diff --git a/examples/freertos_plus_tcp/z_sub_st.c b/examples/freertos_plus_tcp/z_sub_st.c index 62d70532a..102b46d76 100644 --- a/examples/freertos_plus_tcp/z_sub_st.c +++ b/examples/freertos_plus_tcp/z_sub_st.c @@ -33,12 +33,11 @@ int msg_nb = 0; void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); - z_drop(z_move(keystr)); z_drop(z_move(value)); msg_nb++; } diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index 35f304556..5ed3824e2 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -36,14 +36,13 @@ void reply_dropper(void *ctx) { printf(" >> Received query final notification\n" void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { if (z_reply_is_ok(oreply)) { const z_loaned_sample_t *sample = z_reply_ok(oreply); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t replystr; z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr); - printf(" >> Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)), + printf(" >> Received ('%s': '%s')\n", z_string_data(z_view_string_loan(&keystr)), z_string_data(z_string_loan(&replystr))); - z_string_drop(z_string_move(&keystr)); z_string_drop(z_string_move(&replystr)); } else { printf(" >> Received an error\n"); diff --git a/examples/mbed/z_pull.cpp b/examples/mbed/z_pull.cpp index 52a4f9be0..35e4c39c4 100644 --- a/examples/mbed/z_pull.cpp +++ b/examples/mbed/z_pull.cpp @@ -32,12 +32,11 @@ // @TODO // void data_handler(const z_loaned_sample_t *sample, void *arg) { -// z_owned_string_t keystr; -// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); -// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)), +// z_view_string_t keystr; +// z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); +// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_view_string_loan(&keystr)), // (int)sample->payload.len, // sample->payload.start); -// z_string_drop(z_string_move(&keystr)); // } int main(int argc, char **argv) { diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index e56962b52..8aa221f7d 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -33,11 +33,11 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t pred; z_query_parameters(query, &pred); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_string_loan(&keystr)), + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_view_string_loan(&keystr)), (int)z_view_string_loan(&pred)->len, z_view_string_loan(&pred)->val); // Process value z_owned_string_t payload_string; @@ -52,7 +52,6 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_bytes_serialize_from_str(&reply_payload, VALUE); z_query_reply(query, z_query_keyexpr(query), z_bytes_move(&reply_payload), NULL); - z_string_drop(z_string_move(&keystr)); } int main(int argc, char **argv) { diff --git a/examples/mbed/z_sub.cpp b/examples/mbed/z_sub.cpp index 52ac02755..b93287274 100644 --- a/examples/mbed/z_sub.cpp +++ b/examples/mbed/z_sub.cpp @@ -31,13 +31,12 @@ #define KEYEXPR "demo/example/**" void data_handler(const z_loaned_sample_t *sample, void *arg) { - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); - printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)), + printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_view_string_loan(&keystr)), z_string_data(z_string_loan(&value))); - z_string_drop(z_string_move(&keystr)); z_string_drop(z_string_move(&value)); } diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index cef82656f..62dbb6537 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -35,14 +35,13 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { (void)(ctx); if (z_reply_is_ok(reply)) { const z_loaned_sample_t *sample = z_reply_ok(reply); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t replystr; z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr); printf(">> Received %s ('%s': '%s')\n", kind_to_str(z_sample_kind(sample)), z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr))); - z_drop(z_move(keystr)); z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); diff --git a/examples/unix/c11/z_get_attachment.c b/examples/unix/c11/z_get_attachment.c index b45b12854..06c49ca3a 100644 --- a/examples/unix/c11/z_get_attachment.c +++ b/examples/unix/c11/z_get_attachment.c @@ -102,8 +102,8 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { (void)(ctx); if (z_reply_is_ok(reply)) { const z_loaned_sample_t *sample = z_reply_ok(reply); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t replystr; z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr); z_owned_string_t encoding; @@ -121,7 +121,6 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { } drop_attachment(&kvp); - z_drop(z_move(keystr)); z_drop(z_move(replystr)); z_drop(z_move(encoding)); } else { diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index 8b9d8bae7..fd4d6ba94 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -109,13 +109,12 @@ int main(int argc, char **argv) { for (z_recv(z_loan(handler), &reply); z_check(reply); z_recv(z_loan(handler), &reply)) { if (z_reply_is_ok(z_loan(reply))) { const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply)); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t replystr; z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr); printf(">> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr))); - z_drop(z_move(keystr)); z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); diff --git a/examples/unix/c11/z_pull.c b/examples/unix/c11/z_pull.c index 6dcf44b0f..95e275518 100644 --- a/examples/unix/c11/z_pull.c +++ b/examples/unix/c11/z_pull.c @@ -89,13 +89,12 @@ int main(int argc, char **argv) { z_null(&sample); while (true) { for (z_try_recv(z_loan(handler), &sample); z_check(sample); z_try_recv(z_loan(handler), &sample)) { - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(z_loan(sample)), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(z_loan(sample)), &value); printf(">> [Subscriber] Pulled ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); - z_drop(z_move(keystr)); z_drop(z_move(value)); z_drop(z_move(sample)); } diff --git a/examples/unix/c11/z_put.c b/examples/unix/c11/z_put.c index 8cc5eae6e..e0f012047 100644 --- a/examples/unix/c11/z_put.c +++ b/examples/unix/c11/z_put.c @@ -106,7 +106,7 @@ int main(int argc, char **argv) { printf("Oh no! Put has failed...\n"); } // Clean up - z_undeclare_keyexpr(z_loan(s), z_move(ke)); + z_undeclare_keyexpr(z_move(ke), z_loan(s)); zp_stop_read_task(z_loan_mut(s)); zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index c12c71080..34fade86c 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -26,8 +26,8 @@ static z_sample_kind_t reply_kind = Z_SAMPLE_KIND_PUT; void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t params; z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, @@ -55,7 +55,6 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { printf("Unknown reply kind\n"); break; } - z_drop(z_move(keystr)); msg_nb++; } diff --git a/examples/unix/c11/z_queryable_attachment.c b/examples/unix/c11/z_queryable_attachment.c index bbdbe8b63..11c6a469e 100644 --- a/examples/unix/c11/z_queryable_attachment.c +++ b/examples/unix/c11/z_queryable_attachment.c @@ -105,8 +105,8 @@ void drop_attachment(kv_pairs_rx_t *kvp) { void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t params; z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, @@ -154,7 +154,6 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { options.encoding = z_move(reply_encoding); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); - z_drop(z_move(keystr)); msg_nb++; } diff --git a/examples/unix/c11/z_queryable_channel.c b/examples/unix/c11/z_queryable_channel.c index 2e98dc5ed..3c9c5a047 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -101,8 +101,8 @@ int main(int argc, char **argv) { z_null(&query); for (z_recv(z_loan(handler), &query); z_check(query); z_recv(z_loan(handler), &query)) { const z_loaned_query_t *q = z_loan(query); - z_owned_string_t keystr; - z_keyexpr_to_string(z_query_keyexpr(q), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_query_keyexpr(q), &keystr); z_view_string_t params; z_query_parameters(q, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), @@ -122,7 +122,6 @@ int main(int argc, char **argv) { z_bytes_serialize_from_str(&reply_payload, value); z_query_reply(q, z_query_keyexpr(q), z_move(reply_payload), &options); - z_drop(z_move(keystr)); z_drop(z_move(query)); } diff --git a/examples/unix/c11/z_sub.c b/examples/unix/c11/z_sub.c index 118ac2d31..ce3880fb1 100644 --- a/examples/unix/c11/z_sub.c +++ b/examples/unix/c11/z_sub.c @@ -26,12 +26,11 @@ static int msg_nb = 0; void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); - z_drop(z_move(keystr)); z_drop(z_move(value)); msg_nb++; } diff --git a/examples/unix/c11/z_sub_attachment.c b/examples/unix/c11/z_sub_attachment.c index db3198b6d..5ef7d9717 100644 --- a/examples/unix/c11/z_sub_attachment.c +++ b/examples/unix/c11/z_sub_attachment.c @@ -70,8 +70,8 @@ void drop_attachment(kv_pairs_t *kvp) { void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); z_owned_string_t encoding; @@ -86,7 +86,6 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { print_attachment(&kvp); } drop_attachment(&kvp); - z_drop(z_move(keystr)); z_drop(z_move(value)); z_drop(z_move(encoding)); msg_nb++; diff --git a/examples/unix/c11/z_sub_channel.c b/examples/unix/c11/z_sub_channel.c index f683d6576..da197dc6e 100644 --- a/examples/unix/c11/z_sub_channel.c +++ b/examples/unix/c11/z_sub_channel.c @@ -80,12 +80,11 @@ int main(int argc, char **argv) { z_owned_sample_t sample; z_null(&sample); for (z_recv(z_loan(handler), &sample); z_check(sample); z_recv(z_loan(handler), &sample)) { - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(z_loan(sample)), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(z_loan(sample)), &value); printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); - z_drop(z_move(keystr)); z_drop(z_move(value)); z_drop(z_move(sample)); z_null(&sample); diff --git a/examples/unix/c11/z_sub_st.c b/examples/unix/c11/z_sub_st.c index 410d8566e..2522730b5 100644 --- a/examples/unix/c11/z_sub_st.c +++ b/examples/unix/c11/z_sub_st.c @@ -25,12 +25,11 @@ static int msg_nb = 0; void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); - z_drop(z_move(keystr)); z_drop(z_move(value)); msg_nb++; } diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 819742367..5200fa19f 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -33,14 +33,13 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { (void)(ctx); if (z_reply_is_ok(reply)) { const z_loaned_sample_t *sample = z_reply_ok(reply); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t replystr; z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr); - printf(">> Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)), + printf(">> Received ('%s': '%s')\n", z_string_data(z_view_string_loan(&keystr)), z_string_data(z_string_loan(&replystr))); - z_string_drop(z_string_move(&keystr)); z_string_drop(z_string_move(&replystr)); } else { printf(">> Received an error\n"); diff --git a/examples/unix/c99/z_pull.c b/examples/unix/c99/z_pull.c index 460389878..fb5d89356 100644 --- a/examples/unix/c99/z_pull.c +++ b/examples/unix/c99/z_pull.c @@ -22,12 +22,11 @@ // @TODO // void data_handler(const z_loaned_sample_t *sample, void *ctx) { // (void)(ctx); -// z_owned_string_t keystr; -// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); -// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)), +// z_view_string_t keystr; +// z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); +// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_view_string_loan(&keystr)), // (int)sample->payload.len, // sample->payload.start); -// z_string_drop(z_string_move(&keystr)); // } int main(int argc, char **argv) { diff --git a/examples/unix/c99/z_put.c b/examples/unix/c99/z_put.c index 6053b59f1..9ccde511c 100644 --- a/examples/unix/c99/z_put.c +++ b/examples/unix/c99/z_put.c @@ -103,7 +103,7 @@ int main(int argc, char **argv) { } // Clean up - z_undeclare_keyexpr(z_session_loan(&s), z_keyexpr_move(&ke)); + z_undeclare_keyexpr(z_keyexpr_move(&ke), z_session_loan(&s)); zp_stop_read_task(z_session_loan_mut(&s)); zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index 5ee04a56a..5649c2ca5 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -24,11 +24,11 @@ const char *value = "Queryable from Pico!"; void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t params; z_query_parameters(query, ¶ms); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_string_loan(&keystr)), + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_view_string_loan(&keystr)), (int)z_view_string_loan(¶ms)->len, z_view_string_loan(¶ms)->val); // Process value z_owned_string_t payload_string; @@ -43,7 +43,6 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_bytes_serialize_from_str(&reply_payload, value); z_query_reply(query, z_query_keyexpr(query), z_bytes_move(&reply_payload), NULL); - z_string_drop(z_string_move(&keystr)); } int main(int argc, char **argv) { diff --git a/examples/unix/c99/z_sub.c b/examples/unix/c99/z_sub.c index aa6155ede..d3958e919 100644 --- a/examples/unix/c99/z_sub.c +++ b/examples/unix/c99/z_sub.c @@ -22,13 +22,12 @@ #if Z_FEATURE_SUBSCRIPTION == 1 void data_handler(const z_loaned_sample_t *sample, void *arg) { (void)(arg); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); - printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)), + printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_view_string_loan(&keystr)), z_string_data(z_string_loan(&value))); - z_string_drop(z_string_move(&keystr)); z_string_drop(z_string_move(&value)); } diff --git a/examples/unix/c99/z_sub_st.c b/examples/unix/c99/z_sub_st.c index 8dfd01ae2..764afb05c 100644 --- a/examples/unix/c99/z_sub_st.c +++ b/examples/unix/c99/z_sub_st.c @@ -25,13 +25,12 @@ static int msg_nb = 0; void data_handler(const z_loaned_sample_t *sample, void *arg) { (void)(arg); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); - printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)), + printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_view_string_loan(&keystr)), z_string_data(z_string_loan(&value))); - z_string_drop(z_string_move(&keystr)); z_string_drop(z_string_move(&value)); msg_nb++; } diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index d14cedb80..9437b927e 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -32,13 +32,12 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { (void)(ctx); if (z_reply_is_ok(reply)) { const z_loaned_sample_t *sample = z_reply_ok(reply); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t replystr; z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr); printf(">> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr))); - z_drop(z_move(keystr)); z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); diff --git a/examples/windows/z_pull.c b/examples/windows/z_pull.c index 9256cf4d9..70378b7e5 100644 --- a/examples/windows/z_pull.c +++ b/examples/windows/z_pull.c @@ -21,11 +21,10 @@ // @TODO // void data_handler(const z_loaned_sample_t *sample, void *ctx) { // (void)(ctx); -// z_owned_string_t keystr; -// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// z_view_string_t keystr; +// z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); // printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)sample->payload.len, // sample->payload.start); -// z_drop(z_move(keystr)); // } int main(int argc, char **argv) { diff --git a/examples/windows/z_put.c b/examples/windows/z_put.c index 9ed5ea503..65287dbfa 100644 --- a/examples/windows/z_put.c +++ b/examples/windows/z_put.c @@ -70,7 +70,7 @@ int main(int argc, char **argv) { } // Clean up - z_undeclare_keyexpr(z_loan(s), z_move(ke)); + z_undeclare_keyexpr(z_move(ke), z_loan(s)); zp_stop_read_task(z_loan_mut(s)); zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index 2b739e97f..acb60d680 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -23,8 +23,8 @@ const char *value = "Queryable from Pico!"; void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t params; z_query_parameters(query, ¶ms); @@ -43,8 +43,6 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_bytes_serialize_from_str(&reply_payload, value); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); - - z_drop(z_move(keystr)); } int main(int argc, char **argv) { diff --git a/examples/windows/z_sub.c b/examples/windows/z_sub.c index 3084b807b..b1861728f 100644 --- a/examples/windows/z_sub.c +++ b/examples/windows/z_sub.c @@ -21,12 +21,11 @@ #if Z_FEATURE_SUBSCRIPTION == 1 void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); - z_drop(z_move(keystr)); z_drop(z_move(value)); } diff --git a/examples/windows/z_sub_st.c b/examples/windows/z_sub_st.c index b33e5d248..6d71883eb 100644 --- a/examples/windows/z_sub_st.c +++ b/examples/windows/z_sub_st.c @@ -24,12 +24,11 @@ int msg_nb = 0; #if Z_FEATURE_SUBSCRIPTION == 1 void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); - z_drop(z_move(keystr)); z_drop(z_move(value)); msg_nb++; } diff --git a/examples/zephyr/z_get.c b/examples/zephyr/z_get.c index baa7610be..380efa1c5 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -37,13 +37,12 @@ void reply_dropper(void *ctx) { printf(" >> Received query final notification\n" void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { if (z_reply_is_ok(oreply)) { const z_loaned_sample_t *sample = z_reply_ok(oreply); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t replystr; z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr); printf(" >> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr))); - z_drop(z_move(keystr)); z_drop(z_move(replystr)); } else { printf(" >> Received an error\n"); diff --git a/examples/zephyr/z_pull.c b/examples/zephyr/z_pull.c index 007b20ba5..c0e9d4b9a 100644 --- a/examples/zephyr/z_pull.c +++ b/examples/zephyr/z_pull.c @@ -31,12 +31,11 @@ // @TODO // void data_handler(const z_loaned_sample_t *sample, void *arg) { -// z_owned_string_t keystr; -// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// z_view_string_t keystr; +// z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); // printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), // (int)sample->payload.len, // sample->payload.start); -// z_drop(z_move(keystr)); // } int main(int argc, char **argv) { diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index eb4b9e1e2..d30daea95 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -33,8 +33,8 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr); z_view_string_t params; z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), z_loan(params)->len, @@ -52,7 +52,6 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_bytes_serialize_from_str(&reply_payload, VALUE); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); - z_drop(z_move(keystr)); } int main(int argc, char **argv) { diff --git a/examples/zephyr/z_sub.c b/examples/zephyr/z_sub.c index 6c1b03de0..e1e278343 100644 --- a/examples/zephyr/z_sub.c +++ b/examples/zephyr/z_sub.c @@ -30,13 +30,12 @@ #define KEYEXPR "demo/example/**" void data_handler(const z_loaned_sample_t *sample, void *arg) { - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); - z_drop(z_move(keystr)); z_drop(z_move(value)); } diff --git a/include/zenoh-pico/api/constants.h b/include/zenoh-pico/api/constants.h index d65ade1df..2f396d4a5 100644 --- a/include/zenoh-pico/api/constants.h +++ b/include/zenoh-pico/api/constants.h @@ -83,6 +83,22 @@ typedef enum { Z_KEYEXPR_CANON_CONTAINS_UNBOUND_DOLLAR = -8 } zp_keyexpr_canon_status_t; +/** + * Intersection level of 2 key expressions. + * + * Enumerators: + * Z_KEYEXPR_INTERSECTION_LEVEL_DISJOINT: 2 key expression do not intersect. + * Z_KEYEXPR_INTERSECTION_LEVEL_INTERSECTS: 2 key expressions intersect, i.e. there exists at least one key expression + * that is included by both. Z_KEYEXPR_INTERSECTION_LEVEL_INCLUDES: First key expression is the superset of second one. + * Z_KEYEXPR_INTERSECTION_LEVEL_EQUALS: 2 key expressions are equal. + */ +typedef enum { + Z_KEYEXPR_INTERSECTION_LEVEL_DISJOINT = 0, + Z_KEYEXPR_INTERSECTION_LEVEL_INTERSECTS = 1, + Z_KEYEXPR_INTERSECTION_LEVEL_INCLUDES = 2, + Z_KEYEXPR_INTERSECTION_LEVEL_EQUALS = 3, +} z_keyexpr_intersection_level_t; + /** * Sample kind values. * diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 5cac0d486..1fd710715 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -139,7 +139,7 @@ #define z_check(x) _Generic((x), \ z_owned_keyexpr_t : z_keyexpr_check, \ - z_view_keyexpr_t : z_keyexpr_is_initialized, \ + z_view_keyexpr_t : z_view_keyexpr_check, \ z_owned_reply_err_t : z_reply_err_check, \ z_owned_config_t : z_config_check, \ z_owned_session_t : z_session_check, \ @@ -149,6 +149,7 @@ z_owned_reply_t : z_reply_check, \ z_owned_hello_t : z_hello_check, \ z_owned_string_t : z_string_check, \ + z_view_string_t : z_view_string_check, \ z_owned_string_array_t : z_string_array_check, \ z_owned_slice_t : z_slice_check, \ z_owned_bytes_t : z_bytes_check, \ @@ -437,6 +438,7 @@ inline void z_null(z_owned_fifo_handler_sample_t* v) { return z_fifo_handler_sam inline bool z_check(const z_owned_session_t& v) { return z_session_check(&v); } inline bool z_check(const z_owned_publisher_t& v) { return z_publisher_check(&v); } inline bool z_check(const z_owned_keyexpr_t& v) { return z_keyexpr_check(&v); } +inline bool z_check(const z_view_keyexpr_t& v) { return z_view_keyexpr_check(&v); } inline bool z_check(const z_owned_config_t& v) { return z_config_check(&v); } inline bool z_check(const z_owned_subscriber_t& v) { return z_subscriber_check(&v); } inline bool z_check(const z_owned_queryable_t& v) { return z_queryable_check(&v); } @@ -444,6 +446,7 @@ inline bool z_check(const z_owned_reply_t& v) { return z_reply_check(&v); } inline bool z_check(const z_owned_query_t& v) { return z_query_check(&v); } inline bool z_check(const z_owned_hello_t& v) { return z_hello_check(&v); } inline bool z_check(const z_owned_string_t& v) { return z_string_check(&v); } +inline bool z_check(const z_view_string_t& v) { return z_view_string_check(&v); } inline bool z_check(const z_owned_sample_t& v) { return z_sample_check(&v); } inline bool z_check(const z_owned_bytes_t& v) { return z_bytes_check(&v); } inline bool z_check(const z_owned_bytes_writer_t& v) { return z_bytes_writer_check(&v); } diff --git a/include/zenoh-pico/api/olv_macros.h b/include/zenoh-pico/api/olv_macros.h index bd24a9c2e..7364d53c6 100644 --- a/include/zenoh-pico/api/olv_macros.h +++ b/include/zenoh-pico/api/olv_macros.h @@ -74,6 +74,7 @@ void z_##name##_null(z_owned_##name##_t *obj); #define _Z_VIEW_FUNCTIONS_DEF(name) \ + _Bool z_view_##name##_check(const z_view_##name##_t *obj); \ const z_loaned_##name##_t *z_view_##name##_loan(const z_view_##name##_t *name); \ z_loaned_##name##_t *z_view_##name##_loan_mut(z_view_##name##_t *name); \ void z_view_##name##_null(z_view_##name##_t *name); @@ -152,7 +153,8 @@ void z_##name##_null(z_owned_##name##_t *obj) { (void)obj; } \ z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj) { return obj; } -#define _Z_VIEW_FUNCTIONS_IMPL(type, name) \ +#define _Z_VIEW_FUNCTIONS_IMPL(type, name, f_check) \ + _Bool z_view_##name##_check(const z_view_##name##_t *obj) { return f_check((&obj->_val)); } \ const z_loaned_##name##_t *z_view_##name##_loan(const z_view_##name##_t *obj) { return &obj->_val; } \ z_loaned_##name##_t *z_view_##name##_loan_mut(z_view_##name##_t *obj) { return &obj->_val; } diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 32489cae7..a61e7cdb0 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -98,7 +98,7 @@ int8_t z_view_keyexpr_from_str(z_view_keyexpr_t *keyexpr, const char *name); int8_t z_view_keyexpr_from_str_unchecked(z_view_keyexpr_t *keyexpr, const char *name); /** - * Builds a :c:type:`z_keyexpr_t` from a null-terminated string with auto canonization. + * 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). @@ -114,16 +114,63 @@ 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); /** - * Gets a null-terminated string from a :c:type:`z_keyexpr_t`. + * Gets a null-terminated string view from a :c:type:`z_keyexpr_t`. * * Parameters: * keyexpr: Pointer to a loaned instance of :c:type:`z_keyexpr_t`. - * str: Pointer to an uninitialized :c:type:`z_owned_string_t`. + * str: Pointer to an uninitialized :c:type:`z_view_string_t`. * * Return: * ``0`` if creation successful, ``negative value`` otherwise. */ -int8_t z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *str); +int8_t z_keyexpr_as_view_string(const z_loaned_keyexpr_t *keyexpr, z_view_string_t *str); + +/** + * Constructs key expression by concatenation of key expression in `left` with a string in `right`. + * Returns 0 in case of success, negative error code otherwise. + * + * To avoid odd behaviors, concatenating a key expression starting with `*` to one ending with `*` is forbidden by this + * operation, as this would extremely likely cause bugs. + * + * Parameters: + * keyexpr: Pointer to an uninitialized :c:type:`z_owned_keyexpr_t` to store the keyexpr. + * left: Pointer to :c:type:`z_loaned_keyexpr_t` to keyexpr to concatenate to. + * right: Pointer to the start of the substring that will be concatenated. + * len: Length of the substring to concatenate. + * + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. + */ +int8_t z_keyexpr_concat(z_owned_keyexpr_t *key, const z_loaned_keyexpr_t *left, const char *right, size_t len); + +/** + * Constructs key expression by performing path-joining (automatically inserting '/'). The resulting key expression is + * automatically canonized. + * + * Parameters: + * keyexpr: Pointer to an uninitialized :c:type:`z_owned_keyexpr_t` to store the keyexpr. + * left: Pointer to :c:type:`z_loaned_keyexpr_t` to the left part of resulting key expression. + * right: Pointer to :c:type:`z_loaned_keyexpr_t` to the right part of resulting key expression. + * + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. + */ +int8_t z_keyexpr_join(z_owned_keyexpr_t *key, const z_loaned_keyexpr_t *left, const z_loaned_keyexpr_t *right); + +/** + * Returns the relation between `left` and `right` from `left`'s point of view. + * + * Note that this is slower than `z_keyexpr_intersects` and `keyexpr_includes`, so you should favor these methods for + * most applications. + * + * Parameters: + * left: Pointer to :c:type:`z_loaned_keyexpr_t` representing left key expression. + * right: Pointer to :c:type:`z_loaned_keyexpr_t` representing right key expression. + * + * Return: + * Relation between `left` and `right` from `left`'s point of view. + */ +z_keyexpr_intersection_level_t z_keyexpr_relation_to(const z_loaned_keyexpr_t *left, const z_loaned_keyexpr_t *right); /** * Checks if a given keyexpr is valid. @@ -1687,6 +1734,45 @@ int8_t z_query_reply_del(const z_loaned_query_t *query, const z_loaned_keyexpr_t */ int8_t z_keyexpr_from_str(z_owned_keyexpr_t *keyexpr, const char *name); +/** + * Builds a new keyexpr from a substring. + * + * Parameters: + * keyexpr: Pointer to an uninitialized :c:type:`z_owned_keyexpr_t` to store the keyexpr. + * name: Pointer to the start of the substring for keyxpr. + * len: Length of the substring to consider. + * + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. + */ +int8_t z_keyexpr_from_substr(z_owned_keyexpr_t *keyexpr, const char *name, size_t len); + +/** + * Builds a :c:type:`z_owned_keyexpr_t` from a null-terminated string with auto canonization. + * + * Parameters: + * keyexpr: Pointer to an uninitialized :c:type:`z_owned_keyexpr_t`. + * name: Pointer to string representation of the keyexpr as a null terminated string. + * + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. + */ +int8_t z_keyexpr_from_str_autocanonize(z_owned_keyexpr_t *keyexpr, const char *name); + +/** + * Builds a :c:type:`z_owned_keyexpr_t` from a substring with auto canonization. + * + * Parameters: + * keyexpr: Pointer to an uninitialized :c:type:`z_owned_keyexpr_t` to store the keyexpr. + * name: Pointer to the start of the substring for keyxpr. + * len: Length of the substring to consider. After the function return it will be equal to the canonized key + * expression string length. + * + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. + */ +int8_t z_keyexpr_from_substr_autocanonize(z_owned_keyexpr_t *keyexpr, const char *name, size_t *len); + /** * Declares a keyexpr, so that it is mapped on a numerical id. * @@ -1694,27 +1780,27 @@ int8_t z_keyexpr_from_str(z_owned_keyexpr_t *keyexpr, const char *name); * in the routing tables. * * Parameters: - * ke: Pointer to an uninitialized :c:type:`z_owned_keyexpr_t` to contain the declared keyexpr. + * declared_keyexpr: Pointer to an uninitialized :c:type:`z_owned_keyexpr_t` to contain the declared keyexpr. * zs: Pointer to a :c:type:`z_loaned_session_t` to declare the keyexpr through. * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to bind the keyexpr with. * * Return: * ``0`` if declare successful, ``negative value`` otherwise. */ -int8_t z_declare_keyexpr(z_owned_keyexpr_t *ke, const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr); +int8_t z_declare_keyexpr(z_owned_keyexpr_t *declared_keyexpr, const z_loaned_session_t *zs, + const z_loaned_keyexpr_t *keyexpr); /** * Undeclares a keyexpr. * * Parameters: - * zs: Pointer to a :c:type:`z_loaned_session_t` to undeclare the data through. * keyexpr: Pointer to a moved :c:type:`z_owned_keyexpr_t` to undeclare. + * zs: Pointer to a :c:type:`z_loaned_session_t` to undeclare the data through. * * Return: * ``0`` if undeclare successful, ``negative value`` otherwise. */ -// TODO(sashacmc): change parameters order? -int8_t z_undeclare_keyexpr(const z_loaned_session_t *zs, z_owned_keyexpr_t *keyexpr); +int8_t z_undeclare_keyexpr(z_owned_keyexpr_t *keyexpr, const z_loaned_session_t *zs); #if Z_FEATURE_SUBSCRIPTION == 1 /** diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 2b5bf0feb..863b6a2a7 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -106,7 +106,7 @@ _Z_VIEW_TYPE(_z_string_t, string) * * - :c:func:`z_keyexpr_from_str` * - :c:func:`z_keyexpr_is_initialized` - * - :c:func:`z_keyexpr_to_string` + * - :c:func:`z_keyexpr_as_view_string` */ _Z_OWNED_TYPE_VALUE(_z_keyexpr_t, keyexpr) _Z_LOANED_TYPE(_z_keyexpr_t, keyexpr) diff --git a/src/api/api.c b/src/api/api.c index d22df34be..cd81f222a 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -90,7 +90,7 @@ int8_t z_view_keyexpr_from_str(z_view_keyexpr_t *keyexpr, const char *name) { } int8_t z_view_keyexpr_from_str_autocanonize(z_view_keyexpr_t *keyexpr, char *name) { - zp_keyexpr_canonize_null_terminated(name); + _Z_RETURN_IF_ERR(zp_keyexpr_canonize_null_terminated(name)); keyexpr->_val = _z_rname(name); return _Z_RES_OK; } @@ -100,16 +100,78 @@ int8_t z_view_keyexpr_from_str_unchecked(z_view_keyexpr_t *keyexpr, const char * return _Z_RES_OK; } -int8_t z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *s) { +int8_t z_keyexpr_as_view_string(const z_loaned_keyexpr_t *keyexpr, z_view_string_t *s) { int8_t ret = _Z_RES_OK; - s->_val = _z_string_make(keyexpr->_suffix); - if (!_z_string_check(&s->_val)) { - ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; + if (keyexpr->_id == Z_RESOURCE_ID_NONE) { + s->_val = _z_string_wrap(keyexpr->_suffix); + } else { + ret = _Z_ERR_GENERIC; } return ret; } +int8_t z_keyexpr_concat(z_owned_keyexpr_t *key, const z_loaned_keyexpr_t *left, const char *right, size_t len) { + z_keyexpr_null(key); + if (len == 0) { + return z_keyexpr_clone(key, left); + } else if (right == NULL) { + return _Z_ERR_INVALID; + } + size_t left_len = strlen(left->_suffix); + if (left_len == 0) { + return _Z_ERR_INVALID; + } + if (left->_suffix[left_len - 1] == '*' && right[0] == '*') { + return _Z_ERR_INVALID; + } + + char *s = z_malloc(left_len + len + 1); + if (s == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + s[left_len + len] = '\0'; + + memcpy(s, left->_suffix, left_len); + memcpy(s + left_len, right, len); + + key->_val = _z_rname(s); + _z_keyexpr_set_owns_suffix(&key->_val, true); + return _Z_RES_OK; +} + +int8_t z_keyexpr_join(z_owned_keyexpr_t *key, const z_loaned_keyexpr_t *left, const z_loaned_keyexpr_t *right) { + z_keyexpr_null(key); + + size_t left_len = strlen(left->_suffix); + size_t right_len = strlen(right->_suffix); + + char *s = z_malloc(left_len + right_len + 2); + if (s == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + s[left_len + right_len + 1] = '\0'; + s[left_len] = '/'; + memcpy(s, left->_suffix, left_len); + memcpy(s + left_len + 1, right->_suffix, right_len); + + _Z_CLEAN_RETURN_IF_ERR(zp_keyexpr_canonize_null_terminated(s), z_free(s)); + key->_val = _z_rname(s); + _z_keyexpr_set_owns_suffix(&key->_val, true); + return _Z_RES_OK; +} + +z_keyexpr_intersection_level_t z_keyexpr_relation_to(const z_loaned_keyexpr_t *left, const z_loaned_keyexpr_t *right) { + if (z_keyexpr_equals(left, right)) { + return Z_KEYEXPR_INTERSECTION_LEVEL_EQUALS; + } else if (z_keyexpr_includes(left, right)) { + return Z_KEYEXPR_INTERSECTION_LEVEL_INCLUDES; + } else if (z_keyexpr_intersects(left, right)) { + return Z_KEYEXPR_INTERSECTION_LEVEL_INTERSECTS; + } + return Z_KEYEXPR_INTERSECTION_LEVEL_DISJOINT; +} + _Bool z_keyexpr_is_initialized(const z_loaned_keyexpr_t *keyexpr) { _Bool ret = false; @@ -127,10 +189,7 @@ int8_t zp_keyexpr_is_canon_null_terminated(const char *start) { return _z_keyexp int8_t z_keyexpr_canonize(char *start, size_t *len) { return _z_keyexpr_canonize(start, len); } _Bool z_keyexpr_includes(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r) { - if ((l->_id == Z_RESOURCE_ID_NONE) && (r->_id == Z_RESOURCE_ID_NONE)) { - return zp_keyexpr_includes_null_terminated(l->_suffix, r->_suffix); - } - return false; + return zp_keyexpr_includes_null_terminated(l->_suffix, r->_suffix); } _Bool zp_keyexpr_includes_null_terminated(const char *l, const char *r) { @@ -141,10 +200,7 @@ _Bool zp_keyexpr_includes_null_terminated(const char *l, const char *r) { } _Bool z_keyexpr_intersects(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r) { - if ((l->_id == Z_RESOURCE_ID_NONE) && (r->_id == Z_RESOURCE_ID_NONE)) { - return zp_keyexpr_intersect_null_terminated(l->_suffix, r->_suffix); - } - return false; + return zp_keyexpr_intersect_null_terminated(l->_suffix, r->_suffix); } _Bool zp_keyexpr_intersect_null_terminated(const char *l, const char *r) { @@ -155,10 +211,7 @@ _Bool zp_keyexpr_intersect_null_terminated(const char *l, const char *r) { } _Bool z_keyexpr_equals(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r) { - if ((l->_id == Z_RESOURCE_ID_NONE) && (r->_id == Z_RESOURCE_ID_NONE)) { - return zp_keyexpr_equals_null_terminated(l->_suffix, r->_suffix); - } - return false; + return zp_keyexpr_equals_null_terminated(l->_suffix, r->_suffix); } _Bool zp_keyexpr_equals_null_terminated(const char *l, const char *r) { @@ -643,8 +696,8 @@ _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_value_t, reply_err, _z_value_check, _z_value_nu _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_keyexpr_t, keyexpr, _z_keyexpr_check, _z_keyexpr_null, _z_keyexpr_copy, _z_keyexpr_clear) -_Z_VIEW_FUNCTIONS_IMPL(_z_keyexpr_t, keyexpr) -_Z_VIEW_FUNCTIONS_IMPL(_z_string_t, string) +_Z_VIEW_FUNCTIONS_IMPL(_z_keyexpr_t, keyexpr, _z_keyexpr_check) +_Z_VIEW_FUNCTIONS_IMPL(_z_string_t, string, _z_string_check) _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_hello_t, hello, _z_hello_check, _z_hello_null, _z_hello_copy, _z_hello_clear) @@ -684,7 +737,7 @@ int8_t _z_string_array_copy(_z_string_svec_t *dst, const _z_string_svec_t *src) _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_VIEW_FUNCTIONS_IMPL(_z_string_vec_t, string_array) +_Z_VIEW_FUNCTIONS_IMPL(_z_string_vec_t, string_array, _z_string_array_check) _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_slice_t, slice, _z_slice_check, _z_slice_empty, _z_slice_copy, _z_slice_clear) _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_bytes_t, bytes, _z_bytes_check, _z_bytes_null, _z_bytes_copy, _z_bytes_drop) @@ -1266,12 +1319,37 @@ int8_t z_query_reply_del(const z_loaned_query_t *query, const z_loaned_keyexpr_t } #endif +int8_t z_keyexpr_from_str_autocanonize(z_owned_keyexpr_t *key, const char *name) { + size_t len = strlen(name); + return z_keyexpr_from_substr_autocanonize(key, name, &len); +} + +int8_t z_keyexpr_from_substr_autocanonize(z_owned_keyexpr_t *key, const char *name, size_t *len) { + z_keyexpr_null(key); + char *name_copy = _z_str_n_clone(name, *len); + if (name_copy == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + + _Z_CLEAN_RETURN_IF_ERR(z_keyexpr_canonize(name_copy, len), z_free(name_copy)); + name_copy[*len] = '\0'; + key->_val = _z_rname(name_copy); + _z_keyexpr_set_owns_suffix(&key->_val, true); + return _Z_RES_OK; +} + int8_t z_keyexpr_from_str(z_owned_keyexpr_t *key, const char *name) { - if (name != NULL) { - key->_val = _z_rid_with_suffix(Z_RESOURCE_ID_NONE, name); - } else { - key->_val = _z_keyexpr_null(); + return z_keyexpr_from_substr(key, name, strlen(name)); +} + +int8_t z_keyexpr_from_substr(z_owned_keyexpr_t *key, const char *name, size_t len) { + z_keyexpr_null(key); + char *name_copy = _z_str_n_clone(name, len); + if (name_copy == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } + key->_val = _z_rname(name_copy); + _z_keyexpr_set_owns_suffix(&key->_val, true); return _Z_RES_OK; } @@ -1279,7 +1357,9 @@ int8_t z_declare_keyexpr(z_owned_keyexpr_t *key, const z_loaned_session_t *zs, c _z_keyexpr_t k = _z_keyexpr_alias_from_user_defined(*keyexpr, false); uint16_t id = _z_declare_resource(&_Z_RC_IN_VAL(zs), k); key->_val = _z_rid_with_suffix(id, NULL); - if (keyexpr->_suffix) key->_val._suffix = _z_str_clone(keyexpr->_suffix); + if (keyexpr->_suffix) { + key->_val._suffix = _z_str_clone(keyexpr->_suffix); + } // we still need to store the original suffix, for user needs // (for example to compare keys or perform other operations on their string representation). // Generally this breaks internal keyexpr representation, but is ok for user-defined keyexprs @@ -1292,7 +1372,7 @@ int8_t z_declare_keyexpr(z_owned_keyexpr_t *key, const z_loaned_session_t *zs, c return _Z_RES_OK; } -int8_t z_undeclare_keyexpr(const z_loaned_session_t *zs, z_owned_keyexpr_t *keyexpr) { +int8_t z_undeclare_keyexpr(z_owned_keyexpr_t *keyexpr, const z_loaned_session_t *zs) { int8_t ret = _Z_RES_OK; ret = _z_undeclare_resource(&_Z_RC_IN_VAL(zs), keyexpr->_val._id); diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 9b8c88e92..f17e89ad4 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -65,8 +65,8 @@ void query_handler(const z_loaned_query_t *query, void *arg) { queries++; const z_loaned_keyexpr_t *query_ke = z_query_keyexpr(query); - z_owned_string_t k_str; - z_keyexpr_to_string(query_ke, &k_str); + z_view_string_t k_str; + z_keyexpr_as_view_string(query_ke, &k_str); (void)arg; assert(z_check(k_str)); @@ -83,8 +83,6 @@ void query_handler(const z_loaned_query_t *query, void *arg) { z_bytes_serialize_from_str(&reply_payload, value); z_query_reply(query, query_ke, z_move(reply_payload), &_ret_qreply_opt); - - z_drop(z_move(k_str)); } volatile unsigned int replies = 0; @@ -96,10 +94,10 @@ void reply_handler(const z_loaned_reply_t *reply, void *arg) { if (z_reply_is_ok(reply)) { const z_loaned_sample_t *sample = z_reply_ok(reply); - z_owned_string_t k_str; - z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str); + z_view_string_t k_str; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &k_str); + (void)arg; assert(z_check(k_str)); - z_drop(z_move(k_str)); } else { const z_loaned_reply_err_t *_ret_zerr = z_reply_err(reply); (void)(_ret_zerr); @@ -111,11 +109,10 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { printf("%s\n", __func__); datas++; - z_owned_string_t k_str; - z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str); - assert(z_check(k_str)); + z_view_string_t k_str; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &k_str); (void)arg; - z_drop(z_move(k_str)); + assert(z_check(k_str)); } int main(int argc, char **argv) { @@ -324,7 +321,7 @@ int main(int argc, char **argv) { assert_eq(datas, 2); printf("Undeclaring Keyexpr..."); - _ret_int8 = z_undeclare_keyexpr(z_loan(s1), z_move(_ret_expr)); + _ret_int8 = z_undeclare_keyexpr(z_move(_ret_expr), z_loan(s1)); printf(" %02x\n", _ret_int8); assert_eq(_ret_int8, 0); assert(!z_check(_ret_expr)); diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 64a1f19cb..68ad15f65 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -52,8 +52,8 @@ void query_handler(const z_loaned_query_t *query, void *arg) { snprintf(res, 64, "%s%u", uri, *(unsigned int *)arg); printf(">> Received query: %s\t(%u/%u)\n", res, queries, total); - z_owned_string_t k_str; - z_keyexpr_to_string(z_query_keyexpr(query), &k_str); + z_view_string_t k_str; + z_keyexpr_as_view_string(z_query_keyexpr(query), &k_str); assert(_z_str_eq(z_loan(k_str)->val, res) == true); z_view_string_t pred; @@ -67,7 +67,6 @@ void query_handler(const z_loaned_query_t *query, void *arg) { z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); queries++; - z_drop(z_move(k_str)); free(res); } @@ -79,8 +78,8 @@ void reply_handler(const z_loaned_reply_t *reply, void *arg) { const z_loaned_sample_t *sample = z_reply_ok(reply); printf(">> Received reply data: %s\t(%u/%u)\n", res, replies, total); - z_owned_string_t k_str; - z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str); + z_view_string_t k_str; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &k_str); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); assert(z_string_len(z_loan(value)) == strlen(res)); @@ -88,7 +87,6 @@ void reply_handler(const z_loaned_reply_t *reply, void *arg) { assert(_z_str_eq(z_loan(k_str)->val, res) == true); replies++; - z_drop(z_move(k_str)); z_drop(z_move(value)); } else { printf(">> Received an error\n"); @@ -102,8 +100,8 @@ 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_owned_string_t k_str; - z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str); + z_view_string_t k_str; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &k_str); z_owned_slice_t value; z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); size_t payload_len = z_slice_len(z_loan(value)); @@ -111,7 +109,6 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { assert(_z_str_eq(z_loan(k_str)->val, res) == true); datas++; - z_drop(z_move(k_str)); z_drop(z_move(value)); free(res); } @@ -379,14 +376,14 @@ int main(int argc, char **argv) { // Undeclare resources on both sessions for (unsigned int i = 0; i < SET; i++) { printf("Undeclared resource on session 1: %u\n", z_loan(rids1[i])->_id); - z_undeclare_keyexpr(z_loan(s1), z_move(rids1[i])); + z_undeclare_keyexpr(z_move(rids1[i]), z_loan(s1)); } z_sleep_s(SLEEP); for (unsigned int i = 0; i < SET; i++) { printf("Undeclared resource on session 2: %u\n", z_loan(rids2[i])->_id); - z_undeclare_keyexpr(z_loan(s2), z_move(rids2[i])); + z_undeclare_keyexpr(z_move(rids2[i]), z_loan(s2)); } z_sleep_s(SLEEP); diff --git a/tests/z_keyexpr_test.c b/tests/z_keyexpr_test.c index 2d6aa68fd..94bdc459a 100644 --- a/tests/z_keyexpr_test.c +++ b/tests/z_keyexpr_test.c @@ -20,7 +20,7 @@ #undef NDEBUG #include -int main(void) { +void test_intersects(void) { assert(_z_keyexpr_intersects("a", strlen("a"), "a", strlen("a"))); assert(_z_keyexpr_intersects("a/b", strlen("a/b"), "a/b", strlen("a/b"))); assert(_z_keyexpr_intersects("*", strlen("*"), "abc", strlen("abc"))); @@ -199,7 +199,9 @@ int main(void) { assert((zp_keyexpr_intersect_null_terminated("@a/@b/**", "@a/@b"))); assert((zp_keyexpr_intersect_null_terminated("@a/**/@c/@b", "@a/**/@c/**/@b"))); assert((zp_keyexpr_intersect_null_terminated("@a/**/@c/**/@b", "@a/**/@c/@b"))); +} +void test_includes(void) { assert(_z_keyexpr_includes("a", strlen("a"), "a", strlen("a"))); assert(_z_keyexpr_includes("a/b", strlen("a/b"), "a/b", strlen("a/b"))); assert(_z_keyexpr_includes("*", strlen("*"), "a", strlen("a"))); @@ -282,7 +284,9 @@ int main(void) { assert(!zp_keyexpr_includes_null_terminated("@a/**", "@a/@b")); assert((zp_keyexpr_includes_null_terminated("@a/**/@b", "@a/@b"))); assert((zp_keyexpr_includes_null_terminated("@a/@b/**", "@a/@b"))); +} +void test_canonize(void) { // clang-format off #define N 31 @@ -414,10 +418,108 @@ int main(void) { } printf("\n"); } +} +void test_equals(void) { assert(!zp_keyexpr_equals_null_terminated("a/**/$*b", "a/cb")); assert(!zp_keyexpr_equals_null_terminated("a/bc", "a/cb")); assert(zp_keyexpr_equals_null_terminated("greetings/hello/there", "greetings/hello/there")); +} + +_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); + return strncmp(z_string_data(z_view_string_loan(&vs)), s, z_string_len(z_view_string_loan(&vs))) == 0; +} + +void test_keyexpr_constructor(void) { + z_owned_keyexpr_t ke; + z_keyexpr_from_str(&ke, "a/b/c"); + assert(keyexpr_equals_string(z_keyexpr_loan(&ke), "a/b/c")); + z_keyexpr_drop(z_keyexpr_move(&ke)); + + z_keyexpr_from_substr(&ke, "a/b/c/d/e", 5); + assert(keyexpr_equals_string(z_keyexpr_loan(&ke), "a/b/c")); + z_keyexpr_drop(z_keyexpr_move(&ke)); + + assert(0 == z_keyexpr_from_str_autocanonize(&ke, "a/**/**")); + assert(keyexpr_equals_string(z_keyexpr_loan(&ke), "a/**")); + z_keyexpr_drop(z_keyexpr_move(&ke)); + + size_t len = 9; + assert(0 == z_keyexpr_from_substr_autocanonize(&ke, "a/**/**/m/b/c", &len)); + assert(keyexpr_equals_string(z_keyexpr_loan(&ke), "a/**/m")); + assert(len == 6); + z_keyexpr_drop(z_keyexpr_move(&ke)); + + z_view_keyexpr_t vke; + z_view_keyexpr_from_str(&vke, "a/b/c"); + assert(keyexpr_equals_string(z_view_keyexpr_loan(&vke), "a/b/c")); + + char s[] = "a/**/**"; + z_view_keyexpr_from_str_autocanonize(&vke, s); + assert(keyexpr_equals_string(z_view_keyexpr_loan(&vke), "a/**")); +} + +void test_concat(void) { + z_owned_keyexpr_t ke1, ke2; + z_keyexpr_from_str(&ke1, "a/b/c/*"); + assert(0 == z_keyexpr_concat(&ke2, z_keyexpr_loan(&ke1), "/d/e/*", 4)); + assert(keyexpr_equals_string(z_keyexpr_loan(&ke2), "a/b/c/*/d/e")); + z_keyexpr_drop(z_keyexpr_move(&ke2)); + + assert(0 != z_keyexpr_concat(&ke2, z_keyexpr_loan(&ke1), "*/e/*", 3)); + assert(!z_keyexpr_check(&ke2)); + + z_keyexpr_drop(z_keyexpr_move(&ke1)); +} + +void test_join(void) { + z_owned_keyexpr_t ke1, ke2, ke3; + z_keyexpr_from_str(&ke1, "a/b/c/*"); + z_keyexpr_from_str(&ke2, "d/e/*"); + assert(0 == z_keyexpr_join(&ke3, z_keyexpr_loan(&ke1), z_keyexpr_loan(&ke2))); + assert(keyexpr_equals_string(z_keyexpr_loan(&ke3), "a/b/c/*/d/e/*")); + z_keyexpr_drop(z_keyexpr_move(&ke1)); + z_keyexpr_drop(z_keyexpr_move(&ke2)); + z_keyexpr_drop(z_keyexpr_move(&ke3)); + + z_keyexpr_from_str(&ke1, "a/*/**"); + z_keyexpr_from_str(&ke2, "**/d/e/c"); + + assert(0 == z_keyexpr_join(&ke3, z_keyexpr_loan(&ke1), z_keyexpr_loan(&ke2))); + assert(keyexpr_equals_string(z_keyexpr_loan(&ke3), "a/*/**/d/e/c")); + + z_keyexpr_drop(z_keyexpr_move(&ke1)); + z_keyexpr_drop(z_keyexpr_move(&ke2)); + z_keyexpr_drop(z_keyexpr_move(&ke3)); +} + +void test_relation_to(void) { + z_view_keyexpr_t foobar, foostar, barstar; + z_view_keyexpr_from_str(&foobar, "foo/bar"); + z_view_keyexpr_from_str(&foostar, "foo/*"); + z_view_keyexpr_from_str(&barstar, "bar/*"); + + assert(z_keyexpr_relation_to(z_view_keyexpr_loan(&foostar), z_view_keyexpr_loan(&foobar)) == + Z_KEYEXPR_INTERSECTION_LEVEL_INCLUDES); + assert(z_keyexpr_relation_to(z_view_keyexpr_loan(&foobar), z_view_keyexpr_loan(&foostar)) == + Z_KEYEXPR_INTERSECTION_LEVEL_INTERSECTS); + assert(z_keyexpr_relation_to(z_view_keyexpr_loan(&foostar), z_view_keyexpr_loan(&foostar)) == + Z_KEYEXPR_INTERSECTION_LEVEL_EQUALS); + assert(z_keyexpr_relation_to(z_view_keyexpr_loan(&barstar), z_view_keyexpr_loan(&foobar)) == + Z_KEYEXPR_INTERSECTION_LEVEL_DISJOINT); +} + +int main(void) { + test_intersects(); + test_includes(); + test_canonize(); + test_equals(); + test_keyexpr_constructor(); + test_concat(); + test_join(); + test_relation_to(); return 0; } diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 7bfdf78df..fec2764ac 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -46,8 +46,8 @@ 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_owned_string_t k_str; - z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str); + z_view_string_t k_str; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &k_str); z_owned_slice_t value; z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); assert(z_slice_len(z_loan(value)) == MSG_LEN); @@ -56,7 +56,6 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { (void)(sample); datas++; - z_drop(z_move(k_str)); z_drop(z_move(value)); free(res); } diff --git a/tests/z_test_fragment_rx.c b/tests/z_test_fragment_rx.c index c2678e8ea..b0fb8395f 100644 --- a/tests/z_test_fragment_rx.c +++ b/tests/z_test_fragment_rx.c @@ -20,8 +20,8 @@ #if Z_FEATURE_SUBSCRIPTION == 1 void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_string_t keystr; - z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_view_string_t keystr; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); bool is_valid = true; z_owned_slice_t value; z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); @@ -35,7 +35,6 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { } printf("[rx]: Received packet on %s, len: %d, validity: %d\n", z_string_data(z_loan(keystr)), (int)data_len, is_valid); - z_drop(z_move(keystr)); z_drop(z_move(value)); } diff --git a/zenohpico.pc b/zenohpico.pc index e51f4bbb9..c93696ff5 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 1.0.20240704dev +Version: 1.0.20240709dev Cflags: -I${prefix}/include Libs: -L${prefix}/lib -lzenohpico