| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using System.Globalization; |
| 4 |
using System.Text; |
| 5 |
|
| 6 |
namespace GodotTools.ProjectEditor |
| 7 |
{ |
| 8 |
public static class IdentifierUtils |
| 9 |
{ |
| 10 |
public static string SanitizeQualifiedIdentifier(string qualifiedIdentifier, bool allowEmptyIdentifiers) |
| 11 |
{ |
| 12 |
if (string.IsNullOrEmpty(qualifiedIdentifier)) |
| 13 |
throw new ArgumentException($"{nameof(qualifiedIdentifier)} cannot be empty", nameof(qualifiedIdentifier)); |
| 14 |
|
| 15 |
string[] identifiers = qualifiedIdentifier.Split('.'); |
| 16 |
|
| 17 |
for (int i = 0; i < identifiers.Length; i++) |
| 18 |
{ |
| 19 |
identifiers[i] = SanitizeIdentifier(identifiers[i], allowEmpty: allowEmptyIdentifiers); |
| 20 |
} |
| 21 |
|
| 22 |
return string.Join(".", identifiers); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
private static void SkipInvalidCharacters(string source, int startIndex, StringBuilder outputBuilder) |
| 29 |
{ |
| 30 |
for (int i = startIndex; i < source.Length; i++) |
| 31 |
{ |
| 32 |
char @char = source[i]; |
| 33 |
|
| 34 |
switch (char.GetUnicodeCategory(@char)) |
| 35 |
{ |
| 36 |
case UnicodeCategory.UppercaseLetter: |
| 37 |
case UnicodeCategory.LowercaseLetter: |
| 38 |
case UnicodeCategory.TitlecaseLetter: |
| 39 |
case UnicodeCategory.ModifierLetter: |
| 40 |
case UnicodeCategory.LetterNumber: |
| 41 |
case UnicodeCategory.OtherLetter: |
| 42 |
outputBuilder.Append(@char); |
| 43 |
break; |
| 44 |
case UnicodeCategory.NonSpacingMark: |
| 45 |
case UnicodeCategory.SpacingCombiningMark: |
| 46 |
case UnicodeCategory.ConnectorPunctuation: |
| 47 |
case UnicodeCategory.DecimalDigitNumber: |
| 48 |
|
| 49 |
if (outputBuilder.Length > startIndex || @char == '_') |
| 50 |
outputBuilder.Append(@char); |
| 51 |
break; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public static string SanitizeIdentifier(string identifier, bool allowEmpty) |
| 57 |
{ |
| 58 |
if (string.IsNullOrEmpty(identifier)) |
| 59 |
{ |
| 60 |
if (allowEmpty) |
| 61 |
return "Empty"; |
| 62 |
|
| 63 |
throw new ArgumentException($"{nameof(identifier)} cannot be empty if {nameof(allowEmpty)} is false", nameof(identifier)); |
| 64 |
} |
| 65 |
|
| 66 |
if (identifier.Length > 511) |
| 67 |
identifier = identifier.Substring(0, 511); |
| 68 |
|
| 69 |
var identifierBuilder = new StringBuilder(); |
| 70 |
int startIndex = 0; |
| 71 |
|
| 72 |
if (identifier[0] == '@') |
| 73 |
{ |
| 74 |
identifierBuilder.Append('@'); |
| 75 |
startIndex += 1; |
| 76 |
} |
| 77 |
|
| 78 |
SkipInvalidCharacters(identifier, startIndex, identifierBuilder); |
| 79 |
|
| 80 |
if (identifierBuilder.Length == startIndex) |
| 81 |
{ |
| 82 |
|
| 83 |
identifierBuilder.Append("Empty"); |
| 84 |
} |
| 85 |
|
| 86 |
identifier = identifierBuilder.ToString(); |
| 87 |
|
| 88 |
if (identifier[0] != '@' && IsKeyword(identifier, anyDoubleUnderscore: true)) |
| 89 |
identifier = '@' + identifier; |
| 90 |
|
| 91 |
return identifier; |
| 92 |
} |
| 93 |
|
| 94 |
private static bool IsKeyword(string value, bool anyDoubleUnderscore) |
| 95 |
{ |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
if (anyDoubleUnderscore) |
| 100 |
{ |
| 101 |
if (value.Length > 2 && value[0] == '_' && value[1] == '_' && value[2] != '_') |
| 102 |
return true; |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
if (_doubleUnderscoreKeywords.Contains(value)) |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
return _keywords.Contains(value); |
| 111 |
} |
| 112 |
|
| 113 |
private static readonly HashSet<string> _doubleUnderscoreKeywords = new HashSet<string> |
| 114 |
{ |
| 115 |
"__arglist", |
| 116 |
"__makeref", |
| 117 |
"__reftype", |
| 118 |
"__refvalue", |
| 119 |
}; |
| 120 |
|
| 121 |
private static readonly HashSet<string> _keywords = new HashSet<string> |
| 122 |
{ |
| 123 |
"as", |
| 124 |
"do", |
| 125 |
"if", |
| 126 |
"in", |
| 127 |
"is", |
| 128 |
"for", |
| 129 |
"int", |
| 130 |
"new", |
| 131 |
"out", |
| 132 |
"ref", |
| 133 |
"try", |
| 134 |
"base", |
| 135 |
"bool", |
| 136 |
"byte", |
| 137 |
"case", |
| 138 |
"char", |
| 139 |
"else", |
| 140 |
"enum", |
| 141 |
"goto", |
| 142 |
"lock", |
| 143 |
"long", |
| 144 |
"null", |
| 145 |
"this", |
| 146 |
"true", |
| 147 |
"uint", |
| 148 |
"void", |
| 149 |
"break", |
| 150 |
"catch", |
| 151 |
"class", |
| 152 |
"const", |
| 153 |
"event", |
| 154 |
"false", |
| 155 |
"fixed", |
| 156 |
"float", |
| 157 |
"sbyte", |
| 158 |
"short", |
| 159 |
"throw", |
| 160 |
"ulong", |
| 161 |
"using", |
| 162 |
"where", |
| 163 |
"while", |
| 164 |
"yield", |
| 165 |
"double", |
| 166 |
"extern", |
| 167 |
"object", |
| 168 |
"params", |
| 169 |
"public", |
| 170 |
"return", |
| 171 |
"sealed", |
| 172 |
"sizeof", |
| 173 |
"static", |
| 174 |
"string", |
| 175 |
"struct", |
| 176 |
"switch", |
| 177 |
"typeof", |
| 178 |
"unsafe", |
| 179 |
"ushort", |
| 180 |
"checked", |
| 181 |
"decimal", |
| 182 |
"default", |
| 183 |
"finally", |
| 184 |
"foreach", |
| 185 |
"partial", |
| 186 |
"private", |
| 187 |
"virtual", |
| 188 |
"abstract", |
| 189 |
"continue", |
| 190 |
"delegate", |
| 191 |
"explicit", |
| 192 |
"implicit", |
| 193 |
"internal", |
| 194 |
"operator", |
| 195 |
"override", |
| 196 |
"readonly", |
| 197 |
"volatile", |
| 198 |
"interface", |
| 199 |
"namespace", |
| 200 |
"protected", |
| 201 |
"unchecked", |
| 202 |
"stackalloc", |
| 203 |
}; |
| 204 |
} |
| 205 |
} |