72 lines
2.4 KiB
Text
72 lines
2.4 KiB
Text
String buffer API
|
|
-----------------
|
|
|
|
First off, the major design choices. the most important thing to keep in mind
|
|
when using the API is that it is designed for low-level usage, The basic error checking
|
|
is removed (like checking if the input strbuf pointer is null). +
|
|
This is not done to gain speed, but because this is a buffer API, you should in almost every case allocate
|
|
the structure on the stack or have it passed from a function that has it allocated on the stack. +
|
|
Other types of skipped error checking is for example if the ->len member is in range of
|
|
the allocated block. (obviously this is checked in functions that may need to expand the memory)
|
|
|
|
Data structures
|
|
~~~~~~~~~~~~~~~
|
|
|
|
* `strbuf_t`
|
|
|
|
The ->buf member is yours to mess with if you want, but you should never go beyond ->len. +
|
|
A `NULL` terminating character is located at ->len+1 at all times so it is safe to use the ->buf member
|
|
on any function that relies on the input being a valid C string. The API will never rely on this
|
|
and it's possible to have embedded `NULL`'s because of that.
|
|
|
|
NOTE: ->alloc_size and ->len should *not* be messed with, only `strbuf_*` functions will know how to handle those properly.
|
|
|
|
Functions
|
|
~~~~~~~~~
|
|
|
|
`strbuf_append()`::
|
|
|
|
This function will append the buffer with the contents of 'ptr'
|
|
and will always copy exactly 'len' bytes. (if memory can be obtained ofcourse)
|
|
|
|
`strbuf_append_str()`::
|
|
|
|
Will add the 'str' C-string to the end of ->buf
|
|
|
|
`strbuf_append_ch()`::
|
|
|
|
Adds one character 'ch' to the end of ->buf
|
|
|
|
`strbuf_reduce()`::
|
|
|
|
Will reduce ->buf by 'len' bytes from the end.
|
|
+
|
|
NOTE: This doesn't shrink the memory block
|
|
just changes the size and properly `NULL` terminates the now reduced space.
|
|
|
|
`strbuf_trim()`::
|
|
`strbuf_rtrim()`::
|
|
`strbuf_ltrim()`::
|
|
|
|
Removes space characters from the beginning (ltrim) of the ->buf string, the end (rtrim) or both (trim).
|
|
|
|
`strbuf_rev()`::
|
|
|
|
Reverses the ->buf string.
|
|
|
|
`strbuf_squeeze()`::
|
|
|
|
Squeezes "together" sequences of 'ch' into one character.
|
|
|
|
`strbuf_term()`::
|
|
|
|
ensure that ->buf is terminated with 'ch'. Will not change ->buf if it is already terminated.
|
|
|
|
`strbuf_release()`::
|
|
|
|
This function should be used to detach the ->buf member from the `strbuf_t` structure. +
|
|
A `malloc()`'ed C string of size `strlen()+1` is returned that you are now responsible for.
|
|
|
|
`strbuf_free()`::
|
|
|
|
Free's all the memory (allocated on the heap) associted with the `strbuf_t` structure.
|