IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 12, 2006, 11:31:47 AM (20 years ago)
Author:
Paul Price
Message:

Fix to psStringSubstitute to allow the replace string to contain the key that is being replaced.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sys/psString.c

    r8607 r9509  
    1313 *  @author David Robbins, MHPCC
    1414 *
    15  *  @version $Revision: 1.43 $ $Name: not supported by cvs2svn $
    16  *  @date $Date: 2006-08-25 22:42:37 $
     15 *  @version $Revision: 1.44 $ $Name: not supported by cvs2svn $
     16 *  @date $Date: 2006-10-12 21:31:47 $
    1717 *
    1818 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    304304    }
    305305
     306    char *search = input;               // Search this string
    306307    while (true) {
    307         char *p = strstr(input, key);
    308         if (!p) {
     308        char *found = strstr(search, key); // Found occurence of the key
     309        if (!found) {
    309310            return input;
    310311        }
     
    315316        // this is safe since we will subtract strlen(key) elements from input
    316317        psString output = psStringAlloc(strlen(input) + replaceLength + 1); // Output string
    317         int Nc = p - input;             // Number of characters to copy
     318        int numChar = found - input;    // Number of characters to copy
    318319
    319320        // copy the first segement into 'output'
    320         strncpy(output, input, Nc);
     321        strncpy(output, input, numChar);
    321322
    322323        // copy the key replacement to the start of the key
    323324        if (replace && replaceLength > 0) {
    324             strcpy(&output[Nc], replace);
    325             Nc += replaceLength;
     325            strcpy(output + numChar, replace);
     326            numChar += replaceLength;
    326327        }
    327328
    328329        // copy the remainder to the end of the replacement
    329         strcpy(&output[Nc], p + strlen(key));
     330        strcpy(output + numChar, found + strlen(key));
    330331
    331332        psFree(input);
    332333        input = output;
     334        search = output + numChar;
    333335    }
    334336
Note: See TracChangeset for help on using the changeset viewer.