Merge branch 'mh/strbuf-split'
Cleanups and documentation for strbuf_split. * mh/strbuf-split: strbuf_split*(): document functions strbuf_split*(): rename "delim" parameter to "terminator" strbuf_split_buf(): simplify iteration strbuf_split_buf(): use ALLOC_GROW()
This commit is contained in:
commit
2be3d85a1c
@ -279,6 +279,22 @@ same behaviour as well.
|
|||||||
Strip whitespace from a buffer. The second parameter controls if
|
Strip whitespace from a buffer. The second parameter controls if
|
||||||
comments are considered contents to be removed or not.
|
comments are considered contents to be removed or not.
|
||||||
|
|
||||||
|
`strbuf_split_buf`::
|
||||||
|
`strbuf_split_str`::
|
||||||
|
`strbuf_split_max`::
|
||||||
|
`strbuf_split`::
|
||||||
|
|
||||||
|
Split a string or strbuf into a list of strbufs at a specified
|
||||||
|
terminator character. The returned substrings include the
|
||||||
|
terminator characters. Some of these functions take a `max`
|
||||||
|
parameter, which, if positive, limits the output to that
|
||||||
|
number of substrings.
|
||||||
|
|
||||||
|
`strbuf_list_free`::
|
||||||
|
|
||||||
|
Free a list of strbufs (for example, the return values of the
|
||||||
|
`strbuf_split()` functions).
|
||||||
|
|
||||||
`launch_editor`::
|
`launch_editor`::
|
||||||
|
|
||||||
Launch the user preferred editor to edit a file and fill the buffer
|
Launch the user preferred editor to edit a file and fill the buffer
|
||||||
|
39
strbuf.c
39
strbuf.c
@ -106,35 +106,30 @@ void strbuf_ltrim(struct strbuf *sb)
|
|||||||
sb->buf[sb->len] = '\0';
|
sb->buf[sb->len] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
struct strbuf **strbuf_split_buf(const char *str, size_t slen, int delim, int max)
|
struct strbuf **strbuf_split_buf(const char *str, size_t slen,
|
||||||
|
int terminator, int max)
|
||||||
{
|
{
|
||||||
int alloc = 2, pos = 0;
|
struct strbuf **ret = NULL;
|
||||||
const char *n, *p;
|
size_t nr = 0, alloc = 0;
|
||||||
struct strbuf **ret;
|
|
||||||
struct strbuf *t;
|
struct strbuf *t;
|
||||||
|
|
||||||
ret = xcalloc(alloc, sizeof(struct strbuf *));
|
while (slen) {
|
||||||
p = n = str;
|
int len = slen;
|
||||||
while (n < str + slen) {
|
if (max <= 0 || nr + 1 < max) {
|
||||||
int len;
|
const char *end = memchr(str, terminator, slen);
|
||||||
if (max <= 0 || pos + 1 < max)
|
if (end)
|
||||||
n = memchr(n, delim, slen - (n - str));
|
len = end - str + 1;
|
||||||
else
|
|
||||||
n = NULL;
|
|
||||||
if (pos + 1 >= alloc) {
|
|
||||||
alloc = alloc * 2;
|
|
||||||
ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
|
|
||||||
}
|
}
|
||||||
if (!n)
|
|
||||||
n = str + slen - 1;
|
|
||||||
len = n - p + 1;
|
|
||||||
t = xmalloc(sizeof(struct strbuf));
|
t = xmalloc(sizeof(struct strbuf));
|
||||||
strbuf_init(t, len);
|
strbuf_init(t, len);
|
||||||
strbuf_add(t, p, len);
|
strbuf_add(t, str, len);
|
||||||
ret[pos] = t;
|
ALLOC_GROW(ret, nr + 2, alloc);
|
||||||
ret[++pos] = NULL;
|
ret[nr++] = t;
|
||||||
p = ++n;
|
str += len;
|
||||||
|
slen -= len;
|
||||||
}
|
}
|
||||||
|
ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
|
||||||
|
ret[nr] = NULL;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
48
strbuf.h
48
strbuf.h
@ -44,22 +44,56 @@ extern void strbuf_rtrim(struct strbuf *);
|
|||||||
extern void strbuf_ltrim(struct strbuf *);
|
extern void strbuf_ltrim(struct strbuf *);
|
||||||
extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
|
extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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).
|
||||||
|
*
|
||||||
|
* For lighter-weight alternatives, see string_list_split() and
|
||||||
|
* string_list_split_in_place().
|
||||||
|
*/
|
||||||
extern struct strbuf **strbuf_split_buf(const char *, size_t,
|
extern struct strbuf **strbuf_split_buf(const char *, size_t,
|
||||||
int delim, int max);
|
int terminator, int max);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Split a NUL-terminated string at the specified terminator
|
||||||
|
* character. See strbuf_split_buf() for more information.
|
||||||
|
*/
|
||||||
static inline struct strbuf **strbuf_split_str(const char *str,
|
static inline struct strbuf **strbuf_split_str(const char *str,
|
||||||
int delim, int max)
|
int terminator, int max)
|
||||||
{
|
{
|
||||||
return strbuf_split_buf(str, strlen(str), delim, max);
|
return strbuf_split_buf(str, strlen(str), terminator, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Split a strbuf at the specified terminator character. See
|
||||||
|
* strbuf_split_buf() for more information.
|
||||||
|
*/
|
||||||
static inline struct strbuf **strbuf_split_max(const struct strbuf *sb,
|
static inline struct strbuf **strbuf_split_max(const struct strbuf *sb,
|
||||||
int delim, int max)
|
int terminator, int max)
|
||||||
{
|
{
|
||||||
return strbuf_split_buf(sb->buf, sb->len, delim, max);
|
return strbuf_split_buf(sb->buf, sb->len, terminator, max);
|
||||||
}
|
}
|
||||||
static inline struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
|
|
||||||
|
/*
|
||||||
|
* Split a strbuf at the specified terminator character. See
|
||||||
|
* strbuf_split_buf() for more information.
|
||||||
|
*/
|
||||||
|
static inline struct strbuf **strbuf_split(const struct strbuf *sb,
|
||||||
|
int terminator)
|
||||||
{
|
{
|
||||||
return strbuf_split_max(sb, delim, 0);
|
return strbuf_split_max(sb, terminator, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Free a NULL-terminated list of strbufs (for example, the return
|
||||||
|
* values of the strbuf_split*() functions).
|
||||||
|
*/
|
||||||
extern void strbuf_list_free(struct strbuf **);
|
extern void strbuf_list_free(struct strbuf **);
|
||||||
|
|
||||||
/*----- add data in your buffer -----*/
|
/*----- add data in your buffer -----*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user