diff mbox series

builtin: Reduce size of conv_escape

Message ID ZziAShiLhciUX_FJ@gondor.apana.org.au (mailing list archive)
State Under Review
Delegated to: Herbert Xu
Headers show
Series builtin: Reduce size of conv_escape | expand

Commit Message

Herbert Xu Nov. 16, 2024, 11:21 a.m. UTC
The jump table is unnecessarily large for a function that is
not performance-critical.  Move some of the cases out of the
switch statement to reduce its size.

Move the value = ch assignment to the common path.

Merge the code for '\a', '\b' and '\f'.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
diff mbox series

Patch

diff --git a/src/bltin/printf.c b/src/bltin/printf.c
index 46c6295..ff576ff 100644
--- a/src/bltin/printf.c
+++ b/src/bltin/printf.c
@@ -335,21 +335,29 @@  unsigned conv_escape(char *str0, char *out0, bool mbchar)
 	int ch;
 
 	ch = *str;
+	value = ch;
 
 	switch (ch) {
 	default:
-		if (!isodigit(ch)) {
-			value = '\\';
-			str--;
+		if (ch == '"' || ch == '\'')
 			break;
+
+		if (ch == 'U') {
+			ch = 8;
+			goto hex;
+		}
+
+		value = '\\';
+
+		if (isodigit(ch)) {
+			ch = 3;
+			value = 0;
+			do {
+				value <<= 3;
+				value += octtobin(*str++);
+			} while (--ch && isodigit(*str));
 		}
 
-		ch = 3;
-		value = 0;
-		do {
-			value <<= 3;
-			value += octtobin(*str++);
-		} while (--ch && isodigit(*str));
 		str--;
 		break;
 
@@ -424,20 +432,17 @@  hex:
 		ch = 4;
 		goto hex;
 
-	case 'U':
-		ch = 8;
-		goto hex;
-
 	case '\\':
-	case '\"':
-	case '\'':
-		value = ch;
 		break;
 
-	case 'a':	value = '\a';	break;	/* alert */
-	case 'b':	value = '\b';	break;	/* backspace */
+	case 'a':				/* alert */
+	case 'b':				/* backspace */
+	case 'f':				/* form-feed */
+		value -= 'a';
+		value += '\a';
+		break;
+
 	case 'e':	value = '\033';	break;	/* <ESC> */
-	case 'f':	value = '\f';	break;	/* form-feed */
 	case 'n':	value = '\n';	break;	/* newline */
 	case 'r':	value = '\r';	break;	/* carriage-return */
 	case 't':	value = '\t';	break;	/* tab */