From: Miklos Vajna <vmiklos@frugalware.org>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: [PATCH] Strbuf documentation: document most functions
Date: Tue, 3 Jun 2008 00:59:51 +0200 [thread overview]
Message-ID: <1212447591-4870-1-git-send-email-vmiklos@frugalware.org> (raw)
All functions in strbuf.h are documented, except strbuf_expand() and
launch_editor().
Signed-off-by: Miklos Vajna <vmiklos@frugalware.org>
---
Actually this is a bit of request for help, I haven't figured out what
strbuf_expand() does, and I don't know the second argument of
launch_editor() is good for, but I *think* I'm familiar with the rest of
the functions, so here is an attempt to document them.
Documentation/technical/api-strbuf.txt | 173 +++++++++++++++++++++++++++++++-
1 files changed, 171 insertions(+), 2 deletions(-)
diff --git a/Documentation/technical/api-strbuf.txt b/Documentation/technical/api-strbuf.txt
index a52e4f3..3879e0e 100644
--- a/Documentation/technical/api-strbuf.txt
+++ b/Documentation/technical/api-strbuf.txt
@@ -1,6 +1,175 @@
strbuf API
==========
-Talk about <strbuf.h>
+strbuf's can be use in many ways: as a byte array, or to store arbitrary
+long, overflow safe strings.
-(Pierre, JC)
+strbufs has some invariants that are very important to keep in mind:
+
+. The `->buf` member is always malloc-ed, hence strbuf's can be used to
+ build complex strings/buffers whose final size isn't easily known.
++
+It is *not* legal to copy the `->buf` pointer away. `strbuf_detach()` is
+the operation that detachs a buffer from its shell while keeping the
+shell valid wrt its invariants.
+
+. The `->buf` member is a byte array that has at least `->len + 1` bytes
+ allocated. The extra byte is used to store a `'\0'`, allowing the
+ `->buf` member to be a valid C-string. Every strbuf function ensure this
+ invariant is preserved.
++
+NOTE: It is OK to "play" with the buffer directly if you work it that
+ way:
++
+----
+strbuf_grow(sb, SOME_SIZE);
+ ... Here, the memory array starting at sb->buf, and of length
+ ... strbuf_avail(sb) is all yours, and you are sure that
+ ... strbuf_avail(sb) is at least SOME_SIZE.
+strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE);
+----
++
+Of course, SOME_OTHER_SIZE must be smaller or equal to strbuf_avail(sb).
++
+Doing so is safe, though if it has to be done in many places, adding the
+missing API to the strbuf module is the way to go.
++
+WARNING: Do _not_ assume that the area that is yours is of size `->alloc
+- 1` even if it's true in the current implementation. Alloc is somehow a
+"private" member that should not be messed with.
+
+Data structures
+---------------
+
+* `struct strbuf`
+
+This is string buffer structure. The `->len` variable can be used to
+determine the current length of the string, and `->buf` provides access
+to the string itself.
+
+Functions
+---------
+
+* Life cycle
+
+`strbuf_init`::
+
+ Initializes the structure. The second parameter can be zero or a bigger
+ number to allocate memory, in case you want to prevent further reallocs.
+
+`strbuf_release`::
+
+ Releases a string buffer and the memory it used. You should not use the
+ string buffer after using this function, unless you initialize it again.
+
+`strbuf_detach`::
+
+ Detaches the string from the string buffer. The function returns a
+ pointer to the old string and empties the buffer.
+
+`strbuf_attach`::
+
+ Attaches a string to a buffer. You should specify the string to attach,
+ the current length of the string and the amount of allocated memory.
+
+`strbuf_swap`::
+
+ Swaps the contents of two string buffers.
+
+* Related to the size of the buffer
+
+`strbuf_avail`::
+
+ Determines the amount of allocated but not used memory.
+
+`strbuf_grow`::
+
+ Allocated extra memory for the buffer.
+
+`strbuf_setlen`::
+
+ Sets the length of the buffer to a given value.
+
+`strbuf_reset`::
+
+ Empties the buffer by setting the size of it to zero.
+
+* Related to the contents of the buffer
+
+`strbuf_rtrim`::
+
+ Strip whitespace from the end of a string.
+
+`strbuf_cmp`::
+
+ Compares two buffers. Returns an integer less than, equal to, or greater
+ than zero if the first buffer is found, respectively, to be less than,
+ to match, or be greater than the second buffer.
+
+* Adding data to the buffer
+
+`strbuf_addch`::
+
+Adds a single character.
+
+`strbuf_insert`::
+
+ Insert data to the given position of the buffer. The remaining contents
+ will be shifted, not overwritten.
+
+`strbuf_remove`::
+
+ Remove given amount of data from a given position of the buffer.
+
+`strbuf_splice`::
+
+ Splice pos..pos+len with given data.
+
+`strbuf_add`::
+
+ Add data of given length to the buffer.
+
+`strbuf_addstr`::
+
+ Add a NULL-terminated string to the buffer.
+
+`strbuf_addbuf`::
+
+ Add an other buffer to the current one.
+
+`strbuf_adddup`::
+
+ Copy part of the buffer from a given position till a given length to the
+ end of the buffer.
+
+`strbuf_expand`::
+
+`strbuf_addf`::
+
+ Add a formatted string to the buffer.
+
+`strbuf_fread`::
+
+ Read a given size of data from a FILE* pointer to the buffer.
+
+`strbuf_read`::
+
+ Read the contents of a given file descriptor. The third argument can be
+ used to give a hint about the file, to avoid reallocs.
+
+`strbuf_read_file`::
+
+ Read the contents of a file, specified by its path. The third argument
+ can be used to give a hint about the file, to avoid reallocs.
+
+`strbuf_getline`::
+
+ Read a line from a FILE* pointer. The second argument specifies the line
+ terminator character, like `'\n'`.
+
+`stripspace`::
+
+ Strips whitespace from a buffer. The second parameter controls if
+ comments are considered contents to be removed or not.
+
+`launch_editor`::
--
1.5.6.rc0.dirty
next reply other threads:[~2008-06-02 23:00 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-06-02 22:59 Miklos Vajna [this message]
2008-06-02 23:39 ` [PATCH] Strbuf documentation: document most functions Junio C Hamano
2008-06-03 0:00 ` Johannes Schindelin
2008-06-03 8:42 ` Pierre Habouzit
2008-06-03 15:41 ` René Scharfe
2008-06-04 1:15 ` Miklos Vajna
2008-06-04 7:45 ` Junio C Hamano
2008-06-04 21:20 ` Miklos Vajna
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1212447591-4870-1-git-send-email-vmiklos@frugalware.org \
--to=vmiklos@frugalware.org \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).