Ocaml buffer modification specific position

169 Views Asked by At

I'm currently building/concatenating pretty big strings in a program I'm developing. To give some context, the full string has the size of a terminal. This happens quite a lot since I'm developing a terminal application. I found the datastructure Buffer, which seems to be the most performant way to concatenate strings with the standard library.

Is this the right choice if I also frequently need to update some part of a buffer at a specific position? Let's say character 20 to 50 ? Is there a better way in this case?

2

There are 2 best solutions below

0
On BEST ANSWER

AFAICS, there is no way to alter the contents of a Buffer.t except adding to the end. Maybe you should take a look at the module Bytes. A Bytes.t is mutable everywhere, only the length cannot change. But it seems the length you need is fixed anyway: the size of the terminal (and if the terminal window is resized you can replace the Bytes.t by a new one).

2
On

@coredump and @kne have given good answers. I might just add that in today's world a byte is a poor representation of a character. So you might consider using an array or a bigarray.