Jeff King <p...@peff.net> writes:
> + * Parse features of the form "feature=value". Returns NULL if the feature
> + * does not exist, the empty string if it exists but does not have an "=", or
> + * the content to the right of the "=" until the first space (or end of
> + * string). The cannot contain literal spaces; double-quoting or similar
> + * schemes would break compatibility, since older versions of git treat the
> + * space as a hard-delimiter without any context.
> + *
> + * The return value (if non-NULL) is newly allocated on the heap and belongs
> + * the caller.
> + */
> +char *parse_feature_request_value(const char *feature_list, const char
> + const char *start = parse_feature_request(feature_list, feature);
> + const char *end;
> + if (!start || prefixcmp(start, feature))
> + return NULL;
> + start += strlen(feature);
> + if (*start == '=')
> + start++;
> + end = strchrnul(start, ' ');
> + return xmemdupz(start, end - start);
Having to run strlen(feature) three times in this function (once in
parse_feature_request() as part of strstr() and the edge check of
the found string, once as part of prefixcmp() here, and then an
explicit strlen() to skip it) makes me feel dirty.
It is not wrong per-se, but it is likely that the caller has a
constant string as the feature when it called this function, so
perhaps just changing the function signature of server_supports,
const char *server_supports(const char *feature)
return parse_feature_request(server_capabilities, feature);
to return "var=val " would be more than sufficient.
Then the existing callers can keep doing
and a new caller can do something like
agent = server_supports("agent");
if (!agent || !agent)
... no agent ...
int span = strcspn(agent + 6, " \t\n");
printf("I found agent=<%.*s>!\n", span, agent + 6);
which doesn't look too bad.
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html