util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: Sami Kerola <kerolasa@iki.fi>,
	Frodo Looijaard <frodo@frodo.looijaard.name>
Subject: [PATCH 09/11] getopt: prefer switch-case rather than long if statement
Date: Sun,  7 Dec 2014 10:13:53 +0000	[thread overview]
Message-ID: <1417947235-24229-9-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1417947235-24229-1-git-send-email-kerolasa@iki.fi>

And avoid testing same thing time after time.

CC: Frodo Looijaard <frodo@frodo.looijaard.name>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/getopt.c | 64 +++++++++++++++++++++++++++++++----------------------
 1 file changed, 37 insertions(+), 27 deletions(-)

diff --git a/misc-utils/getopt.c b/misc-utils/getopt.c
index 67603b5..8134558 100644
--- a/misc-utils/getopt.c
+++ b/misc-utils/getopt.c
@@ -122,42 +122,49 @@ static void normalize(const struct getopt_control *ctl, const char *arg)
 	 * quote, and one extra character for '\0'.
 	 */
 	buf = xmalloc(strlen(arg) * 4 + 3);
-
 	bufptr = buf;
-	*bufptr++ = '\'';
 
-	while (*argptr) {
+	for (*bufptr++ = '\''; *argptr; argptr++) {
+		if (ctl->shell == TCSH) {
+			switch (*argptr) {
+			case '\\':
+				/* Backslash: replace it with: '\\' */
+				*bufptr++ = '\\';
+				*bufptr++ = '\\';
+				continue;
+			case '!':
+				/* Exclamation mark: replace it with: \! */
+				*bufptr++ = '\'';
+				*bufptr++ = '\\';
+				*bufptr++ = '!';
+				*bufptr++ = '\'';
+				continue;
+			case '\n':
+				/* Newline: replace it with: \n */
+				*bufptr++ = '\\';
+				*bufptr++ = 'n';
+				continue;
+			}
+			if (isspace(*argptr)) {
+				/* Non-newline whitespace: replace it with \<ws> */
+				*bufptr++ = '\'';
+				*bufptr++ = '\\';
+				*bufptr++ = *argptr;
+				*bufptr++ = '\'';
+				continue;
+			}
+		}
 		if (*argptr == '\'') {
 			/* Quote: replace it with: '\'' */
 			*bufptr++ = '\'';
 			*bufptr++ = '\\';
 			*bufptr++ = '\'';
 			*bufptr++ = '\'';
-		} else if (ctl->shell == TCSH && *argptr == '\\') {
-			/* Backslash: replace it with: '\\' */
-			*bufptr++ = '\\';
-			*bufptr++ = '\\';
-		} else if (ctl->shell == TCSH && *argptr == '!') {
-			/* Exclamation mark: replace it with: \! */
-			*bufptr++ = '\'';
-			*bufptr++ = '\\';
-			*bufptr++ = '!';
-			*bufptr++ = '\'';
-		} else if (ctl->shell == TCSH && *argptr == '\n') {
-			/* Newline: replace it with: \n */
-			*bufptr++ = '\\';
-			*bufptr++ = 'n';
-		} else if (ctl->shell == TCSH && isspace(*argptr)) {
-			/* Non-newline whitespace: replace it with \<ws> */
-			*bufptr++ = '\'';
-			*bufptr++ = '\\';
-			*bufptr++ = *argptr;
-			*bufptr++ = '\'';
 		} else
 			/* Just copy */
 			*bufptr++ = *argptr;
-		argptr++;
 	}
+
 	*bufptr++ = '\'';
 	*bufptr++ = '\0';
 	printf(" %s", buf);
@@ -190,13 +197,16 @@ static int generate_output(const struct getopt_control *ctl, char *argv[], int a
 		if (opt == '?' || opt == ':')
 			exit_code = GETOPT_EXIT_CODE;
 		else if (!ctl->quiet_output) {
-			if (opt == LONG_OPT) {
+			switch (opt) {
+			case LONG_OPT:
 				printf(" --%s", ctl->long_options[longindex].name);
 				if (ctl->long_options[longindex].has_arg)
 					normalize(ctl, optarg ? optarg : "");
-			} else if (opt == NON_OPT)
+				break;
+			case NON_OPT:
 				normalize(ctl, optarg ? optarg : "");
-			else {
+				break;
+			default:
 				printf(" -%c", opt);
 				charptr = strchr(ctl->optstr, opt);
 				if (charptr != NULL && *++charptr == ':')
-- 
2.1.3


  parent reply	other threads:[~2014-12-07 10:14 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-07 10:13 [PATCH 01/11] tests: copy test-suite from Frodo Looijaard's getopt Sami Kerola
2014-12-07 10:13 ` [PATCH 02/11] tests: add more getopt tests Sami Kerola
2014-12-07 10:13 ` [PATCH 03/11] getopt: make nonoptions optstring comment correct Sami Kerola
2014-12-07 10:13 ` [PATCH 04/11] getopt: remove function prototypes Sami Kerola
2014-12-07 10:13 ` [PATCH 05/11] getopt: add struct getopt_control and remove global variables Sami Kerola
2014-12-07 10:13 ` [PATCH 06/11] getopt: use xstrdup rather than malloc + strcpy Sami Kerola
2014-12-07 10:13 ` [PATCH 07/11] getopt: make normalize() print strings Sami Kerola
2014-12-07 10:13 ` [PATCH 08/11] getopt: remove unnecessary code Sami Kerola
2014-12-07 10:13 ` Sami Kerola [this message]
2014-12-07 10:13 ` [PATCH 10/11] getopt: change --shell argument parsing function Sami Kerola
2014-12-07 10:13 ` [PATCH 11/11] getopt: avoid re-terminating long_option list at every update Sami Kerola
2014-12-08 11:37   ` Sami Kerola
2014-12-08 20:05     ` Sami Kerola
2014-12-09 11:09 ` [PATCH 01/11] tests: copy test-suite from Frodo Looijaard's getopt Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1417947235-24229-9-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=frodo@frodo.looijaard.name \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).