mscorlib(4.0.0.0) API with additions
CodeIdentifier.cs
1 using Microsoft.CSharp;
4 using System.Text;
5 
7 {
9  public class CodeIdentifier
10  {
11  internal static CodeDomProvider csharp = new CSharpCodeProvider();
12 
13  internal const int MaxIdentifierLength = 511;
14 
16  [Obsolete("This class should never get constructed as it contains only static methods.")]
17  public CodeIdentifier()
18  {
19  }
20 
24  public static string MakePascal(string identifier)
25  {
26  identifier = MakeValid(identifier);
27  if (identifier.Length <= 2)
28  {
29  return identifier.ToUpper(CultureInfo.InvariantCulture);
30  }
31  if (char.IsLower(identifier[0]))
32  {
33  return char.ToUpper(identifier[0], CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture) + identifier.Substring(1);
34  }
35  return identifier;
36  }
37 
41  public static string MakeCamel(string identifier)
42  {
43  identifier = MakeValid(identifier);
44  if (identifier.Length <= 2)
45  {
46  return identifier.ToLower(CultureInfo.InvariantCulture);
47  }
48  if (char.IsUpper(identifier[0]))
49  {
50  return char.ToLower(identifier[0], CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture) + identifier.Substring(1);
51  }
52  return identifier;
53  }
54 
58  public static string MakeValid(string identifier)
59  {
60  StringBuilder stringBuilder = new StringBuilder();
61  for (int i = 0; i < identifier.Length; i++)
62  {
63  if (stringBuilder.Length >= 511)
64  {
65  break;
66  }
67  char c = identifier[i];
68  if (IsValid(c))
69  {
70  if (stringBuilder.Length == 0 && !IsValidStart(c))
71  {
72  stringBuilder.Append("Item");
73  }
74  stringBuilder.Append(c);
75  }
76  }
77  if (stringBuilder.Length == 0)
78  {
79  return "Item";
80  }
81  return stringBuilder.ToString();
82  }
83 
84  internal static string MakeValidInternal(string identifier)
85  {
86  if (identifier.Length > 30)
87  {
88  return "Item";
89  }
90  return MakeValid(identifier);
91  }
92 
93  private static bool IsValidStart(char c)
94  {
95  if (char.GetUnicodeCategory(c) == UnicodeCategory.DecimalDigitNumber)
96  {
97  return false;
98  }
99  return true;
100  }
101 
102  private static bool IsValid(char c)
103  {
104  switch (char.GetUnicodeCategory(c))
105  {
106  case UnicodeCategory.EnclosingMark:
107  case UnicodeCategory.LetterNumber:
108  case UnicodeCategory.OtherNumber:
109  case UnicodeCategory.SpaceSeparator:
110  case UnicodeCategory.LineSeparator:
111  case UnicodeCategory.ParagraphSeparator:
112  case UnicodeCategory.Control:
113  case UnicodeCategory.Format:
114  case UnicodeCategory.Surrogate:
115  case UnicodeCategory.PrivateUse:
116  case UnicodeCategory.DashPunctuation:
117  case UnicodeCategory.OpenPunctuation:
118  case UnicodeCategory.ClosePunctuation:
119  case UnicodeCategory.InitialQuotePunctuation:
120  case UnicodeCategory.FinalQuotePunctuation:
121  case UnicodeCategory.OtherPunctuation:
122  case UnicodeCategory.MathSymbol:
123  case UnicodeCategory.CurrencySymbol:
124  case UnicodeCategory.ModifierSymbol:
125  case UnicodeCategory.OtherSymbol:
126  case UnicodeCategory.OtherNotAssigned:
127  return false;
128  default:
129  return false;
130  case UnicodeCategory.UppercaseLetter:
131  case UnicodeCategory.LowercaseLetter:
132  case UnicodeCategory.TitlecaseLetter:
133  case UnicodeCategory.ModifierLetter:
134  case UnicodeCategory.OtherLetter:
135  case UnicodeCategory.NonSpacingMark:
136  case UnicodeCategory.SpacingCombiningMark:
137  case UnicodeCategory.DecimalDigitNumber:
138  case UnicodeCategory.ConnectorPunctuation:
139  return true;
140  }
141  }
142 
143  internal static void CheckValidIdentifier(string ident)
144  {
146  {
147  throw new ArgumentException(Res.GetString("XmlInvalidIdentifier", ident), "ident");
148  }
149  }
150 
151  internal static string GetCSharpName(string name)
152  {
153  return EscapeKeywords(name.Replace('+', '.'), csharp);
154  }
155 
156  private static int GetCSharpName(Type t, Type[] parameters, int index, StringBuilder sb)
157  {
158  if (t.DeclaringType != null && t.DeclaringType != t)
159  {
160  index = GetCSharpName(t.DeclaringType, parameters, index, sb);
161  sb.Append(".");
162  }
163  string name = t.Name;
164  int num = name.IndexOf('`');
165  if (num < 0)
166  {
167  num = name.IndexOf('!');
168  }
169  if (num > 0)
170  {
171  EscapeKeywords(name.Substring(0, num), csharp, sb);
172  sb.Append("<");
173  int num2 = int.Parse(name.Substring(num + 1), CultureInfo.InvariantCulture) + index;
174  while (index < num2)
175  {
176  sb.Append(GetCSharpName(parameters[index]));
177  if (index < num2 - 1)
178  {
179  sb.Append(",");
180  }
181  index++;
182  }
183  sb.Append(">");
184  }
185  else
186  {
187  EscapeKeywords(name, csharp, sb);
188  }
189  return index;
190  }
191 
192  internal static string GetCSharpName(Type t)
193  {
194  int num = 0;
195  while (t.IsArray)
196  {
197  t = t.GetElementType();
198  num++;
199  }
200  StringBuilder stringBuilder = new StringBuilder();
201  stringBuilder.Append("global::");
202  string @namespace = t.Namespace;
203  if (@namespace != null && @namespace.Length > 0)
204  {
205  string[] array = @namespace.Split('.');
206  for (int i = 0; i < array.Length; i++)
207  {
208  EscapeKeywords(array[i], csharp, stringBuilder);
209  stringBuilder.Append(".");
210  }
211  }
212  Type[] parameters = (t.IsGenericType || t.ContainsGenericParameters) ? t.GetGenericArguments() : new Type[0];
213  GetCSharpName(t, parameters, 0, stringBuilder);
214  for (int j = 0; j < num; j++)
215  {
216  stringBuilder.Append("[]");
217  }
218  return stringBuilder.ToString();
219  }
220 
221  private static void EscapeKeywords(string identifier, CodeDomProvider codeProvider, StringBuilder sb)
222  {
223  if (identifier != null && identifier.Length != 0)
224  {
225  string text = identifier;
226  int num = 0;
227  while (identifier.EndsWith("[]", StringComparison.Ordinal))
228  {
229  num++;
230  identifier = identifier.Substring(0, identifier.Length - 2);
231  }
232  if (identifier.Length > 0)
233  {
234  CheckValidIdentifier(identifier);
235  identifier = codeProvider.CreateEscapedIdentifier(identifier);
236  sb.Append(identifier);
237  }
238  for (int i = 0; i < num; i++)
239  {
240  sb.Append("[]");
241  }
242  }
243  }
244 
245  private static string EscapeKeywords(string identifier, CodeDomProvider codeProvider)
246  {
247  if (identifier == null || identifier.Length == 0)
248  {
249  return identifier;
250  }
251  string[] array = identifier.Split('.', ',', '<', '>');
252  StringBuilder stringBuilder = new StringBuilder();
253  int num = -1;
254  for (int i = 0; i < array.Length; i++)
255  {
256  if (num >= 0)
257  {
258  stringBuilder.Append(identifier.Substring(num, 1));
259  }
260  num++;
261  num += array[i].Length;
262  string identifier2 = array[i].Trim();
263  EscapeKeywords(identifier2, codeProvider, stringBuilder);
264  }
265  if (stringBuilder.Length == identifier.Length)
266  {
267  return identifier;
268  }
269  return stringBuilder.ToString();
270  }
271  }
272 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
static string MakeCamel(string identifier)
Produces a camel-case string from an input string.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
Definition: __Canon.cs:3
Provides an example implementation of the T:System.CodeDom.Compiler.ICodeGenerator interface....
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
int Length
Gets or sets the length of the current T:System.Text.StringBuilder object.
CodeIdentifier()
Initializes a new instance of the T:System.Xml.Serialization.CodeIdentifier class.
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
static bool IsValidLanguageIndependentIdentifier(string value)
Gets a value indicating whether the specified string is a valid identifier.
virtual string CreateEscapedIdentifier(string value)
Creates an escaped identifier for the specified value.
static string MakeValid(string identifier)
Produces a valid code entity name from an input string.
UnicodeCategory
Defines the Unicode category of a character.
static string MakePascal(string identifier)
Produces a Pascal-case string from an input string.
Provides a base class for T:System.CodeDom.Compiler.CodeDomProvider implementations....
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Provides static methods to convert input text into names for code entities.