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
Truncating buffers could come in handy #6975
Comments
Comment author: @alainfrisch I'm in favor of the change: it's useful, easy to implement (and impossible outside the implementation with the same performance profile) and doesn't break invariants of the data structure (one could already reset the buffer and re-inject a prefix of the previous content). Another useful addition would be to allow extracting a substring of the buffer's content. As a matter of fact, it would be worth turning Buffer into a full-fledged implementation of "mutable and extensible strings". This would include blitting (from strings, bytes, Buffers) into arbitrary segments of a Buffer.t. |
Comment author: @damiendoligez I'm also in favor for truncate and substring extraction. Blitting into buffers is more tricky. |
Comment author: maro I would also really like truncate. I see however at least two things that could lead to further discussions:
let safe_append_stuff buf s = Instead of having to do something as: let safe_append_stuff buf s = But that's a minor detail, honestly both would be fine for me.
Considering these aspects, one implementation of truncate might be as simple as: (** [truncate b n] truncates the length of [b] to be no larger than [n]. Is it worth creating a Github PR for that, or is it too small of a change? |
Comment author: @alainfrisch |
Original bug ID: 6975
Reporter: omion
Assigned to: @alainfrisch
Status: resolved (set by @alainfrisch on 2016-12-06T15:26:39Z)
Resolution: fixed
Priority: low
Severity: feature
Version: 4.02.0
Fixed in version: 4.05.0 +dev/beta1/beta2/beta3/rc1
Category: standard library
Tags: junior_job
Related to: #3774 #6538
Monitored by: @diml @hcarty
Bug description
I've been writing some code that incrementally adds data to a buffer, but will fail if it gets too long. However, exactly how long the data will be would be difficult to know in advance, so I just have to start writing and give up if it gets too long. I'd like to be able to clear out my half-written data when that happens, but it looks like the Buffer module doesn't really allow that.
Essentially, I'm doing:
let try_write buf data max_chars =
let start_len = Buffer.length buf in
write_lots_of_stuff buf data;
if Buffer.length buf > start_len + max_chars then (
(* Too long )
Buffer.truncate buf start_len;
false
) else (
( OK length *)
true
)
Buffer.truncate would be trivial to implement in O(1) with access to the Buffer.t internals, but would be difficult and inefficient without.
I was thinking of something like this:
let truncate b len =
if len < 0 || len > b.position
then invalid_arg "Buffer.truncate"
else b.position <- len
The text was updated successfully, but these errors were encountered: