Need help how to get started with D ranges

Uranuz neuranuz at gmail.com
Mon Mar 24 05:13:42 PDT 2014


I see that ranges is primitive to organzie universal approach to 
write some algorithms. But I'm using algorithms from the library 
but I still can't start with writing my own algorithms based on 
ranges. For example I have the following function written without 
ranges. I want to improve it and make it working with different 
types of strings (char[], string, wchar[], etc...). So could 
someone give me an example how to rewrite this function in 
`range`-style?

//Parses HTML form data
dstring[dstring] parseFormData2(dstring queryStr)
{	size_t LexStart = 0;
	dstring curKey;
	dstring curValue;
	for( size_t i = 0; i < queryStr.length; ++i )
	{	if( queryStr[i] == '=' )
		{	curKey = queryStr[LexStart..i].idup;
			curValue = null;
			LexStart = i+1;
		}
		if( (queryStr[i] == '&') || (i+1 == queryStr.length) )
		{	curValue = queryStr[ LexStart .. (i+1 == queryStr.length) ? 
++i : i ].idup;
			if( curKey.length > 0)
			{	result[curKey] = curValue;
				//result[curKey] ~= curValue;
			}
			curKey = null;
			LexStart = i+1;
		}
	}
	return result;
}


More information about the Digitalmars-d-learn mailing list