[hackers] [tabbed][PATCH] arg.h: optimization

From: Nikita Zlobin <nick87720z_AT_gmail.com>
Date: Sun, 4 Jul 2021 04:25:43 +0500

- option argument functions are almost same, differing in one line
- renamed argc_ to arg_ to clear difference
- no need in argv_ comparison, since brk_ is set anyway
- deduplicated same calculation
---
 arg.h | 37 ++++++++++++++-----------------------
 1 file changed, 14 insertions(+), 23 deletions(-)
diff --git a/arg.h b/arg.h
index ac5824e..ee0bbc6 100644
--- a/arg.h
+++ b/arg.h
_AT_@ -14,8 +14,7 @@ extern char *argv0;
 			     argv[0] && argv[0][0] == '-' && argv[0][1];     \
 			     argc--, argv++                 )                \
 			{                                                    \
-				char argc_;                                  \
-				char **argv_;                                \
+				char arg_;                                   \
 				int brk_;                                    \
 				if (argv[0][1] == '-' && argv[0][2] == '\0') \
 				{                                            \
_AT_@ -23,36 +22,28 @@ extern char *argv0;
 					argc--;                              \
 					break;                               \
 				}                                            \
-				for (brk_ = 0, argv[0]++, argv_ = argv;      \
-				     argv[0][0] && !brk_;                    \
+				for (brk_ = 0, argv[0]++;                    \
+				     !brk_ && (arg_ = argv[0][0]);           \
 				     argv[0]++             )                 \
 				{                                            \
-					if (argv_ != argv)                   \
-						break;                       \
-					argc_ = argv[0][0];                  \
-					switch (argc_)
+					switch (arg_)
 #define ARGEND \
 				} \
 			}
 
-#define ARGC()		argc_
-
-#define EARGF(x) \
+#define _ARGF(x)	\
 			( (argv[0][1] == '\0' && argv[1] == NULL)  \
-			  ? ((x), abort(), (char *)0)              \
-			  : (brk_ = 1, (argv[0][1] != '\0')        \
-			               ? (&argv[0][1])             \
-			               : (argc--, argv++, argv[0]) \
+			  ? (x)                                    \
+			  : (brk_ = 1, ( (++argv[0]) [0] != '\0')  \
+			               ? argv[0]                   \
+			               : ( argc--, (++argv)[0] )   \
 			    )                                      \
 			)
 
-#define ARGF()	\
-			( (argv[0][1] == '\0' && argv[1] == NULL)  \
-			  ? (char *)0                              \
-			  : (brk_ = 1, (argv[0][1] != '\0')        \
-			               ? (&argv[0][1])             \
-			               : (argc--, argv++, argv[0]) \
-			    )                                      \
-			)
+#define ARGC()		arg_
+
+#define EARGF(x) (_ARGF( ((x), abort(), NULL) ))
+
+#define ARGF()   (_ARGF( NULL ))
 
 #endif
-- 
2.31.1
Received on Sun Jul 04 2021 - 01:25:43 CEST

This archive was generated by hypermail 2.3.0 : Sun Jul 04 2021 - 01:36:30 CEST