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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 30, 2014 at 12:58 UTC 26936bfd9bde1ec46901bea3e53d4fb9ae1b4a4c
4 files changed +14 -15
builtin/remote.c
+7 -6
@@ -265,16 +265,17 @@ static int config_read_branches(const char *key, const char *value, void *cb)
265 struct string_list_item *item;
266 struct branch_info *info;
267 enum { REMOTE, MERGE, REBASE } type;
268 + size_t key_len;
269
270 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);
273 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);
276 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);
279 type = REBASE;
280 } else
281 return 0;
builtin/repack.c
+2 -3
@@ -77,16 +77,15 @@ static void get_non_kept_pack_filenames(struct string_list *fname_list)
77 DIR *dir;
78 struct dirent *e;
79 char *fname;
80 - size_t len;
80
81 if (!(dir = opendir(packdir)))
82 return;
83
84 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))
87 continue;
88
89 - len = strlen(e->d_name) - strlen(".pack");
89 fname = xmemdupz(e->d_name, len);
90
91 if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
connected.c
+3 -3
@@ -31,6 +31,7 @@ static int check_everything_connected_real(sha1_iterate_fn fn,
31 unsigned char sha1[20];
32 int err = 0, ac = 0;
33 struct packed_git *new_pack = NULL;
34 + size_t base_len;
35
36 if (fn(cb_data, sha1))
37 return err;
@@ -38,10 +39,9 @@ static int check_everything_connected_real(sha1_iterate_fn fn,
39 if (transport && transport->smart_options &&
40 transport->smart_options->self_contained_and_connected &&
41 transport->pack_lockfile &&
41 - ends_with(transport->pack_lockfile, ".keep")) {
42 + strip_suffix(transport->pack_lockfile, ".keep", &base_len)) {
43 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);
45 strbuf_addstr(&idx_file, ".idx");
46 new_pack = add_packed_git(idx_file.buf, idx_file.len, 1);
47 strbuf_release(&idx_file);
help.c
+2 -3
@@ -145,7 +145,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
145 len = buf.len;
146
147 while ((de = readdir(dir)) != NULL) {
148 - int entlen;
148 + size_t entlen;
149
150 if (!starts_with(de->d_name, prefix))
151 continue;
@@ -156,8 +156,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
156 continue;
157
158 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);
160
161 add_cmdname(cmds, de->d_name + prefix_len, entlen);
162 }