]> diplodocus.org Git - nmh/blobdiff - sbr/trimcpy.c
Added -debug switch to pick(1) and deprecated $MHPDEBUG.
[nmh] / sbr / trimcpy.c
index d13554850c9e0939807bad6c79d11e0400e2f9fb..6bcf3aec693d3002bd934ee2513f56277bb50d86 100644 (file)
@@ -4,10 +4,13 @@
  *           -- replace internal whitespace with spaces,
  *           -- then return a copy.
  *
- * $Id$
+ * This code is Copyright (c) 2002, by the authors of nmh.  See the
+ * COPYRIGHT file in the root directory of the nmh distribution for
+ * complete copyright information.
  */
 
 #include <h/mh.h>
+#include <h/utils.h>
 
 
 char *
@@ -16,12 +19,12 @@ trimcpy (char *cp)
     char *sp;
 
     /* skip over leading whitespace */
-    while (isspace(*cp))
+    while (isspace((unsigned char) *cp))
        cp++;
 
     /* start at the end and zap trailing whitespace */
     for (sp = cp + strlen(cp) - 1; sp >= cp; sp--) {
-       if (isspace(*sp))
+       if (isspace((unsigned char) *sp))
            *sp = '\0';
        else
            break;
@@ -29,10 +32,43 @@ trimcpy (char *cp)
 
     /* replace remaining whitespace with spaces */
     for (sp = cp; *sp; sp++) {
-       if (isspace(*sp))
+       if (isspace((unsigned char) *sp))
            *sp = ' ';
     }
 
     /* now return a copy */
     return getcpy(cp);
 }
+
+
+/*
+ * cpytrim() -- return a copy of the argument with:
+ *           -- stripped leading and trailing whitespace, and
+ *           -- internal whitespace replaced with spaces.
+ *
+ * This code is Copyright (c) 2013, by the authors of nmh.  See the
+ * COPYRIGHT file in the root directory of the nmh distribution for
+ * complete copyright information.
+ */
+char *
+cpytrim (const char *sp) {
+    char *dp;
+    char *cp;
+
+    /* skip over leading whitespace */
+    while (isspace ((unsigned char) *sp)) ++sp;
+
+    dp = add (sp, NULL);
+
+    /* start at the end and zap trailing whitespace */
+    for (cp = dp + strlen (dp) - 1;
+         cp >= dp  &&  isspace ((unsigned char) *cp);
+         *cp-- = '\0') continue;
+
+    /* replace remaining whitespace with spaces */
+    for (cp = dp; *cp; ++cp) {
+        if (isspace ((unsigned char) *cp)) *cp = ' ';
+    }
+
+    return dp;
+}