digitalmars.D.bugs - [Issue 18336] New: Add std.algorithm.untilClosingParens
- d-bugmail puremagic.com (47/47) Jan 30 2018 https://issues.dlang.org/show_bug.cgi?id=18336
https://issues.dlang.org/show_bug.cgi?id=18336 Issue ID: 18336 Summary: Add std.algorithm.untilClosingParens Product: D Version: D2 Hardware: x86_64 OS: Linux Status: NEW Severity: enhancement Priority: P1 Component: phobos Assignee: nobody puremagic.com Reporter: greensunny12 gmail.com Similar to balancedParens, but returning a range (i.e. it's a generalization of it and balancedParens should be able to use it) See also: https://github.com/dlang/dlang.org/pull/2060#discussion_r163830331 https://github.com/dlang/phobos/pull/6098 A naive, non-efficient implementation: ``` // a range until the next ')', nested () are ignored auto untilClosingParentheses(R)(R rs) { return rs.cumulativeFold!((count, r){ switch(r) { case '(': count++; break; case ')': count--; break; default: } return count; })(1).zip(rs).until!(e => e[0] == 0).map!(e => e[1]); } unittest { import std.algorithm.comparison : equal; assert("aa $(foo $(bar)foobar)".untilClosingParentheses.equal("aa $(foo $(bar)foobar)")); assert("$(FOO a, b, $(ARGS e, f)))".untilClosingParentheses.equal("$(FOO a, b, $(ARGS e, f))")); } ``` --
Jan 30 2018