Autogenerated HTML docs for v1.7.4
[git/jnareb-git.git] / technical / api-strbuf.html
blobcd63f6a661b61934d26aaa4f286a24d69047e7d7
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">
4 <head>
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">
9 /* Debug borders */
10 p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
12 border: 1px solid red;
16 body {
17 margin: 1em 5% 1em 5%;
20 a {
21 color: blue;
22 text-decoration: underline;
24 a:visited {
25 color: fuchsia;
28 em {
29 font-style: italic;
30 color: navy;
33 strong {
34 font-weight: bold;
35 color: #083194;
38 tt {
39 color: navy;
42 h1, h2, h3, h4, h5, h6 {
43 color: #527bbd;
44 font-family: sans-serif;
45 margin-top: 1.2em;
46 margin-bottom: 0.5em;
47 line-height: 1.3;
50 h1, h2, h3 {
51 border-bottom: 2px solid silver;
53 h2 {
54 padding-top: 0.5em;
56 h3 {
57 float: left;
59 h3 + * {
60 clear: left;
63 div.sectionbody {
64 font-family: serif;
65 margin-left: 0;
68 hr {
69 border: 1px solid silver;
72 p {
73 margin-top: 0.5em;
74 margin-bottom: 0.5em;
77 ul, ol, li > p {
78 margin-top: 0;
81 pre {
82 padding: 0;
83 margin: 0;
86 span#author {
87 color: #527bbd;
88 font-family: sans-serif;
89 font-weight: bold;
90 font-size: 1.1em;
92 span#email {
94 span#revnumber, span#revdate, span#revremark {
95 font-family: sans-serif;
98 div#footer {
99 font-family: sans-serif;
100 font-size: small;
101 border-top: 2px solid silver;
102 padding-top: 0.5em;
103 margin-top: 4.0em;
105 div#footer-text {
106 float: left;
107 padding-bottom: 0.5em;
109 div#footer-badges {
110 float: right;
111 padding-bottom: 0.5em;
114 div#preamble {
115 margin-top: 1.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 {
121 margin-top: 1.5em;
122 margin-bottom: 1.5em;
124 div.admonitionblock {
125 margin-top: 2.5em;
126 margin-bottom: 2.5em;
129 div.content { /* Block element content. */
130 padding: 0;
133 /* Block element titles. */
134 div.title, caption.title {
135 color: #527bbd;
136 font-family: sans-serif;
137 font-weight: bold;
138 text-align: left;
139 margin-top: 1.0em;
140 margin-bottom: 0.5em;
142 div.title + * {
143 margin-top: 0;
146 td div.title:first-child {
147 margin-top: 0.0em;
149 div.content div.title:first-child {
150 margin-top: 0.0em;
152 div.content + div.title {
153 margin-top: 0.0em;
156 div.sidebarblock > div.content {
157 background: #ffffee;
158 border: 1px solid silver;
159 padding: 0.5em;
162 div.listingblock > div.content {
163 border: 1px solid silver;
164 background: #f4f4f4;
165 padding: 0.5em;
168 div.quoteblock {
169 padding-left: 2.0em;
170 margin-right: 10%;
172 div.quoteblock > div.attribution {
173 padding-top: 0.5em;
174 text-align: right;
177 div.verseblock {
178 padding-left: 2.0em;
179 margin-right: 10%;
181 div.verseblock > div.content {
182 white-space: pre;
184 div.verseblock > div.attribution {
185 padding-top: 0.75em;
186 text-align: left;
188 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
189 div.verseblock + div.attribution {
190 text-align: left;
193 div.admonitionblock .icon {
194 vertical-align: top;
195 font-size: 1.1em;
196 font-weight: bold;
197 text-decoration: underline;
198 color: #527bbd;
199 padding-right: 0.5em;
201 div.admonitionblock td.content {
202 padding-left: 0.5em;
203 border-left: 2px solid silver;
206 div.exampleblock > div.content {
207 border-left: 2px solid silver;
208 padding: 0.5em;
211 div.imageblock div.content { padding-left: 0; }
212 span.image img { border-style: none; }
213 a.image:visited { color: white; }
215 dl {
216 margin-top: 0.8em;
217 margin-bottom: 0.8em;
219 dt {
220 margin-top: 0.5em;
221 margin-bottom: 0;
222 font-style: normal;
223 color: navy;
225 dd > *:first-child {
226 margin-top: 0.1em;
229 ul, ol {
230 list-style-position: outside;
232 ol.arabic {
233 list-style-type: decimal;
235 ol.loweralpha {
236 list-style-type: lower-alpha;
238 ol.upperalpha {
239 list-style-type: upper-alpha;
241 ol.lowerroman {
242 list-style-type: lower-roman;
244 ol.upperroman {
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 {
251 margin-top: 0.1em;
252 margin-bottom: 0.1em;
255 div.tableblock > table {
256 border: 3px solid #527bbd;
258 thead {
259 font-family: sans-serif;
260 font-weight: bold;
262 tfoot {
263 font-weight: bold;
265 td > div.verse {
266 white-space: pre;
268 p.table {
269 margin-top: 0;
271 /* Because the table frame attribute is overriden by CSS in most browsers. */
272 div.tableblock > table[frame="void"] {
273 border-style: none;
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;
285 div.hdlist {
286 margin-top: 0.8em;
287 margin-bottom: 0.8em;
289 div.hdlist tr {
290 padding-bottom: 15px;
292 dt.hdlist1.strong, td.hdlist1.strong {
293 font-weight: bold;
295 td.hdlist1 {
296 vertical-align: top;
297 font-style: normal;
298 padding-right: 0.8em;
299 color: navy;
301 td.hdlist2 {
302 vertical-align: top;
304 div.hdlist.compact tr {
305 margin: 0;
306 padding-bottom: 0;
309 .comment {
310 background: yellow;
313 @media print {
314 div#footer-badges { display: none; }
317 div#toctitle {
318 color: #527bbd;
319 font-family: sans-serif;
320 font-size: 1.1em;
321 font-weight: bold;
322 margin-top: 1.0em;
323 margin-bottom: 0.1em;
326 div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
327 margin-top: 0;
328 margin-bottom: 0;
330 div.toclevel2 {
331 margin-left: 2em;
332 font-size: 0.9em;
334 div.toclevel3 {
335 margin-left: 4em;
336 font-size: 0.9em;
338 div.toclevel4 {
339 margin-left: 6em;
340 font-size: 0.9em;
342 /* Workarounds for IE6's broken and incomplete CSS2. */
344 div.sidebar-content {
345 background: #ffffee;
346 border: 1px solid silver;
347 padding: 0.5em;
349 div.sidebar-title, div.image-title {
350 color: #527bbd;
351 font-family: sans-serif;
352 font-weight: bold;
353 margin-top: 0.0em;
354 margin-bottom: 0.5em;
357 div.listingblock div.content {
358 border: 1px solid silver;
359 background: #f4f4f4;
360 padding: 0.5em;
363 div.quoteblock-attribution {
364 padding-top: 0.5em;
365 text-align: right;
368 div.verseblock-content {
369 white-space: pre;
371 div.verseblock-attribution {
372 padding-top: 0.75em;
373 text-align: left;
376 div.exampleblock-content {
377 border-left: 2px solid silver;
378 padding-left: 0.5em;
381 /* IE6 sets dynamically generated links as visited. */
382 div#toc a:visited { color: blue; }
383 </style>
384 </head>
385 <body>
386 <div id="header">
387 <h1>strbuf API</h1>
388 </div>
389 <div id="preamble">
390 <div class="sectionbody">
391 <div class="paragraph"><p>strbuf&#8217;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&#8217;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">
400 <li>
402 The <tt>buf</tt> member is never NULL, so it can be used in any usual C
403 string operations safely. strbuf&#8217;s <em>have</em> to be initialized either by
404 <tt>strbuf_init()</tt> or by <tt>= STRBUF_INIT</tt> before the invariants, though.
405 </p>
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>
412 </li>
413 <li>
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.
419 </p>
420 <div class="admonitionblock">
421 <table><tr>
422 <td class="icon">
423 <div class="title">Note</div>
424 </td>
425 <td class="content">It is OK to "play" with the buffer directly if you work it this
426 way:</td>
427 </tr></table>
428 </div>
429 <div class="listingblock">
430 <div class="content">
431 <pre><tt>strbuf_grow(sb, SOME_SIZE); <b>&lt;1&gt;</b>
432 strbuf_setlen(sb, sb-&gt;len + SOME_OTHER_SIZE);</tt></pre>
433 </div></div>
434 <div class="colist arabic"><ol>
435 <li>
437 Here, the memory array starting at <tt>sb&#8594;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>.
440 </p>
441 <div class="admonitionblock">
442 <table><tr>
443 <td class="icon">
444 <div class="title">Note</div>
445 </td>
446 <td class="content"><tt>SOME_OTHER_SIZE</tt> must be smaller or equal to <tt>strbuf_avail(sb)</tt>.</td>
447 </tr></table>
448 </div>
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">
452 <table><tr>
453 <td class="icon">
454 <div class="title">Warning</div>
455 </td>
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&#8217;s true in the current implementation. Alloc is somehow a
458 "private" member that should not be messed with. Use <tt>strbuf_avail()</tt>
459 instead.</td>
460 </tr></table>
461 </div>
462 </li>
463 </ol></div>
464 </li>
465 </ol></div>
466 </div>
467 </div>
468 <h2 id="_data_structures">Data structures</h2>
469 <div class="sectionbody">
470 <div class="ulist"><ul>
471 <li>
473 <tt>struct strbuf</tt>
474 </p>
475 </li>
476 </ul></div>
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>
480 </div>
481 <h2 id="_functions">Functions</h2>
482 <div class="sectionbody">
483 <div class="ulist"><ul>
484 <li>
486 Life cycle
487 </p>
488 <div class="dlist"><dl>
489 <dt class="hdlist1">
490 <tt>strbuf_init</tt>
491 </dt>
492 <dd>
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.
496 </p>
497 </dd>
498 <dt class="hdlist1">
499 <tt>strbuf_release</tt>
500 </dt>
501 <dd>
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.
505 </p>
506 </dd>
507 <dt class="hdlist1">
508 <tt>strbuf_detach</tt>
509 </dt>
510 <dd>
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.
515 </p>
516 </dd>
517 <dt class="hdlist1">
518 <tt>strbuf_attach</tt>
519 </dt>
520 <dd>
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.
528 </p>
529 </dd>
530 <dt class="hdlist1">
531 <tt>strbuf_swap</tt>
532 </dt>
533 <dd>
535 Swap the contents of two string buffers.
536 </p>
537 </dd>
538 </dl></div>
539 </li>
540 <li>
542 Related to the size of the buffer
543 </p>
544 <div class="dlist"><dl>
545 <dt class="hdlist1">
546 <tt>strbuf_avail</tt>
547 </dt>
548 <dd>
550 Determine the amount of allocated but unused memory.
551 </p>
552 </dd>
553 <dt class="hdlist1">
554 <tt>strbuf_grow</tt>
555 </dt>
556 <dd>
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
562 some cases.
563 </p>
564 </dd>
565 <dt class="hdlist1">
566 <tt>strbuf_setlen</tt>
567 </dt>
568 <dd>
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
574 with</em>.
575 </p>
576 </dd>
577 <dt class="hdlist1">
578 <tt>strbuf_reset</tt>
579 </dt>
580 <dd>
582 Empty the buffer by setting the size of it to zero.
583 </p>
584 </dd>
585 </dl></div>
586 </li>
587 <li>
589 Related to the contents of the buffer
590 </p>
591 <div class="dlist"><dl>
592 <dt class="hdlist1">
593 <tt>strbuf_rtrim</tt>
594 </dt>
595 <dd>
597 Strip whitespace from the end of a string.
598 </p>
599 </dd>
600 <dt class="hdlist1">
601 <tt>strbuf_cmp</tt>
602 </dt>
603 <dd>
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.
608 </p>
609 </dd>
610 </dl></div>
611 </li>
612 <li>
614 Adding data to the buffer
615 </p>
616 </li>
617 </ul></div>
618 <div class="admonitionblock">
619 <table><tr>
620 <td class="icon">
621 <div class="title">Note</div>
622 </td>
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&#8217;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>
627 </tr></table>
628 </div>
629 <div class="dlist"><dl>
630 <dt class="hdlist1">
631 <tt>strbuf_addch</tt>
632 </dt>
633 <dd>
635 Add a single character to the buffer.
636 </p>
637 </dd>
638 <dt class="hdlist1">
639 <tt>strbuf_insert</tt>
640 </dt>
641 <dd>
643 Insert data to the given position of the buffer. The remaining contents
644 will be shifted, not overwritten.
645 </p>
646 </dd>
647 <dt class="hdlist1">
648 <tt>strbuf_remove</tt>
649 </dt>
650 <dd>
652 Remove given amount of data from a given position of the buffer.
653 </p>
654 </dd>
655 <dt class="hdlist1">
656 <tt>strbuf_splice</tt>
657 </dt>
658 <dd>
660 Remove the bytes between <tt>pos..pos+len</tt> and replace it with the given
661 data.
662 </p>
663 </dd>
664 <dt class="hdlist1">
665 <tt>strbuf_add</tt>
666 </dt>
667 <dd>
669 Add data of given length to the buffer.
670 </p>
671 </dd>
672 <dt class="hdlist1">
673 <tt>strbuf_addstr</tt>
674 </dt>
675 <dd>
677 Add a NUL-terminated string to the buffer.
678 </p>
679 <div class="admonitionblock">
680 <table><tr>
681 <td class="icon">
682 <div class="title">Note</div>
683 </td>
684 <td class="content">This function will <strong>always</strong> be implemented as an inline or a macro
685 that expands to:</td>
686 </tr></table>
687 </div>
688 <div class="listingblock">
689 <div class="content">
690 <pre><tt>strbuf_add(..., s, strlen(s));</tt></pre>
691 </div></div>
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>
696 </div></div>
697 </dd>
698 <dt class="hdlist1">
699 <tt>strbuf_addbuf</tt>
700 </dt>
701 <dd>
703 Copy the contents of an other buffer at the end of the current one.
704 </p>
705 </dd>
706 <dt class="hdlist1">
707 <tt>strbuf_adddup</tt>
708 </dt>
709 <dd>
711 Copy part of the buffer from a given position till a given length to the
712 end of the buffer.
713 </p>
714 </dd>
715 <dt class="hdlist1">
716 <tt>strbuf_expand</tt>
717 </dt>
718 <dd>
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.
723 </p>
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
729 over it.</p></div>
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>
739 </dd>
740 <dt class="hdlist1">
741 <tt>strbuf_expand_dict_cb</tt>
742 </dt>
743 <dd>
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.
749 </p>
750 </dd>
751 <dt class="hdlist1">
752 <tt>strbuf_addbuf_percentquote</tt>
753 </dt>
754 <dd>
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.
760 </p>
761 </dd>
762 <dt class="hdlist1">
763 <tt>strbuf_addf</tt>
764 </dt>
765 <dd>
767 Add a formatted string to the buffer.
768 </p>
769 </dd>
770 <dt class="hdlist1">
771 <tt>strbuf_fread</tt>
772 </dt>
773 <dd>
775 Read a given size of data from a FILE* pointer to the buffer.
776 </p>
777 <div class="admonitionblock">
778 <table><tr>
779 <td class="icon">
780 <div class="title">Note</div>
781 </td>
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>
786 </tr></table>
787 </div>
788 </dd>
789 <dt class="hdlist1">
790 <tt>strbuf_read</tt>
791 </dt>
792 <dd>
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.
796 </p>
797 </dd>
798 <dt class="hdlist1">
799 <tt>strbuf_read_file</tt>
800 </dt>
801 <dd>
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.
805 </p>
806 </dd>
807 <dt class="hdlist1">
808 <tt>strbuf_readlink</tt>
809 </dt>
810 <dd>
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.
814 </p>
815 </dd>
816 <dt class="hdlist1">
817 <tt>strbuf_getline</tt>
818 </dt>
819 <dd>
821 Read a line from a FILE* pointer. The second argument specifies the line
822 terminator character, typically <tt><em>\n</em></tt>.
823 </p>
824 </dd>
825 <dt class="hdlist1">
826 <tt>stripspace</tt>
827 </dt>
828 <dd>
830 Strip whitespace from a buffer. The second parameter controls if
831 comments are considered contents to be removed or not.
832 </p>
833 </dd>
834 <dt class="hdlist1">
835 <tt>launch_editor</tt>
836 </dt>
837 <dd>
839 Launch the user preferred editor to edit a file and fill the buffer
840 with the file&#8217;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&#8217;s contents are not read into the buffer upon completion.
844 </p>
845 </dd>
846 </dl></div>
847 </div>
848 <div id="footer">
849 <div id="footer-text">
850 Last updated 2010-01-21 00:41:31 UTC
851 </div>
852 </div>
853 </body>
854 </html>