From 6dc5fb8803613177b3305d3d547bf155778cd83d Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 9 Oct 2024 18:45:12 +0200 Subject: [PATCH] reintroduce z_encoding_equals; fix encoding_from_substr to work similarly to zenoh-rust; --- src/api/api.c | 4 ++++ src/api/encoding.c | 11 ++++++++--- tests/z_api_encoding_test.c | 12 ++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 313649691..727ef0462 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -215,6 +215,10 @@ z_result_t zp_config_insert(z_loaned_config_t *config, uint8_t key, const char * _Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_encoding_t, encoding, _z_encoding_check, _z_encoding_null, _z_encoding_copy, _z_encoding_clear) +bool z_encoding_equals(const z_loaned_encoding_t *left, const z_loaned_encoding_t *right) { + return left->id == right->id && _z_string_equals(&left->schema, &right->schema); +} + z_result_t z_slice_copy_from_buf(z_owned_slice_t *slice, const uint8_t *data, size_t len) { slice->_val = _z_slice_copy_from_buf(data, len); if (slice->_val.start == NULL) { diff --git a/src/api/encoding.c b/src/api/encoding.c index 61535811b..8cbe24ae1 100644 --- a/src/api/encoding.c +++ b/src/api/encoding.c @@ -157,12 +157,17 @@ static z_result_t _z_encoding_convert_from_substr(z_owned_encoding_t *encoding, } // Check id_end value + corner cases - if ((pos != len) && (pos != 0)) { + if (pos != 0) { uint16_t id = _z_encoding_values_str_to_int(s, pos); // Check id if (id != UINT16_MAX) { - const char *ptr = (pos + 1 == len) ? NULL : s + pos + 1; - return _z_encoding_make(&encoding->_val, id, ptr, len - pos - 1); + const char *ptr = NULL; + size_t remaining = 0; + if (pos + 1 < len) { + ptr = s + pos + 1; + remaining = len - pos - 1; + } + return _z_encoding_make(&encoding->_val, id, ptr, remaining); } } // By default store the string as schema diff --git a/tests/z_api_encoding_test.c b/tests/z_api_encoding_test.c index 8f3577085..7565c48dd 100644 --- a/tests/z_api_encoding_test.c +++ b/tests/z_api_encoding_test.c @@ -4,6 +4,7 @@ #include "zenoh-pico/api/primitives.h" #include "zenoh-pico/api/types.h" +#include "zenoh-pico/api/encoding.h" #undef NDEBUG #include @@ -94,9 +95,20 @@ void test_with_schema(void) { z_string_drop(z_string_move(&s)); } +void test_equals(void) { +#if Z_FEATURE_ENCODING_VALUES == 1 + z_owned_encoding_t e; + z_encoding_from_str(&e, "zenoh/string"); + assert(z_encoding_equals(z_encoding_loan(&e), z_encoding_zenoh_string())); + assert(!z_encoding_equals(z_encoding_loan(&e), z_encoding_zenoh_serialized())); + z_encoding_drop(z_encoding_move(&e)); +#endif +} + int main(void) { test_null_encoding(); test_encoding_without_id(); test_encoding_with_id(); test_with_schema(); + test_equals(); }