Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CodeIdentifier.cs
Go to the documentation of this file.
3using System.Text;
4
6
7public class CodeIdentifier
8{
9 [Obsolete("This class should never get constructed as it contains only static methods.")]
11 {
12 }
13
14 public static string MakePascal(string identifier)
15 {
17 if (identifier.Length <= 2)
18 {
19 return identifier.ToUpperInvariant();
20 }
21 if (char.IsLower(identifier[0]))
22 {
23 return string.Create(identifier.Length, identifier, delegate(Span<char> buffer, string identifier)
24 {
25 identifier.CopyTo(buffer);
26 buffer[0] = char.ToUpperInvariant(buffer[0]);
27 });
28 }
29 return identifier;
30 }
31
32 public static string MakeCamel(string identifier)
33 {
35 if (identifier.Length <= 2)
36 {
37 return identifier.ToLowerInvariant();
38 }
39 if (char.IsUpper(identifier[0]))
40 {
41 return string.Create(identifier.Length, identifier, delegate(Span<char> buffer, string identifier)
42 {
43 identifier.CopyTo(buffer);
44 buffer[0] = char.ToLowerInvariant(buffer[0]);
45 });
46 }
47 return identifier;
48 }
49
50 public static string MakeValid(string identifier)
51 {
53 for (int i = 0; i < identifier.Length; i++)
54 {
55 if (stringBuilder.Length >= 511)
56 {
57 break;
58 }
59 char c = identifier[i];
60 if (IsValid(c))
61 {
62 if (stringBuilder.Length == 0 && !IsValidStart(c))
63 {
64 stringBuilder.Append("Item");
65 }
66 stringBuilder.Append(c);
67 }
68 }
69 if (stringBuilder.Length == 0)
70 {
71 return "Item";
72 }
73 return stringBuilder.ToString();
74 }
75
76 internal static string MakeValidInternal(string identifier)
77 {
78 if (identifier.Length > 30)
79 {
80 return "Item";
81 }
82 return MakeValid(identifier);
83 }
84
85 private static bool IsValidStart(char c)
86 {
87 if (CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.DecimalDigitNumber)
88 {
89 return false;
90 }
91 return true;
92 }
93
94 private static bool IsValid(char c)
95 {
97 {
98 case UnicodeCategory.EnclosingMark:
99 case UnicodeCategory.LetterNumber:
100 case UnicodeCategory.OtherNumber:
101 case UnicodeCategory.SpaceSeparator:
102 case UnicodeCategory.LineSeparator:
103 case UnicodeCategory.ParagraphSeparator:
104 case UnicodeCategory.Control:
105 case UnicodeCategory.Format:
106 case UnicodeCategory.Surrogate:
107 case UnicodeCategory.PrivateUse:
108 case UnicodeCategory.DashPunctuation:
109 case UnicodeCategory.OpenPunctuation:
110 case UnicodeCategory.ClosePunctuation:
111 case UnicodeCategory.InitialQuotePunctuation:
112 case UnicodeCategory.FinalQuotePunctuation:
113 case UnicodeCategory.OtherPunctuation:
114 case UnicodeCategory.MathSymbol:
115 case UnicodeCategory.CurrencySymbol:
116 case UnicodeCategory.ModifierSymbol:
117 case UnicodeCategory.OtherSymbol:
118 case UnicodeCategory.OtherNotAssigned:
119 return false;
120 default:
121 return false;
122 case UnicodeCategory.UppercaseLetter:
123 case UnicodeCategory.LowercaseLetter:
124 case UnicodeCategory.TitlecaseLetter:
125 case UnicodeCategory.ModifierLetter:
126 case UnicodeCategory.OtherLetter:
127 case UnicodeCategory.NonSpacingMark:
128 case UnicodeCategory.SpacingCombiningMark:
129 case UnicodeCategory.DecimalDigitNumber:
130 case UnicodeCategory.ConnectorPunctuation:
131 return true;
132 }
133 }
134
135 internal static void CheckValidIdentifier([NotNull] string ident)
136 {
138 {
140 }
141 }
142
143 internal static string GetCSharpName(string name)
144 {
145 return EscapeKeywords(name.Replace('+', '.'));
146 }
147
148 private static int GetCSharpName(Type t, Type[] parameters, int index, StringBuilder sb)
149 {
150 if (t.DeclaringType != null && t.DeclaringType != t)
151 {
152 index = GetCSharpName(t.DeclaringType, parameters, index, sb);
153 sb.Append('.');
154 }
155 string name = t.Name;
156 int num = name.IndexOf('`');
157 if (num < 0)
158 {
159 num = name.IndexOf('!');
160 }
161 if (num > 0)
162 {
163 EscapeKeywords(name.Substring(0, num), sb);
164 sb.Append('<');
165 int num2 = int.Parse(name.AsSpan(num + 1), NumberStyles.Integer, CultureInfo.InvariantCulture) + index;
166 while (index < num2)
167 {
168 sb.Append(GetCSharpName(parameters[index]));
169 if (index < num2 - 1)
170 {
171 sb.Append(',');
172 }
173 index++;
174 }
175 sb.Append('>');
176 }
177 else
178 {
179 EscapeKeywords(name, sb);
180 }
181 return index;
182 }
183
184 internal static string GetCSharpName(Type t)
185 {
186 int num = 0;
187 while (t.IsArray)
188 {
189 t = t.GetElementType();
190 num++;
191 }
193 stringBuilder.Append("global::");
194 string @namespace = t.Namespace;
195 if (@namespace != null && @namespace.Length > 0)
196 {
197 string[] array = @namespace.Split('.');
198 for (int i = 0; i < array.Length; i++)
199 {
200 EscapeKeywords(array[i], stringBuilder);
201 stringBuilder.Append('.');
202 }
203 }
204 Type[] parameters = ((t.IsGenericType || t.ContainsGenericParameters) ? t.GetGenericArguments() : Type.EmptyTypes);
205 GetCSharpName(t, parameters, 0, stringBuilder);
206 for (int j = 0; j < num; j++)
207 {
208 stringBuilder.Append("[]");
209 }
210 return stringBuilder.ToString();
211 }
212
213 private static void EscapeKeywords(string identifier, StringBuilder sb)
214 {
215 if (identifier != null && identifier.Length != 0)
216 {
217 int num = 0;
218 while (identifier.EndsWith("[]", StringComparison.Ordinal))
219 {
220 num++;
221 identifier = identifier.Substring(0, identifier.Length - 2);
222 }
223 if (identifier.Length > 0)
224 {
227 sb.Append(identifier);
228 }
229 for (int i = 0; i < num; i++)
230 {
231 sb.Append("[]");
232 }
233 }
234 }
235
236 [return: NotNullIfNotNull("identifier")]
237 private static string EscapeKeywords(string identifier)
238 {
239 if (identifier == null || identifier.Length == 0)
240 {
241 return identifier;
242 }
243 string[] array = identifier.Split('.', ',', '<', '>');
245 int num = -1;
246 for (int i = 0; i < array.Length; i++)
247 {
248 if (num >= 0)
249 {
250 stringBuilder.Append(identifier[num]);
251 }
252 num++;
253 num += array[i].Length;
254 string identifier2 = array[i].Trim();
256 }
257 if (stringBuilder.Length == identifier.Length)
258 {
259 return identifier;
260 }
261 return stringBuilder.ToString();
262 }
263}
static string CreateEscapedIdentifier(string name)
static bool IsValidLanguageIndependentIdentifier(string value)
static UnicodeCategory GetUnicodeCategory(char ch)
static CultureInfo InvariantCulture
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string XmlInvalidIdentifier
Definition SR.cs:1606
Definition SR.cs:7
override string ToString()
StringBuilder Append(char value, int repeatCount)
Type? GetElementType()
virtual Type[] GetGenericArguments()
Definition Type.cs:500
bool IsArray
Definition Type.cs:71
override? Type DeclaringType
Definition Type.cs:55
static readonly Type[] EmptyTypes
Definition Type.cs:19
virtual bool ContainsGenericParameters
Definition Type.cs:336
static void EscapeKeywords(string identifier, StringBuilder sb)
static string MakePascal(string identifier)
static void CheckValidIdentifier([NotNull] string ident)
static string MakeValid(string identifier)
static int GetCSharpName(Type t, Type[] parameters, int index, StringBuilder sb)
static string EscapeKeywords(string identifier)
static string MakeValidInternal(string identifier)
static string GetCSharpName(string name)
static string MakeCamel(string identifier)