From: Mark Wooding Date: Mon, 11 Jun 2018 23:17:12 +0000 (+0100) Subject: codec/url.c (encode): Rewrite more compactly and perspicuously. X-Git-Tag: 2.3.0~1 X-Git-Url: https://git.distorted.org.uk/~mdw/mLib/commitdiff_plain/96e10f286c055c81d9474e727bf0231a94aa2b43 codec/url.c (encode): Rewrite more compactly and perspicuously. 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. --- diff --git a/codec/url.c b/codec/url.c index 4e7f190..3c1fbfd 100644 --- a/codec/url.c +++ b/codec/url.c @@ -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++; }