3 static const string LOWERCASE =
"abcdefghijklmnopqrstuvwxyz";
4 static const string UPPERCASE =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
5 static const string LETTERS = LOWERCASE + UPPERCASE;
6 static const string DIGITS =
"0123456789";
7 static const string UNDERSCORE =
"_";
8 static const string DASH =
"-";
9 static const string SPACE =
" ";
10 static const string TAB =
"\t";
11 static const string LINE_RETURN =
"\n";
12 static const string LIPSUM =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
13 protected static const string TRANSLATION_KEY_CHARS = UNDERSCORE + DASH;
19 static bool ContainsDigit(
string input)
21 for (
int i, len = input.Length(); i < len; i++)
23 int asciiValue = input[i].ToAscii();
24 if (asciiValue >= 48 && asciiValue <= 57)
35 static bool ContainsUppercase(
string input)
37 for (
int i, len = input.Length(); i < len; i++)
39 int asciiValue = input[i].ToAscii();
40 if (asciiValue >= 65 && asciiValue <= 90)
51 static bool ContainsLowercase(
string input)
53 for (
int i, len = input.Length(); i < len; i++)
55 int asciiValue = input[i].ToAscii();
56 if (asciiValue >= 97 && asciiValue <= 122)
70 static int CountOccurrences(
string haystack,
string needle,
bool caseInsensitive =
false)
72 if (needle.IsEmpty() || haystack.IsEmpty())
81 int needleLength = needle.Length();
82 int haystackLength = haystack.Length();
84 if (needleLength > haystackLength)
89 while (searchIndex < haystackLength)
91 int resultIndex = haystack.IndexOfFrom(searchIndex, needle);
96 searchIndex = resultIndex + needleLength;
121 static string Filter(
string input,
string characters,
bool useCharactersAsBlacklist =
false)
123 if (input.IsEmpty() || (!useCharactersAsBlacklist && characters.IsEmpty()))
127 for (
int i, length = input.Length(); i < length; i++)
129 string letter = input[i];
130 if (characters.Contains(letter) != useCharactersAsBlacklist)
139 static bool IsFormat(SCR_EStringFormat format,
string input)
143 case SCR_EStringFormat.ALPHABETICAL_UC:
return CheckCharacters(input,
false,
true,
false);
144 case SCR_EStringFormat.ALPHABETICAL_LC:
return CheckCharacters(input,
true,
false,
false);
145 case SCR_EStringFormat.ALPHABETICAL_I:
return CheckCharacters(input,
true,
true,
false);
146 case SCR_EStringFormat.ALPHANUMERICAL_UC:
return CheckCharacters(input,
false,
true,
true);
147 case SCR_EStringFormat.ALPHANUMERICAL_LC:
return CheckCharacters(input,
true,
false,
true);
148 case SCR_EStringFormat.ALPHANUMERICAL_I:
return CheckCharacters(input,
true,
true,
true);
149 case SCR_EStringFormat.DIGITS_ONLY:
return CheckCharacters(input,
false,
false,
true);
161 static bool CheckCharacters(
string input,
bool allowLC,
bool allowUC,
bool allowDigits,
bool allowUnderscore =
false)
166 for (
int i, len = input.Length(); i < len; i++)
168 int asciiValue = input[i].ToAscii();
170 (allowLC && asciiValue >= 97 && asciiValue <= 122) ||
171 (allowUC && asciiValue >= 65 && asciiValue <= 90) ||
172 (allowDigits && asciiValue >= 48 && asciiValue <= 57) ||
173 (allowUnderscore && asciiValue == 95)
212 static string Format(
string input, notnull array<string> arguments)
217 if (!input.Contains(
"%"))
220 switch (arguments.Count())
222 case 0:
return string.Format(input);
223 case 1:
return string.Format(input, arguments[0]);
224 case 2:
return string.Format(input, arguments[0], arguments[1]);
225 case 3:
return string.Format(input, arguments[0], arguments[1], arguments[2]);
226 case 4:
return string.Format(input, arguments[0], arguments[1], arguments[2], arguments[3]);
227 case 5:
return string.Format(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]);
228 case 6:
return string.Format(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
229 case 7:
return string.Format(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]);
230 case 8:
return string.Format(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7]);
234 return string.Format(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8]);
240 static array<int> GetIntsFromString(
string input)
242 array<int> result = {};
243 array<string> splits = {};
244 input.Split(SPACE, splits,
true);
246 foreach (
string split : splits)
248 int value = split.ToInt();
249 if (value != 0 || split ==
"0")
250 result.Insert(value);
258 static int IndexOf(
string input, notnull array<string> samples)
260 if (input.IsEmpty() || samples.IsEmpty())
263 int result =
int.MAX;
264 foreach (
string sample : samples)
266 int index = input.IndexOf(sample);
271 if (result ==
int.MAX)
280 static int IndexOfFrom(
string input,
int start, notnull array<string> samples)
282 if (start < 0 || start > input.Length() || input.IsEmpty() || samples.IsEmpty())
285 int result =
int.MAX;
286 foreach (
string sample : samples)
288 int index = input.IndexOfFrom(start, sample);
293 if (result ==
int.MAX)
302 static array<float> GetFloatsFromString(
string input)
304 array<float> result = {};
305 array<string> splits = {};
306 input.Split(SPACE, splits,
true);
308 foreach (
string split : splits)
310 float value = split.ToFloat();
311 if (value != 0 || split.StartsWith(
"0"))
312 result.Insert(value);
323 static array<string> GetLines(
string input,
bool removeEmptyLines =
false,
bool trimLines =
false)
327 if (removeEmptyLines)
330 return {
string.Empty };
333 array<string> result = {};
334 input.Split(
"\n", result, removeEmptyLines);
339 for (
int i = result.Count() - 1; i >= 0; --i)
343 if (removeEmptyLines && !line)
344 result.RemoveOrdered(i);
359 static string InsertAt(
string input,
string insertion,
int insertionIndex = 0)
361 if (input.IsEmpty() || insertion.IsEmpty() || insertionIndex < 0 || insertionIndex > input.Length())
364 if (insertionIndex == 0)
365 return insertion + input;
367 return input.Substring(0, insertionIndex) + insertion + input.Substring(insertionIndex, input.Length() - insertionIndex);
372 static bool IsEmptyOrWhiteSpace(
string input)
374 return input.Trim().IsEmpty();
381 static bool IsTranslationKey(
string input)
383 if (IsEmptyOrWhiteSpace(input))
386 if (input != input.Trim())
389 if (input.Length() < 2 || !input.StartsWith(
"#"))
392 for (
int i, count = TRANSLATION_KEY_CHARS.Length(); i < count; i++)
394 if (input.EndsWith(TRANSLATION_KEY_CHARS[i]))
398 string filter = LETTERS;
399 if (!filter.Contains(input[1]))
402 filter += DIGITS + TRANSLATION_KEY_CHARS;
403 for (
int i, len = input.Length(); i < len; i++)
405 if (!filter.Contains(input[i]))
418 static string Join(
string separator, notnull array<string> pieces,
bool joinEmptyEntries =
true)
420 if (pieces.IsEmpty())
424 foreach (
int i,
string piece : pieces)
429 if (joinEmptyEntries || piece)
430 result += separator + piece;
443 static string PadLeft(
string input,
int length,
string padding = SPACE)
448 if (input.Length() >= length)
451 int padW = padding.Length();
452 for (
int i = length - input.Length() - 1; i >= 0; i -= padW)
454 input = padding + input;
457 if (input.Length() > length)
458 input = input.Substring(input.Length() - length, length);
471 static string PadRight(
string input,
int length,
string padding = SPACE)
476 if (input.Length() >= length)
479 int padW = padding.Length();
480 for (
int i = length - input.Length() - 1; i >= 0; i -= padW)
485 if (input.Length() > length)
486 input = input.Substring(0, length);
503 static string ReplaceRecursive(
string input,
string sample,
string replace)
505 if (input.IsEmpty() || sample.IsEmpty() || sample == replace || replace.Contains(sample))
508 while (input.IndexOf(sample) > -1)
510 input.Replace(sample, replace);
532 static string ReplaceTimes(
string input,
string sample,
string replace,
int howMany = 1,
int skip = 0)
534 if (howMany < 1 || input.IsEmpty() || sample.IsEmpty() || sample == replace)
537 int sampleLength = sample.Length();
538 int replaceLength = replace.Length();
550 index += sampleLength;
555 input = replace + input.Substring(sampleLength, input.Length() - sampleLength);
557 input = input.Substring(0,
index) + replace + input.Substring(
index + sampleLength, input.Length() - (
index + sampleLength));
560 index += replaceLength;
574 static string Reverse(
string input)
577 for (
int i = input.Length() - 1; i >= 0; i--)
586 static bool ContainsAny(
string input, notnull array<string> needles)
591 foreach (
string needle : needles)
593 if (input.Contains(needle))
602 static bool ContainsEvery(
string input, notnull array<string> needles)
607 foreach (
string needle : needles)
609 if (!input.Contains(needle))
621 static bool StartsWithAny(
string input, notnull array<string> lineStarts)
626 foreach (
string lineStart : lineStarts)
628 if (input.StartsWith(lineStart))
640 static bool EndsWithAny(
string input, notnull array<string> lineEnds)
645 foreach (
string lineEnd : lineEnds)
647 if (input.EndsWith(lineEnd))
659 static string Translate(
661 string param1 =
string.Empty,
662 string param2 =
string.Empty,
663 string param3 =
string.Empty,
664 string param4 =
string.Empty,
665 string param5 =
string.Empty,
666 string param6 =
string.Empty,
667 string param7 =
string.Empty,
668 string param8 =
string.Empty,
669 string param9 =
string.Empty)
671 return WidgetManager.Translate(input, param1, param2, param3, param4, param5, param6, param7, param8, param9);
679 static string Translate(
string input, notnull array<string> arguments)
684 if (!input.Contains(
"%"))
687 switch (arguments.Count())
689 case 0:
return WidgetManager.Translate(input);
690 case 1:
return WidgetManager.Translate(input, arguments[0]);
691 case 2:
return WidgetManager.Translate(input, arguments[0], arguments[1]);
692 case 3:
return WidgetManager.Translate(input, arguments[0], arguments[1], arguments[2]);
693 case 4:
return WidgetManager.Translate(input, arguments[0], arguments[1], arguments[2], arguments[3]);
694 case 5:
return WidgetManager.Translate(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]);
695 case 6:
return WidgetManager.Translate(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
696 case 7:
return WidgetManager.Translate(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]);
697 case 8:
return WidgetManager.Translate(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7]);
701 return WidgetManager.Translate(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8]);
709 static string TrimLeft(
string input)
714 for (
int i, count = input.Length(); i < count; i++)
716 string character = input[i];
717 if (character == SPACE || character == TAB || character == LINE_RETURN)
720 return input.Substring(i, count - i);
731 static string TrimRight(
string input)
736 for (
int i = input.Length() - 1; i >= 0; i--)
738 string character = input[i];
739 if (character == SPACE || character == TAB || character == LINE_RETURN)
742 return input.Substring(0, i + 1);
749 enum SCR_EStringFormat