using System; using System.Text.RegularExpressions; namespace UnityEditor.Rendering { /// /// Set of utility functions with /// public static class StringExtensions { private static Regex k_InvalidRegEx = new (string.Format(@"([{0}]*\.+$)|([{0}]+)", Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars()))), RegexOptions.Compiled); /// /// Replaces invalid characters for a filename or a directory with a given optional replacemenet string /// /// The input filename or directory /// The replacement /// The string with the invalid characters replaced public static string ReplaceInvalidFileNameCharacters(this string input, string replacement = "_") => k_InvalidRegEx.Replace(input, replacement); /// /// Checks if the given string ends with the given extension /// /// The input string /// The extension /// True if the extension is found on the string path public static bool HasExtension(this string input, string extension) => input.EndsWith(extension, StringComparison.OrdinalIgnoreCase); /// /// Checks if a string contains any of the strings given in strings to check and early out if it does /// /// The input string /// List of strings to check /// True if the input contains any of the strings from stringsToCheck; otherwise, false. public static bool ContainsAny(this string input, params string[] stringsToCheck) { if(string.IsNullOrEmpty(input)) return false; foreach (var value in stringsToCheck) { if(string.IsNullOrEmpty(value)) continue; if (input.Contains(value)) return true; } return false; } } }