forked from nikhilk/scriptsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.cs
More file actions
282 lines (243 loc) · 9.69 KB
/
Copy pathUtility.cs
File metadata and controls
282 lines (243 loc) · 9.69 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
// Utility.cs
// Script#/Core/Compiler
// This source code is subject to terms and conditions of the Apache License, Version 2.0.
//
using System;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
namespace ScriptSharp {
internal static class Utility {
private static readonly string Base64Alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_";
private static readonly string Base54Alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_";
private static readonly string[] ScriptKeywords =
new string[] {
// Current keywords
"break", "delete", "function", "return", "typeof", "case", "do",
"if", "switch", "var", "catch", "else", "in", "this",
"void", "continue", "false", "instanceof", "throw", "while",
"debugger", "finally", "new", "true", "with", "default",
"for", "null", "try",
// Future reserved words
"abstract", "double", "goto", "native", "static", "boolean",
"enum", "implements", "package", "super", "byte", "export",
"import", "private", "synchronized", "char", "extends", "int",
"protected", "throws", "class", "final", "interface", "public",
"transient", "const", "float", "long", "short", "volatile",
// Script#-specific words
"ss", "global"
};
private static Hashtable _keywordTable;
public static string CreateCamelCaseName(string name) {
if (String.IsNullOrEmpty(name)) {
return name;
}
// Some exceptions that simply need to be special cased
if (name.Equals("ID", StringComparison.Ordinal)) {
return "id";
}
bool hasLowerCase = false;
int conversionLength = 0;
for (int i = 0; i < name.Length; i++) {
if (Char.IsUpper(name, i)) {
conversionLength++;
}
else {
hasLowerCase = true;
break;
}
}
if (((hasLowerCase == false) && (name.Length != 1)) || (conversionLength == 0)) {
// Name is all upper case, or all lower case;
// leave it as-is.
return name;
}
if (conversionLength > 1) {
// Convert the leading uppercase segment, except the last character
// which is assumed to be the first letter of the next word
return name.Substring(0, conversionLength - 1).ToLower(CultureInfo.InvariantCulture) +
name.Substring(conversionLength - 1);
}
else if (name.Length == 1) {
return name.ToLower(CultureInfo.InvariantCulture);
}
else {
// Convert the leading upper case character to lower case
return Char.ToLower(name[0], CultureInfo.InvariantCulture) + name.Substring(1);
}
}
public static string CreateEncodedName(int number, bool useDigits) {
string alphabet = useDigits ? Base64Alphabet : Base54Alphabet;
if (number == 0) {
return alphabet[0].ToString();
}
string name = String.Empty;
while (number > 0) {
int remainder = number % alphabet.Length;
number = (int)(number / alphabet.Length);
name = alphabet[remainder] + name;
}
return name;
}
public static string GetResourceFileLocale(string fileName) {
string extension = Path.GetExtension(fileName);
if (String.Compare(extension, ".resx", StringComparison.OrdinalIgnoreCase) == 0) {
fileName = Path.GetFileNameWithoutExtension(fileName);
extension = Path.GetExtension(fileName);
if (String.IsNullOrEmpty(extension) == false) {
return extension.Substring(1);
}
}
return String.Empty;
}
public static string GetResourceFileName(string fileName) {
string extension = Path.GetExtension(fileName);
if (String.Compare(extension, ".resx", StringComparison.OrdinalIgnoreCase) == 0) {
fileName = Path.GetFileNameWithoutExtension(fileName);
extension = Path.GetExtension(fileName);
if (String.IsNullOrEmpty(extension) == false) {
fileName = Path.GetFileNameWithoutExtension(fileName);
}
}
return fileName;
}
public static bool IsKeyword(string identifier) {
return IsKeyword(identifier, /* testCamelCase */ false);
}
public static bool IsKeyword(string identifier, bool testCamelCase) {
Debug.Assert(String.IsNullOrEmpty(identifier) == false);
if (_keywordTable == null) {
_keywordTable = new Hashtable(StringComparer.Ordinal);
foreach (string word in ScriptKeywords) {
_keywordTable.Add(word, null);
}
}
if (testCamelCase) {
identifier = CreateCamelCaseName(identifier);
}
return _keywordTable.ContainsKey(identifier);
}
public static bool IsValidIdentifier(string name) {
if (String.IsNullOrEmpty(name)) {
return false;
}
if (IsKeyword(name)) {
return false;
}
if (Char.IsDigit(name[0])) {
return false;
}
for (int i = 0; i < name.Length; i++) {
char ch = name[i];
if ((ch != '_') && (ch != '$') &&
(Char.IsLetterOrDigit(ch) == false)) {
return false;
}
}
return true;
}
public static bool IsValidScriptNamespace(string name) {
if (String.IsNullOrEmpty(name)) {
return false;
}
foreach (string part in name.Split('.')) {
if (!IsValidIdentifier(part)) {
return false;
}
}
return true;
}
public static bool IsValidScriptName(string name) {
if (String.IsNullOrEmpty(name)) {
return true;
}
if (IsKeyword(name)) {
return false;
}
if (Char.IsDigit(name[0])) {
return false;
}
for (int i = 0; i < name.Length; i++) {
char ch = name[i];
if ((Char.IsLetterOrDigit(ch) == false) && (ch != '.') && (ch != '_')) {
return false;
}
}
return true;
}
public static string QuoteString(string s) {
if (String.IsNullOrEmpty(s)) {
return "''";
}
bool useDoubleQuotes = (s.IndexOf('\'') >= 0);
StringBuilder b = null;
int startIndex = 0;
int count = 0;
for (int i = 0; i < s.Length; i++) {
char c = s[i];
// Append the unhandled characters (that do not require special treament)
// to the string builder when special characters are detected.
if (c == '\r' || c == '\t' ||
c == '\\' || c == '\r' || c < ' ' || c > 0x7F ||
((c == '\"') && useDoubleQuotes) ||
((c == '\'') && (useDoubleQuotes == false))) {
if (b == null) {
b = new StringBuilder(s.Length + 6);
}
if (count > 0) {
b.Append(s, startIndex, count);
}
startIndex = i + 1;
count = 0;
}
switch (c) {
case '\r':
b.Append("\\r");
break;
case '\t':
b.Append("\\t");
break;
case '\\':
b.Append("\\\\");
break;
case '\n':
b.Append("\\n");
break;
case '\"':
if (useDoubleQuotes) {
b.Append("\\\"");
break;
}
goto default;
case '\'':
if (!useDoubleQuotes) {
b.Append("\\\'");
break;
}
goto default;
default:
if ((c < ' ') || (c > 0x7F)) {
b.AppendFormat(CultureInfo.InvariantCulture, "\\u{0:x4}", (int)c);
}
else {
count++;
}
break;
}
}
string processedString = s;
if (b != null) {
if (count > 0) {
b.Append(s, startIndex, count);
}
processedString = b.ToString();
}
if (useDoubleQuotes) {
return "\"" + processedString + "\"";
}
return "'" + processedString + "'";
}
}
}