summary refs log tree commit diff
path: root/src/bltin
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2014-10-27 16:12:49 +0800
committerHerbert Xu <herbert@gondor.apana.org.au>2014-10-27 16:12:49 +0800
commita68498993413cd15f9b852ca32afd368e1792b51 (patch)
tree8f03b795c89f93418eafbabb0b096f1f05cb5dd0 /src/bltin
parent[BUILTIN] Use error instead of warnx for fatal errors in printf (diff)
downloaddash-a68498993413cd15f9b852ca32afd368e1792b51.tar.gz
dash-a68498993413cd15f9b852ca32afd368e1792b51.zip
[BUILTIN] Optimise handling of backslash octals in printf
This patch removes the duplicate octal handling for %b by reusing
the existing code in conv_escape.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'src/bltin')
-rw-r--r--src/bltin/printf.c30
1 files changed, 8 insertions, 22 deletions
diff --git a/src/bltin/printf.c b/src/bltin/printf.c
index 4812e40..6f39526 100644
--- a/src/bltin/printf.c
+++ b/src/bltin/printf.c
@@ -272,8 +272,8 @@ conv_escape_str(char *str, char **sp)
 		if (ch != '\\')
 			continue;
 
-		ch = *str++;
-		if (ch == 'c') {
+		c = *str++;
+		if (c == 'c') {
 			/* \c as in SYSV echo - abort all processing.... */
 			c = ch = 0x100;
 			continue;
@@ -284,24 +284,11 @@ conv_escape_str(char *str, char **sp)
 		 * They start with a \0, and are followed by 0, 1, 2, 
 		 * or 3 octal digits. 
 		 */
-		if (ch == '0') {
-			unsigned char i;
-			i = 3;
-			c = 0;
-			do {
-				unsigned k = octtobin(*str);
-				if (k > 7)
-					break;
-				str++;
-				c <<= 3;
-				c += k;
-			} while (--i);
-			continue;
-		}
+		if (c == '0' && isodigit(*str))
+			str++;
 
 		/* Finally test for sequences valid in the format string */
 		str = conv_escape(str - 1, &c);
-		ch = c;
 	} while (STPUTC(c, cp), (char)ch);
 
 	*sp = cp;
@@ -322,12 +309,11 @@ conv_escape(char *str, int *conv_ch)
 
 	switch (ch) {
 	default:
-	case 0:
-		value = '\\';
-		goto out;
+		if (!isodigit(*str)) {
+			value = '\\';
+			goto out;
+		}
 
-	case '0': case '1': case '2': case '3':
-	case '4': case '5': case '6': case '7':
 		ch = 3;
 		value = 0;
 		do {