Re: [hackers] [PATCH][sbase] libutil/unescape.c: add \E and simplify \x

From: Evan Gates <evan.gates_AT_gmail.com>
Date: Mon, 6 Feb 2017 13:29:54 -0800

On Sat, Feb 4, 2017 at 1:32 PM, Mattias Andrée <maandree_AT_kth.se> wrote:
> _AT_@ -39,10 +39,8 @@ unescape(char *s)
> off += m - i - 1;
> for (--m, q = 0, factor = 1; m > i + 1; m--) {
> - if (s[m] >= '0' && s[m] <= '9')
> - q += (s[m] - '0') * factor;
> - else if (s[m] >= 'A' && s[m] <= 'F')
> - q += ((s[m] - 'A') + 10) * factor;
> - else if (s[m] >= 'a' && s[m] <= 'f')
> - q += ((s[m] - 'a') + 10) * factor;
> + if (isdigit(s[m]))
> + q += (s[m] & 15) * factor;
> + else
> + q += ((s[m] & 15) + 9) * factor;
> factor *= 16;
> }
> --
> 2.11.0
>
>

I think this would be clearer as:

if (isdigit(s[m]))
    q += (s[m] - '0') * factor;
else
    q += (tolower(s[m]) - 'a' + 10) * factor;

But it is just a personal style preference. Or, if we do keep the bit
twiddling, I highly recommend using hex instead of decimal, e.g. (s[m]
& 0xf).

Thoughts?
Received on Mon Feb 06 2017 - 22:29:54 CET

This archive was generated by hypermail 2.3.0 : Mon Feb 06 2017 - 22:36:16 CET