Here is how I would do it but note that I excluded the outer parentheses requirement because it makes no sense (it's an inconsistent use of the parentheses). This requires no temporary strings and is only limited by the results array passed into the function.
PHP Code:
stock Parse(const text[], results[][], resultCount, maxlen)
{
new index = 0
new iItems = 0
new iStartIndex = 0, iLen = 0
new iParenCounter = 0
while( text[index] != EOS && iItems < resultCount )
{
if( text[index] == '(' )
{
iParenCounter++
}
if( text[index] == ')' )
{
iParenCounter--
}
if( (text[index] == ',' || text[index+1] == EOS) && iParenCounter == 0 )
{
iLen = index - iStartIndex + (text[index+1] == EOS ? 1 : 0)
copy(results[iItems++], min(maxlen, iLen), text[iStartIndex])
iStartIndex = index + 1
}
index++
}
return iItems
}
If for some reason you still want the outer parentheses, you can remove them before passing to this function.
__________________