Optimized win32_nextpath Based on a suggestion by Russell Belfer. Signed-off-by: Sven Strickroth <email@cs-ware.de> Signed-off-by: Russell Belfer <rb@github.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
diff --git a/src/fileops.c b/src/fileops.c
index cbc7419..d99e117 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -418,55 +418,23 @@ static int win32_find_file(git_buf *path, const struct win32_path *root, const c
git__free(file_utf16);
return 0;
}
-
-static wchar_t * win32_nextpath(wchar_t * src, wchar_t * dst, size_t maxlen)
+static wchar_t* win32_nextpath(wchar_t *path, wchar_t *buf, size_t buflen)
{
- wchar_t * orgsrc;
-
- while (*src == L';')
- src++;
+ wchar_t term, *base = path;
- orgsrc = src;
+ assert(path && buf && buflen);
- if (!--maxlen)
- goto nullterm;
+ term = (*path == L'"') ? *path++ : L';';
- while (*src && *src != L';')
- {
- if (*src != L'"')
- {
- *dst++ = *src++;
- if (!--maxlen)
- {
- orgsrc = src;
- goto nullterm;
- }
- }
- else
- {
- src++;
- while (*src && *src != L'"')
- {
- *dst++ = *src++;
- if (!--maxlen)
- {
- orgsrc = src;
- goto nullterm;
- }
- }
-
- if (*src)
- src++;
- }
- }
+ for (buflen--; *path && *path != term && buflen; buflen--)
+ *buf++ = *path++;
- while (*src == L';')
- src++;
+ *buf = L'\0'; /* reserved a byte via initial subtract */
-nullterm:
- *dst = 0;
+ while (*path == term || *path == L';')
+ path++;
- return (orgsrc != src) ? (wchar_t *)src : NULL;
+ return (path != base) ? path : NULL;
}
static int win32_find_system_file_using_path(git_buf *path, const char *filename)