From cec65cceb098ee01d90f598ad771ab42591dd759 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Mon, 5 Jun 2023 15:57:49 -0400 Subject: [PATCH] bring in advanced string splitting functions from Universal Editor --- MBS.Framework/StringExtensions.cs | 274 ++++++++++++++++++++++++++++++ 1 file changed, 274 insertions(+) diff --git a/MBS.Framework/StringExtensions.cs b/MBS.Framework/StringExtensions.cs index f83930d..ef9e2c2 100755 --- a/MBS.Framework/StringExtensions.cs +++ b/MBS.Framework/StringExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Text; namespace MBS.Framework { @@ -114,5 +115,278 @@ namespace MBS.Framework output = default(T); return false; } + + public static bool IsAllUpperCase(this string str) + { + return (!(new System.Text.RegularExpressions.Regex("[a-z]")).IsMatch(str)); + } + public static string UrlEncode(this string value) + { + return value; + } + public static string UrlDecode(this string input) + { + StringBuilder output = new StringBuilder(); + for (int i = 0; i < input.Length; i++) + { + if (input[i] == '%') + { + char c = input[i + 1]; + string arg_45_0 = c.ToString(); + c = input[i + 2]; + string numeric = arg_45_0 + c.ToString(); + int hexcode = int.Parse(numeric, System.Globalization.NumberStyles.HexNumber); + i += 2; + output.Append((char)hexcode); + } + else + { + output.Append(input[i]); + } + } + return output.ToString(); + } + public static bool Match(this string input, params string[] filters) + { + foreach (string filter in filters) + { + if (input.Match(filter)) return true; + } + return false; + } + public static bool Match(this string input, string filter) + { + if (filter == null) + return false; + + string wildcardToRegex = "^" + System.Text.RegularExpressions.Regex.Escape(filter).Replace("\\*", ".*").Replace("\\?", ".") + "$"; + return new System.Text.RegularExpressions.Regex(wildcardToRegex).IsMatch(input); + } + + /// + /// Removes all characters past and including the first occurrence of a + /// NUL (0x00) byte from the given string. + /// + /// + /// All characters before the first occurrence of a NUL (0x00) byte in + /// . + /// + /// The string to trim. + public static string TrimNull(this string value) + { + int i = value.IndexOf('\0'); + if (i > -1) return value.Substring(0, i); + return value; + } + + public static string Format(this string input, Dictionary formatWhat) + { + return Format(input, formatWhat, "$(", ")"); + } + public static string Format(this string input, Dictionary formatWhat, string formatBegin, string formatEnd) + { + string val = input; + foreach (KeyValuePair kvp in formatWhat) + { + val = val.Replace(formatBegin + kvp.Key + formatEnd, kvp.Value); + } + return val; + } + /// + /// Inserts the specified value "count" times, with "spacing" characters between. + /// + /// The number of times to insert value. + /// The amount of characters to leave between insertions. + /// The value to insert. + /// + public static string Insert(this string input, int count, int spacing, string value) + { + int j = 0; + string retval = String.Empty; + for (int i = 0; i < count; i++) + { + retval += input.Substring(j, spacing) + value; + j += spacing; + } + retval += input.Substring(j); + return retval; + } + + + public static T[] Split(this string value, params char[] separator) + { + return value.Split(separator, -1, StringSplitOptions.None); + } + public static T[] Split(this string value, char[] separator, int count) + { + return value.Split(separator, count, StringSplitOptions.None); + } + public static T[] Split(this string value, char[] separator, int count, StringSplitOptions options) + { + string[] separators = new string[separator.Length]; + for (int i = 0; i < separator.Length; i++) + { + separators[i] = separator[i].ToString(); + } + return value.Split(separators, count, options); + } + public static T[] Split(this string value, params string[] separator) + { + return value.Split(separator, -1, StringSplitOptions.None); + } + public static T[] Split(this string value, string[] separator, int count) + { + return value.Split(separator, count, StringSplitOptions.None); + } + public static T[] Split(this string value, string[] separator, int count, StringSplitOptions options) + { + string[] splitt = null; + if (count < 0) + { + splitt = value.Split(separator, options); + } + else + { + splitt = value.Split(separator, count, options); + } + T[] values = new T[splitt.Length]; + for (int i = 0; i < splitt.Length; i++) + { + if (!string.IsNullOrEmpty(splitt[i])) + { + values[i] = (T)Convert.ChangeType(splitt[i], typeof(T)); + } + } + return values; + } + public static string[] Split(this string value, string separator) + { + return value.Split(new string[] { separator }); + } + public static string[] Split(this string value, string[] separator) + { + return value.Split(separator, StringSplitOptions.None); + } + public static string[] Split(this string value, string[] separator, string ignore) + { + return value.Split(separator, ignore, ignore); + } + public static string[] Split(this string value, string[] separator, string ignoreBegin, string ignoreEnd) + { + return value.Split(separator, ignoreBegin, ignoreEnd, StringSplitOptions.None, -1); + } + public static string[] Split(this string value, string[] separator, StringSplitOptions options, int count, string ignore) + { + return value.Split(separator, ignore, ignore, options, count); + } + public static string[] Split(this string value, char[] separator, string ignore) + { + return value.Split(separator, ignore, ignore); + } + public static string[] Split(this string value, char[] separator, string ignoreBegin, string ignoreEnd) + { + return value.Split(separator, ignoreBegin, ignoreEnd, StringSplitOptions.None, -1); + } + public static string[] Split(this string value, char[] separator, string ignore, StringSplitOptions options, int count) + { + return value.Split(separator, ignore, ignore, options, count); + } + public static string[] Split(this string value, char[] separator, string ignoreBegin, string ignoreEnd, StringSplitOptions options, int count) + { + List entries = new List(); + for (int i = 0; i < separator.Length; i++) + { + char sep = separator[i]; + entries.Add(sep.ToString()); + } + return value.Split(entries.ToArray(), ignoreBegin, ignoreEnd, options, count); + } + public static string[] Split(this string value, string[] separator, string ignoreBegin, string ignoreEnd, StringSplitOptions options, int count) + { + return value.Split(separator, ignoreBegin, ignoreEnd, options, count, true); + } + public static string[] Split(this string value, string[] separator, string ignoreBegin, string ignoreEnd, StringSplitOptions options, int count, bool discardIgnoreString) + { + List entries = new List(); + bool ignoring = false; + bool continueOutside = false; + string next = string.Empty; + int i = 0; + while (i < value.Length) + { + if (i + ignoreBegin.Length > value.Length) + { + goto IL_70; + } + if (ignoring || !(value.Substring(i, ignoreBegin.Length) == ignoreBegin)) + { + goto IL_70; + } + ignoring = true; + if (!discardIgnoreString) + { + next += ignoreBegin; + } + IL_16F: + i++; + continue; + IL_70: + if (i + ignoreEnd.Length <= value.Length) + { + if (ignoring && value.Substring(i, ignoreEnd.Length) == ignoreEnd) + { + ignoring = false; + if (!discardIgnoreString) + { + next += ignoreEnd; + } + goto IL_16F; + } + } + if (!ignoring) + { + int j = 0; + while (j < separator.Length) + { + if (i + separator[j].Length <= value.Length) + { + if (value.Substring(i, separator[j].Length) == separator[j]) + { + if (count > -1 && (entries.Count >= count - 1)) + { + next = value.Substring(i - next.Length); + entries.Add(next); + i = value.Length - 1; + break; + } + else + { + entries.Add(next); + next = string.Empty; + i += separator[j].Length - 1; + continueOutside = true; + } + } + } + + j++; + continue; + } + } + if (continueOutside) + { + continueOutside = false; + goto IL_16F; + } + next += value[i]; + goto IL_16F; + } + if (!string.IsNullOrEmpty(next)) + { + entries.Add(next); + next = null; + } + return entries.ToArray(); + } } }