codec/url.c (encode): Rewrite more compactly and perspicuously.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 11 Jun 2018 23:17:12 +0000 (00:17 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 11 Jun 2018 23:17:12 +0000 (00:17 +0100)
The condition in the `default' case was a bit gnarly: rewrite it as a
ladder of if/else.  Take the opportunity to compress the code, because
the individual case labels really don't deserve all of that vertical space.

codec/url.c

index 4e7f190..3c1fbfd 100644 (file)
@@ -63,33 +63,19 @@ static void encode(url_ectx *ctx, dstr *d, const char *p)
 {
   while (*p) {
     switch (*p) {
-      case ' ':
-       DPUTC(d, '+');
+      case ' ': DPUTC(d, '+');
        break;
       default:
-       if (!isspace((unsigned char)*p) &&
-           ((ctx->f & URLF_LAX) || isalnum((unsigned char)*p)))
-         goto safe;
-       else
-         goto unsafe;
-      case '/':
-      case '~':
-       if (ctx->f & URLF_STRICT)
-         goto unsafe;
-      case '-':
-      case '.':
-      case '_':
-      safe:
-       DPUTC(d, *p);
-       break;
-      unsafe:
-      case '+':
-      case '%':
-      case '=':
-      case '&':
-      case ';':
-       dstr_putf(d, "%%%02x", *p);
-       break;
+       if (isspace((unsigned char)*p)) goto unsafe;
+       else if (isalnum((unsigned char *)p)) goto safe;
+       else if (ctx->f&URLF_LAX) goto safe;
+       else goto unsafe;
+      case '/': case '~':
+       if (ctx->f&URLF_STRICT) goto unsafe; /* else fall through... */
+      safe: case '-': case '.': case '_':
+       DPUTC(d, *p);   break;
+      unsafe: case '+': case '%': case '=': case '&': case ';':
+       dstr_putf(d, "%%%02x", *p); break;
     }
     p++;
   }