Thread: [Solved] Parsing a string
View Single Post
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-08-2020 , 19:16   Re: Parsing a string
Reply With Quote #4

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[][], resultCountmaxlen)
{
    new 
index 0
    
new iItems 0
    
new iStartIndex 0iLen 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 == )
        {
            
iLen index iStartIndex + (text[index+1] == EOS 0)
            
copy(results[iItems++], min(maxleniLen), 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.
__________________

Last edited by fysiks; 03-08-2020 at 19:20.
fysiks is offline