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 ALPHANUMERICAL = LETTERS + DIGITS;
8 static const string UNDERSCORE =
"_";
9 static const string ALPHANUMERICAL_U = ALPHANUMERICAL + UNDERSCORE;
10 static const string DASH =
"-";
11 static const string COLON =
":";
12 static const string SEMICOLON =
";";
13 static const string COMMA =
",";
14 static const string SPACE =
" ";
15 static const string STAR =
"*";
16 static const string POUND =
"#";
17 static const string HASHTAG = POUND;
18 static const string EQUALS =
"=";
19 static const string QUESTION_MARK =
"?";
20 static const string EXCLAMATION_MARK =
"!";
21 static const string ELLIPSIS =
"…";
22 static const string DOUBLE_SPACE = SPACE + SPACE;
23 static const string QUADRUPLE_SPACE = DOUBLE_SPACE + DOUBLE_SPACE;
24 static const string SINGLE_QUOTE =
"'";
25 static const string DOUBLE_QUOTE =
"\"";
26 static const string TAB =
"\t";
27 static const string LINE_RETURN =
"\n";
28 static const string SLASH =
"/";
29 static const string DOUBLE_SLASH = SLASH + SLASH;
30 static const string ANTISLASH =
"\\";
31 static const string DOUBLE_ANTISLASH = ANTISLASH + ANTISLASH;
32 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.";
35 protected static const int MIN_LC = 97;
36 protected static const int MAX_LC = 122;
38 protected static const int MIN_UC = 65;
39 protected static const int MAX_UC = 90;
50 for (
int i, len = input.Length(); i < len; i++)
52 int asciiValue = input[i].ToAscii();
66 for (
int i, len = input.Length(); i < len; i++)
68 int asciiValue = input[i].ToAscii();
82 for (
int i, len = input.Length(); i < len; i++)
84 int asciiValue = input[i].ToAscii();
99 static int CountOccurrences(
string haystack,
string needle,
bool caseInsensitive =
false)
101 if (needle.IsEmpty() || haystack.IsEmpty())
110 int needleLength = needle.Length();
111 int haystackLength = haystack.Length();
113 if (needleLength > haystackLength)
118 while (searchIndex < haystackLength)
120 int resultIndex = haystack.IndexOfFrom(searchIndex, needle);
125 searchIndex = resultIndex + needleLength;
136 static string Filter(
string input,
string characters,
bool useCharactersAsBlacklist =
false)
138 if (input.IsEmpty() || (!useCharactersAsBlacklist && characters.IsEmpty()))
142 for (
int i, length = input.Length(); i < length; i++)
144 string letter = input[i];
145 if (characters.Contains(letter) != useCharactersAsBlacklist)
155 static bool IsFormat(SCR_EStringFormat format,
string input)
159 case SCR_EStringFormat.ALPHABETICAL_UC:
return CheckCharacters(input,
false,
true,
false);
160 case SCR_EStringFormat.ALPHABETICAL_LC:
return CheckCharacters(input,
true,
false,
false);
161 case SCR_EStringFormat.ALPHABETICAL_I:
return CheckCharacters(input,
true,
true,
false);
162 case SCR_EStringFormat.ALPHANUMERICAL_UC:
return CheckCharacters(input,
false,
true,
true);
163 case SCR_EStringFormat.ALPHANUMERICAL_LC:
return CheckCharacters(input,
true,
false,
true);
164 case SCR_EStringFormat.ALPHANUMERICAL_I:
return CheckCharacters(input,
true,
true,
true);
165 case SCR_EStringFormat.DIGITS_ONLY:
return CheckCharacters(input,
false,
false,
true);
178 static bool CheckCharacters(
string input,
bool allowLC,
bool allowUC,
bool allowDigits,
bool allowUnderscore =
false)
183 for (
int i, len = input.Length(); i < len; i++)
185 int asciiValue = input[i].ToAscii();
187 (allowLC && asciiValue >=
MIN_LC && asciiValue <=
MAX_LC) ||
188 (allowUC && asciiValue >=
MIN_UC && asciiValue <=
MAX_UC) ||
190 (allowUnderscore && asciiValue == 95)
229 static string Ellipsis(
string input,
int maxLength)
234 int length = input.Length();
235 if (length <= maxLength)
238 return input.Substring(0, maxLength - 1) + ELLIPSIS;
246 static string Format(
string input, notnull array<string> arguments)
251 if (!input.Contains(
"%"))
254 switch (arguments.Count())
256 case 0:
return string.Format(input);
257 case 1:
return string.Format(input, arguments[0]);
258 case 2:
return string.Format(input, arguments[0], arguments[1]);
259 case 3:
return string.Format(input, arguments[0], arguments[1], arguments[2]);
260 case 4:
return string.Format(input, arguments[0], arguments[1], arguments[2], arguments[3]);
261 case 5:
return string.Format(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]);
262 case 6:
return string.Format(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
263 case 7:
return string.Format(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]);
264 case 8:
return string.Format(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7]);
268 return string.Format(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8]);
282 if (valueName.StartsWith(
"m_") || valueName.StartsWith(
"s_"))
284 int length = valueName.Length();
288 valueName = valueName.Substring(2, valueName.Length() - 2);
291 asciiValue = valueName[0].ToAscii();
295 valueName = valueName.Substring(1, valueName.Length() - 1);
298 if (valueName.IsEmpty())
302 array<string> pieces = {};
306 for (
int i, len = valueName.Length(); i < len; i++)
308 currChar = valueName[i];
310 asciiValue = currChar.ToAscii();
313 if (prevCharType < 2)
319 if (!piece.IsEmpty())
320 pieces.Insert(piece);
329 bool isNextCharUppercase;
332 int nextCharAsciiValue = valueName[i + 1].ToAscii();
333 isNextCharUppercase = nextCharAsciiValue >=
MIN_UC && nextCharAsciiValue <=
MAX_UC;
337 isNextCharUppercase =
true;
341 isNextCharUppercase &&
343 (asciiValue < MIN_LC || asciiValue >
MAX_LC))
349 if (!piece.IsEmpty())
350 pieces.Insert(piece);
359 if (prevCharType == 2)
365 if (!piece.IsEmpty())
366 pieces.Insert(piece);
375 if (!piece.IsEmpty())
377 pieces.Insert(piece);
378 piece =
string.Empty;
385 if (!piece.IsEmpty())
386 pieces.Insert(piece);
388 return Join(SPACE, pieces);
397 array<string> pieces = {};
399 return Join(SPACE, pieces);
408 array<string> pieces = {};
409 snakeCase.Split(UNDERSCORE, pieces,
true);
410 foreach (
int i,
string piece : pieces)
412 pieces[i] =
UCFirst(piece,
true);
415 return Join(SPACE, pieces);
425 array<float> result = {};
426 array<string> splits = {};
427 input.Split(splitter, splits,
true);
429 foreach (
string split : splits)
431 float value = split.ToFloat();
432 if (value != 0 || split.StartsWith(
"0"))
433 result.Insert(value);
446 array<int> result = {};
447 array<string> splits = {};
448 input.Split(splitter, splits,
true);
450 foreach (
string split : splits)
452 int value = split.ToInt();
453 if (value != 0 || split.StartsWith(
"0"))
454 result.Insert(value);
466 static array<string>
GetLines(
string input,
bool removeEmptyLines =
false,
bool trimLines =
false)
470 if (removeEmptyLines)
473 return {
string.Empty };
476 array<string> result = {};
477 input.Split(LINE_RETURN, result, removeEmptyLines);
481 for (
int i = result.Count() - 1; i >= 0; --i)
483 string line = result[i];
485 if (removeEmptyLines && !line)
486 result.RemoveOrdered(i);
501 static int IndexOf(
string input, notnull array<string> samples)
503 if (input.IsEmpty() || samples.IsEmpty())
506 int result =
int.MAX;
507 foreach (
string sample : samples)
509 int index = input.IndexOf(sample);
514 if (result ==
int.
MAX)
527 static int IndexOfFrom(
string input,
int start, notnull array<string> samples)
529 if (start < 0 || start > input.Length() || input.IsEmpty() || samples.IsEmpty())
532 int result =
int.MAX;
533 foreach (
string sample : samples)
535 int index = input.IndexOfFrom(start, sample);
540 if (result ==
int.
MAX)
553 if (!input || !search)
556 int inputLength = input.Length();
557 int searchLength = search.Length();
559 array<int> result = {};
560 for (
int i; i < inputLength; ++i)
562 int index = input.IndexOfFrom(i, search);
566 result.Insert(
index);
567 if (searchLength > 1)
568 i += searchLength - 1;
580 static string InsertAt(
string input,
string insertion,
int insertionIndex = 0)
582 if (input.IsEmpty() || insertion.IsEmpty() || insertionIndex < 0 || insertionIndex > input.Length())
585 if (insertionIndex == 0)
586 return insertion + input;
588 return input.Substring(0, insertionIndex) + insertion + input.Substring(insertionIndex, input.Length() - insertionIndex);
596 return input.Trim().IsEmpty();
608 if (input != input.Trim())
611 if (input.Length() < 2 || !input.StartsWith(
"#"))
620 string filter = LETTERS;
621 if (!filter.Contains(input[1]))
625 for (
int i, len = input.Length(); i < len; i++)
627 if (!filter.Contains(input[i]))
641 static string Join(
string separator, notnull array<string> pieces,
bool joinEmptyEntries =
true)
643 if (pieces.IsEmpty())
647 foreach (
int i,
string piece : pieces)
652 if (joinEmptyEntries || piece)
653 result += separator + piece;
665 static string Join(
string separator, notnull array<bool> pieces,
bool numerical =
false)
667 if (pieces.IsEmpty())
671 foreach (
int i,
bool piece : pieces)
674 result = piece.ToString(numerical);
676 result += separator + piece.ToString(numerical);
687 static string Join(
string separator, notnull array<int> pieces)
689 if (pieces.IsEmpty())
693 foreach (
int i,
int piece : pieces)
696 result = piece.ToString();
698 result += separator + piece;
709 static string Join(
string separator, notnull array<float> pieces)
711 if (pieces.IsEmpty())
715 foreach (
int i,
float piece : pieces)
718 result = piece.ToString();
720 result += separator + piece;
736 return word2.Length();
739 return word1.Length();
750 int word1Length = word1.Length();
751 int word2Length = word2.Length();
755 v0.Resize(word1Length + 1);
756 v1.Resize(word1Length + 1);
758 for (
int i = 1; i <= word1Length; ++i)
764 for (
int j = 1; j <= word2Length; ++j)
771 for (
int i = 1; i <= word1Length; ++i)
773 int cost = word1[i - 1] != word2[j - 1];
777 int tmp = v0[i - 1] + 1;
781 tmp = v1[i - 1] + cost;
790 int tmp = v1[i - 1] + 1;
794 tmp = v0[i - 1] + cost;
806 return v1[word1Length];
808 return v0[word1Length];
820 int word1Length = word1.Length();
821 int word2Length = word2.Length();
823 if (word1Length > word2Length)
839 return word2.Length();
842 return word1.Length();
853 int word1Length = word1.Length();
854 int word2Length = word2.Length();
857 array<ref array<int>> matrix = {};
858 matrix.Reserve(word1Length + 1);
859 for (
int i; i <= word1Length; ++i)
862 line.Resize(word2Length + 1);
866 for (
int i = 1; i <= word1Length; ++i)
868 for (
int j = 1; j <= word2Length; ++j)
870 string word1iMinus1 = word1[i - 1];
871 string word2jMinus1 = word2[j - 1];
872 int cost = word1iMinus1 != word2jMinus1;
876 int min = matrix[i - 1][j] + 1;
877 int tmp = matrix[i][j - 1] + 1;
881 tmp = matrix[i - 1][j - 1] + cost;
887 if (word1iMinus1 == word2[j - 2] && word1[i - 2] == word2jMinus1)
889 tmp = matrix[i - 2][j - 2] + cost;
899 return matrix[word1Length][word2Length];
911 int word1Length = word1.Length();
912 int word2Length = word2.Length();
914 if (word1Length > word2Length)
928 static string PadLeft(
string input,
int length,
string padding = SPACE)
933 if (input.Length() >= length)
936 int padW = padding.Length();
937 for (
int i = length - input.Length() - 1; i >= 0; i -= padW)
939 input = padding + input;
942 if (input.Length() > length)
943 input = input.Substring(input.Length() - length, length);
956 static string PadRight(
string input,
int length,
string padding = SPACE)
961 if (input.Length() >= length)
964 int padW = padding.Length();
965 for (
int i = length - input.Length() - 1; i >= 0; i -= padW)
970 if (input.Length() > length)
971 input = input.Substring(0, length);
990 if (!input || !sample || sample == replacement || replacement.Contains(sample))
993 while (input.IndexOf(sample) > -1)
995 input.Replace(sample, replacement);
1007 static string ReplaceMultiple(
string input, notnull array<string> samples,
string replacement)
1009 for (
int i = samples.Count() - 1; i >= 0; --i)
1011 if (replacement.Contains(samples[i]))
1015 if (samples.IsEmpty())
1018 foreach (
string sample : samples)
1020 input.Replace(sample, replacement);
1042 static string ReplaceTimes(
string input,
string sample,
string replacement,
int howMany = 1,
int skip = 0)
1044 if (howMany < 1 || input.IsEmpty() || sample.IsEmpty() || sample == replacement)
1047 int sampleLength = sample.Length();
1048 int replaceLength = replacement.Length();
1060 index += sampleLength;
1065 input = replacement + input.Substring(sampleLength, input.Length() - sampleLength);
1067 input = input.Substring(0,
index) + replacement + input.Substring(
index + sampleLength, input.Length() - (
index + sampleLength));
1070 index += replaceLength;
1088 for (
int i = input.Length() - 1; i >= 0; i--)
1111 if (!haystack || !needle)
1120 if (!needle.Contains(STAR))
1121 return needle == haystack;
1123 bool startsWithStar = needle.StartsWith(STAR);
1124 bool endsWithStar = needle.EndsWith(STAR);
1128 int length = needle.Length();
1132 needle = needle.Substring(1, length - 1);
1137 int length = needle.Length();
1141 needle = needle.Substring(0, length - 1);
1144 bool isMatch = !strictMatch || needle != haystack;
1148 if (!startsWithStar && endsWithStar)
1149 return haystack.StartsWith(needle);
1151 if (startsWithStar && endsWithStar)
1152 return (!strictMatch || (needle != haystack && !haystack.StartsWith(needle) && !haystack.EndsWith(needle))) && haystack.Contains(needle);
1154 if (startsWithStar && !endsWithStar)
1155 return haystack.EndsWith(needle);
1170 static string UCFirst(
string input,
bool lcRest =
false)
1178 for (
int i, length = input.Length(); i < length; ++i)
1180 string c = input[i];
1189 return c + input.Substring(1, length - 1);
1191 if (i == length - 1)
1192 return input.Substring(0, length - 1) + c;
1194 return input.Substring(0, i) + c + input.Substring(i + 1, length - i - 1);
1213 const int separatorsCount = 6;
1214 const string separators[separatorsCount] = {
" ",
"\n",
"\t",
"-",
":",
";" };
1216 array<string> pieces = {};
1217 for (
int i; i < separatorsCount; ++i)
1219 string separator = separators[i];
1220 if (!input.Contains(separator))
1223 input.Split(separator, pieces,
false);
1224 foreach (
int j,
string piece : pieces)
1226 pieces[j] =
UCFirst(piece,
false);
1242 if (input.IsEmpty())
1245 foreach (
string needle : needles)
1247 if (input.Contains(needle))
1261 if (input.IsEmpty())
1264 foreach (
string needle : needles)
1266 if (!input.Contains(needle))
1278 static bool ContainsOnly(
string input,
string characters,
bool useCharactersAsBlacklist =
false)
1280 int inputLength = input.Length();
1281 if (inputLength < 1)
1284 int charsLength = characters.Length();
1285 if (charsLength < 1)
1288 if (charsLength == 1)
1290 for (
int i; i < inputLength; ++i)
1292 if ((input[i] == characters) == useCharactersAsBlacklist)
1301 array<int> asciiValues = {};
1302 for (
int i; i < charsLength; ++i)
1304 asciiValues.Insert(characters[i].ToAscii());
1307 for (
int i; i < inputLength; i++)
1309 if (asciiValues.Contains(input[i].ToAscii()) == useCharactersAsBlacklist)
1323 if (input.IsEmpty())
1326 foreach (
string lineStart : lineStarts)
1328 if (input.StartsWith(lineStart))
1342 if (input.IsEmpty())
1345 foreach (
string lineEnd : lineEnds)
1347 if (input.EndsWith(lineEnd))
1362 if (input.IsEmpty())
1363 return string.Empty;
1365 for (
int i, count = input.Length(); i < count; i++)
1367 string character = input[i];
1368 if (character == SPACE || character == TAB || character == LINE_RETURN)
1371 return input.Substring(i, count - i);
1374 return string.Empty;
1384 if (input.IsEmpty())
1385 return string.Empty;
1387 for (
int i = input.Length() - 1; i >= 0; i--)
1389 string character = input[i];
1390 if (character == SPACE || character == TAB || character == LINE_RETURN)
1393 return input.Substring(0, i + 1);
1396 return string.Empty;
1400enum SCR_EStringFormat
ResourceName resourceName
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
class SCR_StringHelper ALPHABETICAL_LC
[a-z]+ (LC = LowerCase)
class SCR_StringHelper ALPHANUMERICAL_LC
[a-z0-9]+ (LC = LowerCase)
class SCR_StringHelper ALPHABETICAL_I
[a-zA-Z]+ (I = Insensitive)
class SCR_StringHelper ALPHANUMERICAL_UC
[A-Z0-9]+ (UC = UpperCase)
class SCR_StringHelper ALPHANUMERICAL_I
[a-zA-Z0-9]+ (I = Insensitive)
class SCR_StringHelper DIGITS_ONLY
[0-9]+
class SCR_StringHelper ALPHABETICAL_UC
[A-Z]+ (UC = UpperCase)
static string FormatSnakeCaseToUserFriendly(string snakeCase)
static string FormatValueNameToUserFriendly(string valueName)
static int GetLevenshteinDistance(string word1, string word2, bool caseSensitive=true)
static string Filter(string input, string characters, bool useCharactersAsBlacklist=false)
static array< string > GetLines(string input, bool removeEmptyLines=false, bool trimLines=false)
static bool IsTranslationKey(string input)
static const int MAX_DIGIT
static string TrimLeft(string input)
static bool ContainsLowercase(string input)
static string PadLeft(string input, int length, string padding=SPACE)
static bool EndsWithAny(string input, notnull array< string > lineEnds)
static string Format(string input, notnull array< string > arguments)
static string UCFirst(string input, bool lcRest=false)
static bool ContainsUppercase(string input)
static bool ContainsOnly(string input, string characters, bool useCharactersAsBlacklist=false)
static string ReplaceRecursive(string input, string sample, string replacement)
static int CountOccurrences(string haystack, string needle, bool caseInsensitive=false)
static string Join(string separator, notnull array< float > pieces)
static bool CheckCharacters(string input, bool allowLC, bool allowUC, bool allowDigits, bool allowUnderscore=false)
static int IndexOfFrom(string input, int start, notnull array< string > samples)
static string UCFirstAll(string input, bool lcRest=false)
static int IndexOf(string input, notnull array< string > samples)
static bool StartsWithAny(string input, notnull array< string > lineStarts)
static bool ContainsAny(string input, notnull array< string > needles)
static array< int > GetIntsFromString(string input, string splitter=SPACE)
static int GetDamerauLevenshteinDistance(string word1, string word2, bool caseSensitive=true)
static string ReplaceTimes(string input, string sample, string replacement, int howMany=1, int skip=0)
static bool ContainsEvery(string input, notnull array< string > needles)
static string Join(string separator, notnull array< bool > pieces, bool numerical=false)
static const int MIN_DIGIT
static const string TRANSLATION_KEY_CHARS
static string PadRight(string input, int length, string padding=SPACE)
static string Join(string separator, notnull array< int > pieces)
static float GetLevenshteinDistanceScore(string word1, string word2, bool caseSensitive=true)
static string Ellipsis(string input, int maxLength)
static string Reverse(string input)
static bool SimpleStarSearchMatches(string haystack, string needle, bool caseSensitive, bool strictMatch)
static bool IsEmptyOrWhiteSpace(string input)
array< int > IndicesOf(string input, string search)
static bool IsFormat(SCR_EStringFormat format, string input)
static array< float > GetFloatsFromString(string input, string splitter=SPACE)
static string TrimRight(string input)
static bool ContainsDigit(string input)
static string FormatResourceNameToUserFriendly(ResourceName resourceName)
static string Join(string separator, notnull array< string > pieces, bool joinEmptyEntries=true)
static float GetDamerauLevenshteinDistanceScore(string word1, string word2, bool caseSensitive=true)
static string InsertAt(string input, string insertion, int insertionIndex=0)
static string ReplaceMultiple(string input, notnull array< string > samples, string replacement)