getClosingBracketIndex method

int getClosingBracketIndex(
  1. int openIndex
)

Finds '}' of '{' in string where openIndex is index of first '{'

Implementation

int getClosingBracketIndex(int openIndex) {
  int result = openIndex;

  int opened = 0, closed = 0;
  for (int i = openIndex; i < length; i++) {
    result = i;
    if (this[i] == '{') {
      opened++;
    }
    if (this[i] == '}') {
      closed++;
    }
    if (opened == closed) {
      break;
    }
  }

  return result;
}