Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add serialize from substr functions #742

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,7 @@ Functions
.. autocfunction:: serialization.h::ze_serializer_serialize_buf
.. autocfunction:: serialization.h::ze_serializer_serialize_string
.. autocfunction:: serialization.h::ze_serializer_serialize_str
.. autocfunction:: serialization.h::ze_serializer_serialize_substr
.. autocfunction:: serialization.h::ze_serializer_serialize_sequence_length
.. autocfunction:: serialization.h::ze_deserialize_int8
.. autocfunction:: serialization.h::ze_deserialize_int16
Expand Down Expand Up @@ -1267,6 +1268,7 @@ Functions
.. autocfunction:: serialization.h::ze_serialize_buf
.. autocfunction:: serialization.h::ze_serialize_string
.. autocfunction:: serialization.h::ze_serialize_str
.. autocfunction:: serialization.h::ze_serialize_substr


Others
Expand Down
36 changes: 34 additions & 2 deletions include/zenoh-pico/api/serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ z_result_t ze_deserializer_deserialize_slice(ze_deserializer_t *deserializer, z_

/**
* Serializes a null-terminated string and writes it into an underlying :c:type:`z_owned_bytes_t`.
* The string should be a valid UTF-8.
*
* Parameters:
* serializer: A serializer instance.
Expand All @@ -468,8 +469,23 @@ z_result_t ze_deserializer_deserialize_slice(ze_deserializer_t *deserializer, z_
*/
z_result_t ze_serializer_serialize_str(ze_loaned_serializer_t *serializer, const char *val);

/**
* Serializes a substring and writes it into an underlying :c:type:`z_owned_bytes_t`.
* The substring should be a valid UTF-8.
*
* Parameters:
* serializer: A serializer instance.
* start: Pointer to the start of the substring to serialize.
* len: Length of the substring to serialize.
*
* Return:
* ``0`` if serialization is successful, ``negative value`` otherwise.
*/
z_result_t ze_serializer_serialize_substr(ze_loaned_serializer_t *serializer, const char *start, size_t len);

/**
* Serializes a string and writes it into an underlying :c:type:`z_owned_bytes_t`.
* The string should be a valid UTF-8.
*
* Parameters:
* serializer: A serializer instance.
Expand Down Expand Up @@ -523,7 +539,7 @@ z_result_t ze_deserializer_deserialize_sequence_length(ze_deserializer_t *deseri
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the serialized data.
* data: Pointer to the data to serialize. Ownership is transferred to the `bytes`.
* data: Pointer to the data to serialize.
* len: Number of bytes to serialize.
*
* Return:
Expand All @@ -534,6 +550,7 @@ z_result_t ze_serialize_buf(z_owned_bytes_t *bytes, const uint8_t *data, size_t
/**
* Serializes a string into a :c:type:`z_owned_bytes_t`.
*
* The string should be a valid UTF-8.
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the serialized string.
* s: Pointer to the string to serialize.
Expand All @@ -545,16 +562,31 @@ z_result_t ze_serialize_string(z_owned_bytes_t *bytes, const z_loaned_string_t *

/**
* Serializes a null-terminated string into a :c:type:`z_owned_bytes_t`.
* The string should be a valid UTF-8.
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the serialized string.
* value: Pointer to the string to serialize. Ownership is transferred to the `bytes`.
* value: Pointer to the string to serialize.
*
* Return:
* ``0`` if serialization is successful, ``negative value`` otherwise.
*/
z_result_t ze_serialize_str(z_owned_bytes_t *bytes, const char *value);

/**
* Serializes a substring into a :c:type:`z_owned_bytes_t`.
* The substring should be a valid UTF-8.
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the serialized string.
* start: Pointer to the the start of string to serialize.
* len: Length of the substring to serialize.
*
* Return:
* ``0`` if serialization is successful, ``negative value`` otherwise.
*/
z_result_t ze_serialize_substr(z_owned_bytes_t *bytes, const char *start, size_t len);

/**
* Serializes a slice into a :c:type:`z_owned_bytes_t`.
*
Expand Down
13 changes: 11 additions & 2 deletions src/api/serialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ z_result_t ze_deserializer_deserialize_slice(ze_deserializer_t *deserializer, z_
return Z_OK;
}

z_result_t ze_serializer_serialize_substr(ze_loaned_serializer_t *serializer, const char *start, size_t len) {
// TODO: perform a UTF-8 correctness check.
return ze_serializer_serialize_buf(serializer, (const uint8_t *)start, len);
}

z_result_t ze_serializer_serialize_str(ze_loaned_serializer_t *serializer, const char *val) {
size_t len = strlen(val);
return ze_serializer_serialize_buf(serializer, (const uint8_t *)val, len);
return ze_serializer_serialize_substr(serializer, val, strlen(val));
}

z_result_t ze_serializer_serialize_string(ze_loaned_serializer_t *serializer, const z_loaned_string_t *val) {
Expand Down Expand Up @@ -121,6 +125,11 @@ z_result_t ze_deserialize_slice(const z_loaned_bytes_t *bytes, z_owned_slice_t *
return ze_deserializer_deserialize_slice(&deserializer, data);
}

z_result_t ze_serialize_substr(z_owned_bytes_t *bytes, const char *start, size_t len) {
_Z_BUILD_BYTES_FROM_SERIALIZER(ze_serializer_serialize_substr(&serializer, start, len));
return _Z_RES_OK;
}

z_result_t ze_serialize_str(z_owned_bytes_t *bytes, const char *data) {
_Z_BUILD_BYTES_FROM_SERIALIZER(ze_serializer_serialize_str(&serializer, data));
return _Z_RES_OK;
Expand Down
Loading