1
1
using System ;
2
2
using System . Runtime . InteropServices ;
3
3
using System . Text ;
4
+ using System . Text . RegularExpressions ;
4
5
5
6
namespace Rubberduck . VBA . Grammar
6
7
{
@@ -36,9 +37,9 @@ public static string StripTrailingComment(this string line)
36
37
/// <summary>
37
38
/// Returns a value indicating whether line of code is/contains a comment.
38
39
/// </summary>
39
- /// <param name="line"></param>
40
- /// <param name="index">Returns the start index of the comment string, including the comment marker .</param>
41
- /// <returns></returns>
40
+ /// <param name="line">The extended string. </param>
41
+ /// <param name="index">The start index of the comment string.</param>
42
+ /// <returns>Returns <c>true</c> if specified string contains a VBA comment marker. </returns>
42
43
public static bool HasComment ( this string line , out int index )
43
44
{
44
45
var instruction = line . StripStringLiterals ( ) ;
@@ -53,41 +54,46 @@ public static bool HasComment(this string line, out int index)
53
54
return index >= 0 ;
54
55
}
55
56
57
+ public static string StripStringLiterals ( this string line )
58
+ {
59
+ return Regex . Replace ( line , "\" [^\" ]*\" " , match => new string ( ' ' , match . Length ) ) ;
60
+ }
61
+
56
62
/// <summary>
57
63
/// Strips all string literals from a line of code or instruction.
58
64
/// Replaces string literals with whitespace characters, to maintain original length.
59
65
/// </summary>
60
66
/// <param name="line"></param>
61
67
/// <returns>Returns a new string, stripped of all string literals and string delimiters.</returns>
62
- public static string StripStringLiterals ( this string line )
63
- {
64
- var builder = new StringBuilder ( line . Length ) ;
65
- var isInsideString = false ;
66
- for ( var cursor = 0 ; cursor < line . Length ; cursor ++ )
67
- {
68
- if ( line [ cursor ] == StringDelimiter )
69
- {
70
- if ( isInsideString )
71
- {
72
- isInsideString = cursor + 1 == line . Length || line [ cursor + 1 ] == StringDelimiter || cursor > 0 && ( line [ cursor - 1 ] == StringDelimiter ) ;
73
- }
74
- else
75
- {
76
- isInsideString = true ;
77
- }
78
- }
68
+ // public static string StripStringLiterals(this string line)
69
+ // {
70
+ // var builder = new StringBuilder(line.Length);
71
+ // var isInsideString = false;
72
+ // for (var cursor = 0; cursor < line.Length; cursor++)
73
+ // {
74
+ // if (line[cursor] == StringDelimiter)
75
+ // {
76
+ // if (isInsideString)
77
+ // {
78
+ // isInsideString = cursor + 1 == line.Length || line[cursor + 1] == StringDelimiter || cursor > 0 && (line[cursor - 1] == StringDelimiter);
79
+ // }
80
+ // else
81
+ // {
82
+ // isInsideString = true;
83
+ // }
84
+ // }
79
85
80
- if ( ! isInsideString && line [ cursor ] != StringDelimiter )
81
- {
82
- builder . Append ( line [ cursor ] ) ;
83
- }
84
- else
85
- {
86
- builder . Append ( ' ' ) ;
87
- }
88
- }
86
+ // if (!isInsideString && line[cursor] != StringDelimiter)
87
+ // {
88
+ // builder.Append(line[cursor]);
89
+ // }
90
+ // else
91
+ // {
92
+ // builder.Append(' ');
93
+ // }
94
+ // }
89
95
90
- return builder . ToString ( ) ;
91
- }
96
+ // return builder.ToString();
97
+ // }
92
98
}
93
99
}
0 commit comments