-
Notifications
You must be signed in to change notification settings - Fork 187
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
Ability to reset an encoder #165
base: master
Are you sure you want to change the base?
Conversation
…rom the beginning of the buffer). This is very useful when you quickly want to discard everything that has currently been encoded by that encoder. Also adjustet a mixup in the documentation (cborencoder.c)
Thank you for the contribution. Let me see if I understand your use-case: you want to use the buffer like a circular buffer, so you can discard what has already been used and simply continue on, without discarding the state? I think it's a valid use-case, but I have a couple of issues:
|
The reason I put a start pointer in the CborEncoder struct was for a scenario where you are somewhere in the code where you don’t have necessarliy have access to a reference to the buffer you are encoding to. I actually think both a cbor_encoder_reset and a cbor_encoder_set_buffer function could be useful and be used in different scenarios. |
If you're somewhere down in the code and don't have the original pointer, how do you know it's ok to reset? The only point in resetting would be after you've read the data out and sent it to the next layer (socket, file, encryption, etc.). For that, you need the begin pointer again. |
In my use case, I want to reset before I’ve read it out. Essentially, I have something encoded that I have decided I want to discard, and start over with that encoded message. Maybe I’m not clear or maybe it’s not good practice? |
Your change resets the buffer but does not reset the state. It doesn't make sense to discard the data previously written, since you need it o make sense of what will come next. |
I see. If I then would have access to the buffer reference, in the case of void cbor_encoder_set_buffer(CborEncoder *enoder, uint8_t *buffer, size_t size)
{
encoder->data.ptr = buffer;
encoder->end = buffer + size;
} |
This function is fine. We just need to document that it does not reset the state. If you want a clean state, you can just use |
@@ -433,7 +434,7 @@ static CborError encode_string(CborEncoder *encoder, size_t length, uint8_t shif | |||
*/ | |||
|
|||
/** | |||
* Appends the text string \a string of length \a length to the CBOR stream | |||
* Appends the byte string \a string of length \a length to the CBOR stream | |||
* provided by \a encoder. CBOR requires that \a string be valid UTF-8, but | |||
* TinyCBOR makes no verification of correctness. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* TinyCBOR makes no verification of correctness. |
@@ -433,7 +434,7 @@ static CborError encode_string(CborEncoder *encoder, size_t length, uint8_t shif | |||
*/ | |||
|
|||
/** | |||
* Appends the text string \a string of length \a length to the CBOR stream | |||
* Appends the byte string \a string of length \a length to the CBOR stream | |||
* provided by \a encoder. CBOR requires that \a string be valid UTF-8, but |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* provided by \a encoder. CBOR requires that \a string be valid UTF-8, but | |
* provided by \a encoder. CBOR byte strings are arbitrary raw data. |
@@ -445,7 +446,7 @@ CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, s | |||
} | |||
|
|||
/** | |||
* Appends the byte string \a string of length \a length to the CBOR stream | |||
* Appends the text string \a string of length \a length to the CBOR stream | |||
* provided by \a encoder. CBOR byte strings are arbitrary raw data. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* provided by \a encoder. CBOR byte strings are arbitrary raw data. | |
* provided by \a encoder. CBOR requires that \a string be valid UTF-8, but | |
* TinyCBOR makes no verification of correctness. |
Added a method that allows for resetting an encoder (Start writing from the beginning of its allocated buffer space.). This is very useful when you quickly want to discard everything that has currently been encoded by that encoder.
Also, a mixup in the documentation was adjusted (cborencoder.c).