IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 30, 2006, 2:57:07 PM (20 years ago)
Author:
magnier
Message:

fixed psStringSplit to use strpbrk, added boolean for significance of multiple sequential splitters

File:
1 edited

Legend:

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

    r6989 r7015  
    1313 *  @author David Robbins, MHPCC
    1414 *
    15  *  @version $Revision: 1.27 $ $Name: not supported by cvs2svn $
    16  *  @date $Date: 2006-04-26 02:19:23 $
     15 *  @version $Revision: 1.28 $ $Name: not supported by cvs2svn $
     16 *  @date $Date: 2006-05-01 00:57:07 $
    1717 *
    1818 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    173173}
    174174
     175# if 0
    175176// XXX: This should probably be implemented using strpbrk
     177// XXX : add a boolean "repeats significant"
     178// XXX : NULL input string returns empty (not NULL) list
     179// XXX : NULL splitters is an error
    176180psList *psStringSplit(const char *string,
    177181                      const char *splitters)
    178182{
     183    PS_ASSERT_PTR_NON_NULL(splitters, NULL);
     184
    179185    psList *values = psListAlloc(NULL); // The list of values to return
    180186    PS_ASSERT_PTR_NON_NULL(string, values);
    181     PS_ASSERT_PTR_NON_NULL(splitters, values);
    182     //    if (string == NULL)
    183     //        return values;
    184187
    185188    unsigned int length = strlen(string); // The length of the string
     
    215218    return values;
    216219}
     220# endif
     221
     222// split the string by the given splitters
     223// NULL input string returns empty (not NULL) list
     224// NULL splitters is an error
     225psList *psStringSplit(const char *string,
     226                      const char *splitters,
     227                      bool multipleAreSignificant)
     228{
     229    PS_ASSERT_PTR_NON_NULL(splitters, NULL);
     230
     231    psList *values = psListAlloc(NULL); // The list of values to return
     232    PS_ASSERT_PTR_NON_NULL(string, values);
     233
     234    char *next = NULL;
     235    char *current = (char *) string;
     236    while ((next = strpbrk (current, splitters)) != NULL) {
     237
     238        // are multiple splitters in-a-row significant?
     239        if ((next == current) && !multipleAreSignificant) {
     240            current ++;
     241            continue;
     242        }
     243
     244        // Copy the current word
     245        psString word = psStringNCopy(current, next - current);
     246        psListAdd(values, PS_LIST_TAIL, word);
     247        psFree(word);
     248
     249        current = next + 1;
     250    }
     251
     252    if (strlen(current) > 0) {
     253        // Copy the last word
     254        psString word = psStringCopy(current);
     255        (void)psListAdd(values, PS_LIST_TAIL, word);
     256        psFree(word);
     257    }
     258
     259    return values;
     260}
    217261
    218262// given the input string, search for all copies of the key, and replace with the replacement value
Note: See TracChangeset for help on using the changeset viewer.