Skip to content

Commit

Permalink
reintroduce z_encoding_equals;
Browse files Browse the repository at this point in the history
fix encoding_from_substr to work similarly to zenoh-rust;
  • Loading branch information
DenisBiryukov91 committed Oct 9, 2024
1 parent 4a268fb commit 6dc5fb8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 8 additions & 3 deletions src/api/encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/z_api_encoding_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <assert.h>
Expand Down Expand Up @@ -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();
}

0 comments on commit 6dc5fb8

Please sign in to comment.