Skip to content

Commit 8f2aecf

Browse files
committed
strbuf: refactor strbuf_trim_trailing_ch()
We often have to split strings at some specified terminator character. The strbuf_split*() functions, that we can use for this purpose, return substrings that include the terminator character, so we often need to remove that character. When it is a whitespace, newline or directory separator, the terminator character can easily be removed using an existing triming function like strbuf_rtrim(), strbuf_trim_trailing_newline() or strbuf_trim_trailing_dir_sep(). There is no function to remove that character when it's not one of those characters though. Let's introduce a new strbuf_trim_trailing_ch() function that can be used to remove any trailing character, and let's refactor existing code that manually removed trailing characters using this new function. We are also going to use this new function in a following commit. Signed-off-by: Christian Couder <[email protected]>
1 parent 13dd730 commit 8f2aecf

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

strbuf.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ void strbuf_trim_trailing_dir_sep(struct strbuf *sb)
134134
sb->buf[sb->len] = '\0';
135135
}
136136

137+
void strbuf_trim_trailing_ch(struct strbuf *sb, int c)
138+
{
139+
while (sb->len > 0 && sb->buf[sb->len - 1] == c)
140+
sb->len--;
141+
sb->buf[sb->len] = '\0';
142+
}
143+
137144
void strbuf_trim_trailing_newline(struct strbuf *sb)
138145
{
139146
if (sb->len > 0 && sb->buf[sb->len - 1] == '\n') {

strbuf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ void strbuf_trim_trailing_dir_sep(struct strbuf *sb);
197197
/* Strip trailing LF or CR/LF */
198198
void strbuf_trim_trailing_newline(struct strbuf *sb);
199199

200+
/* Strip trailing character c */
201+
void strbuf_trim_trailing_ch(struct strbuf *sb, int c);
202+
200203
/**
201204
* Replace the contents of the strbuf with a reencoded form. Returns -1
202205
* on error, 0 on success.

trace2/tr2_cfg.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ static int tr2_cfg_load_patterns(void)
3535

3636
tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1);
3737
for (s = tr2_cfg_patterns; *s; s++) {
38-
struct strbuf *buf = *s;
39-
40-
if (buf->len && buf->buf[buf->len - 1] == ',')
41-
strbuf_setlen(buf, buf->len - 1);
38+
strbuf_trim_trailing_ch(*s, ',');
4239
strbuf_trim_trailing_newline(*s);
4340
strbuf_trim(*s);
4441
}
@@ -74,10 +71,7 @@ static int tr2_load_env_vars(void)
7471

7572
tr2_cfg_env_vars = strbuf_split_buf(varlist, strlen(varlist), ',', -1);
7673
for (s = tr2_cfg_env_vars; *s; s++) {
77-
struct strbuf *buf = *s;
78-
79-
if (buf->len && buf->buf[buf->len - 1] == ',')
80-
strbuf_setlen(buf, buf->len - 1);
74+
strbuf_trim_trailing_ch(*s, ',');
8175
strbuf_trim_trailing_newline(*s);
8276
strbuf_trim(*s);
8377
}

0 commit comments

Comments
 (0)