xxxxxxxxxx
import std.algorithm, std.range;
auto findMatchingParen(Range, L, R)(Range rs, const L lPar, const R rPar, int startLevel = 1)
{
rs.popFront;
return rs.cumulativeFold!((count, r){
switch(r)
{
case '<':
count++;
break;
case '>':
count--;
break;
default:
}
return count;
})(startLevel).zip(rs).until!(e => e[0] == 0).map!(e => e[1]);
}
void main()
{
import std.stdio;
"<foo foo=bar/>".findMatchingParen('<', '>').writeln;
"<foo><bar/><foobar><bar/></foobar></foo>".findMatchingParen('<', '>', 2).writeln;
}