Nmap Development mailing list archives

Re: [PATCH] Shorten chomp() in utils.cc


From: "Kris Katterjohn" <kjak () ispwest com>
Date: Sun, 26 Feb 2006 09:35:58 -0800

From: Kris Katterjohn
Sent: 2/26/2006 9:25:39 AM
This slightly shortens chomp() so that it uses 'if, if/else' instead of 'if, if,
if/else'. The first two if statements both returned string if true, which is also
what is returned at the end, which made it easy to shorten.

text  data     bss     dec     hex    filename
5079    28       0    5107    13f3    utils.o
5095    28       0    5123    1403    utils.o.orig

Thanks,
Kris Katterjohn

--- utils.cc.orig     2006-02-24 09:21:36.000000000 -0600
+++ utils.cc  2006-02-26 11:14:32.000000000 -0600
@@ -197,15 +197,13 @@ char *strerror(int errnum) {
 /* Like the perl equivialent -- It removes the terminating newline from string
    IF one exists.  It then returns the POSSIBLY MODIFIED string */
 char *chomp(char *string) {
-  int len;
-  len = strlen(string);
-  if (len < 1)
-    return string;
-  if (string[len - 1] != '\n')
-    return string;  
-  if (len > 1 && string[len-2] == '\r') {
-    string[len-2] = '\0';
-  } else string[len-1] = '\0';
+  int len = strlen(string);
+  if (len && string[len - 1] == '\n') {
+    if (string[len - 2] == '\r')
+      string[len - 2] = '\0';
+    else
+      string[len - 1] = '\0';
+  }
   return string;
 }

Whoops, that "if (string[len - 2] == '\r')" should test for "len > 1" like
before. Sorry.

--- utils.cc.orig       2006-02-24 09:21:36.000000000 -0600
+++ utils.cc    2006-02-26 11:14:32.000000000 -0600
@@ -197,15 +197,13 @@ char *strerror(int errnum) {
 /* Like the perl equivialent -- It removes the terminating newline from string
    IF one exists.  It then returns the POSSIBLY MODIFIED string */
 char *chomp(char *string) {
-  int len;
-  len = strlen(string);
-  if (len < 1)
-    return string;
-  if (string[len - 1] != '\n')
-    return string;  
-  if (len > 1 && string[len-2] == '\r') {
-    string[len-2] = '\0';
-  } else string[len-1] = '\0';
+  int len = strlen(string);
+  if (len && string[len - 1] == '\n') {
+    if (len > 1 && string[len - 2] == '\r')
+      string[len - 2] = '\0';
+    else
+      string[len - 1] = '\0';
+  }
   return string;
 }
 




_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev


Current thread: