trim_last_path_component(): replace last_path_elm()
Rewrite last_path_elm() to take a strbuf parameter and to trim off the last path name element in place rather than returning a pointer to the beginning of the last path name element. This simplifies the function a bit and makes it integrate better with its caller, which is now also strbuf-based. Rename the function accordingly and a bit less tersely. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Haggerty committed
Oct 1, 2014 at 12:28 UTC
0c0d6e8601a1cfb8ebbdadb6a25a9f6fadc91359
1 file changed
+16
-22
lockfile.c
+16
-22
@@ -76,32 +76,28 @@ static void remove_lock_file_on_signal(int signo)
76
}
77
78
/*
79
- * p = absolute or relative path name
79
+ * path = absolute or relative path name
80
*
81
- * Return a pointer into p showing the beginning of the last path name
82
- * element. If p is empty or the root directory ("/"), just return p.
81
+ * Remove the last path name element from path (leaving the preceding
82
+ * "/", if any). If path is empty or the root directory ("/"), set
83
+ * path to the empty string.
84
*/
84
-static char *last_path_elm(char *p)
85
+static void trim_last_path_component(struct strbuf *path)
86
{
86
- /* r starts pointing to null at the end of the string */
87
- char *r = strchr(p, '\0');
88
-
89
- if (r == p)
90
- return p; /* just return empty string */
91
-
92
- r--; /* back up to last non-null character */
87
+ int i = path->len;
88
89
/* back up past trailing slashes, if any */
95
- while (r > p && *r == '/')
96
- r--;
90
+ while (i && path->buf[i - 1] == '/')
91
+ i--;
92
93
/*
99
- * then go backwards until I hit a slash, or the beginning of
100
- * the string
94
+ * then go backwards until a slash, or the beginning of the
95
+ * string
96
*/
102
- while (r > p && *(r-1) != '/')
103
- r--;
104
- return r;
97
+ while (i && path->buf[i - 1] != '/')
98
+ i--;
99
+
100
+ strbuf_setlen(path, i);
101
}
102
103
@@ -131,14 +127,12 @@ static void resolve_symlink(struct strbuf *path)
127
if (is_absolute_path(link.buf))
128
/* absolute path simply replaces p */
129
strbuf_reset(path);
134
- else {
130
+ else
131
/*
132
* link is a relative path, so replace the
133
* last element of p with it.
134
*/
139
- char *r = last_path_elm(path->buf);
140
- strbuf_setlen(path, r - path->buf);
141
- }
135
+ trim_last_path_component(path);
136
137
strbuf_addbuf(path, &link);
138
}