@@ -51,45 +51,48 @@ public List<String> removePythonComments(List<String> sourceCode) {
5151 String patternComment_2 = ".*\" \" \" .*" ;
5252 String inLineComment_2 = ".*'''.*'''.*" ;
5353 String inLineComment_3 = ".*\" \" \" .*\" \" \" .*" ;
54+ try {
55+ for (int index = 0 ; index < sizeOfCode ; index ++) {
56+ codeLine = sourceCode .get (index );
57+
58+ if (codeLine .matches (inLineComment_1 )) {
59+ codeLine = codeLine .replaceAll ("#.*" , "" );
60+ filtered .add (codeLine );
61+ } else if (codeLine .matches (inLineComment_2 )) {
62+ codeLine = codeLine .replaceAll ("'''.*'''" , "" );
63+ filtered .add (codeLine );
64+ } else if (codeLine .matches (inLineComment_3 )) {
65+ codeLine = codeLine .replaceAll ("\" \" \" .*\" \" \" " , "" );
66+ filtered .add (codeLine );
67+ } else if (codeLine .matches (patternComment_1 )) {
68+ codeLine = codeLine .replaceAll ("'''.*" , "" );
69+ filtered .add (codeLine );
70+
71+ while (!codeLine .matches (patternComment_1 ) && index < sizeOfCode ) {
72+ codeLine = sourceCode .get (index );
73+ index ++;
74+ }
5475
55- for (int index = 0 ; index < sizeOfCode ; index ++) {
56- codeLine = sourceCode .get (index );
57-
58- if (codeLine .matches (inLineComment_1 )) {
59- codeLine = codeLine .replaceAll ("#.*" , "" );
60- filtered .add (codeLine );
61- } else if (codeLine .matches (inLineComment_2 )) {
62- codeLine = codeLine .replaceAll ("'''.*'''" , "" );
63- filtered .add (codeLine );
64- } else if (codeLine .matches (inLineComment_3 )) {
65- codeLine = codeLine .replaceAll ("\" \" \" .*\" \" \" " , "" );
66- filtered .add (codeLine );
67- } else if (codeLine .matches (patternComment_1 )) {
68- codeLine = codeLine .replaceAll ("'''.*" , "" );
69- filtered .add (codeLine );
70-
71- while (!codeLine .matches (patternComment_1 )) {
72- index ++;
73- codeLine = sourceCode .get (index );
74- }
75-
76- codeLine = codeLine .replaceAll (".*'''" , "" );
77- filtered .add (codeLine );
78- } else if (codeLine .matches (patternComment_2 )) {
79- codeLine = codeLine .replaceAll ("\" \" \" .*" , "" );
80- filtered .add (codeLine );
76+ codeLine = codeLine .replaceAll (".*'''" , "" );
77+ filtered .add (codeLine );
78+ } else if (codeLine .matches (patternComment_2 )) {
79+ codeLine = codeLine .replaceAll ("\" \" \" .*" , "" );
80+ filtered .add (codeLine );
8181
82- while (!codeLine .matches (patternComment_2 )) {
83- index ++ ;
84- codeLine = sourceCode . get ( index ) ;
85- }
82+ while (!codeLine .matches (patternComment_2 ) && index < sizeOfCode ) {
83+ codeLine = sourceCode . get ( index ) ;
84+ index ++ ;
85+ }
8686
87- codeLine = codeLine .replaceAll (".*\" \" \" " , "" );
87+ codeLine = codeLine .replaceAll (".*\" \" \" " , "" );
8888
89- filtered .add (codeLine );
90- } else {
91- filtered .add (codeLine );
89+ filtered .add (codeLine );
90+ } else {
91+ filtered .add (codeLine );
92+ }
9293 }
94+ } catch (Exception e ) {
95+ e .getMessage ();
9396 }
9497
9598 return filtered ;
@@ -141,38 +144,51 @@ public List<String> removeJavascriptComments(List<String> sourceCode) {
141144
142145 private List <String > removeCommentJavaCppJavascriptPattern (List <String > sourceCode ) {
143146 List <String > filtered = null ;
147+ filtered = new ArrayList <>();
144148 try {
145- filtered = new ArrayList <>();
149+
146150 for (int i = 0 ; i < sourceCode .size (); i ++) {
147151 String line = sourceCode .get (i );
148152 if (line .matches (".*//.*" )) {
149- line = line .split ("//" )[0 ];
153+ if (line .split ("//" ) != null ) {
154+ line = line .split ("//" )[0 ];
155+ }
156+
150157 } else if (line .matches (".+/\\ *.*\\ */.+" )) {
151- line = line .split ("/\\ *.*\\ */" )[0 ] + " " + line .split ("/\\ *.*\\ */" )[1 ];
158+ if (line .split ("/\\ *.*\\ */" )[0 ] != null && line .split ("/\\ *.*\\ */" )[1 ] != null ) {
159+ line = line .split ("/\\ *.*\\ */" )[0 ] + " " + line .split ("/\\ *.*\\ */" )[1 ];
160+ }
161+
152162 } else if (line .matches ("\\ s*/\\ *.*\\ */\\ s*" )) {
153163 line = "" ;
154164 } else if (line .matches (".+/\\ *.*\\ */\\ s*" )) {
155- line = line .split ("/\\ *.*\\ */\\ s*" )[0 ];
165+ if (line .split ("/\\ *.*\\ */\\ s*" )[0 ] != null ) {
166+ line = line .split ("/\\ *.*\\ */\\ s*" )[0 ];
167+ }
168+
156169 } else if (line .matches (".*/\\ *.*" )) {
157- while (!line .matches (".*\\ */.*" )) {
158- i ++;
170+ while (!line .matches (".*\\ */.*" ) && i < sourceCode .size ()) {
159171 line = sourceCode .get (i );
172+ i ++;
160173 }
161- if (line .matches (".*\\ */" )) {
162- line = "" ;
163- } else if ( line .matches (".*\\ */.+" )) {
164- line = line . split ( ".* \\ */" )[ 0 ];
174+ if (line .matches (".*\\ */.+ " )) {
175+ if ( line . split ( ".* \\ */" )[ 1 ] != null ) {
176+ line = line .split (".*\\ */" )[ 1 ];
177+ }
165178 }
166179 }
167180 filtered .add (line );
168181 }
169182 } catch (Exception e ) {
170- System .out .println ("In CommmentRemover:" );
171- System .out .println (e .getMessage ());
183+ // System.out.println("In CommmentRemover:");
184+ // System.out.println(e.getCause());
185+ e .getStackTrace ();
172186 // error threat
173187 }
174188
175- // whether filtered equals null appoint an error.
189+ // if (filtered == null || filtered.isEmpty()) {
190+ // ErrorHandler.removeCommentsError();
191+ // }
176192 return filtered ;
177193 }
178194}
0 commit comments