Index: trunk/psLib/src/sys/psString.c
===================================================================
--- trunk/psLib/src/sys/psString.c	(revision 7831)
+++ trunk/psLib/src/sys/psString.c	(revision 7852)
@@ -13,6 +13,6 @@
  *  @author David Robbins, MHPCC
  *
- *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-07-06 22:26:13 $
+ *  @version $Revision: 1.35 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-07-10 20:15:08 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -26,4 +26,5 @@
 #include "psError.h"
 #include "psAssert.h"
+#include "psAbort.h"
 
 #include "psErrorText.h"
@@ -218,7 +219,7 @@
 // given the input string, search for all copies of the key, and replace with the replacement value
 // the input string may be freed if not needed
-char *psStringSubstitute(char *input,
-                         const char *replace,
-                         const char *key)
+psString psStringSubstitute(psString input,
+                            const char *replace,
+                            const char *key)
 {
     if (input == NULL ) {
@@ -231,22 +232,21 @@
         return NULL;
     }
-    if (replace == NULL) {
+    if (key == NULL || strlen(key) == 0) {
         return NULL;
     }
-    if (key == NULL ) {
-        return NULL;
-    } else if (strlen(key) == 0) {
-        return NULL;
-    }
-
-    psString in = psStringCopy(input);
+
+    // replace == NULL is valid: it just means that we strip out the key
+    // without putting anything else in its place
+    size_t replaceLength;               // Size of "replace" string
+    if (replace) {
+        replaceLength = strlen(replace);
+    } else {
+        replaceLength = 0;
+    }
+
     while (true) {
-        //        char *p = strstr (input, key);
-        char *p = strstr (in, key);
+        char *p = strstr(input, key);
         if (!p) {
-            //            return input;
-            //            strcpy(input, in);
-            //            psFree(in);
-            return in;
+            return input;
         }
 
@@ -255,34 +255,25 @@
 
         // this is safe since we will subtract strlen(key) elements from input
-        //        char *output = psAlloc(strlen(input) + strlen(replace) + 1);
-        //        int Nc = p - input;
-        int Nc = p - in;
+        psString output = psAlloc(strlen(input) + replaceLength + 1); // Output string
+        int Nc = p - input;             // Number of characters to copy
 
         // copy the first segement into 'output'
-        psString output = psStringNCopy(in, strlen(in)+strlen(replace)+1);
-
-        //        strncpy(output, input, Nc);
+        strncpy(output, input, Nc);
 
         // copy the key replacement to the start of the key
-        strcpy(&output[Nc], replace);
-        Nc += strlen (replace);
+        if (replace && replaceLength > 0) {
+            strcpy(&output[Nc], replace);
+            Nc += replaceLength;
+        }
 
         // copy the remainder to the end of the replacement
         strcpy(&output[Nc], p + strlen(key));
 
-        //        psFree(input);
-        //        input = output;
-        //        psFree(in);
-        //        strcpy(in, "\0");
-        //in = psStringCopy("\0");
-        psFree(in);
-        //        input = output;
-        //        strcpy(in, output);
-        in = psStringCopy(output);
-        psFree(output);
-    }
-    //    strcpy(input, in);
-    //    psFree(in);
-    return in;
+        psFree(input);
+        input = output;
+    }
+
+    psAbort(__func__, "Should never get here.\n");
+    return NULL;
 }
 
