2005-04-26 09:26:45 +08:00
|
|
|
#ifndef STRBUF_H
|
|
|
|
#define STRBUF_H
|
2007-09-06 19:20:05 +08:00
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
/**
|
|
|
|
* strbuf's are meant to be used with all the usual C string and memory
|
|
|
|
* APIs. Given that the length of the buffer is known, it's often better to
|
|
|
|
* use the mem* functions than a str* one (memchr vs. strchr e.g.).
|
|
|
|
* Though, one has to be careful about the fact that str* functions often
|
|
|
|
* stop on NULs and that strbufs may have embedded NULs.
|
|
|
|
*
|
|
|
|
* A strbuf is NUL terminated for convenience, but no function in the
|
|
|
|
* strbuf API actually relies on the string being free of NULs.
|
|
|
|
*
|
|
|
|
* strbufs have some invariants that are very important to keep in mind:
|
|
|
|
*
|
2015-01-16 17:05:10 +08:00
|
|
|
* - The `buf` member is never NULL, so it can be used in any usual C
|
|
|
|
* string operations safely. strbuf's _have_ to be initialized either by
|
|
|
|
* `strbuf_init()` or by `= STRBUF_INIT` before the invariants, though.
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
*
|
2015-01-16 17:05:10 +08:00
|
|
|
* Do *not* assume anything on what `buf` really is (e.g. if it is
|
|
|
|
* allocated memory or not), use `strbuf_detach()` to unwrap a memory
|
|
|
|
* buffer from its strbuf shell in a safe way. That is the sole supported
|
|
|
|
* way. This will give you a malloced buffer that you can later `free()`.
|
|
|
|
*
|
|
|
|
* However, it is totally safe to modify anything in the string pointed by
|
|
|
|
* the `buf` member, between the indices `0` and `len-1` (inclusive).
|
|
|
|
*
|
|
|
|
* - 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 this
|
|
|
|
* way:
|
|
|
|
*
|
2015-01-16 17:05:16 +08:00
|
|
|
* strbuf_grow(sb, SOME_SIZE); <1>
|
|
|
|
* strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE);
|
|
|
|
*
|
2015-01-16 17:05:10 +08:00
|
|
|
* <1> Here, the memory array starting at `sb->buf`, and of length
|
|
|
|
* `strbuf_avail(sb)` is all yours, and you can be sure that
|
|
|
|
* `strbuf_avail(sb)` is at least `SOME_SIZE`.
|
|
|
|
*
|
|
|
|
* NOTE: `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. Use `strbuf_avail()`
|
|
|
|
* instead.
|
|
|
|
*/
|
2007-09-06 19:20:05 +08:00
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
/**
|
|
|
|
* Data Structures
|
|
|
|
* ---------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the string buffer structure. The `len` member can be used to
|
|
|
|
* determine the current length of the string, and `buf` member provides
|
|
|
|
* access to the string itself.
|
|
|
|
*/
|
2005-04-26 09:26:45 +08:00
|
|
|
struct strbuf {
|
2007-09-06 19:20:05 +08:00
|
|
|
size_t alloc;
|
|
|
|
size_t len;
|
2005-05-18 20:14:09 +08:00
|
|
|
char *buf;
|
2005-04-26 09:26:45 +08:00
|
|
|
};
|
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
extern char strbuf_slopbuf[];
|
2007-09-27 18:58:23 +08:00
|
|
|
#define STRBUF_INIT { 0, 0, strbuf_slopbuf }
|
2007-09-06 19:20:05 +08:00
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
/**
|
2015-01-16 17:05:28 +08:00
|
|
|
* Life Cycle Functions
|
|
|
|
* --------------------
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the structure. The second parameter can be zero or a bigger
|
|
|
|
* number to allocate memory, in case you want to prevent further reallocs.
|
|
|
|
*/
|
2007-09-10 18:35:04 +08:00
|
|
|
extern void strbuf_init(struct strbuf *, size_t);
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Release a string buffer and the memory it used. You should not use the
|
|
|
|
* string buffer after using this function, unless you initialize it again.
|
|
|
|
*/
|
2007-09-06 19:20:05 +08:00
|
|
|
extern void strbuf_release(struct strbuf *);
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Detach the string from the strbuf and returns it; you now own the
|
|
|
|
* storage the string occupies and it is your responsibility from then on
|
|
|
|
* to release it with `free(3)` when you are done with it.
|
|
|
|
*/
|
2007-09-27 18:58:23 +08:00
|
|
|
extern char *strbuf_detach(struct strbuf *, size_t *);
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach a string to a buffer. You should specify the string to attach,
|
|
|
|
* the current length of the string and the amount of allocated memory.
|
|
|
|
* The amount must be larger than the string length, because the string you
|
|
|
|
* pass is supposed to be a NUL-terminated string. This string _must_ be
|
|
|
|
* malloc()ed, and after attaching, the pointer cannot be relied upon
|
|
|
|
* anymore, and neither be free()d directly.
|
|
|
|
*/
|
2007-09-15 21:56:50 +08:00
|
|
|
extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Swap the contents of two string buffers.
|
|
|
|
*/
|
2014-03-01 10:50:55 +08:00
|
|
|
static inline void strbuf_swap(struct strbuf *a, struct strbuf *b)
|
|
|
|
{
|
2017-01-29 05:40:58 +08:00
|
|
|
SWAP(*a, *b);
|
2007-09-20 06:42:12 +08:00
|
|
|
}
|
2007-09-06 19:20:05 +08:00
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
2015-01-16 17:05:28 +08:00
|
|
|
* Functions related to the size of the buffer
|
|
|
|
* -------------------------------------------
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the amount of allocated but unused memory.
|
|
|
|
*/
|
2014-03-01 10:50:55 +08:00
|
|
|
static inline size_t strbuf_avail(const struct strbuf *sb)
|
|
|
|
{
|
2007-09-20 06:42:12 +08:00
|
|
|
return sb->alloc ? sb->alloc - sb->len - 1 : 0;
|
2007-09-06 19:20:05 +08:00
|
|
|
}
|
2007-09-26 17:26:06 +08:00
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
/**
|
|
|
|
* Ensure that at least this amount of unused memory is available after
|
|
|
|
* `len`. This is used when you know a typical size for what you will add
|
|
|
|
* and want to avoid repetitive automatic resizing of the underlying buffer.
|
|
|
|
* This is never a needed operation, but can be critical for performance in
|
|
|
|
* some cases.
|
|
|
|
*/
|
2007-09-26 17:26:06 +08:00
|
|
|
extern void strbuf_grow(struct strbuf *, size_t);
|
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
/**
|
|
|
|
* Set the length of the buffer to a given value. This function does *not*
|
|
|
|
* allocate new memory, so you should not perform a `strbuf_setlen()` to a
|
|
|
|
* length that is larger than `len + strbuf_avail()`. `strbuf_setlen()` is
|
|
|
|
* just meant as a 'please fix invariants from this strbuf I just messed
|
|
|
|
* with'.
|
|
|
|
*/
|
2014-03-01 10:50:55 +08:00
|
|
|
static inline void strbuf_setlen(struct strbuf *sb, size_t len)
|
|
|
|
{
|
2011-04-28 01:24:50 +08:00
|
|
|
if (len > (sb->alloc ? sb->alloc - 1 : 0))
|
|
|
|
die("BUG: strbuf_setlen() beyond buffer");
|
2007-09-20 06:42:12 +08:00
|
|
|
sb->len = len;
|
|
|
|
sb->buf[len] = '\0';
|
2007-09-06 19:20:05 +08:00
|
|
|
}
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Empty the buffer by setting the size of it to zero.
|
|
|
|
*/
|
2007-09-27 18:58:23 +08:00
|
|
|
#define strbuf_reset(sb) strbuf_setlen(sb, 0)
|
2007-09-06 19:20:05 +08:00
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
2015-01-16 17:05:28 +08:00
|
|
|
* Functions related to the contents of the buffer
|
|
|
|
* -----------------------------------------------
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2015-01-16 17:06:04 +08:00
|
|
|
* Strip whitespace from the beginning (`ltrim`), end (`rtrim`), or both side
|
|
|
|
* (`trim`) of a string.
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
*/
|
2008-07-14 02:29:18 +08:00
|
|
|
extern void strbuf_trim(struct strbuf *);
|
2007-09-10 18:35:04 +08:00
|
|
|
extern void strbuf_rtrim(struct strbuf *);
|
2008-07-14 02:29:18 +08:00
|
|
|
extern void strbuf_ltrim(struct strbuf *);
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the contents of the strbuf with a reencoded form. Returns -1
|
|
|
|
* on error, 0 on success.
|
|
|
|
*/
|
2014-05-22 17:30:14 +08:00
|
|
|
extern int strbuf_reencode(struct strbuf *sb, const char *from, const char *to);
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Lowercase each character in the buffer using `tolower`.
|
|
|
|
*/
|
2014-05-24 04:03:47 +08:00
|
|
|
extern void strbuf_tolower(struct strbuf *sb);
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare 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.
|
|
|
|
*/
|
2008-07-14 02:28:24 +08:00
|
|
|
extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
|
2008-07-14 02:29:18 +08:00
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
2015-01-16 17:05:28 +08:00
|
|
|
* Adding data to the buffer
|
|
|
|
* -------------------------
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
*
|
|
|
|
* NOTE: All of the functions in this section will grow the buffer as
|
|
|
|
* necessary. If they fail for some reason other than memory shortage and the
|
|
|
|
* buffer hadn't been allocated before (i.e. the `struct strbuf` was set to
|
|
|
|
* `STRBUF_INIT`), then they will free() it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a single character to the buffer.
|
|
|
|
*/
|
|
|
|
static inline void strbuf_addch(struct strbuf *sb, int c)
|
|
|
|
{
|
2015-04-16 16:53:56 +08:00
|
|
|
if (!strbuf_avail(sb))
|
|
|
|
strbuf_grow(sb, 1);
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
sb->buf[sb->len++] = c;
|
|
|
|
sb->buf[sb->len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a character the specified number of times to the buffer.
|
|
|
|
*/
|
|
|
|
extern void strbuf_addchars(struct strbuf *sb, int c, size_t n);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert data to the given position of the buffer. The remaining contents
|
|
|
|
* will be shifted, not overwritten.
|
|
|
|
*/
|
|
|
|
extern void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove given amount of data from a given position of the buffer.
|
|
|
|
*/
|
|
|
|
extern void strbuf_remove(struct strbuf *, size_t pos, size_t len);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the bytes between `pos..pos+len` and replace it with the given
|
|
|
|
* data.
|
|
|
|
*/
|
|
|
|
extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,
|
|
|
|
const void *, size_t);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a NUL-terminated string to the buffer. Each line will be prepended
|
|
|
|
* by a comment character and a blank.
|
|
|
|
*/
|
|
|
|
extern void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add data of given length to the buffer.
|
|
|
|
*/
|
|
|
|
extern void strbuf_add(struct strbuf *, const void *, size_t);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a NUL-terminated string to the buffer.
|
|
|
|
*
|
|
|
|
* NOTE: This function will *always* be implemented as an inline or a macro
|
|
|
|
* using strlen, meaning that this is efficient to write things like:
|
|
|
|
*
|
2015-01-16 17:05:16 +08:00
|
|
|
* strbuf_addstr(sb, "immediate string");
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static inline void strbuf_addstr(struct strbuf *sb, const char *s)
|
|
|
|
{
|
|
|
|
strbuf_add(sb, s, strlen(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy the contents of another buffer at the end of the current one.
|
|
|
|
*/
|
2016-07-22 00:46:44 +08:00
|
|
|
extern void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2);
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy part of the buffer from a given position till a given length to the
|
|
|
|
* end of the buffer.
|
|
|
|
*/
|
|
|
|
extern void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function can be used to expand a format string containing
|
|
|
|
* placeholders. To that end, it parses the string and calls the specified
|
|
|
|
* function for every percent sign found.
|
|
|
|
*
|
|
|
|
* The callback function is given a pointer to the character after the `%`
|
|
|
|
* and a pointer to the struct strbuf. It is expected to add the expanded
|
|
|
|
* version of the placeholder to the strbuf, e.g. to add a newline
|
|
|
|
* character if the letter `n` appears after a `%`. The function returns
|
|
|
|
* the length of the placeholder recognized and `strbuf_expand()` skips
|
|
|
|
* over it.
|
|
|
|
*
|
|
|
|
* The format `%%` is automatically expanded to a single `%` as a quoting
|
|
|
|
* mechanism; callers do not need to handle the `%` placeholder themselves,
|
|
|
|
* and the callback function will not be invoked for this placeholder.
|
|
|
|
*
|
|
|
|
* All other characters (non-percent and not skipped ones) are copied
|
|
|
|
* verbatim to the strbuf. If the callback returned zero, meaning that the
|
|
|
|
* placeholder is unknown, then the percent sign is copied, too.
|
|
|
|
*
|
|
|
|
* In order to facilitate caching and to make it possible to give
|
|
|
|
* parameters to the callback, `strbuf_expand()` passes a context pointer,
|
|
|
|
* which can be used by the programmer of the callback as she sees fit.
|
|
|
|
*/
|
|
|
|
typedef size_t (*expand_fn_t) (struct strbuf *sb, const char *placeholder, void *context);
|
|
|
|
extern void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, void *context);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used as callback for `strbuf_expand()`, expects an array of
|
|
|
|
* struct strbuf_expand_dict_entry as context, i.e. pairs of
|
|
|
|
* placeholder and replacement string. The array needs to be
|
|
|
|
* terminated by an entry with placeholder set to NULL.
|
|
|
|
*/
|
|
|
|
struct strbuf_expand_dict_entry {
|
|
|
|
const char *placeholder;
|
|
|
|
const char *value;
|
|
|
|
};
|
|
|
|
extern size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, void *context);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append the contents of one strbuf to another, quoting any
|
|
|
|
* percent signs ("%") into double-percents ("%%") in the
|
|
|
|
* destination. This is useful for literal data to be fed to either
|
|
|
|
* strbuf_expand or to the *printf family of functions.
|
|
|
|
*/
|
|
|
|
extern void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append the given byte size as a human-readable string (i.e. 12.23 KiB,
|
|
|
|
* 3.50 MiB).
|
|
|
|
*/
|
|
|
|
extern void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a formatted string to the buffer.
|
|
|
|
*/
|
|
|
|
__attribute__((format (printf,2,3)))
|
|
|
|
extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a formatted string prepended by a comment character and a
|
|
|
|
* blank to the buffer.
|
|
|
|
*/
|
|
|
|
__attribute__((format (printf, 2, 3)))
|
|
|
|
extern void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
|
|
|
|
|
|
|
|
__attribute__((format (printf,2,0)))
|
|
|
|
extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
|
|
|
|
|
2015-06-26 00:55:45 +08:00
|
|
|
/**
|
|
|
|
* Add the time specified by `tm`, as formatted by `strftime`.
|
|
|
|
*/
|
|
|
|
extern void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm);
|
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
/**
|
|
|
|
* Read a given size of data from a FILE* pointer to the buffer.
|
|
|
|
*
|
|
|
|
* NOTE: The buffer is rewound if the read fails. If -1 is returned,
|
|
|
|
* `errno` must be consulted, like you would do for `read(3)`.
|
2016-01-14 10:32:23 +08:00
|
|
|
* `strbuf_read()`, `strbuf_read_file()` and `strbuf_getline_*()`
|
|
|
|
* family of functions have the same behaviour as well.
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
*/
|
|
|
|
extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the contents of a given file descriptor. The third argument can be
|
|
|
|
* used to give a hint about the file size, to avoid reallocs. If read fails,
|
|
|
|
* any partial read is undone.
|
|
|
|
*/
|
|
|
|
extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
|
|
|
|
|
2015-12-16 08:04:08 +08:00
|
|
|
/**
|
|
|
|
* Read the contents of a given file descriptor partially by using only one
|
|
|
|
* attempt of xread. The third argument can be used to give a hint about the
|
|
|
|
* file size, to avoid reallocs. Returns the number of new bytes appended to
|
|
|
|
* the sb.
|
|
|
|
*/
|
|
|
|
extern ssize_t strbuf_read_once(struct strbuf *, int fd, size_t hint);
|
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
/**
|
|
|
|
* Read the contents of a file, specified by its path. The third argument
|
|
|
|
* can be used to give a hint about the file size, to avoid reallocs.
|
2016-06-14 14:14:11 +08:00
|
|
|
* Return the number of bytes read or a negative value if some error
|
|
|
|
* occurred while opening or reading the file.
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
*/
|
2015-07-03 21:59:32 +08:00
|
|
|
extern ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the target of a symbolic link, specified by its path. The third
|
|
|
|
* argument can be used to give a hint about the size, to avoid reallocs.
|
|
|
|
*/
|
|
|
|
extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
|
|
|
|
|
2016-03-01 10:07:15 +08:00
|
|
|
/**
|
|
|
|
* Write the whole content of the strbuf to the stream not stopping at
|
|
|
|
* NUL bytes.
|
|
|
|
*/
|
|
|
|
extern ssize_t strbuf_write(struct strbuf *sb, FILE *stream);
|
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
/**
|
2016-01-14 10:32:23 +08:00
|
|
|
* Read a line from a FILE *, overwriting the existing contents of
|
|
|
|
* the strbuf. The strbuf_getline*() family of functions share
|
|
|
|
* this signature, but have different line termination conventions.
|
|
|
|
*
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
* Reading stops after the terminator or at EOF. The terminator
|
|
|
|
* is removed from the buffer before returning. Returns 0 unless
|
|
|
|
* there was nothing left before EOF, in which case it returns `EOF`.
|
|
|
|
*/
|
2016-01-14 07:31:17 +08:00
|
|
|
typedef int (*strbuf_getline_fn)(struct strbuf *, FILE *);
|
|
|
|
|
|
|
|
/* Uses LF as the line terminator */
|
|
|
|
extern int strbuf_getline_lf(struct strbuf *sb, FILE *fp);
|
|
|
|
|
|
|
|
/* Uses NUL as the line terminator */
|
|
|
|
extern int strbuf_getline_nul(struct strbuf *sb, FILE *fp);
|
|
|
|
|
2015-10-29 04:17:29 +08:00
|
|
|
/*
|
2016-01-14 07:31:17 +08:00
|
|
|
* Similar to strbuf_getline_lf(), but additionally treats a CR that
|
|
|
|
* comes immediately before the LF as part of the terminator.
|
2016-01-14 10:32:23 +08:00
|
|
|
* This is the most friendly version to be used to read "text" files
|
|
|
|
* that can come from platforms whose native text format is CRLF
|
|
|
|
* terminated.
|
2015-10-29 04:17:29 +08:00
|
|
|
*/
|
2016-01-14 10:32:23 +08:00
|
|
|
extern int strbuf_getline(struct strbuf *, FILE *);
|
2015-10-29 04:17:29 +08:00
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Like `strbuf_getline`, but keeps the trailing terminator (if
|
|
|
|
* any) in the buffer.
|
|
|
|
*/
|
|
|
|
extern int strbuf_getwholeline(struct strbuf *, FILE *, int);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like `strbuf_getwholeline`, but operates on a file descriptor.
|
|
|
|
* It reads one character at a time, so it is very slow. Do not
|
|
|
|
* use it unless you need the correct position in the file
|
|
|
|
* descriptor.
|
|
|
|
*/
|
|
|
|
extern int strbuf_getwholeline_fd(struct strbuf *, int, int);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the buffer to the path of the current working directory.
|
|
|
|
*/
|
|
|
|
extern int strbuf_getcwd(struct strbuf *sb);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a path to a buffer, converting a relative path to an
|
|
|
|
* absolute one in the process. Symbolic links are not
|
|
|
|
* resolved.
|
|
|
|
*/
|
|
|
|
extern void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
|
|
|
|
|
2017-02-26 00:00:33 +08:00
|
|
|
/**
|
|
|
|
* Canonize `path` (make it absolute, resolve symlinks, remove extra
|
|
|
|
* slashes) and append it to `sb`. Die with an informative error
|
|
|
|
* message if there is a problem.
|
|
|
|
*
|
|
|
|
* The directory part of `path` (i.e., everything up to the last
|
|
|
|
* dir_sep) must denote a valid, existing directory, but the last
|
|
|
|
* component need not exist.
|
|
|
|
*
|
|
|
|
* Callers that don't mind links should use the more lightweight
|
|
|
|
* strbuf_add_absolute_path() instead.
|
|
|
|
*/
|
|
|
|
extern void strbuf_add_real_path(struct strbuf *sb, const char *path);
|
|
|
|
|
link_alt_odb_entry: handle normalize_path errors
When we add a new alternate to the list, we try to normalize
out any redundant "..", etc. However, we do not look at the
return value of normalize_path_copy(), and will happily
continue with a path that could not be normalized. Worse,
the normalizing process is done in-place, so we are left
with whatever half-finished working state the normalizing
function was in.
Fortunately, this cannot cause us to read past the end of
our buffer, as that working state will always leave the
NUL from the original path in place. And we do tend to
notice problems when we check is_directory() on the path.
But you can see the nonsense that we feed to is_directory
with an entry like:
this/../../is/../../way/../../too/../../deep/../../to/../../resolve
in your objects/info/alternates, which yields:
error: object directory
/to/e/deep/too/way//ects/this/../../is/../../way/../../too/../../deep/../../to/../../resolve
does not exist; check .git/objects/info/alternates.
We can easily fix this just by checking the return value.
But that makes it hard to generate a good error message,
since we're normalizing in-place and our input value has
been overwritten by cruft.
Instead, let's provide a strbuf helper that does an in-place
normalize, but restores the original contents on error. This
uses a second buffer under the hood, which is slightly less
efficient, but this is not a performance-critical code path.
The strbuf helper can also properly set the "len" parameter
of the strbuf before returning. Just doing:
normalize_path_copy(buf.buf, buf.buf);
will shorten the string, but leave buf.len at the original
length. That may be confusing to later code which uses the
strbuf.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-10-04 04:34:17 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize in-place the path contained in the strbuf. See
|
|
|
|
* normalize_path_copy() for details. If an error occurs, the contents of "sb"
|
|
|
|
* are left untouched, and -1 is returned.
|
|
|
|
*/
|
|
|
|
extern int strbuf_normalize_path(struct strbuf *sb);
|
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
/**
|
|
|
|
* Strip whitespace from a buffer. The second parameter controls if
|
|
|
|
* comments are considered contents to be removed or not.
|
|
|
|
*/
|
2015-10-16 23:16:42 +08:00
|
|
|
extern void strbuf_stripspace(struct strbuf *buf, int skip_comments);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Temporary alias until all topic branches have switched to use
|
|
|
|
* strbuf_stripspace directly.
|
|
|
|
*/
|
|
|
|
static inline void stripspace(struct strbuf *buf, int skip_comments)
|
|
|
|
{
|
|
|
|
strbuf_stripspace(buf, skip_comments);
|
|
|
|
}
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
|
2014-07-01 01:01:51 +08:00
|
|
|
static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
|
|
|
|
{
|
|
|
|
if (strip_suffix_mem(sb->buf, &sb->len, suffix)) {
|
|
|
|
strbuf_setlen(sb, sb->len);
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-16 17:04:51 +08:00
|
|
|
/**
|
2012-11-04 14:46:54 +08:00
|
|
|
* Split str (of length slen) at the specified terminator character.
|
|
|
|
* Return a null-terminated array of pointers to strbuf objects
|
|
|
|
* holding the substrings. The substrings include the terminator,
|
|
|
|
* except for the last substring, which might be unterminated if the
|
|
|
|
* original string did not end with a terminator. If max is positive,
|
|
|
|
* then split the string into at most max substrings (with the last
|
|
|
|
* substring containing everything following the (max-1)th terminator
|
|
|
|
* character).
|
|
|
|
*
|
2015-01-16 17:05:57 +08:00
|
|
|
* The most generic form is `strbuf_split_buf`, which takes an arbitrary
|
|
|
|
* pointer/len buffer. The `_str` variant takes a NUL-terminated string,
|
|
|
|
* the `_max` variant takes a strbuf, and just `strbuf_split` is a convenience
|
|
|
|
* wrapper to drop the `max` parameter.
|
|
|
|
*
|
2012-11-04 14:46:54 +08:00
|
|
|
* For lighter-weight alternatives, see string_list_split() and
|
|
|
|
* string_list_split_in_place().
|
|
|
|
*/
|
2011-06-09 23:54:58 +08:00
|
|
|
extern struct strbuf **strbuf_split_buf(const char *, size_t,
|
2012-11-04 14:46:53 +08:00
|
|
|
int terminator, int max);
|
2012-11-04 14:46:54 +08:00
|
|
|
|
2011-06-09 23:54:58 +08:00
|
|
|
static inline struct strbuf **strbuf_split_str(const char *str,
|
2012-11-04 14:46:53 +08:00
|
|
|
int terminator, int max)
|
2011-06-09 23:54:58 +08:00
|
|
|
{
|
2012-11-04 14:46:53 +08:00
|
|
|
return strbuf_split_buf(str, strlen(str), terminator, max);
|
2011-06-09 23:54:58 +08:00
|
|
|
}
|
2012-11-04 14:46:54 +08:00
|
|
|
|
2011-06-09 23:54:58 +08:00
|
|
|
static inline struct strbuf **strbuf_split_max(const struct strbuf *sb,
|
2012-11-04 14:46:53 +08:00
|
|
|
int terminator, int max)
|
2011-06-09 23:54:58 +08:00
|
|
|
{
|
2012-11-04 14:46:53 +08:00
|
|
|
return strbuf_split_buf(sb->buf, sb->len, terminator, max);
|
2011-06-09 23:54:58 +08:00
|
|
|
}
|
2012-11-04 14:46:54 +08:00
|
|
|
|
2012-11-04 14:46:53 +08:00
|
|
|
static inline struct strbuf **strbuf_split(const struct strbuf *sb,
|
|
|
|
int terminator)
|
2011-06-09 23:51:22 +08:00
|
|
|
{
|
2012-11-04 14:46:53 +08:00
|
|
|
return strbuf_split_max(sb, terminator, 0);
|
2011-06-09 23:51:22 +08:00
|
|
|
}
|
2012-11-04 14:46:54 +08:00
|
|
|
|
2015-01-16 17:04:51 +08:00
|
|
|
/**
|
2012-11-04 14:46:54 +08:00
|
|
|
* Free a NULL-terminated list of strbufs (for example, the return
|
|
|
|
* values of the strbuf_split*() functions).
|
|
|
|
*/
|
2008-07-14 02:29:18 +08:00
|
|
|
extern void strbuf_list_free(struct strbuf **);
|
2007-09-10 18:35:04 +08:00
|
|
|
|
2015-09-25 05:05:45 +08:00
|
|
|
/**
|
|
|
|
* Add the abbreviation, as generated by find_unique_abbrev, of `sha1` to
|
|
|
|
* the strbuf `sb`.
|
|
|
|
*/
|
|
|
|
extern void strbuf_add_unique_abbrev(struct strbuf *sb,
|
|
|
|
const unsigned char *sha1,
|
|
|
|
int abbrev_len);
|
|
|
|
|
strbuf.h: integrate api-strbuf.txt documentation
Some of strbuf is documented as comments above functions,
and some is separate in Documentation/technical/api-strbuf.txt.
This makes it annoying to find the appropriate documentation.
We'd rather have it all in one place, which means all in the
text document, or all in the header.
Let's choose the header as that place. Even though the
formatting is not quite as pretty, this keeps the
documentation close to the related code. The hope is that
this makes it easier to find what you want (human-readable
comments are right next to the C declarations), and easier
for writers to keep the documentation up to date.
This is more or less a straight import of the text from
api-strbuf.txt into C comments, complete with asciidoc
formatting. The exceptions are:
1. All comments created in this way are started with "/**"
to indicate they are part of the API documentation. This
may help later with extracting the text to pretty-print
it.
2. Function descriptions do not repeat the function name,
as it is available in the context directly below. So:
`strbuf_add`::
Add data of given length to the buffer.
from api-strbuf.txt becomes:
/**
* Add data of given length to the buffer.
*/
void strbuf_add(struct strbuf *sb, const void *, size_t);
As a result, any block-continuation required in asciidoc
for that list item was dropped in favor of straight
blank-line paragraph (since it is not necessary when we
are not in a list item).
3. There is minor re-wording to integrate existing comments
and api-strbuf text. In each case, I took whichever
version was more descriptive, and eliminated any
redundancies. In one case, for strbuf_addstr, the api
documentation gave its inline definition; I eliminated
this as redundant with the actual definition, which can
be seen directly below the comment.
4. The functions in the header file are re-ordered to match
the ordering of the API documentation, under the
assumption that more thought went into the grouping
there.
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-01-16 17:04:04 +08:00
|
|
|
/**
|
|
|
|
* Launch the user preferred editor to edit a file and fill the buffer
|
|
|
|
* with the file's contents upon the user completing their editing. The
|
|
|
|
* third argument can be used to set the environment which the editor is
|
|
|
|
* run in. If the buffer is NULL the editor is launched as usual but the
|
|
|
|
* file's contents are not read into the buffer upon completion.
|
|
|
|
*/
|
|
|
|
extern int launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
|
2007-09-06 19:20:05 +08:00
|
|
|
|
2011-11-05 12:06:30 +08:00
|
|
|
extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *buf, size_t size);
|
|
|
|
|
2015-01-16 17:04:51 +08:00
|
|
|
/**
|
2012-11-25 19:08:34 +08:00
|
|
|
* Append s to sb, with the characters '<', '>', '&' and '"' converted
|
|
|
|
* into XML entities.
|
|
|
|
*/
|
|
|
|
extern void strbuf_addstr_xml_quoted(struct strbuf *sb, const char *s);
|
|
|
|
|
2015-09-25 05:05:43 +08:00
|
|
|
/**
|
|
|
|
* "Complete" the contents of `sb` by ensuring that either it ends with the
|
|
|
|
* character `term`, or it is empty. This can be used, for example,
|
|
|
|
* to ensure that text ends with a newline, but without creating an empty
|
|
|
|
* blank line if there is no content in the first place.
|
|
|
|
*/
|
|
|
|
static inline void strbuf_complete(struct strbuf *sb, char term)
|
|
|
|
{
|
|
|
|
if (sb->len && sb->buf[sb->len - 1] != term)
|
|
|
|
strbuf_addch(sb, term);
|
|
|
|
}
|
|
|
|
|
2011-11-05 12:06:30 +08:00
|
|
|
static inline void strbuf_complete_line(struct strbuf *sb)
|
|
|
|
{
|
2015-09-25 05:05:43 +08:00
|
|
|
strbuf_complete(sb, '\n');
|
2011-11-05 12:06:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-02 16:21:30 +08:00
|
|
|
/*
|
|
|
|
* Copy "name" to "sb", expanding any special @-marks as handled by
|
|
|
|
* interpret_branch_name(). The result is a non-qualified branch name
|
|
|
|
* (so "foo" or "origin/master" instead of "refs/heads/foo" or
|
|
|
|
* "refs/remotes/origin/master").
|
|
|
|
*
|
|
|
|
* Note that the resulting name may not be a syntactically valid refname.
|
interpret_branch_name: allow callers to restrict expansions
The interpret_branch_name() function converts names like
@{-1} and @{upstream} into branch names. The expanded ref
names are not fully qualified, and may be outside of the
refs/heads/ namespace (e.g., "@" expands to "HEAD", and
"@{upstream}" is likely to be in "refs/remotes/").
This is OK for callers like dwim_ref() which are primarily
interested in resolving the resulting name, no matter where
it is. But callers like "git branch" treat the result as a
branch name in refs/heads/. When we expand to a ref outside
that namespace, the results are very confusing (e.g., "git
branch @" tries to create refs/heads/HEAD, which is
nonsense).
Callers can't know from the returned string how the
expansion happened (e.g., did the user really ask for a
branch named "HEAD", or did we do a bogus expansion?). One
fix would be to return some out-parameters describing the
types of expansion that occurred. This has the benefit that
the caller can generate precise error messages ("I
understood @{upstream} to mean origin/master, but that is a
remote tracking branch, so you cannot create it as a local
name").
However, out-parameters make the function interface somewhat
cumbersome. Instead, let's do the opposite: let the caller
tell us which elements to expand. That's easier to pass in,
and none of the callers give more precise error messages
than "@{upstream} isn't a valid branch name" anyway (which
should be sufficient).
The strbuf_branchname() function needs a similar parameter,
as most of the callers access interpret_branch_name()
through it.
We can break the callers down into two groups:
1. Callers that are happy with any kind of ref in the
result. We pass "0" here, so they continue to work
without restrictions. This includes merge_name(),
the reflog handling in add_pending_object_with_path(),
and substitute_branch_name(). This last is what powers
dwim_ref().
2. Callers that have funny corner cases (mostly in
git-branch and git-checkout). These need to make use of
the new parameter, but I've left them as "0" in this
patch, and will address them individually in follow-on
patches.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-03-02 16:23:01 +08:00
|
|
|
*
|
|
|
|
* If "allowed" is non-zero, restrict the set of allowed expansions. See
|
|
|
|
* interpret_branch_name() for details.
|
2017-03-02 16:21:30 +08:00
|
|
|
*/
|
interpret_branch_name: allow callers to restrict expansions
The interpret_branch_name() function converts names like
@{-1} and @{upstream} into branch names. The expanded ref
names are not fully qualified, and may be outside of the
refs/heads/ namespace (e.g., "@" expands to "HEAD", and
"@{upstream}" is likely to be in "refs/remotes/").
This is OK for callers like dwim_ref() which are primarily
interested in resolving the resulting name, no matter where
it is. But callers like "git branch" treat the result as a
branch name in refs/heads/. When we expand to a ref outside
that namespace, the results are very confusing (e.g., "git
branch @" tries to create refs/heads/HEAD, which is
nonsense).
Callers can't know from the returned string how the
expansion happened (e.g., did the user really ask for a
branch named "HEAD", or did we do a bogus expansion?). One
fix would be to return some out-parameters describing the
types of expansion that occurred. This has the benefit that
the caller can generate precise error messages ("I
understood @{upstream} to mean origin/master, but that is a
remote tracking branch, so you cannot create it as a local
name").
However, out-parameters make the function interface somewhat
cumbersome. Instead, let's do the opposite: let the caller
tell us which elements to expand. That's easier to pass in,
and none of the callers give more precise error messages
than "@{upstream} isn't a valid branch name" anyway (which
should be sufficient).
The strbuf_branchname() function needs a similar parameter,
as most of the callers access interpret_branch_name()
through it.
We can break the callers down into two groups:
1. Callers that are happy with any kind of ref in the
result. We pass "0" here, so they continue to work
without restrictions. This includes merge_name(),
the reflog handling in add_pending_object_with_path(),
and substitute_branch_name(). This last is what powers
dwim_ref().
2. Callers that have funny corner cases (mostly in
git-branch and git-checkout). These need to make use of
the new parameter, but I've left them as "0" in this
patch, and will address them individually in follow-on
patches.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-03-02 16:23:01 +08:00
|
|
|
extern void strbuf_branchname(struct strbuf *sb, const char *name,
|
|
|
|
unsigned allowed);
|
2017-03-02 16:21:30 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Like strbuf_branchname() above, but confirm that the result is
|
|
|
|
* syntactically valid to be used as a local branch name in refs/heads/.
|
|
|
|
*
|
|
|
|
* The return value is "0" if the result is valid, and "-1" otherwise.
|
|
|
|
*/
|
2009-03-22 05:35:51 +08:00
|
|
|
extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
|
2009-03-22 04:17:30 +08:00
|
|
|
|
2011-12-10 18:34:20 +08:00
|
|
|
extern void strbuf_addstr_urlencode(struct strbuf *, const char *,
|
|
|
|
int reserved);
|
2014-07-29 02:33:55 +08:00
|
|
|
|
2012-04-23 20:30:22 +08:00
|
|
|
__attribute__((format (printf,1,2)))
|
|
|
|
extern int printf_ln(const char *fmt, ...);
|
|
|
|
__attribute__((format (printf,2,3)))
|
|
|
|
extern int fprintf_ln(FILE *fp, const char *fmt, ...);
|
|
|
|
|
2014-05-22 17:44:09 +08:00
|
|
|
char *xstrdup_tolower(const char *);
|
|
|
|
|
2015-01-16 17:04:51 +08:00
|
|
|
/**
|
strbuf: add xstrfmt helper
You can use a strbuf to build up a string from parts, and
then detach it. In the general case, you might use multiple
strbuf_add* functions to do the building. However, in many
cases, a single strbuf_addf is sufficient, and we end up
with:
struct strbuf buf = STRBUF_INIT;
...
strbuf_addf(&buf, fmt, some, args);
str = strbuf_detach(&buf, NULL);
We can make this much more readable (and avoid introducing
an extra variable, which can clutter the code) by
introducing a convenience function:
str = xstrfmt(fmt, some, args);
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-06-19 04:01:34 +08:00
|
|
|
* Create a newly allocated string using printf format. You can do this easily
|
|
|
|
* with a strbuf, but this provides a shortcut to save a few lines.
|
|
|
|
*/
|
|
|
|
__attribute__((format (printf, 1, 0)))
|
|
|
|
char *xstrvfmt(const char *fmt, va_list ap);
|
|
|
|
__attribute__((format (printf, 1, 2)))
|
|
|
|
char *xstrfmt(const char *fmt, ...);
|
|
|
|
|
2005-04-26 09:26:45 +08:00
|
|
|
#endif /* STRBUF_H */
|