[hackers] [PATCH][sbase] libutil/unescape.c: add \E and simplify \x

From: Mattias Andrée <maandree_AT_kth.se>
Date: Sat, 4 Feb 2017 22:32:25 +0100

Signed-off-by: Mattias Andrée <maandree_AT_kth.se>
---
 libutil/unescape.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
diff --git a/libutil/unescape.c b/libutil/unescape.c
index d1503e6..aae15cf 100644
--- a/libutil/unescape.c
+++ b/libutil/unescape.c
_AT_@ -1,3 +1,4 @@
 /* See LICENSE file for copyright and license details. */
+#include <ctype.h>
 #include <string.h>
 
_AT_@ -18,8 +19,9 @@ unescape(char *s)
 		switch (s[i + 1]) {
 		case '\\': s[i] = '\\'; off++; break;
-		case '\'': s[i] = '\'', off++; break;
-		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':
 		case 'e':  s[i] =  033; off++; break;
 		case 'f':  s[i] = '\f'; off++; break;
_AT_@ -31,7 +33,5 @@ unescape(char *s)
 			/* "\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'))
+				if (!isxdigit(s[m]))
 					break;
 			if (m == i + 2)
_AT_@ -39,10 +39,8 @@ unescape(char *s)
 			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;
+				if (isdigit(s[m]))
+					q += (s[m] & 15) * factor;
+				else
+					q += ((s[m] & 15) + 9) * factor;
 				factor *= 16;
 			}
-- 
2.11.0
Received on Sat Feb 04 2017 - 22:32:25 CET

This archive was generated by hypermail 2.3.0 : Sat Feb 04 2017 - 22:36:17 CET