Guest User

Untitled

a guest
Dec 11th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. /// <summary>
  2. /// Searches forward for a matching closing bracket.
  3. /// </summary>
  4. /// <param name="document">The document to search.</param>
  5. /// <param name="offset">The offset to start from.</param>
  6. /// <param name="openBracket">The opening bracket.</param>
  7. /// <param name="bracketClose">The closing bracket.</param>
  8. /// <returns>If found, the index of the bracket; otherwise -1.</returns>
  9. public int SearchBracketForward(IDocument document, int offset, char openBracket, char bracketClose)
  10. {
  11. char bracketOpenX = char.MinValue;
  12. char bracketCloseX = char.MinValue;
  13. char bracketOpenY = char.MinValue;
  14. char bracketCloseY = char.MinValue;
  15. switch (openBracket)
  16. {
  17. case '{':
  18. bracketOpenX = squareBrace[0];
  19. bracketCloseX = squareBrace[1];
  20. bracketOpenY = roundBrace[0];
  21. bracketCloseY = roundBrace[1];
  22. break;
  23. case '[':
  24. bracketOpenX = curlyBrace[0];
  25. bracketCloseX = curlyBrace[1];
  26. bracketOpenY = roundBrace[0];
  27. bracketCloseY = roundBrace[1];
  28. break;
  29. case '(':
  30. bracketOpenX = curlyBrace[0];
  31. bracketCloseX = curlyBrace[1];
  32. bracketOpenY = squareBrace[0];
  33. bracketCloseY = squareBrace[1];
  34. break;
  35. }
  36. int depth = 0;
  37. int depthX = 0;
  38. int depthY = 0;
  39.  
  40. offset -= 1;
  41. if (offset < document.TextContent.Length &&
  42. (offset - 1) >= 0 &&
  43. document.TextContent[offset - 1] == bracketEscapeCharacter)
  44. return -1;
  45.  
  46. char current = document.TextContent[offset];
  47. while(offset < document.TextContent.Length)
  48. {
  49. char previous = current;
  50. current = document.TextContent[offset];
  51.  
  52. if (previous != bracketEscapeCharacter)
  53. {
  54. if (current == openBracket)
  55. depth++;
  56. else if (current == bracketClose)
  57. depth--;
  58. else if (current == bracketOpenX)
  59. depthX++;
  60. else if (current == bracketCloseX)
  61. depthX--;
  62. else if (current == bracketOpenY)
  63. depthY++;
  64. else if (current == bracketCloseY)
  65. depthY--;
  66. }
  67.  
  68. if (depth < 0)
  69. return -1;
  70. if (depth == 0)
  71. {
  72. if (depthX != 0 || depthY != 0)
  73. return -1;
  74. return offset;
  75. }
  76.  
  77. offset++;
  78. }
  79. return -1;
  80. }
  81. /// <summary>
  82. /// Searches backward for a matching open bracket.
  83. /// </summary>
  84. /// <param name="document">The document to search.</param>
  85. /// <param name="offset">The offset to start from.</param>
  86. /// <param name="bracketOpen">The opening bracket.</param>
  87. /// <param name="bracketClose">The closing bracket.</param>
  88. /// <returns>If found, the index of the bracket; otherwise -1.</returns>
  89. public int SearchBracketBackward(IDocument document, int offset, char bracketOpen, char bracketClose)
  90. {
  91. char bracketOpenX = char.MinValue;
  92. char bracketCloseX = char.MinValue;
  93. char bracketOpenY = char.MinValue;
  94. char bracketCloseY = char.MinValue;
  95. switch (bracketOpen)
  96. {
  97. case '{':
  98. bracketOpenX = squareBrace[0];
  99. bracketCloseX = squareBrace[1];
  100. bracketOpenY = roundBrace[0];
  101. bracketCloseY = roundBrace[1];
  102. break;
  103. case '[':
  104. bracketOpenX = curlyBrace[0];
  105. bracketCloseX = curlyBrace[1];
  106. bracketOpenY = roundBrace[0];
  107. bracketCloseY = roundBrace[1];
  108. break;
  109. case '(':
  110. bracketOpenX = curlyBrace[0];
  111. bracketCloseX = curlyBrace[1];
  112. bracketOpenY = squareBrace[0];
  113. bracketCloseY = squareBrace[1];
  114. break;
  115. }
  116. int depth = 1;
  117. int depthX = 0;
  118. int depthY = 0;
  119.  
  120. if (offset >= 0 && document.TextContent[offset] == '\\')
  121. return -1;
  122.  
  123. char current = char.MinValue;
  124. while (offset >= 0)
  125. {
  126. char previous = current;
  127. current = document.TextContent[offset];
  128.  
  129. if (current != bracketEscapeCharacter)
  130. {
  131. if (previous == bracketClose)
  132. depth++;
  133. else if (previous == bracketOpen)
  134. depth--;
  135. else if (previous == bracketCloseX)
  136. depthX++;
  137. else if (previous == bracketOpenX)
  138. depthX--;
  139. else if (previous == bracketCloseY)
  140. depthY++;
  141. else if (previous == bracketOpenY)
  142. depthY--;
  143. }
  144.  
  145. if (offset == 0)
  146. depth--;
  147.  
  148. if (depth < 0)
  149. return -1;
  150. if (depth == 0)
  151. {
  152. if (depthX != 0 || depthY != 0)
  153. return -1;
  154. return offset + (offset != 0 ? 1 : 0);
  155. }
  156.  
  157. offset--;
  158. }
  159. return -1;
  160. }
Add Comment
Please, Sign In to add comment