bring in advanced string splitting functions from Universal Editor
This commit is contained in:
parent
58b272b873
commit
cec65cceb0
@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes all characters past and including the first occurrence of a
|
||||
/// NUL (0x00) byte from the given string.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// All characters before the first occurrence of a NUL (0x00) byte in
|
||||
/// <paramref name="value" />.
|
||||
/// </returns>
|
||||
/// <param name="value">The string to trim.</param>
|
||||
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<string, string> formatWhat)
|
||||
{
|
||||
return Format(input, formatWhat, "$(", ")");
|
||||
}
|
||||
public static string Format(this string input, Dictionary<string, string> formatWhat, string formatBegin, string formatEnd)
|
||||
{
|
||||
string val = input;
|
||||
foreach (KeyValuePair<string, string> kvp in formatWhat)
|
||||
{
|
||||
val = val.Replace(formatBegin + kvp.Key + formatEnd, kvp.Value);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
/// <summary>
|
||||
/// Inserts the specified value "count" times, with "spacing" characters between.
|
||||
/// </summary>
|
||||
/// <param name="count">The number of times to insert value.</param>
|
||||
/// <param name="spacing">The amount of characters to leave between insertions.</param>
|
||||
/// <param name="value">The value to insert.</param>
|
||||
/// <returns></returns>
|
||||
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<T>(this string value, params char[] separator)
|
||||
{
|
||||
return value.Split<T>(separator, -1, StringSplitOptions.None);
|
||||
}
|
||||
public static T[] Split<T>(this string value, char[] separator, int count)
|
||||
{
|
||||
return value.Split<T>(separator, count, StringSplitOptions.None);
|
||||
}
|
||||
public static T[] Split<T>(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<T>(separators, count, options);
|
||||
}
|
||||
public static T[] Split<T>(this string value, params string[] separator)
|
||||
{
|
||||
return value.Split<T>(separator, -1, StringSplitOptions.None);
|
||||
}
|
||||
public static T[] Split<T>(this string value, string[] separator, int count)
|
||||
{
|
||||
return value.Split<T>(separator, count, StringSplitOptions.None);
|
||||
}
|
||||
public static T[] Split<T>(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<string> entries = new List<string>();
|
||||
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<string> entries = new List<string>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user