chaining splitters

Baz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 11 04:27:46 PDT 2015


On Wednesday, 11 March 2015 at 00:00:39 UTC, dnoob wrote:
> Hello,
>
> I am parsing some text and I have the following;
>
> string text = "some very long text";
>
> foreach(line; splitter(text, [13, 10]))
> {
> 	foreach(record; splitter(line, '*'))
> 	{
> 		foreach(field; splitter(record, '='))
> 		{
> 			foreach(value; splitter(field, ','))
> 			{
> 				// do something...
> 			}
> 		}
> 	}
> }
>
> I know there is a better way to do that but I'm a total D noob.
>
> Thanks!

For this kind of things i tend to write a small lexer because you 
have a good control on what to do where to do it and the finally 
the pattern is quite simple:
---
// empty/front/popFront for build-in arrays
import std.array;

string identifier;
while (!text.empty)
{
   auto current = text.front;
   //
   // test whites/memorize identifier/resetidentifier
   // test punctuation/reset identifier...
   // test keywords/reset identifier...
   identifier ~= current;
   text.popFront;
}
---


More information about the Digitalmars-d-learn mailing list