Re: [PATCHv2] submodule: Port resolve_relative_url from shell to C
- From
Jeff King <peff@peff.net>
- Date
- Dec 18, 2015, 03:27 UTC
- Message-ID
- <20151218032708.GA8530@sigill.intra.peff.net>
- In-Reply-To
- <5673052F.7050000@kdbg.org>
On Thu, Dec 17, 2015 at 07:55:43PM +0100, Johannes Sixt wrote:
Show 23 quoted lines
> -static int has_same_dir_prefix(const char *str, const char **out)
> +static int starts_with_dot_slash(const char *str)
> {
> -#ifdef GIT_WINDOWS_NATIVE
> - return skip_prefix(str, "./", out)
> - || skip_prefix(str, ".\\", out);
> -#else
> - return skip_prefix(str, "./", out);
> -#endif
> + return str[0] == '.' && is_dir_sep(str[1]);
> }
>
> -static int has_upper_dir_prefix(const char *str, const char **out)
> +static int starts_with_dot_dot_slash(const char *str)
> {
> -#ifdef GIT_WINDOWS_NATIVE
> - return skip_prefix(str, "../", out)
> - || skip_prefix(str, "..\\", out);
> -#else
> - return skip_prefix(str, "../", out);
> -#endif
> + return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
> }As the set of prefixes you are looking is probably bounded, it may not be worth generalizing this. But I wondered if something like:
/*
* Like skip_prefix, but consider any "/" in the prefix as a
* directory separator for the platform.
*/
int skip_prefix_fs(const char *str, const char *prefix, const char **out)
{
while (1) {
if (!*prefix) {
*out = str;
return 1;
} else if (*prefix == '/') {
if (!is_dir_sep(*str))
return 0;
} else {
if (*str != *prefix)
return 0;
}
str++;
prefix++;
}
}... /* works on all platforms! */ if (skip_prefix_fs(foo, "./", &out)) ...
would be helpful. I don't know if there are other opportunities in the code base that could make use of this. If it's just these two sites, it's probably not worth it.
-Peff