https://issues.dlang.org/show_bug.cgi?id=18336
Andrei Alexandrescu <[email protected]> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |[email protected] --- Comment #4 from Andrei Alexandrescu <[email protected]> --- (In reply to hsteoh from comment #3) > As Andrei suggested, one way to implement this would be something like this: > > ----- > R untilMatchingParens(R)(R range, dchar rightParen=')') > if (isInputRange!R) > { > if (range.empty) return range; // base case > auto leftParen = range.front; // <-- this is how we know what the > starting parens is > range.popFront; > > int nesting = 1; > foreach (ch; range) > { > if (ch == leftParen) nesting++; > else if (ch == rightParen) nesting--; > if (nesting == 0) break; > } > return range; > } > ----- > > Basically, the start of the range determines what the opening parens is, and > the optional argument specifies what the closing parens is. Yes, with the amendment there is no default for the closing paren. Instead, there's an overload without the closing paren: R findMatchingParen(R)(R range) { ... } That looks at range.front and then infers the closing paren as follows: ")" for "(", "}" for "{", "[" for "]", ">" for "<", and perhaps a few Unicode fancy parens too. --
