mirror of
http://git.haproxy.org/git/haproxy.git/
synced 2025-02-03 03:52:38 +00:00
MINOR: chunks: add chunk_strcat() and chunk_newstr()
These two new functions will make it easier to manipulate small strings from within functions, because at many places, multiple short strings are needed which do not deserve a malloc() nor a free(), and alloca() is often discouraged. Since we already have trash chunks, it's convenient to be able to allocate substrings from a chunk and use them later since our functions already perform all the length checks. chunk_newstr() adds a trailing zero at the end of a chunk and returns the pointer to the next character, which can be used as an independant string. chunk_strcat() does what it says.
This commit is contained in:
parent
0b6044fa24
commit
601360b41d
@ -102,6 +102,44 @@ static inline int chunk_strcpy(struct chunk *chk, const char *str)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* appends str after <chk> followed by a trailing zero. Returns 0 in
|
||||
* case of failure.
|
||||
*/
|
||||
static inline int chunk_strcat(struct chunk *chk, const char *str)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
len = strlen(str);
|
||||
|
||||
if (unlikely(chk->len + len >= chk->size))
|
||||
return 0;
|
||||
|
||||
memcpy(chk->str + chk->len, str, len + 1);
|
||||
chk->len += len;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Adds a trailing zero to the current chunk and returns the pointer to the
|
||||
* following part. The purpose is to be able to use a chunk as a series of
|
||||
* short independant strings with chunk_* functions, which do not need to be
|
||||
* released. Returns NULL if no space is available to ensure that the new
|
||||
* string will have its own trailing zero. For example :
|
||||
* chunk_init(&trash);
|
||||
* pid = chunk_newstr(&trash);
|
||||
* chunk_appendf(&trash, "%d", getpid()));
|
||||
* name = chunk_newstr(&trash);
|
||||
* chunk_appendf(&trash, "%s", gethosname());
|
||||
* printf("hostname=<%s>, pid=<%d>\n", name, pid);
|
||||
*/
|
||||
static inline char *chunk_newstr(struct chunk *chk)
|
||||
{
|
||||
if (chk->len + 1 >= chk->size)
|
||||
return NULL;
|
||||
|
||||
chk->str[chk->len++] = 0;
|
||||
return chk->str + chk->len;
|
||||
}
|
||||
|
||||
static inline void chunk_drop(struct chunk *chk)
|
||||
{
|
||||
chk->str = NULL;
|
||||
|
Loading…
Reference in New Issue
Block a user