int Parenthesis::collapseAtPos(const Parentheses &parentheses, QChar *character) { int result = -1; QChar c; int depth = 0; for (int i = 0; i < parentheses.size(); ++i) { const Parenthesis &p = parentheses.at(i); if (p.chr == QLatin1Char('{') || p.chr == QLatin1Char('+') || p.chr == QLatin1Char('[')) { if (depth == 0) { result = p.pos; c = p.chr; } ++depth; } else if (p.chr == QLatin1Char('}') || p.chr == QLatin1Char('-') || p.chr == QLatin1Char(']')) { if (--depth < 0) depth = 0; result = -1; } } if (result >= 0 && character) *character = c; return result; }
int Parenthesis::closeCollapseAtPos(const Parentheses &parentheses, QChar *character) { int depth = 0; for (int i = 0; i < parentheses.size(); ++i) { const Parenthesis &p = parentheses.at(i); if (p.chr == QLatin1Char('{') || p.chr == QLatin1Char('+') || p.chr == QLatin1Char('[')) { ++depth; } else if (p.chr == QLatin1Char('}') || p.chr == QLatin1Char('-') || p.chr == QLatin1Char(']')) { if (--depth < 0) { if (character) *character = p.chr; return p.pos; } } } return -1; }