url.c: simplify is_url()

The function was implemented in an overly complicated way. Rewrite it to check from left to right in a single pass. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 3, 2011 at 10:56 UTC b33a1b9fe794df53b449ffdbba0b39ef9e1772bf
1 file changed +6 -26
url.c
+6 -26
@@ -17,35 +17,15 @@ int is_urlschemechar(int first_flag, int ch)
17
18 int is_url(const char *url)
19 {
20 - const char *url2, *first_slash;
21 -
22 - if (!url)
23 - return 0;
24 - url2 = url;
25 - first_slash = strchr(url, '/');
26 -
27 - /* Input with no slash at all or slash first can't be URL. */
28 - if (!first_slash || first_slash == url)
29 - return 0;
30 - /* Character before must be : and next must be /. */
31 - if (first_slash[-1] != ':' || first_slash[1] != '/')
20 + /* Is "scheme" part reasonable? */
21 + if (!url || !is_urlschemechar(1, *url++))
22 return 0;
33 - /* There must be something before the :// */
34 - if (first_slash == url + 1)
35 - return 0;
36 - /*
37 - * Check all characters up to first slash - 1. Only alphanum
38 - * is allowed.
39 - */
40 - url2 = url;
41 - while (url2 < first_slash - 1) {
42 - if (!is_urlschemechar(url2 == url, (unsigned char)*url2))
23 + while (*url && *url != ':') {
24 + if (!is_urlschemechar(0, *url++))
25 return 0;
44 - url2++;
26 }
46 -
47 - /* Valid enough. */
48 - return 1;
27 + /* We've seen "scheme"; we want colon-slash-slash */
28 + return (url[0] == ':' && url[1] == '/' && url[2] == '/');
29 }
30
31 static int url_decode_char(const char *q)