@@ -203,7 +203,7 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
203203 {
204204 // We want to excise this entire file.
205205 serverCodeInjections . Add ( new KeyValuePair < int , string > ( 0 , excisionLanguage . ServerScopeStartString + "\r \n " ) ) ;
206- serverCodeInjections . Add ( new KeyValuePair < int , string > ( script . Length , excisionLanguage . ServerScopeEndString ) ) ;
206+ serverCodeInjections . Add ( new KeyValuePair < int , string > ( script . Length , excisionLanguage . ServerScopeEndString + " \r \n " ) ) ;
207207 stats . CharactersExcised += script . Length ;
208208 }
209209 else if ( excisionMode == EExcisionMode . AllFunctions )
@@ -241,17 +241,17 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
241241 {
242242 if ( currentScope . StartIndex == - 1
243243 || currentScope . StopIndex == - 1
244- || InjectedMacroAlreadyExistsAtLocation ( answerText , currentScope . StartIndex , true , excisionLanguage . ServerScopeStartString )
245- || InjectedMacroAlreadyExistsAtLocation ( answerText , currentScope . StartIndex , false , excisionLanguage . ServerScopeStartString )
246- || InjectedMacroAlreadyExistsAtLocation ( answerText , currentScope . StopIndex , false , excisionLanguage . ServerScopeEndString ) )
244+ || InjectedMacroAlreadyExistsAtLocation ( answerText , currentScope . StartIndex , true , true , excisionLanguage . ServerScopeStartString )
245+ || InjectedMacroAlreadyExistsAtLocation ( answerText , currentScope . StartIndex , false , false , excisionLanguage . ServerScopeStartString )
246+ || InjectedMacroAlreadyExistsAtLocation ( answerText , currentScope . StopIndex , false , false , excisionLanguage . ServerScopeEndString ) )
247247 {
248248 continue ;
249249 }
250250
251251 // If there are already injected macros where we want to go, we should skip injecting.
252252 System . Diagnostics . Debug . Assert ( currentScope . StopIndex > currentScope . StartIndex , "There must be some invalid pattern here! Stop is before start!" ) ;
253- serverCodeInjections . Add ( new KeyValuePair < int , string > ( currentScope . StartIndex , excisionLanguage . ServerScopeStartString ) ) ;
254- serverCodeInjections . Add ( new KeyValuePair < int , string > ( currentScope . StopIndex , currentScope . Opt_ElseContent + excisionLanguage . ServerScopeEndString ) ) ;
253+ serverCodeInjections . Add ( new KeyValuePair < int , string > ( currentScope . StartIndex , " \r \n " + excisionLanguage . ServerScopeStartString ) ) ;
254+ serverCodeInjections . Add ( new KeyValuePair < int , string > ( currentScope . StopIndex , currentScope . Opt_ElseContent + excisionLanguage . ServerScopeEndString + " \r \n " ) ) ;
255255 stats . CharactersExcised += currentScope . StopIndex - currentScope . StartIndex ;
256256 }
257257
@@ -266,10 +266,10 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
266266 dummyRefDataBlockString . Append ( "\r \n \t " + dummyVarDef ) ;
267267 }
268268
269- dummyRefDataBlockString . Append ( "\r \n " + excisionLanguage . ServerScopeEndString + "\r \n " ) ;
269+ dummyRefDataBlockString . Append ( "\r \n " + excisionLanguage . ServerScopeEndString + "\r \n \r \n " ) ;
270270
271271 // If there is already a block of dummy reference variables we skip adding new ones, there is no guarantee we are adding the right code.
272- if ( InjectedMacroAlreadyExistsAtLocation ( answerText , dummyRefDataPair . Key , false , dummyVarScope + "\r \n " ) )
272+ if ( InjectedMacroAlreadyExistsAtLocation ( answerText , dummyRefDataPair . Key , false , true , dummyVarScope + "\r \n " ) )
273273 {
274274 continue ;
275275 }
@@ -323,19 +323,47 @@ private ExcisionStats ProcessCodeFile(string fileName, string inputPath, EExcisi
323323 return stats ;
324324 }
325325
326- private bool InjectedMacroAlreadyExistsAtLocation ( StringBuilder script , int index , bool lookAhead , string macro )
326+ private static bool IsWhitespace ( char c )
327327 {
328- int startIndex = lookAhead ? index : ( index - macro . Length ) ;
329- int endIndex = lookAhead ? ( index + macro . Length ) : index ;
328+ return c == ' ' || c == ' \t ' || c == ' \r ' || c == ' \n ' ;
329+ }
330330
331- if ( startIndex < 0 || startIndex >= script . Length
332- || endIndex < 0 || endIndex >= script . Length )
331+ private bool InjectedMacroAlreadyExistsAtLocation ( StringBuilder script , int index , bool lookAhead , bool ignoreWhitespace , string macro )
332+ {
333+ if ( lookAhead )
333334 {
334- return false ;
335+ if ( ignoreWhitespace )
336+ {
337+ while ( index < script . Length && IsWhitespace ( script [ index ] ) )
338+ {
339+ index ++ ;
340+ }
341+ }
342+
343+ if ( script . Length - index < macro . Length )
344+ {
345+ return false ;
346+ }
347+
348+ return script . ToString ( index , macro . Length ) . Equals ( macro ) ;
335349 }
350+ else
351+ {
352+ if ( ignoreWhitespace )
353+ {
354+ while ( index > 0 && IsWhitespace ( script [ index ] ) )
355+ {
356+ index -- ;
357+ }
358+ }
336359
337- string scriptSection = script . ToString ( startIndex , macro . Length ) ;
338- return scriptSection == macro ;
360+ if ( index - macro . Length < 0 )
361+ {
362+ return false ;
363+ }
364+
365+ return script . ToString ( index - macro . Length , macro . Length ) . Equals ( macro ) ;
366+ }
339367 }
340368 }
341369}
0 commit comments