Skip to content

Commit 2e01626

Browse files
deveshidwivedigitster
authored andcommitted
list-objects-filter-options: avoid strbuf_split_str()
parse_combine_filter() splits a combine: filter spec at '+' using strbuf_split_str(), which yields an array of strbufs with the delimiter left at the end of each non-final piece. The code then mutates each non-final piece to strip the trailing '+' before parsing. Allocating an array of strbufs is unnecessary. The function processes one sub-spec at a time and does not use strbuf editing on the pieces. The two helpers it calls, has_reserved_character() and parse_combine_subfilter(), only read the string content of the strbuf they receive. Walk the input string directly with strchr() to find each '+'. Copy each sub-spec into a temporary buffer and strip the '+' only when another sub-spec follows. Change the helpers to take const char * instead of struct strbuf *. Signed-off-by: Deveshi Dwivedi <deveshigurgaon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5ad63f3 commit 2e01626

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

list-objects-filter-options.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ int gently_parse_list_objects_filter(
125125
static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";
126126

127127
static int has_reserved_character(
128-
struct strbuf *sub_spec, struct strbuf *errbuf)
128+
const char *sub_spec, struct strbuf *errbuf)
129129
{
130-
const char *c = sub_spec->buf;
130+
const char *c = sub_spec;
131131
while (*c) {
132132
if (*c <= ' ' || strchr(RESERVED_NON_WS, *c)) {
133133
strbuf_addf(
@@ -144,7 +144,7 @@ static int has_reserved_character(
144144

145145
static int parse_combine_subfilter(
146146
struct list_objects_filter_options *filter_options,
147-
struct strbuf *subspec,
147+
const char *subspec,
148148
struct strbuf *errbuf)
149149
{
150150
size_t new_index = filter_options->sub_nr;
@@ -155,7 +155,7 @@ static int parse_combine_subfilter(
155155
filter_options->sub_alloc);
156156
list_objects_filter_init(&filter_options->sub[new_index]);
157157

158-
decoded = url_percent_decode(subspec->buf);
158+
decoded = url_percent_decode(subspec);
159159

160160
result = has_reserved_character(subspec, errbuf);
161161
if (result)
@@ -182,34 +182,34 @@ static int parse_combine_filter(
182182
const char *arg,
183183
struct strbuf *errbuf)
184184
{
185-
struct strbuf **subspecs = strbuf_split_str(arg, '+', 0);
186-
size_t sub;
185+
const char *p = arg;
187186
int result = 0;
188187

189-
if (!subspecs[0]) {
188+
if (!*p) {
190189
strbuf_addstr(errbuf, _("expected something after combine:"));
191190
result = 1;
192191
goto cleanup;
193192
}
194193

195-
for (sub = 0; subspecs[sub] && !result; sub++) {
196-
if (subspecs[sub + 1]) {
197-
/*
198-
* This is not the last subspec. Remove trailing "+" so
199-
* we can parse it.
200-
*/
201-
size_t last = subspecs[sub]->len - 1;
202-
assert(subspecs[sub]->buf[last] == '+');
203-
strbuf_remove(subspecs[sub], last, 1);
204-
}
205-
result = parse_combine_subfilter(
206-
filter_options, subspecs[sub], errbuf);
194+
while (*p && !result) {
195+
const char *sep = strchr(p, '+');
196+
size_t len = sep ? (size_t)(sep - p + 1) : strlen(p);
197+
char *sub = xmemdupz(p, len);
198+
199+
/* strip '+' separator, but only when more sub-specs follow */
200+
if (sep && *(sep + 1))
201+
sub[len - 1] = '\0';
202+
203+
result = parse_combine_subfilter(filter_options, sub, errbuf);
204+
free(sub);
205+
if (!sep)
206+
break;
207+
p = sep + 1;
207208
}
208209

209210
filter_options->choice = LOFC_COMBINE;
210211

211212
cleanup:
212-
strbuf_list_free(subspecs);
213213
if (result)
214214
list_objects_filter_release(filter_options);
215215
return result;

0 commit comments

Comments
 (0)