Merge branch 'jk/core-comment-string'
core.commentChar used to be limited to a single byte, but has been updated to allow an arbitrary multi-byte sequence. * jk/core-comment-string: config: add core.commentString config: allow multi-byte core.commentChar environment: drop comment_line_char compatibility macro wt-status: drop custom comment-char stringification sequencer: handle multi-byte comment characters when writing todo list find multi-byte comment chars in unterminated buffers find multi-byte comment chars in NUL-terminated strings prefer comment_line_str to comment_line_char for printing strbuf: accept a comment string for strbuf_add_commented_lines() strbuf: accept a comment string for strbuf_commented_addf() strbuf: accept a comment string for strbuf_stripspace() environment: store comment_line_char as a string strbuf: avoid shadowing global comment_line_char name commit: refactor base-case of adjust_comment_line_char() strbuf: avoid static variables in strbuf_add_commented_lines() strbuf: simplify comment-handling in add_lines() helper config: forbid newline as core.commentChar
This commit is contained in:
47
strbuf.c
47
strbuf.c
@ -24,6 +24,17 @@ int istarts_with(const char *str, const char *prefix)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int starts_with_mem(const char *str, size_t len, const char *prefix)
|
||||
{
|
||||
const char *end = str + len;
|
||||
for (; ; str++, prefix++) {
|
||||
if (!*prefix)
|
||||
return 1;
|
||||
else if (str == end || *str != *prefix)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int skip_to_optional_arg_default(const char *str, const char *prefix,
|
||||
const char **arg, const char *def)
|
||||
{
|
||||
@ -340,18 +351,17 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
|
||||
}
|
||||
|
||||
static void add_lines(struct strbuf *out,
|
||||
const char *prefix1,
|
||||
const char *prefix2,
|
||||
const char *buf, size_t size)
|
||||
const char *prefix,
|
||||
const char *buf, size_t size,
|
||||
int space_after_prefix)
|
||||
{
|
||||
while (size) {
|
||||
const char *prefix;
|
||||
const char *next = memchr(buf, '\n', size);
|
||||
next = next ? (next + 1) : (buf + size);
|
||||
|
||||
prefix = ((prefix2 && (buf[0] == '\n' || buf[0] == '\t'))
|
||||
? prefix2 : prefix1);
|
||||
strbuf_addstr(out, prefix);
|
||||
if (space_after_prefix && buf[0] != '\n' && buf[0] != '\t')
|
||||
strbuf_addch(out, ' ');
|
||||
strbuf_add(out, buf, next - buf);
|
||||
size -= next - buf;
|
||||
buf = next;
|
||||
@ -360,19 +370,12 @@ static void add_lines(struct strbuf *out,
|
||||
}
|
||||
|
||||
void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
|
||||
size_t size, char comment_line_char)
|
||||
size_t size, const char *comment_prefix)
|
||||
{
|
||||
static char prefix1[3];
|
||||
static char prefix2[2];
|
||||
|
||||
if (prefix1[0] != comment_line_char) {
|
||||
xsnprintf(prefix1, sizeof(prefix1), "%c ", comment_line_char);
|
||||
xsnprintf(prefix2, sizeof(prefix2), "%c", comment_line_char);
|
||||
}
|
||||
add_lines(out, prefix1, prefix2, buf, size);
|
||||
add_lines(out, comment_prefix, buf, size, 1);
|
||||
}
|
||||
|
||||
void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
|
||||
void strbuf_commented_addf(struct strbuf *sb, const char *comment_prefix,
|
||||
const char *fmt, ...)
|
||||
{
|
||||
va_list params;
|
||||
@ -383,7 +386,7 @@ void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
|
||||
strbuf_vaddf(&buf, fmt, params);
|
||||
va_end(params);
|
||||
|
||||
strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_line_char);
|
||||
strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_prefix);
|
||||
if (incomplete_line)
|
||||
sb->buf[--sb->len] = '\0';
|
||||
|
||||
@ -770,7 +773,7 @@ ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
|
||||
void strbuf_add_lines(struct strbuf *out, const char *prefix,
|
||||
const char *buf, size_t size)
|
||||
{
|
||||
add_lines(out, prefix, NULL, buf, size);
|
||||
add_lines(out, prefix, buf, size, 0);
|
||||
}
|
||||
|
||||
void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
|
||||
@ -1025,10 +1028,10 @@ static size_t cleanup(char *line, size_t len)
|
||||
*
|
||||
* If last line does not have a newline at the end, one is added.
|
||||
*
|
||||
* Pass a non-NUL comment_line_char to skip every line starting
|
||||
* Pass a non-NULL comment_prefix to skip every line starting
|
||||
* with it.
|
||||
*/
|
||||
void strbuf_stripspace(struct strbuf *sb, char comment_line_char)
|
||||
void strbuf_stripspace(struct strbuf *sb, const char *comment_prefix)
|
||||
{
|
||||
size_t empties = 0;
|
||||
size_t i, j, len, newlen;
|
||||
@ -1041,8 +1044,8 @@ void strbuf_stripspace(struct strbuf *sb, char comment_line_char)
|
||||
eol = memchr(sb->buf + i, '\n', sb->len - i);
|
||||
len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
|
||||
|
||||
if (comment_line_char && len &&
|
||||
sb->buf[i] == comment_line_char) {
|
||||
if (comment_prefix && len &&
|
||||
starts_with(sb->buf + i, comment_prefix)) {
|
||||
newlen = 0;
|
||||
continue;
|
||||
}
|
||||
|
Reference in New Issue
Block a user