diff --git a/xpcom/glue/nsVoidArray.cpp b/xpcom/glue/nsVoidArray.cpp index 7586af4afb4..fb93258f5ad 100644 --- a/xpcom/glue/nsVoidArray.cpp +++ b/xpcom/glue/nsVoidArray.cpp @@ -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) { diff --git a/xpcom/glue/nsVoidArray.h b/xpcom/glue/nsVoidArray.h index 95d17b11e9e..c9691473395 100644 --- a/xpcom/glue/nsVoidArray.h +++ b/xpcom/glue/nsVoidArray.h @@ -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