Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Searches forward for a matching closing bracket.
- /// </summary>
- /// <param name="document">The document to search.</param>
- /// <param name="offset">The offset to start from.</param>
- /// <param name="openBracket">The opening bracket.</param>
- /// <param name="bracketClose">The closing bracket.</param>
- /// <returns>If found, the index of the bracket; otherwise -1.</returns>
- public int SearchBracketForward(IDocument document, int offset, char openBracket, char bracketClose)
- {
- char bracketOpenX = char.MinValue;
- char bracketCloseX = char.MinValue;
- char bracketOpenY = char.MinValue;
- char bracketCloseY = char.MinValue;
- switch (openBracket)
- {
- case '{':
- bracketOpenX = squareBrace[0];
- bracketCloseX = squareBrace[1];
- bracketOpenY = roundBrace[0];
- bracketCloseY = roundBrace[1];
- break;
- case '[':
- bracketOpenX = curlyBrace[0];
- bracketCloseX = curlyBrace[1];
- bracketOpenY = roundBrace[0];
- bracketCloseY = roundBrace[1];
- break;
- case '(':
- bracketOpenX = curlyBrace[0];
- bracketCloseX = curlyBrace[1];
- bracketOpenY = squareBrace[0];
- bracketCloseY = squareBrace[1];
- break;
- }
- int depth = 0;
- int depthX = 0;
- int depthY = 0;
- offset -= 1;
- if (offset < document.TextContent.Length &&
- (offset - 1) >= 0 &&
- document.TextContent[offset - 1] == bracketEscapeCharacter)
- return -1;
- char current = document.TextContent[offset];
- while(offset < document.TextContent.Length)
- {
- char previous = current;
- current = document.TextContent[offset];
- if (previous != bracketEscapeCharacter)
- {
- if (current == openBracket)
- depth++;
- else if (current == bracketClose)
- depth--;
- else if (current == bracketOpenX)
- depthX++;
- else if (current == bracketCloseX)
- depthX--;
- else if (current == bracketOpenY)
- depthY++;
- else if (current == bracketCloseY)
- depthY--;
- }
- if (depth < 0)
- return -1;
- if (depth == 0)
- {
- if (depthX != 0 || depthY != 0)
- return -1;
- return offset;
- }
- offset++;
- }
- return -1;
- }
- /// <summary>
- /// Searches backward for a matching open bracket.
- /// </summary>
- /// <param name="document">The document to search.</param>
- /// <param name="offset">The offset to start from.</param>
- /// <param name="bracketOpen">The opening bracket.</param>
- /// <param name="bracketClose">The closing bracket.</param>
- /// <returns>If found, the index of the bracket; otherwise -1.</returns>
- public int SearchBracketBackward(IDocument document, int offset, char bracketOpen, char bracketClose)
- {
- char bracketOpenX = char.MinValue;
- char bracketCloseX = char.MinValue;
- char bracketOpenY = char.MinValue;
- char bracketCloseY = char.MinValue;
- switch (bracketOpen)
- {
- case '{':
- bracketOpenX = squareBrace[0];
- bracketCloseX = squareBrace[1];
- bracketOpenY = roundBrace[0];
- bracketCloseY = roundBrace[1];
- break;
- case '[':
- bracketOpenX = curlyBrace[0];
- bracketCloseX = curlyBrace[1];
- bracketOpenY = roundBrace[0];
- bracketCloseY = roundBrace[1];
- break;
- case '(':
- bracketOpenX = curlyBrace[0];
- bracketCloseX = curlyBrace[1];
- bracketOpenY = squareBrace[0];
- bracketCloseY = squareBrace[1];
- break;
- }
- int depth = 1;
- int depthX = 0;
- int depthY = 0;
- if (offset >= 0 && document.TextContent[offset] == '\\')
- return -1;
- char current = char.MinValue;
- while (offset >= 0)
- {
- char previous = current;
- current = document.TextContent[offset];
- if (current != bracketEscapeCharacter)
- {
- if (previous == bracketClose)
- depth++;
- else if (previous == bracketOpen)
- depth--;
- else if (previous == bracketCloseX)
- depthX++;
- else if (previous == bracketOpenX)
- depthX--;
- else if (previous == bracketCloseY)
- depthY++;
- else if (previous == bracketOpenY)
- depthY--;
- }
- if (offset == 0)
- depth--;
- if (depth < 0)
- return -1;
- if (depth == 0)
- {
- if (depthX != 0 || depthY != 0)
- return -1;
- return offset + (offset != 0 ? 1 : 0);
- }
- offset--;
- }
- return -1;
- }
Add Comment
Please, Sign In to add comment