1 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en">
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8" />
6 <meta name=
"generator" content=
"AsciiDoc 8.4.5" />
7 <title>strbuf API
</title>
8 <style type=
"text/css">
10 p
, li
, dt
, dd
, div
, pre
, h1
, h2
, h3
, h4
, h5
, h6
{
12 border: 1px solid red;
17 margin: 1em 5% 1em 5%;
22 text-decoration: underline
;
42 h1
, h2
, h3
, h4
, h5
, h6
{
44 font-family: sans-serif
;
51 border-bottom: 2px solid silver
;
69 border: 1px solid silver
;
88 font-family: sans-serif
;
94 span#revnumber
, span#revdate
, span#revremark
{
95 font-family: sans-serif
;
99 font-family: sans-serif
;
101 border-top: 2px solid silver
;
107 padding-bottom: 0.5em;
111 padding-bottom: 0.5em;
116 margin-bottom: 1.5em;
118 div
.tableblock
, div
.imageblock
, div
.exampleblock
, div
.verseblock
,
119 div
.quoteblock
, div
.literalblock
, div
.listingblock
, div
.sidebarblock
,
120 div
.admonitionblock
{
122 margin-bottom: 1.5em;
124 div
.admonitionblock
{
126 margin-bottom: 2.5em;
129 div
.content
{ /* Block element content. */
133 /* Block element titles. */
134 div
.title
, caption
.title
{
136 font-family: sans-serif
;
140 margin-bottom: 0.5em;
146 td div
.title:first-child
{
149 div
.content div
.title:first-child
{
152 div
.content
+ div
.title
{
156 div
.sidebarblock
> div
.content
{
158 border: 1px solid silver
;
162 div
.listingblock
> div
.content
{
163 border: 1px solid silver
;
172 div
.quoteblock
> div
.attribution
{
181 div
.verseblock
> div
.content
{
184 div
.verseblock
> div
.attribution
{
188 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
189 div
.verseblock
+ div
.attribution
{
193 div
.admonitionblock
.icon
{
197 text-decoration: underline
;
199 padding-right: 0.5em;
201 div
.admonitionblock td
.content
{
203 border-left: 2px solid silver
;
206 div
.exampleblock
> div
.content
{
207 border-left: 2px solid silver
;
211 div
.imageblock div
.content
{ padding-left: 0; }
212 span
.image img
{ border-style: none
; }
213 a
.image:visited
{ color: white
; }
217 margin-bottom: 0.8em;
230 list-style-position: outside
;
233 list-style-type: decimal
;
236 list-style-type: lower-alpha
;
239 list-style-type: upper-alpha
;
242 list-style-type: lower-roman
;
245 list-style-type: upper-roman
;
248 div
.compact ul
, div
.compact ol
,
249 div
.compact p
, div
.compact p
,
250 div
.compact div
, div
.compact div
{
252 margin-bottom: 0.1em;
255 div
.tableblock
> table
{
256 border: 3px solid
#527bbd;
259 font-family: sans-serif
;
271 /* Because the table frame attribute is overriden by CSS in most browsers. */
272 div
.tableblock
> table
[frame
="void"] {
275 div
.tableblock
> table
[frame
="hsides"] {
276 border-left-style: none
;
277 border-right-style: none
;
279 div
.tableblock
> table
[frame
="vsides"] {
280 border-top-style: none
;
281 border-bottom-style: none
;
287 margin-bottom: 0.8em;
290 padding-bottom: 15px;
292 dt
.hdlist1
.strong
, td
.hdlist1
.strong
{
298 padding-right: 0.8em;
304 div
.hdlist
.compact tr
{
314 div#footer-badges
{ display: none
; }
319 font-family: sans-serif
;
323 margin-bottom: 0.1em;
326 div
.toclevel1
, div
.toclevel2
, div
.toclevel3
, div
.toclevel4
{
342 /* Workarounds for IE6's broken and incomplete CSS2. */
344 div
.sidebar-content
{
346 border: 1px solid silver
;
349 div
.sidebar-title
, div
.image-title
{
351 font-family: sans-serif
;
354 margin-bottom: 0.5em;
357 div
.listingblock div
.content
{
358 border: 1px solid silver
;
363 div
.quoteblock-attribution
{
368 div
.verseblock-content
{
371 div
.verseblock-attribution
{
376 div
.exampleblock-content
{
377 border-left: 2px solid silver
;
381 /* IE6 sets dynamically generated links as visited. */
382 div#toc
a:visited
{ color: blue
; }
390 <div class=
"sectionbody">
391 <div class=
"paragraph"><p>strbuf
’s are meant to be used with all the usual C string and memory
392 APIs. Given that the length of the buffer is known, it
’s often better to
393 use the mem* functions than a str* one (memchr vs. strchr e.g.).
394 Though, one has to be careful about the fact that str* functions often
395 stop on NULs and that strbufs may have embedded NULs.
</p></div>
396 <div class=
"paragraph"><p>An strbuf is NUL terminated for convenience, but no function in the
397 strbuf API actually relies on the string being free of NULs.
</p></div>
398 <div class=
"paragraph"><p>strbufs has some invariants that are very important to keep in mind:
</p></div>
399 <div class=
"olist arabic"><ol class=
"arabic">
402 The
<tt>buf
</tt> member is never NULL, so it can be used in any usual C
403 string operations safely. strbuf
’s
<em>have
</em> to be initialized either by
404 <tt>strbuf_init()
</tt> or by
<tt>= STRBUF_INIT
</tt> before the invariants, though.
406 <div class=
"paragraph"><p>Do
<strong>not
</strong> assume anything on what
<tt>buf
</tt> really is (e.g. if it is
407 allocated memory or not), use
<tt>strbuf_detach()
</tt> to unwrap a memory
408 buffer from its strbuf shell in a safe way. That is the sole supported
409 way. This will give you a malloced buffer that you can later
<tt>free()
</tt>.
</p></div>
410 <div class=
"paragraph"><p>However, it is totally safe to modify anything in the string pointed by
411 the
<tt>buf
</tt> member, between the indices
<tt>0</tt> and
<tt>len-
1</tt> (inclusive).
</p></div>
415 The
<tt>buf
</tt> member is a byte array that has at least
<tt>len +
1</tt> bytes
416 allocated. The extra byte is used to store a
<tt><em>\
0</em></tt>, allowing the
417 <tt>buf
</tt> member to be a valid C-string. Every strbuf function ensure this
418 invariant is preserved.
420 <div class=
"admonitionblock">
423 <div class=
"title">Note
</div>
425 <td class=
"content">It is OK to
"play" with the buffer directly if you work it this
429 <div class=
"listingblock">
430 <div class=
"content">
431 <pre><tt>strbuf_grow(sb, SOME_SIZE);
<b><1></b>
432 strbuf_setlen(sb, sb-
>len + SOME_OTHER_SIZE);
</tt></pre>
434 <div class=
"colist arabic"><ol>
437 Here, the memory array starting at
<tt>sb
→buf
</tt>, and of length
438 <tt>strbuf_avail(sb)
</tt> is all yours, and you can be sure that
439 <tt>strbuf_avail(sb)
</tt> is at least
<tt>SOME_SIZE
</tt>.
441 <div class=
"admonitionblock">
444 <div class=
"title">Note
</div>
446 <td class=
"content"><tt>SOME_OTHER_SIZE
</tt> must be smaller or equal to
<tt>strbuf_avail(sb)
</tt>.
</td>
449 <div class=
"paragraph"><p>Doing so is safe, though if it has to be done in many places, adding the
450 missing API to the strbuf module is the way to go.
</p></div>
451 <div class=
"admonitionblock">
454 <div class=
"title">Warning
</div>
456 <td class=
"content">Do
<em>not
</em> assume that the area that is yours is of size
<tt>alloc
457 -
1</tt> even if it
’s true in the current implementation. Alloc is somehow a
458 "private" member that should not be messed with. Use
<tt>strbuf_avail()
</tt>
468 <h2 id=
"_data_structures">Data structures
</h2>
469 <div class=
"sectionbody">
470 <div class=
"ulist"><ul>
473 <tt>struct strbuf
</tt>
477 <div class=
"paragraph"><p>This is the string buffer structure. The
<tt>len
</tt> member can be used to
478 determine the current length of the string, and
<tt>buf
</tt> member provides access to
479 the string itself.
</p></div>
481 <h2 id=
"_functions">Functions
</h2>
482 <div class=
"sectionbody">
483 <div class=
"ulist"><ul>
488 <div class=
"dlist"><dl>
494 Initialize the structure. The second parameter can be zero or a bigger
495 number to allocate memory, in case you want to prevent further reallocs.
499 <tt>strbuf_release
</tt>
503 Release a string buffer and the memory it used. You should not use the
504 string buffer after using this function, unless you initialize it again.
508 <tt>strbuf_detach
</tt>
512 Detach the string from the strbuf and returns it; you now own the
513 storage the string occupies and it is your responsibility from then on
514 to release it with
<tt>free(
3)
</tt> when you are done with it.
518 <tt>strbuf_attach
</tt>
522 Attach a string to a buffer. You should specify the string to attach,
523 the current length of the string and the amount of allocated memory.
524 The amount must be larger than the string length, because the string you
525 pass is supposed to be a NUL-terminated string. This string
<em>must
</em> be
526 malloc()ed, and after attaching, the pointer cannot be relied upon
527 anymore, and neither be free()d directly.
535 Swap the contents of two string buffers.
542 Related to the size of the buffer
544 <div class=
"dlist"><dl>
546 <tt>strbuf_avail
</tt>
550 Determine the amount of allocated but unused memory.
558 Ensure that at least this amount of unused memory is available after
559 <tt>len
</tt>. This is used when you know a typical size for what you will add
560 and want to avoid repetitive automatic resizing of the underlying buffer.
561 This is never a needed operation, but can be critical for performance in
566 <tt>strbuf_setlen
</tt>
570 Set the length of the buffer to a given value. This function does
<strong>not
</strong>
571 allocate new memory, so you should not perform a
<tt>strbuf_setlen()
</tt> to a
572 length that is larger than
<tt>len + strbuf_avail()
</tt>.
<tt>strbuf_setlen()
</tt> is
573 just meant as a
<em>please fix invariants from this strbuf I just messed
578 <tt>strbuf_reset
</tt>
582 Empty the buffer by setting the size of it to zero.
589 Related to the contents of the buffer
591 <div class=
"dlist"><dl>
593 <tt>strbuf_rtrim
</tt>
597 Strip whitespace from the end of a string.
605 Compare two buffers. Returns an integer less than, equal to, or greater
606 than zero if the first buffer is found, respectively, to be less than,
607 to match, or be greater than the second buffer.
614 Adding data to the buffer
618 <div class=
"admonitionblock">
621 <div class=
"title">Note
</div>
623 <td class=
"content">All of the functions in this section will grow the buffer as necessary.
624 If they fail for some reason other than memory shortage and the buffer hadn
’t
625 been allocated before (i.e. the
<tt>struct strbuf
</tt> was set to
<tt>STRBUF_INIT
</tt>),
626 then they will free() it.
</td>
629 <div class=
"dlist"><dl>
631 <tt>strbuf_addch
</tt>
635 Add a single character to the buffer.
639 <tt>strbuf_insert
</tt>
643 Insert data to the given position of the buffer. The remaining contents
644 will be shifted, not overwritten.
648 <tt>strbuf_remove
</tt>
652 Remove given amount of data from a given position of the buffer.
656 <tt>strbuf_splice
</tt>
660 Remove the bytes between
<tt>pos..pos+len
</tt> and replace it with the given
669 Add data of given length to the buffer.
673 <tt>strbuf_addstr
</tt>
677 Add a NUL-terminated string to the buffer.
679 <div class=
"admonitionblock">
682 <div class=
"title">Note
</div>
684 <td class=
"content">This function will
<strong>always
</strong> be implemented as an inline or a macro
685 that expands to:
</td>
688 <div class=
"listingblock">
689 <div class=
"content">
690 <pre><tt>strbuf_add(..., s, strlen(s));
</tt></pre>
692 <div class=
"paragraph"><p>Meaning that this is efficient to write things like:
</p></div>
693 <div class=
"listingblock">
694 <div class=
"content">
695 <pre><tt>strbuf_addstr(sb,
"immediate string");
</tt></pre>
699 <tt>strbuf_addbuf
</tt>
703 Copy the contents of an other buffer at the end of the current one.
707 <tt>strbuf_adddup
</tt>
711 Copy part of the buffer from a given position till a given length to the
716 <tt>strbuf_expand
</tt>
720 This function can be used to expand a format string containing
721 placeholders. To that end, it parses the string and calls the specified
722 function for every percent sign found.
724 <div class=
"paragraph"><p>The callback function is given a pointer to the character after the
<tt>%
</tt>
725 and a pointer to the struct strbuf. It is expected to add the expanded
726 version of the placeholder to the strbuf, e.g. to add a newline
727 character if the letter
<tt>n
</tt> appears after a
<tt>%
</tt>. The function returns
728 the length of the placeholder recognized and
<tt>strbuf_expand()
</tt> skips
730 <div class=
"paragraph"><p>The format
<tt>%%
</tt> is automatically expanded to a single
<tt>%
</tt> as a quoting
731 mechanism; callers do not need to handle the
<tt>%
</tt> placeholder themselves,
732 and the callback function will not be invoked for this placeholder.
</p></div>
733 <div class=
"paragraph"><p>All other characters (non-percent and not skipped ones) are copied
734 verbatim to the strbuf. If the callback returned zero, meaning that the
735 placeholder is unknown, then the percent sign is copied, too.
</p></div>
736 <div class=
"paragraph"><p>In order to facilitate caching and to make it possible to give
737 parameters to the callback,
<tt>strbuf_expand()
</tt> passes a context pointer,
738 which can be used by the programmer of the callback as she sees fit.
</p></div>
741 <tt>strbuf_expand_dict_cb
</tt>
745 Used as callback for
<tt>strbuf_expand()
</tt>, expects an array of
746 struct strbuf_expand_dict_entry as context, i.e. pairs of
747 placeholder and replacement string. The array needs to be
748 terminated by an entry with placeholder set to NULL.
752 <tt>strbuf_addbuf_percentquote
</tt>
756 Append the contents of one strbuf to another, quoting any
757 percent signs (
"%") into double-percents (
"%%") in the
758 destination. This is useful for literal data to be fed to either
759 strbuf_expand or to the *printf family of functions.
767 Add a formatted string to the buffer.
771 <tt>strbuf_fread
</tt>
775 Read a given size of data from a FILE* pointer to the buffer.
777 <div class=
"admonitionblock">
780 <div class=
"title">Note
</div>
782 <td class=
"content">The buffer is rewound if the read fails. If -
1 is returned,
783 <tt>errno
</tt> must be consulted, like you would do for
<tt>read(
3)
</tt>.
784 <tt>strbuf_read()
</tt>,
<tt>strbuf_read_file()
</tt> and
<tt>strbuf_getline()
</tt> has the
785 same behaviour as well.
</td>
794 Read the contents of a given file descriptor. The third argument can be
795 used to give a hint about the file size, to avoid reallocs.
799 <tt>strbuf_read_file
</tt>
803 Read the contents of a file, specified by its path. The third argument
804 can be used to give a hint about the file size, to avoid reallocs.
808 <tt>strbuf_readlink
</tt>
812 Read the target of a symbolic link, specified by its path. The third
813 argument can be used to give a hint about the size, to avoid reallocs.
817 <tt>strbuf_getline
</tt>
821 Read a line from a FILE* pointer. The second argument specifies the line
822 terminator character, typically
<tt><em>\n
</em></tt>.
830 Strip whitespace from a buffer. The second parameter controls if
831 comments are considered contents to be removed or not.
835 <tt>launch_editor
</tt>
839 Launch the user preferred editor to edit a file and fill the buffer
840 with the file
’s contents upon the user completing their editing. The
841 third argument can be used to set the environment which the editor is
842 run in. If the buffer is NULL the editor is launched as usual but the
843 file
’s contents are not read into the buffer upon completion.
849 <div id=
"footer-text">
850 Last updated
2010-
01-
21 00:
41:
31 UTC