summaryrefslogtreecommitdiff
path: root/libbb/process_escape_sequence.c
diff options
context:
space:
mode:
authorDenys Vlasenko2010-10-24 01:58:04 +0200
committerDenys Vlasenko2010-10-24 01:58:04 +0200
commit2b299fed6a77d3aaf7e4e768fb519f2536c2eff0 (patch)
tree1d86a176ddfbc1f853842629b758c0ab4e8ada9a /libbb/process_escape_sequence.c
parent53600591311a129717abd2e3bcaa302622a6ce67 (diff)
downloadbusybox-2b299fed6a77d3aaf7e4e768fb519f2536c2eff0.zip
busybox-2b299fed6a77d3aaf7e4e768fb519f2536c2eff0.tar.gz
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>
Diffstat (limited to 'libbb/process_escape_sequence.c')
-rw-r--r--libbb/process_escape_sequence.c46
1 files changed, 23 insertions, 23 deletions
diff --git a/libbb/process_escape_sequence.c b/libbb/process_escape_sequence.c
index dd6e076..7b1d97f 100644
--- a/libbb/process_escape_sequence.c
+++ b/libbb/process_escape_sequence.c
@@ -18,18 +18,8 @@
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;
unsigned num_digits;
- unsigned r;
unsigned n;
unsigned base;
@@ -37,18 +27,17 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr)
base = 8;
q = *ptr;
-#if WANT_HEX_ESCAPES
- if (*q == 'x') {
+ if (WANT_HEX_ESCAPES && *q == 'x') {
++q;
base = 16;
++num_digits;
}
-#endif
/* bash requires leading 0 in octal escapes:
* \02 works, \2 does not (prints \ and 2).
* We treat \2 as a valid octal escape sequence. */
do {
+ unsigned r;
#if !WANT_HEX_ESCAPES
unsigned d = (unsigned char)(*q) - '0';
#else
@@ -60,8 +49,9 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr)
if (WANT_HEX_ESCAPES && base == 16) {
--num_digits;
if (num_digits == 0) {
- /* \x<bad_char> */
- --q; /* go back to x */
+ /* \x<bad_char>: return '\',
+ * leave ptr pointing to x */
+ return '\\';
}
}
break;
@@ -76,20 +66,30 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr)
++q;
} while (++num_digits < 3);
- if (num_digits == 0) { /* mnemonic escape sequence? */
- p = charmap;
+ if (num_digits == 0) {
+ /* 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 {
if (*p == *q) {
q++;
break;
}
- } while (*++p);
- /* p points to found escape char or NUL,
+ } while (*++p != '\\');
+ /* p points to found escape char or '\',
* advance it and find what it translates to.
- * Note that unrecognized sequence \z returns '\'
- * and leaves ptr pointing to z. */
- p += sizeof(charmap) / 2;
- n = *p;
+ * Note that \NUL and unrecognized sequence \z return '\'
+ * and leave ptr pointing to NUL or z. */
+ n = p[sizeof(charmap) / 2];
}
*ptr = q;