mirror of
https://github.com/git-for-windows/git.git
synced 2026-03-28 05:46:45 -05:00
Refactor skipping DOS drive prefixes
Junio Hamano pointed out that there is an implicit assumption in pretty much all the code calling has_dos_drive_prefix(): it assumes that the DOS drive prefix is always two bytes long. While this assumption is pretty safe, we can still make the code more readable and less error-prone by introducing a function that skips the DOS drive prefix safely. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
@@ -4,9 +4,7 @@
|
||||
char *gitbasename (char *path)
|
||||
{
|
||||
const char *base;
|
||||
/* Skip over the disk name in MSDOS pathnames. */
|
||||
if (has_dos_drive_prefix(path))
|
||||
path += 2;
|
||||
skip_dos_drive_prefix(&path);
|
||||
for (base = path; *path; path++) {
|
||||
if (is_dir_sep(*path))
|
||||
base = path + 1;
|
||||
|
||||
@@ -1981,26 +1981,22 @@ pid_t waitpid(pid_t pid, int *status, int options)
|
||||
|
||||
int mingw_offset_1st_component(const char *path)
|
||||
{
|
||||
int offset = 0;
|
||||
if (has_dos_drive_prefix(path))
|
||||
offset = 2;
|
||||
char *pos = (char *)path;
|
||||
|
||||
/* unc paths */
|
||||
else if (is_dir_sep(path[0]) && is_dir_sep(path[1])) {
|
||||
|
||||
if (!skip_dos_drive_prefix(&pos) &&
|
||||
is_dir_sep(pos[0]) && is_dir_sep(pos[1])) {
|
||||
/* skip server name */
|
||||
char *pos = strpbrk(path + 2, "\\/");
|
||||
pos = strpbrk(pos + 2, "\\/");
|
||||
if (!pos)
|
||||
return 0; /* Error: malformed unc path */
|
||||
|
||||
do {
|
||||
pos++;
|
||||
} while (*pos && !is_dir_sep(*pos));
|
||||
|
||||
offset = pos - path;
|
||||
}
|
||||
|
||||
return offset + is_dir_sep(path[offset]);
|
||||
return pos + is_dir_sep(*pos) - path;
|
||||
}
|
||||
|
||||
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
|
||||
|
||||
@@ -383,7 +383,15 @@ HANDLE winansi_get_osfhandle(int fd);
|
||||
* git specific compatibility
|
||||
*/
|
||||
|
||||
#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
|
||||
#define has_dos_drive_prefix(path) \
|
||||
(isalpha(*(path)) && (path)[1] == ':' ? 2 : 0)
|
||||
static inline int mingw_skip_dos_drive_prefix(char **path)
|
||||
{
|
||||
int ret = has_dos_drive_prefix(*path);
|
||||
*path += ret;
|
||||
return ret;
|
||||
}
|
||||
#define skip_dos_drive_prefix mingw_skip_dos_drive_prefix
|
||||
#define is_dir_sep(c) ((c) == '/' || (c) == '\\')
|
||||
static inline char *mingw_find_last_dir_sep(const char *path)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user