Repository navigation
Expand file tree
/
Copy pathCodeUtils.cs
More file actions
298 lines (273 loc) · 10.5 KB
/
Copy pathCodeUtils.cs
File metadata and controls
298 lines (273 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.Collections.Generic;
using System.Text;
using FastReport.Data;
using FastReport.Engine;
using FastReport.Utils;
namespace FastReport.Code
{
/// <summary>
/// This class is used to pass find arguments to some methods of the <b>CodeUtils</b> class.
/// </summary>
public class FindTextArgs
{
private int startIndex;
private int endIndex;
private string openBracket;
private string closeBracket;
private FastString text;
private string foundText;
/// <summary>
/// The start position of the search. After the search, this property points to
/// the begin of an expression.
/// </summary>
public int StartIndex
{
get { return startIndex; }
set { startIndex = value; }
}
/// <summary>
/// After the search, this property points to the end of an expression.
/// </summary>
public int EndIndex
{
get { return endIndex; }
set { endIndex = value; }
}
/// <summary>
/// The char sequence used to find the expression's begin.
/// </summary>
public string OpenBracket
{
get { return openBracket; }
set { openBracket = value; }
}
/// <summary>
/// The char sequence used to find the expression's end.
/// </summary>
public string CloseBracket
{
get { return closeBracket; }
set { closeBracket = value; }
}
/// <summary>
/// The text with embedded expressions.
/// </summary>
public FastString Text
{
get { return text; }
set { text = value; }
}
/// <summary>
/// The last found expression.
/// </summary>
public string FoundText
{
get { return foundText; }
set { foundText = value; }
}
}
/// <summary>
/// This static class contains methods that may be used to find expressions embedded
/// in the object's text.
/// </summary>
public static class CodeUtils
{
internal enum Language
{
Cs,
Vb
}
private static bool isTypeSuffixesInitialized = false;
private static Dictionary<Type, string[]> typeSuffixes;
#region Private Methods
private static bool TypeHasSuffix(Type type)
{
return typeSuffixes.ContainsKey(type);
}
private static string GetTypeSuffix(Type type, Language lang)
{
string[] suffix;
typeSuffixes.TryGetValue(type, out suffix);
return suffix[(int)lang];
}
private static void InitializeTypeSuffixes()
{
typeSuffixes = new Dictionary<Type, string[]>();
//c# and vb type prefixes
typeSuffixes.Add(typeof(float), new string[] { "F", "F" });
typeSuffixes.Add(typeof(double), new string[] { "D", "R" });
typeSuffixes.Add(typeof(uint), new string[] { "U", "" });
typeSuffixes.Add(typeof(long), new string[] { "L", "L" });
typeSuffixes.Add(typeof(ulong), new string[] { "UL", "UL" });
typeSuffixes.Add(typeof(decimal), new string[] { "M", "D" });
isTypeSuffixesInitialized = true;
}
// adjusts StartIndex to the next char after end of string. Returns true if string is correct.
private static bool SkipString(FindTextArgs args)
{
if (args.Text[args.StartIndex] == '"')
args.StartIndex++;
else
return true;
while (args.StartIndex < args.Text.Length)
{
if (args.Text[args.StartIndex] == '"')
{
if (args.Text[args.StartIndex - 1] != '\\')
{
args.StartIndex++;
return true;
}
}
args.StartIndex++;
}
return false;
}
// find matching open and close brackets starting from StartIndex. Takes strings into account.
// Returns true if matching brackets found. Also returns FoundText with text inside brackets,
// StartIndex pointing to the OpenBracket and EndIndex pointing to the next char after CloseBracket.
private static bool FindMatchingBrackets(FindTextArgs args, bool skipLeadingStrings)
{
if (!skipLeadingStrings)
{
args.StartIndex = args.Text.IndexOf(args.OpenBracket, args.StartIndex);
if (args.StartIndex == -1)
return false;
}
int saveStartIndex = 0;
int brCount = 0;
while (args.StartIndex < args.Text.Length)
{
if (!SkipString(args))
return false;
if (args.StartIndex + args.OpenBracket.Length > args.Text.Length)
return false;
if (args.Text.SubstringCompare(args.StartIndex, args.OpenBracket))
{
if (brCount == 0)
saveStartIndex = args.StartIndex;
brCount++;
}
else if (args.Text.SubstringCompare(args.StartIndex, args.CloseBracket))
{
brCount--;
if (brCount == 0)
{
args.EndIndex = args.StartIndex + args.CloseBracket.Length;
args.StartIndex = saveStartIndex;
args.FoundText = args.Text.Substring(args.StartIndex + args.OpenBracket.Length,
args.EndIndex - args.StartIndex - args.OpenBracket.Length - args.CloseBracket.Length);
return true;
}
}
args.StartIndex++;
}
return false;
}
#endregion
#region Internal Methods
// determines whether given index is inside brackets, or is after OpenBracket
internal static bool IndexInsideBrackets(FindTextArgs args)
{
int pos = args.StartIndex;
args.StartIndex = 0;
while (args.StartIndex < pos)
{
// find open bracket
args.StartIndex = args.Text.IndexOf(args.OpenBracket, args.StartIndex);
if (args.StartIndex == -1)
return false;
// missing close bracket
if (!FindMatchingBrackets(args, false))
return true;
// pos is inside brackets
if (args.StartIndex < pos && args.EndIndex > pos)
return true;
args.StartIndex = args.EndIndex;
}
return false;
}
internal static string GetOptionalParameter(System.Reflection.ParameterInfo par, Language lang)
{
if(!isTypeSuffixesInitialized)
InitializeTypeSuffixes();
string optionalParamString = " = ";
if (par.DefaultValue != null)
{
if (par.DefaultValue.GetType().IsEnum)
optionalParamString += par.DefaultValue.GetType().Name + ".";
optionalParamString += par.DefaultValue.ToString();
}
if (TypeHasSuffix(par.ParameterType))
optionalParamString += GetTypeSuffix(par.ParameterType, lang);
return optionalParamString;
}
internal static string FixExpressionWithBrackets(string expression)
{
string result = expression;
if (expression.StartsWith("[") && expression.EndsWith("]"))
{
string tempExpression = expression.Substring(1, expression.Length - 2);
int firstOpen = tempExpression.IndexOf("[");
int firstClose = tempExpression.IndexOf("]");
int lastOpen = tempExpression.LastIndexOf("[");
int lastClose = tempExpression.LastIndexOf("]");
if ((firstOpen < 0 && firstClose >= 0) || (lastOpen >= 0 && lastClose < 0)
|| (firstOpen >= 0 && firstClose >= 0 && firstClose < firstOpen)
|| (lastOpen >= 0 && lastClose >= 0 && lastOpen > lastClose))
{
result = expression;
}
else
{
result = tempExpression;
}
}
else
{
result = expression;
}
return result;
}
#endregion
/// <summary>
/// Returns expressions found in the text.
/// </summary>
/// <param name="text">Text that may contain expressions.</param>
/// <param name="openBracket">The char sequence used to find the start of expression.</param>
/// <param name="closeBracket">The char sequence used to find the end of expression.</param>
/// <returns>Array of expressions if found; otherwise return an empty array.</returns>
public static string[] GetExpressions(string text, string openBracket, string closeBracket)
{
List<string> expressions = new List<string>();
FindTextArgs args = new FindTextArgs();
args.Text = new FastString(text);
args.OpenBracket = openBracket;
args.CloseBracket = closeBracket;
while (args.StartIndex < args.Text.Length) //text.Length
{
if (!FindMatchingBrackets(args, false))
break;
expressions.Add(args.FoundText);
args.StartIndex = args.EndIndex;
}
return expressions.ToArray();
}
/// <summary>
/// Gets first expression found in the text.
/// </summary>
/// <param name="args">Object with find arguments.</param>
/// <param name="skipStrings">Indicates whether to skip strings.</param>
/// <returns>The expression if found; otherwise, returns null.</returns>
public static string GetExpression(FindTextArgs args, bool skipStrings)
{
if (args.StartIndex < args.Text.Length)
{
if (FindMatchingBrackets(args, skipStrings))
return args.FoundText;
}
return null;
}
}
}