Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_StringHelper.c
Go to the documentation of this file.
2 {
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;
14 
15  //------------------------------------------------------------------------------------------------
19  static bool ContainsDigit(string input)
20  {
21  for (int i, len = input.Length(); i < len; i++)
22  {
23  int asciiValue = input[i].ToAscii();
24  if (asciiValue >= 48 && asciiValue <= 57)
25  return true;
26  }
27 
28  return false;
29  }
30 
31  //------------------------------------------------------------------------------------------------
35  static bool ContainsUppercase(string input)
36  {
37  for (int i, len = input.Length(); i < len; i++)
38  {
39  int asciiValue = input[i].ToAscii();
40  if (asciiValue >= 65 && asciiValue <= 90)
41  return true;
42  }
43 
44  return false;
45  }
46 
47  //------------------------------------------------------------------------------------------------
51  static bool ContainsLowercase(string input)
52  {
53  for (int i, len = input.Length(); i < len; i++)
54  {
55  int asciiValue = input[i].ToAscii();
56  if (asciiValue >= 97 && asciiValue <= 122)
57  return true;
58  }
59 
60  return false;
61  }
62 
63  //------------------------------------------------------------------------------------------------
70  static int CountOccurrences(string haystack, string needle, bool caseInsensitive = false)
71  {
72  if (needle.IsEmpty() || haystack.IsEmpty())
73  return 0;
74 
75  if (caseInsensitive)
76  {
77  needle.ToLower();
78  haystack.ToLower();
79  }
80 
81  int needleLength = needle.Length();
82  int haystackLength = haystack.Length();
83 
84  if (needleLength > haystackLength)
85  return 0;
86 
87  int result;
88  int searchIndex;
89  while (searchIndex < haystackLength)
90  {
91  int resultIndex = haystack.IndexOfFrom(searchIndex, needle);
92  if (resultIndex < 0)
93  break;
94 
95  result++;
96  searchIndex = resultIndex + needleLength;
97  }
98 
99  return result;
100  }
101 
102 // //------------------------------------------------------------------------------------------------
103 // //! Cut a line if bigger than the wanted length, using #AR-String_Ellipsis
104 // //! \param ellipsisTranslation translation key or ellipsis string; use %1 in it to place the cut text, if %1 is not present default value will be used
105 // static string Ellipsis(string input, int length, string ellipsisTranslation = "#AR-String_Ellipsis")
106 // {
107 // if (input.Length() < length)
108 // return input;
109 //
110 // if (ellipsisTranslation != "#AR-String_Ellipsis" && !ellipsisTranslation.Contains("%1") && !Translate(ellipsisTranslation).Contains("%1"))
111 // ellipsisTranslation = "#AR-String_Ellipsis";
112 //
113 // return String.Format(ellipsisTranslation, input.Substring(0, length));
114 // }
115 
116  //------------------------------------------------------------------------------------------------
121  static string Filter(string input, string characters, bool useCharactersAsBlacklist = false)
122  {
123  if (input.IsEmpty() || (!useCharactersAsBlacklist && characters.IsEmpty()))
124  return string.Empty;
125 
126  string result;
127  for (int i, length = input.Length(); i < length; i++)
128  {
129  string letter = input[i];
130  if (characters.Contains(letter) != useCharactersAsBlacklist)
131  result += letter;
132  }
133  return result;
134  }
135 
136  //------------------------------------------------------------------------------------------------
139  static bool IsFormat(SCR_EStringFormat format, string input)
140  {
141  switch (format)
142  {
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);
150  }
151  return false;
152  }
153 
154  //------------------------------------------------------------------------------------------------
161  static bool CheckCharacters(string input, bool allowLC, bool allowUC, bool allowDigits, bool allowUnderscore = false)
162  {
163  if (input.IsEmpty())
164  return false;
165 
166  for (int i, len = input.Length(); i < len; i++)
167  {
168  int asciiValue = input[i].ToAscii();
169  if (!(
170  (allowLC && asciiValue >= 97 && asciiValue <= 122) ||
171  (allowUC && asciiValue >= 65 && asciiValue <= 90) ||
172  (allowDigits && asciiValue >= 48 && asciiValue <= 57) ||
173  (allowUnderscore && asciiValue == 95)
174  ))
175  return false;
176  }
177 
178  return true;
179  }
180 
181 // //------------------------------------------------------------------------------------------------
182 // //! old method, 4-5× slower but allows for non-ASCII values, not useful for now
183 // protected static bool CheckCharactersOld(string input, bool allowLC, bool allowUC, bool allowDigits, bool allowUnderscore)
184 // {
185 // string filter;
186 // if (allowLC)
187 // filter += LOWERCASE;
188 // if (allowUC)
189 // filter += UPPERCASE;
190 // if (allowDigits)
191 // filter += DIGITS;
192 // if (allowUnderscore)
193 // filter += UNDERSCORE;
194 //
195 // if (filter.IsEmpty())
196 // return false;
197 //
198 // for (int i, len = input.Length(); i < len; i++)
199 // {
200 // if (!filter.Contains(input[i]))
201 // return false;
202 // }
203 //
204 // return true;
205 // }
206 
207  //------------------------------------------------------------------------------------------------
212  static string Format(string input, notnull array<string> arguments)
213  {
214  if (input.IsEmpty())
215  return string.Empty;
216 
217  if (!input.Contains("%"))
218  return input;
219 
220  switch (arguments.Count())
221  {
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]);
231  }
232 
233  // 9 and more
234  return string.Format(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8]);
235  }
236 
237  //------------------------------------------------------------------------------------------------
240  static array<int> GetIntsFromString(string input)
241  {
242  array<int> result = {};
243  array<string> splits = {};
244  input.Split(SPACE, splits, true);
245 
246  foreach (string split : splits)
247  {
248  int value = split.ToInt();
249  if (value != 0 || split == "0")
250  result.Insert(value);
251  }
252  return result;
253  }
254 
255  //------------------------------------------------------------------------------------------------
258  static int IndexOf(string input, notnull array<string> samples)
259  {
260  if (input.IsEmpty() || samples.IsEmpty())
261  return -1;
262 
263  int result = int.MAX;
264  foreach (string sample : samples)
265  {
266  int index = input.IndexOf(sample);
267  if (index != -1 && index < result)
268  result = index;
269  }
270 
271  if (result == int.MAX)
272  return -1;
273 
274  return result;
275  }
276 
277  //------------------------------------------------------------------------------------------------
280  static int IndexOfFrom(string input, int start, notnull array<string> samples)
281  {
282  if (start < 0 || start > input.Length() || input.IsEmpty() || samples.IsEmpty())
283  return -1;
284 
285  int result = int.MAX;
286  foreach (string sample : samples)
287  {
288  int index = input.IndexOfFrom(start, sample);
289  if (index != -1 && index < result)
290  result = index;
291  }
292 
293  if (result == int.MAX)
294  return -1;
295 
296  return result;
297  }
298 
299  //------------------------------------------------------------------------------------------------
302  static array<float> GetFloatsFromString(string input)
303  {
304  array<float> result = {};
305  array<string> splits = {};
306  input.Split(SPACE, splits, true);
307 
308  foreach (string split : splits)
309  {
310  float value = split.ToFloat();
311  if (value != 0 || split.StartsWith("0"))
312  result.Insert(value);
313  }
314 
315  return result;
316  }
317 
318  //------------------------------------------------------------------------------------------------
323  static array<string> GetLines(string input, bool removeEmptyLines = false, bool trimLines = false)
324  {
325  if (!input)
326  {
327  if (removeEmptyLines)
328  return {};
329  else
330  return { string.Empty };
331  }
332 
333  array<string> result = {};
334  input.Split("\n", result, removeEmptyLines);
335 
336  if (trimLines)
337  {
338  string line;
339  for (int i = result.Count() - 1; i >= 0; --i)
340  {
341  line = result[i];
342  line.TrimInPlace();
343  if (removeEmptyLines && !line)
344  result.RemoveOrdered(i);
345  else
346  result[i] = line;
347  }
348  }
349 
350  return result;
351  }
352 
353  //------------------------------------------------------------------------------------------------
359  static string InsertAt(string input, string insertion, int insertionIndex = 0)
360  {
361  if (input.IsEmpty() || insertion.IsEmpty() || insertionIndex < 0 || insertionIndex > input.Length())
362  return input;
363 
364  if (insertionIndex == 0)
365  return insertion + input;
366 
367  return input.Substring(0, insertionIndex) + insertion + input.Substring(insertionIndex, input.Length() - insertionIndex);
368  }
369 
370  //------------------------------------------------------------------------------------------------
372  static bool IsEmptyOrWhiteSpace(string input)
373  {
374  return input.Trim().IsEmpty();
375  }
376 
377  //------------------------------------------------------------------------------------------------
381  static bool IsTranslationKey(string input)
382  {
383  if (IsEmptyOrWhiteSpace(input))
384  return false;
385 
386  if (input != input.Trim())
387  return false;
388 
389  if (input.Length() < 2 || !input.StartsWith("#"))
390  return false;
391 
392  for (int i, count = TRANSLATION_KEY_CHARS.Length(); i < count; i++)
393  {
394  if (input.EndsWith(TRANSLATION_KEY_CHARS[i]))
395  return false;
396  }
397 
398  string filter = LETTERS;
399  if (!filter.Contains(input[1])) // \#[a-zA-Z].*
400  return false;
401 
402  filter += DIGITS + TRANSLATION_KEY_CHARS;
403  for (int i, len = input.Length(); i < len; i++)
404  {
405  if (!filter.Contains(input[i]))
406  return false;
407  }
408 
409  return true;
410  }
411 
412  //------------------------------------------------------------------------------------------------
418  static string Join(string separator, notnull array<string> pieces, bool joinEmptyEntries = true)
419  {
420  if (pieces.IsEmpty())
421  return string.Empty;
422 
423  string result;
424  foreach (int i, string piece : pieces)
425  {
426  if (i == 0)
427  result = piece;
428  else
429  if (joinEmptyEntries || piece) // !piece.IsEmpty()'s fast version
430  result += separator + piece;
431  }
432  return result;
433  }
434 
435  //------------------------------------------------------------------------------------------------
443  static string PadLeft(string input, int length, string padding = SPACE)
444  {
445  if (!padding)
446  return input;
447 
448  if (input.Length() >= length)
449  return input;
450 
451  int padW = padding.Length();
452  for (int i = length - input.Length() - 1; i >= 0; i -= padW)
453  {
454  input = padding + input;
455  }
456 
457  if (input.Length() > length)
458  input = input.Substring(input.Length() - length, length);
459 
460  return input;
461  }
462 
463  //------------------------------------------------------------------------------------------------
471  static string PadRight(string input, int length, string padding = SPACE)
472  {
473  if (!padding)
474  return input;
475 
476  if (input.Length() >= length)
477  return input;
478 
479  int padW = padding.Length();
480  for (int i = length - input.Length() - 1; i >= 0; i -= padW)
481  {
482  input += padding;
483  }
484 
485  if (input.Length() > length)
486  input = input.Substring(0, length);
487 
488  return input;
489  }
490 
491  //------------------------------------------------------------------------------------------------
503  static string ReplaceRecursive(string input, string sample, string replace)
504  {
505  if (input.IsEmpty() || sample.IsEmpty() || sample == replace || replace.Contains(sample))
506  return input;
507 
508  while (input.IndexOf(sample) > -1)
509  {
510  input.Replace(sample, replace);
511  }
512 
513  return input;
514  }
515 
516  //------------------------------------------------------------------------------------------------
532  static string ReplaceTimes(string input, string sample, string replace, int howMany = 1, int skip = 0)
533  {
534  if (howMany < 1 || input.IsEmpty() || sample.IsEmpty() || sample == replace)
535  return input;
536 
537  int sampleLength = sample.Length();
538  int replaceLength = replace.Length();
539 
540  int index;
541  while (howMany > 0)
542  {
543  index = input.IndexOfFrom(index, sample);
544  if (index < 0)
545  break;
546 
547  if (skip > 0)
548  {
549  skip--;
550  index += sampleLength;
551  continue;
552  }
553 
554  if (index == 0)
555  input = replace + input.Substring(sampleLength, input.Length() - sampleLength);
556  else
557  input = input.Substring(0, index) + replace + input.Substring(index + sampleLength, input.Length() - (index + sampleLength));
558 
559  // no overlap
560  index += replaceLength;
561 
562  howMany--;
563  }
564 
565  return input;
566  }
567 
568  //------------------------------------------------------------------------------------------------
574  static string Reverse(string input)
575  {
576  string result;
577  for (int i = input.Length() - 1; i >= 0; i--)
578  {
579  result += input[i];
580  }
581  return result;
582  }
583 
584  //------------------------------------------------------------------------------------------------
586  static bool ContainsAny(string input, notnull array<string> needles)
587  {
588  if (input.IsEmpty())
589  return false;
590 
591  foreach (string needle : needles)
592  {
593  if (input.Contains(needle))
594  return true;
595  }
596 
597  return false;
598  }
599 
600  //------------------------------------------------------------------------------------------------
602  static bool ContainsEvery(string input, notnull array<string> needles)
603  {
604  if (input.IsEmpty())
605  return false;
606 
607  foreach (string needle : needles)
608  {
609  if (!input.Contains(needle))
610  return false;
611  }
612 
613  return true;
614  }
615 
616  //------------------------------------------------------------------------------------------------
621  static bool StartsWithAny(string input, notnull array<string> lineStarts)
622  {
623  if (input.IsEmpty())
624  return false;
625 
626  foreach (string lineStart : lineStarts)
627  {
628  if (input.StartsWith(lineStart))
629  return true;
630  }
631 
632  return false;
633  }
634 
635  //------------------------------------------------------------------------------------------------
640  static bool EndsWithAny(string input, notnull array<string> lineEnds)
641  {
642  if (input.IsEmpty())
643  return false;
644 
645  foreach (string lineEnd : lineEnds)
646  {
647  if (input.EndsWith(lineEnd))
648  return true;
649  }
650 
651  return false;
652  }
653 
654  //------------------------------------------------------------------------------------------------
659  static string Translate(
660  string input,
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)
670  {
671  return WidgetManager.Translate(input, param1, param2, param3, param4, param5, param6, param7, param8, param9);
672  }
673 
674  //------------------------------------------------------------------------------------------------
679  static string Translate(string input, notnull array<string> arguments)
680  {
681  if (input.IsEmpty())
682  return string.Empty;
683 
684  if (!input.Contains("%"))
685  return input;
686 
687  switch (arguments.Count())
688  {
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]);
698  }
699 
700  // 9 and more
701  return WidgetManager.Translate(input, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8]);
702  }
703 
704  //------------------------------------------------------------------------------------------------
709  static string TrimLeft(string input)
710  {
711  if (input.IsEmpty())
712  return string.Empty;
713 
714  for (int i, count = input.Length(); i < count; i++)
715  {
716  string character = input[i];
717  if (character == SPACE || character == TAB || character == LINE_RETURN)
718  continue;
719 
720  return input.Substring(i, count - i);
721  }
722 
723  return string.Empty;
724  }
725 
726  //------------------------------------------------------------------------------------------------
731  static string TrimRight(string input)
732  {
733  if (input.IsEmpty())
734  return string.Empty;
735 
736  for (int i = input.Length() - 1; i >= 0; i--)
737  {
738  string character = input[i];
739  if (character == SPACE || character == TAB || character == LINE_RETURN)
740  continue;
741 
742  return input.Substring(0, i + 1);
743  }
744 
745  return string.Empty;
746  }
747 }
748 
749 enum SCR_EStringFormat
750 {
757  DIGITS_ONLY,
758  // FLOATING_POINT, //!< [0-9][0-9\.]+[0-9]
759 }
ALPHABETICAL_UC
class SCR_StringHelper ALPHABETICAL_UC
[A-Z]+ (UC = UpperCase)
ALPHANUMERICAL_I
class SCR_StringHelper ALPHANUMERICAL_I
[a-zA-Z0-9]+ (I = Insensitive)
DIGITS_ONLY
class SCR_StringHelper DIGITS_ONLY
[0-9]+
SCR_StringHelper
Definition: SCR_StringHelper.c:1
ALPHANUMERICAL_LC
class SCR_StringHelper ALPHANUMERICAL_LC
[a-z0-9]+ (LC = LowerCase)
ALPHABETICAL_I
class SCR_StringHelper ALPHABETICAL_I
[a-zA-Z]+ (I = Insensitive)
ALPHABETICAL_LC
class SCR_StringHelper ALPHABETICAL_LC
[a-z]+ (LC = LowerCase)
ALPHANUMERICAL_UC
class SCR_StringHelper ALPHANUMERICAL_UC
[A-Z0-9]+ (UC = UpperCase)
index
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Definition: SCR_DestructionSynchronizationComponent.c:17