strbuf: remove global variable

As a library that only interacts with other primitives, strbuf should
not utilize the comment_line_char global variable within its
functions. Therefore, add an additional parameter for functions that use
comment_line_char and refactor callers to pass it in instead.
strbuf_stripspace() removes the skip_comments boolean and checks if
comment_line_char is a non-NUL character to determine whether to skip
comments or not.

Signed-off-by: Calvin Wan <calvinwan@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Calvin Wan
2023-06-06 19:48:43 +00:00
committed by Junio C Hamano
parent aba0706832
commit 787cb8a48a
16 changed files with 90 additions and 64 deletions

View File

@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
#include "strbuf.h"
@ -362,7 +361,8 @@ static void add_lines(struct strbuf *out,
strbuf_complete_line(out);
}
void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
size_t size, char comment_line_char)
{
static char prefix1[3];
static char prefix2[2];
@ -374,7 +374,8 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size
add_lines(out, prefix1, prefix2, buf, size);
}
void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
const char *fmt, ...)
{
va_list params;
struct strbuf buf = STRBUF_INIT;
@ -384,7 +385,7 @@ void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
strbuf_vaddf(&buf, fmt, params);
va_end(params);
strbuf_add_commented_lines(sb, buf.buf, buf.len);
strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_line_char);
if (incomplete_line)
sb->buf[--sb->len] = '\0';
@ -1051,10 +1052,10 @@ static size_t cleanup(char *line, size_t len)
*
* If last line does not have a newline at the end, one is added.
*
* Enable skip_comments to skip every line starting with comment
* character.
* Pass a non-NUL comment_line_char to skip every line starting
* with it.
*/
void strbuf_stripspace(struct strbuf *sb, int skip_comments)
void strbuf_stripspace(struct strbuf *sb, char comment_line_char)
{
size_t empties = 0;
size_t i, j, len, newlen;
@ -1067,7 +1068,8 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
eol = memchr(sb->buf + i, '\n', sb->len - i);
len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
if (skip_comments && len && sb->buf[i] == comment_line_char) {
if (comment_line_char && len &&
sb->buf[i] == comment_line_char) {
newlen = 0;
continue;
}