bracketPositionRight function

Option<StrPos> bracketPositionRight(
  1. String source,
  2. BracketType bracketType
)

Finds the brackets furthest to the right '(L) (R)' Returns a start and end position inp: bl(blim) (plumpy(stumpy)) out: 10, 24;

Implementation

Option<StrPos> bracketPositionRight(String source, BracketType bracketType) {
  var bracket = getBracket(bracketType);

  //TODO: need to find and remove all strings and brackets & remove the brackets from there (or not count them)

  //remove bracket and space to the right
  var startIndex = source.lastIndexOf(bracket.end) - 1; //

  //per char check for closing bracket
  var bracketCount = -1;
  for (var i = startIndex; i >= 0; i--) {
    if (source[i] == bracket.start) bracketCount++;
    if (source[i] == bracket.end) bracketCount--;

    if (bracketCount == 0) return some(StrPos(i + 1, startIndex + 1));
  }

  return none();
}