top | item 3943517

(no title)

jaimzob | 14 years ago

This is often how text buffers are implemented in editors. Since most text edits are contiguous, the gap means you don't keep shuffling memory around with each insertion and therefore it's extremely fast. I think it dates back to a pretty early version of emacs, maybe even before.

For anyone interested in a C version, I wrote this a while ago, YMMV: https://github.com/jaimz/core_ds/blob/master/MxStringBuffer....

discuss

order

ExpiredLink|14 years ago

Some stylistic remarks:

- _MxStringBuffer need not be defined in the header, a forward declaration would be sufficient (a.k.a. encapsulation).

- the two #defines can be removed form the header, too.

- I wouldn't try to typedef away a pointer (as in typedef struct _MxStringBuffer * MxStringBufferRef) but either use the pointer (MxStringBuffer* ) or create a handle: struct MxHandleStringBuffer { _MxStringBuffer * impl; };

jaimzob|14 years ago

Thanks, I'll patch this up when I have more time - though I'm not sure there's enough time in the world to fix my sloppy C code... ;)

alexchamberlain|14 years ago

Point 1 needs expanding... Assuming that you only need to access MxStringBuffer via a pointer, you don't need to include it in the header. However, this is somewhat deceptive and doesn't self-document. I don't think there is much advantage to it at all.

Edit: In fact, you must need the declaration to access the internals.

gravitronic|14 years ago

Pretty jerk response to someone sharing something for free.

It's on github. Post a pull request or keep it to yourself. Especially as your comments are stylistic.