Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XsltFunctions.cs
Go to the documentation of this file.
4using System.Text;
7
9
11public static class XsltFunctions
12{
13 private static readonly CompareInfo s_compareInfo = CultureInfo.InvariantCulture.CompareInfo;
14
15 public static bool StartsWith(string s1, string s2)
16 {
17 if (s1.Length >= s2.Length)
18 {
19 return string.CompareOrdinal(s1, 0, s2, 0, s2.Length) == 0;
20 }
21 return false;
22 }
23
24 public static bool Contains(string s1, string s2)
25 {
26 return s_compareInfo.IndexOf(s1, s2, CompareOptions.Ordinal) >= 0;
27 }
28
29 public static string SubstringBefore(string s1, string s2)
30 {
31 if (s2.Length == 0)
32 {
33 return s2;
34 }
35 int num = s_compareInfo.IndexOf(s1, s2, CompareOptions.Ordinal);
36 if (num >= 1)
37 {
38 return s1.Substring(0, num);
39 }
40 return string.Empty;
41 }
42
43 public static string SubstringAfter(string s1, string s2)
44 {
45 if (s2.Length == 0)
46 {
47 return s1;
48 }
49 int num = s_compareInfo.IndexOf(s1, s2, CompareOptions.Ordinal);
50 if (num >= 0)
51 {
52 return s1.Substring(num + s2.Length);
53 }
54 return string.Empty;
55 }
56
57 public static string Substring(string value, double startIndex)
58 {
60 if (startIndex <= 0.0)
61 {
62 return value;
63 }
64 if (startIndex <= (double)value.Length)
65 {
66 return value.Substring((int)startIndex - 1);
67 }
68 return string.Empty;
69 }
70
71 public static string Substring(string value, double startIndex, double length)
72 {
74 if (startIndex >= (double)value.Length)
75 {
76 return string.Empty;
77 }
78 double num = startIndex + Round(length);
79 startIndex = ((startIndex <= 0.0) ? 0.0 : startIndex);
80 if (startIndex < num)
81 {
82 if (num > (double)value.Length)
83 {
84 num = value.Length;
85 }
86 return value.Substring((int)startIndex, (int)(num - startIndex));
87 }
88 return string.Empty;
89 }
90
91 public static string NormalizeSpace(string value)
92 {
94 int num = 0;
95 int num2 = 0;
96 int i;
97 for (i = 0; i < value.Length; i++)
98 {
100 {
101 continue;
102 }
103 if (i == num)
104 {
105 num++;
106 }
107 else if (value[i] != ' ' || num2 == i)
108 {
109 if (stringBuilder == null)
110 {
111 stringBuilder = new StringBuilder(value.Length);
112 }
113 else
114 {
115 stringBuilder.Append(' ');
116 }
117 if (num2 == i)
118 {
119 stringBuilder.Append(value, num, i - num - 1);
120 }
121 else
122 {
123 stringBuilder.Append(value, num, i - num);
124 }
125 num = i + 1;
126 }
127 else
128 {
129 num2 = i + 1;
130 }
131 }
132 if (stringBuilder == null)
133 {
134 if (num == i)
135 {
136 return string.Empty;
137 }
138 if (num == 0 && num2 != i)
139 {
140 return value;
141 }
142 stringBuilder = new StringBuilder(value.Length);
143 }
144 else if (i != num)
145 {
146 stringBuilder.Append(' ');
147 }
148 if (num2 == i)
149 {
150 stringBuilder.Append(value, num, i - num - 1);
151 }
152 else
153 {
154 stringBuilder.Append(value, num, i - num);
155 }
156 return stringBuilder.ToString();
157 }
158
159 public static string Translate(string arg, string mapString, string transString)
160 {
161 if (mapString.Length == 0)
162 {
163 return arg;
164 }
166 for (int i = 0; i < arg.Length; i++)
167 {
168 int num = mapString.IndexOf(arg[i]);
169 if (num < 0)
170 {
171 stringBuilder.Append(arg[i]);
172 }
173 else if (num < transString.Length)
174 {
175 stringBuilder.Append(transString[num]);
176 }
177 }
178 return stringBuilder.ToString();
179 }
180
181 public static bool Lang(string value, XPathNavigator context)
182 {
183 string xmlLang = context.XmlLang;
184 if (!xmlLang.StartsWith(value, StringComparison.OrdinalIgnoreCase))
185 {
186 return false;
187 }
188 if (xmlLang.Length != value.Length)
189 {
190 return xmlLang[value.Length] == '-';
191 }
192 return true;
193 }
194
195 public static double Round(double value)
196 {
197 double num = Math.Round(value);
198 if (value - num != 0.5)
199 {
200 return num;
201 }
202 return num + 1.0;
203 }
204
206 {
207 if (name.Namespace == "http://www.w3.org/1999/XSL/Transform")
208 {
209 switch (name.Name)
210 {
211 case "version":
213 case "vendor":
214 return new XmlAtomicValue(XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String), "Microsoft");
215 case "vendor-url":
216 return new XmlAtomicValue(XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String), "http://www.microsoft.com");
217 }
218 }
219 else if (name.Namespace == "urn:schemas-microsoft-com:xslt" && name.Name == "version")
220 {
222 }
223 return new XmlAtomicValue(XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String), string.Empty);
224 }
225
226 public static string BaseUri(XPathNavigator navigator)
227 {
228 return navigator.BaseURI;
229 }
230
247
249 {
250 if (value.Count != 1)
251 {
252 return "node-set";
253 }
256 {
257 return "RTF";
258 }
259 if (xPathItem.IsNode)
260 {
261 return "node-set";
262 }
263 object typedValue = xPathItem.TypedValue;
264 if (typedValue is string)
265 {
266 return "string";
267 }
268 if (typedValue is double)
269 {
270 return "number";
271 }
272 if (typedValue is bool)
273 {
274 return "boolean";
275 }
276 return "external";
277 }
278
279 public static double MSNumber(IList<XPathItem> value)
280 {
281 if (value.Count == 0)
282 {
283 return double.NaN;
284 }
286 string value2;
287 if (xPathItem.IsNode)
288 {
289 value2 = xPathItem.Value;
290 }
291 else
292 {
293 Type valueType = xPathItem.ValueType;
295 {
297 {
298 return xPathItem.ValueAsDouble;
299 }
300 if (!xPathItem.ValueAsBoolean)
301 {
302 return 0.0;
303 }
304 return 1.0;
305 }
306 value2 = xPathItem.Value;
307 }
308 if (XmlConvert.TryToDouble(value2, out var result) != null)
309 {
310 return double.NaN;
311 }
312 return result;
313 }
314
315 public static string MSFormatDateTime(string dateTime, string format, string lang, bool isDate)
316 {
317 try
318 {
319 string name = GetCultureInfo(lang).Name;
320 if (!XsdDateTime.TryParse(dateTime, XsdDateTimeFlags.AllXsd | XsdDateTimeFlags.XdrDateTime | XsdDateTimeFlags.XdrTimeNoTz, out var result))
321 {
322 return string.Empty;
323 }
324 DateTime dateTime2 = result.ToZulu();
325 if (format.Length == 0)
326 {
327 format = null;
328 }
329 return dateTime2.ToString(format, new CultureInfo(name));
330 }
331 catch (ArgumentException)
332 {
333 return string.Empty;
334 }
335 }
336
337 public static double MSStringCompare(string s1, string s2, string lang, string options)
338 {
341 bool flag = false;
342 for (int i = 0; i < options.Length; i++)
343 {
344 switch (options[i])
345 {
346 case 'i':
348 break;
349 case 'u':
350 flag = true;
351 break;
352 default:
353 flag = true;
354 compareOptions = CompareOptions.IgnoreCase;
355 break;
356 }
357 }
358 if (flag)
359 {
360 if (compareOptions != 0)
361 {
363 }
364 compareOptions = CompareOptions.IgnoreCase;
365 }
366 int num = cultureInfo.CompareInfo.Compare(s1, s2, compareOptions);
367 if (flag && num == 0)
368 {
369 num = -cultureInfo.CompareInfo.Compare(s1, s2, CompareOptions.None);
370 }
371 return num;
372 }
373
374 public static string MSUtc(string dateTime)
375 {
376 XsdDateTime result;
377 DateTime dt;
378 try
379 {
380 if (!XsdDateTime.TryParse(dateTime, XsdDateTimeFlags.AllXsd | XsdDateTimeFlags.XdrDateTime | XsdDateTimeFlags.XdrTimeNoTz, out result))
381 {
382 return string.Empty;
383 }
384 dt = result.ToZulu();
385 }
386 catch (ArgumentException)
387 {
388 return string.Empty;
389 }
390 char[] array = "----------T00:00:00.000".ToCharArray();
391 switch (result.TypeCode)
392 {
393 case XmlTypeCode.DateTime:
396 break;
397 case XmlTypeCode.Time:
399 break;
400 case XmlTypeCode.Date:
402 break;
403 case XmlTypeCode.GYearMonth:
404 PrintYear(array, dt.Year);
405 ShortToCharArray(array, 5, dt.Month);
406 break;
407 case XmlTypeCode.GYear:
408 PrintYear(array, dt.Year);
409 break;
410 case XmlTypeCode.GMonthDay:
411 ShortToCharArray(array, 5, dt.Month);
412 ShortToCharArray(array, 8, dt.Day);
413 break;
414 case XmlTypeCode.GDay:
415 ShortToCharArray(array, 8, dt.Day);
416 break;
417 case XmlTypeCode.GMonth:
418 ShortToCharArray(array, 5, dt.Month);
419 break;
420 }
421 return new string(array);
422 }
423
424 public static string MSLocalName(string name)
425 {
426 int colonOffset;
427 int num = ValidateNames.ParseQName(name, 0, out colonOffset);
428 if (num != name.Length)
429 {
430 return string.Empty;
431 }
432 if (colonOffset == 0)
433 {
434 return name;
435 }
436 return name.Substring(colonOffset + 1);
437 }
438
439 public static string MSNamespaceUri(string name, XPathNavigator currentNode)
440 {
441 int colonOffset;
442 int num = ValidateNames.ParseQName(name, 0, out colonOffset);
443 if (num != name.Length)
444 {
445 return string.Empty;
446 }
447 string text = name.Substring(0, colonOffset);
448 if (text == "xmlns")
449 {
450 return string.Empty;
451 }
452 string text2 = currentNode.LookupNamespace(text);
453 if (text2 != null)
454 {
455 return text2;
456 }
457 if (text == "xml")
458 {
459 return "http://www.w3.org/XML/1998/namespace";
460 }
461 return string.Empty;
462 }
463
464 private static CultureInfo GetCultureInfo(string lang)
465 {
466 if (lang.Length == 0)
467 {
469 }
470 try
471 {
472 return new CultureInfo(lang);
473 }
474 catch (ArgumentException)
475 {
477 }
478 }
479
480 private static void PrintDate(char[] text, DateTime dt)
481 {
482 PrintYear(text, dt.Year);
483 ShortToCharArray(text, 5, dt.Month);
484 ShortToCharArray(text, 8, dt.Day);
485 }
486
487 private static void PrintTime(char[] text, DateTime dt)
488 {
489 ShortToCharArray(text, 11, dt.Hour);
490 ShortToCharArray(text, 14, dt.Minute);
491 ShortToCharArray(text, 17, dt.Second);
492 PrintMsec(text, dt.Millisecond);
493 }
494
495 private static void PrintYear(char[] text, int value)
496 {
497 text[0] = (char)(value / 1000 % 10 + 48);
498 text[1] = (char)(value / 100 % 10 + 48);
499 text[2] = (char)(value / 10 % 10 + 48);
500 text[3] = (char)(value / 1 % 10 + 48);
501 }
502
503 private static void PrintMsec(char[] text, int value)
504 {
505 if (value != 0)
506 {
507 text[20] = (char)(value / 100 % 10 + 48);
508 text[21] = (char)(value / 10 % 10 + 48);
509 text[22] = (char)(value / 1 % 10 + 48);
510 }
511 }
512
513 private static void ShortToCharArray(char[] text, int start, int value)
514 {
515 text[start] = (char)(value / 10 + 48);
516 text[start + 1] = (char)(value % 10 + 48);
517 }
518}
static CultureInfo CurrentCulture
static CultureInfo InvariantCulture
static decimal Round(decimal d)
Definition Math.cs:1096
static string Xslt_InvalidCompareOption
Definition SR.cs:1976
static string Xslt_InvalidLanguage
Definition SR.cs:1974
Definition SR.cs:7
static ? XmlSchemaSimpleType GetBuiltInSimpleType(XmlQualifiedName qualifiedName)
static int ParseQName(string s, int offset, out int colonOffset)
virtual ? string LookupNamespace(string prefix)
static bool IsWhiteSpace(char ch)
static Exception TryToDouble(string s, out double result)
static XmlWriter Create(string outputFileName)
Definition XmlWriter.cs:468
static readonly Type StringType
static readonly Type DoubleType
static string Translate(string arg, string mapString, string transString)
static bool Lang(string value, XPathNavigator context)
static readonly CompareInfo s_compareInfo
static XPathItem SystemProperty(XmlQualifiedName name)
static string EXslObjectType(IList< XPathItem > value)
static void PrintTime(char[] text, DateTime dt)
static string NormalizeSpace(string value)
static string Substring(string value, double startIndex, double length)
static string SubstringBefore(string s1, string s2)
static string OuterXml(XPathNavigator navigator)
static string MSNamespaceUri(string name, XPathNavigator currentNode)
static string MSLocalName(string name)
static string BaseUri(XPathNavigator navigator)
static double Round(double value)
static string MSUtc(string dateTime)
static void ShortToCharArray(char[] text, int start, int value)
static void PrintYear(char[] text, int value)
static CultureInfo GetCultureInfo(string lang)
static string MSFormatDateTime(string dateTime, string format, string lang, bool isDate)
static bool Contains(string s1, string s2)
static string SubstringAfter(string s1, string s2)
static bool StartsWith(string s1, string s2)
static void PrintMsec(char[] text, int value)
static double MSNumber(IList< XPathItem > value)
static double MSStringCompare(string s1, string s2, string lang, string options)
static void PrintDate(char[] text, DateTime dt)
static string Substring(string value, double startIndex)
static bool TryParse(string text, XsdDateTimeFlags kinds, out XsdDateTime result)