IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Opened 20 years ago

Closed 20 years ago

Last modified 20 years ago

#657 closed defect (fixed)

New function: psStringSplit

Reported by: Paul Price Owned by: David.Robbins@…
Priority: high Milestone:
Component: sys Version: unspecified
Severity: normal Keywords:
Cc:

Description

This is sort of similar to Perl's split function. I needed it for the pmFPA
functions, and figured that it would be generally useful so that we should
include it in psLib.

psList *psStringSplit(const char *string, const char *splitters);

\code{psStringSplit} shall split the input \code{string} into a
\code{psList} of \code{psStrings}. The \code{string} is split at any
one of the characters in \code{splitters}. Split strings of zero
length should not be included in the output list.

An implementation (also in psAdditionals.c in psModule branch pap_branch_051214):

XXX: This should probably be implemented using strpbrk
psList *psStringSplit(const char *string,

const char *splitters)

{

psList *values = psListAlloc(NULL); The list of values to return
unsigned int length = strlen(string);
The length of the string
unsigned int numSplitters = strlen(splitters); Number of characters that

might split

unsigned int start = 0; The position of the start of a word
for (int i = 1; i < length; i++) {

bool split = false; Is this character a splitter?
for (int j = 0; j < numSplitters && ! split; j++) {

if (string[i] == splitters[j]) {

split = true;

}

}
if (split) {

if (i == start) {

Some idiot put in two spaces, or two commas or something
start++;

} else {

We're at the end of the word
psString word = psStringNCopy(&string[start], i - start);
(void)psListAdd(values, PS_LIST_TAIL, word);
start = i + 1;
psFree(word);

}

}

}
if (start < length) {

Copy the last word
psString word = psStringNCopy(&string[start], length - start);
(void)psListAdd(values, PS_LIST_TAIL, word);
psFree(word);

}

return values;

}

Change History (4)

comment:1 by robert.desonia@…, 20 years ago

Owner: changed from robert.desonia@… to David.Robbins@…

comment:2 by David.Robbins@…, 20 years ago

Resolution: fixed
Status: newclosed

added to psString

comment:3 by Paul Price, 20 years ago

Keywords: VERIFIED added

Bug has been resolved.... closing.

comment:4 by Paul Price, 20 years ago

Keywords: VERIFIED removed

Bugs have been fixed... closing.

Note: See TracTickets for help on using tickets.