libutil/unescape.c: simplify and add \E

Signed-off-by: Mattias Andrée <maandree@kth.se>
This commit is contained in:
Mattias Andrée 2017-02-05 00:44:35 +01:00 committed by Evan Gates
parent 9a903c63de
commit 30fd43d7f3

View File

@ -1,74 +1,57 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <ctype.h>
#include <string.h> #include <string.h>
#include "../util.h" #include "../util.h"
#define is_odigit(c) ('0' <= c && c <= '7')
size_t size_t
unescape(char *s) unescape(char *s)
{ {
size_t len, i, off, m, factor, q; static const char escapes[256] = {
['"'] = '"',
['\''] = '\'',
['\\'] = '\\',
['a'] = '\a',
['b'] = '\b',
['E'] = 033,
['e'] = 033,
['f'] = '\f',
['n'] = '\n',
['r'] = '\r',
['t'] = '\t',
['v'] = '\v'
};
size_t m, q;
char *r, *w;
len = strlen(s); for (r = w = s; *r;) {
if (*r != '\\') {
for (i = 0; i < len; i++) { *w++ = *r++;
if (s[i] != '\\')
continue; continue;
off = 0;
switch (s[i + 1]) {
case '\\': s[i] = '\\'; off++; break;
case '\'': s[i] = '\'', off++; break;
case '"': s[i] = '"', off++; break;
case 'a': s[i] = '\a'; off++; break;
case 'b': s[i] = '\b'; off++; break;
case 'e': s[i] = 033; off++; break;
case 'f': s[i] = '\f'; off++; break;
case 'n': s[i] = '\n'; off++; break;
case 'r': s[i] = '\r'; off++; break;
case 't': s[i] = '\t'; off++; break;
case 'v': s[i] = '\v'; off++; break;
case 'x':
/* "\xH[H]" hexadecimal escape */
for (m = i + 2; m < i + 1 + 3 && m < len; m++)
if ((s[m] < '0' && s[m] > '9') &&
(s[m] < 'A' && s[m] > 'F') &&
(s[m] < 'a' && s[m] > 'f'))
break;
if (m == i + 2)
eprintf("invalid escape sequence '\\%c'\n", s[i + 1]);
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;
factor *= 16;
} }
s[i] = q; r++;
break; if (!*r) {
case '\0':
eprintf("null escape sequence\n"); eprintf("null escape sequence\n");
default: } else if (escapes[(unsigned char)*r]) {
/* "\O[OOO]" octal escape */ *w++ = escapes[(unsigned char)*r++];
for (m = i + 1; m < i + 1 + 4 && m < len; m++) } else if (is_odigit(*r)) {
if (s[m] < '0' || s[m] > '7') for (q = 0, m = 4; m && is_odigit(*r); m--, r++)
break; q = q * 8 + (*r - '0');
if (m == i + 1) *w++ = MIN(q, 255);
eprintf("invalid escape sequence '\\%c'\n", s[i + 1]); } else if (*r == 'x' && isxdigit(r[1])) {
off += m - i - 1; r++;
for (--m, q = 0, factor = 1; m > i; m--) { for (q = 0, m = 2; m && isxdigit(*r); m--, r++)
q += (s[m] - '0') * factor; if (isdigit(*r))
factor *= 8; q = q * 16 + (*r - '0');
else
q = q * 16 + (tolower(*r) - 'a' + 10);
*w++ = q;
} else {
eprintf("invalid escape sequence '\\%c'\n", *r);
} }
s[i] = (q > 255) ? 255 : q;
} }
for (m = i + 1; m <= len - off; m++) return w - s;
s[m] = s[m + off];
len -= off;
}
return len;
} }