Fixing bug 122309. Adding a new routine that can take a string and parse it on a delimiter to add elements to the nsCStringArray. r=dougt, sr=jag

This commit is contained in:
racham%netscape.com 2005-11-02 16:05:02 +00:00
parent 8e7e7a12be
commit 7b10e797f3
2 changed files with 28 additions and 0 deletions

View File

@ -934,6 +934,27 @@ nsCStringArray::nsCStringArray(void)
{
}
// Parses a given string using the delimiter passed in and appends items
// parsed to the array.
void
nsCStringArray::ParseString(const char* string, const char* delimiter)
{
if (string && *string && delimiter && *delimiter) {
char *newStr;
char *rest = nsCRT::strdup(string);
char *token = nsCRT::strtok(rest, delimiter, &newStr);
while (token) {
if (*token) {
/* calling AppendElement(void*) to avoid extra nsCString copy */
AppendElement(new nsCString(token));
}
token = nsCRT::strtok(newStr, delimiter, &newStr);
}
PR_FREEIF(rest);
}
}
nsCStringArray::nsCStringArray(PRInt32 aCount)
: nsVoidArray(aCount)
{

View File

@ -270,6 +270,13 @@ public:
nsCStringArray& operator=(const nsCStringArray& other);
// Parses a given string using the delimiter passed in. If the array
// already has some elements, items parsed from string will be appended
// to array. For example, array.ParseString("a,b,c", ","); will add strings
// "a", "b" and "c" to the array. Parsing process has the same tokenizing
// behavior as strtok().
void ParseString(const char* string, const char* delimiter);
#ifdef DEBUG
void SizeOf(nsISizeOfHandler* aHandler, PRUint32* aResult) const;
#endif