balancedParens

Checks whether r has "balanced parentheses", i.e. all instances of lPar are closed by corresponding instances of rPar. The parameter maxNestingLevel controls the nesting level allowed. The most common uses are the default or 0. In the latter case, no nesting is allowed.

bool
balancedParens
(
Range
E
)
(
Range r
,,,
size_t maxNestingLevel = size_t.max
)
if (
isInputRange!(Range) &&
is(typeof(r.front == lPar))
)

Parameters

r Range

The range to check.

lPar E

The element corresponding with a left (opening) parenthesis.

rPar E

The element corresponding with a right (closing) parenthesis.

maxNestingLevel size_t

The maximum allowed nesting level.

Return Value

Type: bool

true if the given range has balanced parenthesis within the given maximum nesting level; false otherwise.

Examples

auto s = "1 + (2 * (3 + 1 / 2)";
assert(!balancedParens(s, '(', ')'));
s = "1 + (2 * (3 + 1) / 2)";
assert(balancedParens(s, '(', ')'));
s = "1 + (2 * (3 + 1) / 2)";
assert(!balancedParens(s, '(', ')', 0));
s = "1 + (2 * 3 + 1) / (2 - 5)";
assert(balancedParens(s, '(', ')', 0));
s = "f(x) = ⌈x⌉";
assert(balancedParens(s, '⌈', '⌉'));

Meta