Need help how to get started with D ranges
John Colvin
john.loughran.colvin at gmail.com
Mon Mar 24 05:18:19 PDT 2014
On Monday, 24 March 2014 at 12:13:43 UTC, Uranuz wrote:
> 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;
> }
Have you read this: http://ddili.org/ders/d.en/ranges.html ?
More information about the Digitalmars-d-learn
mailing list