File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -23,14 +23,13 @@ public static String matchToClosingParenthesis(@Nonnull final String string,
2323 }
2424
2525 startIndex += start .length ();
26- int endIndex = startIndex ;
27- while (string .charAt (endIndex ) != '{' ) {
28- ++endIndex ;
29- }
26+ int endIndex = findNextParenthesis (string , startIndex , true );
3027 ++endIndex ;
3128
3229 int openParenthesis = 1 ;
3330 while (openParenthesis > 0 ) {
31+ endIndex = findNextParenthesis (string , endIndex , false );
32+
3433 switch (string .charAt (endIndex )) {
3534 case '{' :
3635 ++openParenthesis ;
@@ -46,4 +45,47 @@ public static String matchToClosingParenthesis(@Nonnull final String string,
4645
4746 return string .substring (startIndex , endIndex );
4847 }
48+
49+ private static int findNextParenthesis (@ Nonnull final String string ,
50+ final int offset ,
51+ final boolean onlyOpen ) {
52+ boolean lastEscaped = false ;
53+ char quote = ' ' ;
54+
55+ for (int i = offset ; i < string .length (); i ++) {
56+ boolean thisEscaped = false ;
57+ final char c = string .charAt (i );
58+
59+ switch (c ) {
60+ case '{' :
61+ if (quote == ' ' ) {
62+ return i ;
63+ }
64+ break ;
65+ case '}' :
66+ if (!onlyOpen && quote == ' ' ) {
67+ return i ;
68+ }
69+ break ;
70+ case '\\' :
71+ if (!lastEscaped ) {
72+ thisEscaped = true ;
73+ }
74+ break ;
75+ case '\'' :
76+ case '"' :
77+ if (!lastEscaped ) {
78+ if (quote == ' ' ) {
79+ quote = c ;
80+ } else if (quote == c ) {
81+ quote = ' ' ;
82+ }
83+ }
84+ }
85+
86+ lastEscaped = thisEscaped ;
87+ }
88+
89+ return -1 ;
90+ }
4991}
Original file line number Diff line number Diff line change @@ -58,4 +58,14 @@ public void lessClosing__success() {
5858
5959 assertEquals (expected , substring );
6060 }
61+
62+ @ Test
63+ void find_closing_with_quotes () {
64+ final String expected = "{return \" ,}\\ \" /\" }" ;
65+ final String string = "function(d){return \" ,}\\ \" /\" }" ;
66+
67+ final String substring = matchToClosingParenthesis (string , "function(d)" );
68+
69+ assertEquals (expected , substring );
70+ }
6171}
You can’t perform that action at this time.
0 commit comments