A game about forced loneliness, made by TACStudios
1using System;
2using System.Text.RegularExpressions;
3
4namespace UnityEditor.Rendering
5{
6 /// <summary>
7 /// Set of utility functions with <see cref="string"/>
8 /// </summary>
9 public static class StringExtensions
10 {
11 private static Regex k_InvalidRegEx = new (string.Format(@"([{0}]*\.+$)|([{0}]+)", Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars()))), RegexOptions.Compiled);
12
13 /// <summary>
14 /// Replaces invalid characters for a filename or a directory with a given optional replacemenet string
15 /// </summary>
16 /// <param name="input">The input filename or directory</param>
17 /// <param name="replacement">The replacement</param>
18 /// <returns>The string with the invalid characters replaced</returns>
19 public static string ReplaceInvalidFileNameCharacters(this string input, string replacement = "_") => k_InvalidRegEx.Replace(input, replacement);
20
21 /// <summary>
22 /// Checks if the given string ends with the given extension
23 /// </summary>
24 /// <param name="input">The input string</param>
25 /// <param name="extension">The extension</param>
26 /// <returns>True if the extension is found on the string path</returns>
27 public static bool HasExtension(this string input, string extension) =>
28 input.EndsWith(extension, StringComparison.OrdinalIgnoreCase);
29
30
31 /// <summary>
32 /// Checks if a string contains any of the strings given in strings to check and early out if it does
33 /// </summary>
34 /// <param name="input">The input string</param>
35 /// <param name="stringsToCheck">List of strings to check</param>
36 /// <returns>True if the input contains any of the strings from stringsToCheck; otherwise, false.</returns>
37 public static bool ContainsAny(this string input, params string[] stringsToCheck)
38 {
39 if(string.IsNullOrEmpty(input))
40 return false;
41
42 foreach (var value in stringsToCheck)
43 {
44 if(string.IsNullOrEmpty(value))
45 continue;
46
47 if (input.Contains(value))
48 return true;
49 }
50
51 return false;
52 }
53 }
54}