awk: fix breakage in last commit

While at it, made bb_process_escape_sequence faster (same size)

function                                             old     new   delta
nextchar                                              49      53      +4
bb_process_escape_sequence                           138     140      +2
next_token                                           838     839      +1
static.charmap                                        20      18      -2
is_assignment                                        143     135      -8
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/2 up/down: 7/-10)              Total: -3 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2010-10-24 01:58:04 +02:00
parent 5360059131
commit 2b299fed6a
2 changed files with 38 additions and 35 deletions

View File

@ -684,8 +684,11 @@ static char nextchar(char **s)
pps = *s; pps = *s;
if (c == '\\') if (c == '\\')
c = bb_process_escape_sequence((const char**)s); c = bb_process_escape_sequence((const char**)s);
if (c == '\\' && *s == pps) if (c == '\\' && *s == pps) { /* unrecognized \z? */
c = *(*s)++; c = *(*s); /* yes, fetch z */
if (c)
(*s)++; /* advance unless z = NUL */
}
return c; return c;
} }
@ -1007,9 +1010,10 @@ static uint32_t next_token(uint32_t expected)
/* it's a string */ /* it's a string */
t_string = s = ++p; t_string = s = ++p;
while (*p != '\"') { while (*p != '\"') {
char *pp = p; char *pp;
if (*p == '\0' || *p == '\n') if (*p == '\0' || *p == '\n')
syntax_error(EMSG_UNEXP_EOS); syntax_error(EMSG_UNEXP_EOS);
pp = p;
*s++ = nextchar(&pp); *s++ = nextchar(&pp);
p = pp; p = pp;
} }
@ -2926,22 +2930,21 @@ static int awk_exit(int r)
* otherwise return 0 */ * otherwise return 0 */
static int is_assignment(const char *expr) static int is_assignment(const char *expr)
{ {
char *exprc, *s, *s0, *s1; char *exprc, *val, *s, *s1;
if (!isalnum_(*expr) || (s0 = strchr(expr, '=')) == NULL) { if (!isalnum_(*expr) || (val = strchr(expr, '=')) == NULL) {
return FALSE; return FALSE;
} }
exprc = xstrdup(expr); exprc = xstrdup(expr);
s0 = exprc + (s0 - expr); val = exprc + (val - expr);
*s++ = '\0'; *val++ = '\0';
s = s1 = s0; s = s1 = val;
while (*s) while ((*s1 = nextchar(&s)) != '\0')
*s1++ = nextchar(&s); s1++;
*s1 = '\0';
setvar_u(newvar(exprc), s0); setvar_u(newvar(exprc), val);
free(exprc); free(exprc);
return TRUE; return TRUE;
} }

View File

@ -18,18 +18,8 @@
char FAST_FUNC bb_process_escape_sequence(const char **ptr) char FAST_FUNC bb_process_escape_sequence(const char **ptr)
{ {
/* bash builtin "echo -e '\ec'" interprets \e as ESC,
* but coreutils "/bin/echo -e '\ec'" does not.
* manpages tend to support coreutils way.
* Update: coreutils added support for \e on 28 Oct 2009. */
static const char charmap[] ALIGN1 = {
'a', 'b', 'e', 'f', 'n', 'r', 't', 'v', '\\', 0,
'\a', '\b', 27, '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
const char *p;
const char *q; const char *q;
unsigned num_digits; unsigned num_digits;
unsigned r;
unsigned n; unsigned n;
unsigned base; unsigned base;
@ -37,18 +27,17 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr)
base = 8; base = 8;
q = *ptr; q = *ptr;
#if WANT_HEX_ESCAPES if (WANT_HEX_ESCAPES && *q == 'x') {
if (*q == 'x') {
++q; ++q;
base = 16; base = 16;
++num_digits; ++num_digits;
} }
#endif
/* bash requires leading 0 in octal escapes: /* bash requires leading 0 in octal escapes:
* \02 works, \2 does not (prints \ and 2). * \02 works, \2 does not (prints \ and 2).
* We treat \2 as a valid octal escape sequence. */ * We treat \2 as a valid octal escape sequence. */
do { do {
unsigned r;
#if !WANT_HEX_ESCAPES #if !WANT_HEX_ESCAPES
unsigned d = (unsigned char)(*q) - '0'; unsigned d = (unsigned char)(*q) - '0';
#else #else
@ -60,8 +49,9 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr)
if (WANT_HEX_ESCAPES && base == 16) { if (WANT_HEX_ESCAPES && base == 16) {
--num_digits; --num_digits;
if (num_digits == 0) { if (num_digits == 0) {
/* \x<bad_char> */ /* \x<bad_char>: return '\',
--q; /* go back to x */ * leave ptr pointing to x */
return '\\';
} }
} }
break; break;
@ -76,20 +66,30 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr)
++q; ++q;
} while (++num_digits < 3); } while (++num_digits < 3);
if (num_digits == 0) { /* mnemonic escape sequence? */ if (num_digits == 0) {
p = charmap; /* Not octal or hex escape sequence.
* Is it one-letter one? */
/* bash builtin "echo -e '\ec'" interprets \e as ESC,
* but coreutils "/bin/echo -e '\ec'" does not.
* Manpages tend to support coreutils way.
* Update: coreutils added support for \e on 28 Oct 2009. */
static const char charmap[] ALIGN1 = {
'a', 'b', 'e', 'f', 'n', 'r', 't', 'v', '\\',
'\a', '\b', 27, '\f', '\n', '\r', '\t', '\v', '\\',
};
const char *p = charmap;
do { do {
if (*p == *q) { if (*p == *q) {
q++; q++;
break; break;
} }
} while (*++p); } while (*++p != '\\');
/* p points to found escape char or NUL, /* p points to found escape char or '\',
* advance it and find what it translates to. * advance it and find what it translates to.
* Note that unrecognized sequence \z returns '\' * Note that \NUL and unrecognized sequence \z return '\'
* and leaves ptr pointing to z. */ * and leave ptr pointing to NUL or z. */
p += sizeof(charmap) / 2; n = p[sizeof(charmap) / 2];
n = *p;
} }
*ptr = q; *ptr = q;