Skip to content

Commit 26936bf

Browse files
peffgitster
authored andcommitted
use strip_suffix instead of ends_with in simple cases
When stripping a suffix like: if (ends_with(str, "foo")) buf = xmemdupz(str, strlen(str) - 3); we can instead use strip_suffix to avoid the constant 3, which must match the literal "foo" (we sometimes use strlen("foo") instead, but that means we are repeating ourselves). The example above becomes: if (strip_suffix(str, "foo", &len)) buf = xmemdupz(str, len); This also saves a strlen(), since we calculate the string length when detecting the suffix. Note that in some cases we also switch from xstrndup to xmemdupz, which saves a further strlen call. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2975c77 commit 26936bf

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

builtin/remote.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,17 @@ static int config_read_branches(const char *key, const char *value, void *cb)
265265
struct string_list_item *item;
266266
struct branch_info *info;
267267
enum { REMOTE, MERGE, REBASE } type;
268+
size_t key_len;
268269

269270
key += 7;
270-
if (ends_with(key, ".remote")) {
271-
name = xstrndup(key, strlen(key) - 7);
271+
if (strip_suffix(key, ".remote", &key_len)) {
272+
name = xmemdupz(key, key_len);
272273
type = REMOTE;
273-
} else if (ends_with(key, ".merge")) {
274-
name = xstrndup(key, strlen(key) - 6);
274+
} else if (strip_suffix(key, ".merge", &key_len)) {
275+
name = xmemdupz(key, key_len);
275276
type = MERGE;
276-
} else if (ends_with(key, ".rebase")) {
277-
name = xstrndup(key, strlen(key) - 7);
277+
} else if (strip_suffix(key, ".rebase", &key_len)) {
278+
name = xmemdupz(key, key_len);
278279
type = REBASE;
279280
} else
280281
return 0;

builtin/repack.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,15 @@ static void get_non_kept_pack_filenames(struct string_list *fname_list)
7777
DIR *dir;
7878
struct dirent *e;
7979
char *fname;
80-
size_t len;
8180

8281
if (!(dir = opendir(packdir)))
8382
return;
8483

8584
while ((e = readdir(dir)) != NULL) {
86-
if (!ends_with(e->d_name, ".pack"))
85+
size_t len;
86+
if (!strip_suffix(e->d_name, ".pack", &len))
8787
continue;
8888

89-
len = strlen(e->d_name) - strlen(".pack");
9089
fname = xmemdupz(e->d_name, len);
9190

9291
if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))

connected.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ static int check_everything_connected_real(sha1_iterate_fn fn,
3131
unsigned char sha1[20];
3232
int err = 0, ac = 0;
3333
struct packed_git *new_pack = NULL;
34+
size_t base_len;
3435

3536
if (fn(cb_data, sha1))
3637
return err;
3738

3839
if (transport && transport->smart_options &&
3940
transport->smart_options->self_contained_and_connected &&
4041
transport->pack_lockfile &&
41-
ends_with(transport->pack_lockfile, ".keep")) {
42+
strip_suffix(transport->pack_lockfile, ".keep", &base_len)) {
4243
struct strbuf idx_file = STRBUF_INIT;
43-
strbuf_addstr(&idx_file, transport->pack_lockfile);
44-
strbuf_setlen(&idx_file, idx_file.len - 5); /* ".keep" */
44+
strbuf_add(&idx_file, transport->pack_lockfile, base_len);
4545
strbuf_addstr(&idx_file, ".idx");
4646
new_pack = add_packed_git(idx_file.buf, idx_file.len, 1);
4747
strbuf_release(&idx_file);

help.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
145145
len = buf.len;
146146

147147
while ((de = readdir(dir)) != NULL) {
148-
int entlen;
148+
size_t entlen;
149149

150150
if (!starts_with(de->d_name, prefix))
151151
continue;
@@ -156,8 +156,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
156156
continue;
157157

158158
entlen = strlen(de->d_name) - prefix_len;
159-
if (ends_with(de->d_name, ".exe"))
160-
entlen -= 4;
159+
strip_suffix(de->d_name, ".exe", &entlen);
161160

162161
add_cmdname(cmds, de->d_name + prefix_len, entlen);
163162
}

0 commit comments

Comments
 (0)