balancedDelimitersMatch

fun balancedDelimitersMatch(string: CharSequence, position: Int, begin: Char, end: Char): Int

Matches a balanced sequence delimited by begin and end, ignoring escaped delimiters.

Scans forward starting at position and returns the number of characters up to, but not including, the balancing end delimiter when the delimiters are balanced. Returns 0 if no balanced end is found.

Rules:

  • A delimiter preceded by a backslash (\) is ignored.

  • Nested delimiters are supported and adjust the depth accordingly.

Return

the length from position to the matching end delimiter, or 0 if none

Parameters

string

the source to scan

position

the starting index to scan from

begin

the opening delimiter

end

the closing delimiter