130 lines
4.8 KiB
C#
130 lines
4.8 KiB
C#
|
|
using System.Text;
|
|||
|
|
using System;
|
|||
|
|
|
|||
|
|
namespace DTS.Common.Utilities
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// encapsulates converting array or IEnumerables to a string
|
|||
|
|
/// </summary>
|
|||
|
|
public static class ArrayToString
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// separator between elements
|
|||
|
|
/// </summary>
|
|||
|
|
/// <remarks>This will change the separator for all callers</remarks>
|
|||
|
|
public static string Separater { get; set; } = ", ";
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// separator between elements with no space
|
|||
|
|
/// </summary>
|
|||
|
|
/// <remarks>This will change the separator for all callers</remarks>
|
|||
|
|
public static string SeparaterNoSpace { get; set; } = ",";
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// symbol for the start of a group
|
|||
|
|
/// </summary>
|
|||
|
|
/// <remarks>this will change the symbol for all callers</remarks>
|
|||
|
|
public static string LeftGroup { get; set; } = "(";
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// symbol for the end of a group
|
|||
|
|
/// </summary>
|
|||
|
|
/// <remarks>this will change the symbol for all callers</remarks>
|
|||
|
|
public static string RightGroup { get; set; } = ")";
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// separator character between elements
|
|||
|
|
/// </summary>
|
|||
|
|
public static char[] SeparaterChar { get; set; } = new char[1] { ',' };
|
|||
|
|
/// <summary>
|
|||
|
|
/// symbol for the opening paren
|
|||
|
|
/// </summary>
|
|||
|
|
public static char[] LeftGroupChar { get; set; } = new char[1] { '(' };
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// symbol for the closing paren
|
|||
|
|
/// </summary>
|
|||
|
|
public static char[] RightGroupChar { get; set; } = new char[1] { ')' };
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// constructs a string out of an array.
|
|||
|
|
/// IEnumerable classes are returned using .ToString()
|
|||
|
|
/// if object is null, a string with null in angle brackets is returned
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="arr">array to serialize to string</param>
|
|||
|
|
/// <param name="leftStr">symbol to put at the start of the string</param>
|
|||
|
|
/// <param name="sepStr">symbol to use between elements</param>
|
|||
|
|
/// <param name="rightStr">symbol to use at the end of the string</param>
|
|||
|
|
/// <returns>string representation of array</returns>
|
|||
|
|
public static string ArrayObjectToString(object arr, string leftStr, string sepStr, string rightStr)
|
|||
|
|
{
|
|||
|
|
if (arr == null)
|
|||
|
|
return "<null>";
|
|||
|
|
var enumerable = arr as System.Collections.IEnumerable;
|
|||
|
|
if (enumerable == null)
|
|||
|
|
{
|
|||
|
|
// not an array
|
|||
|
|
return arr.ToString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (arr is string sArr) { return sArr; }
|
|||
|
|
|
|||
|
|
var str = new StringBuilder(leftStr);
|
|||
|
|
var addsep = false;
|
|||
|
|
foreach (var obj in enumerable)
|
|||
|
|
{
|
|||
|
|
if (addsep)
|
|||
|
|
str.Append(sepStr);
|
|||
|
|
str.Append(obj);
|
|||
|
|
addsep = true;
|
|||
|
|
}
|
|||
|
|
str.Append(rightStr);
|
|||
|
|
return str.ToString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// returns a string representation of an array
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="arr">array to serialize to string</param>
|
|||
|
|
/// <returns>string representation of array</returns>
|
|||
|
|
public static string ArrayObjectToString(object arr)
|
|||
|
|
{
|
|||
|
|
return ArrayObjectToString(arr, LeftGroup, Separater, RightGroup);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// constructs an array of unsigned ints out of a string
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="s"></param>
|
|||
|
|
/// <param name="leftGroup">symbol to remove at the start</param>
|
|||
|
|
/// <param name="separater">symbol used to separate elements</param>
|
|||
|
|
/// <param name="rightGroup">symbol to remove at the end</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static uint[] StringToUIntArray(string s, char[] leftGroup, char[] separater, char[] rightGroup)
|
|||
|
|
{
|
|||
|
|
var noParens = s.TrimStart(leftGroup); //First paren
|
|||
|
|
noParens = noParens.TrimEnd(rightGroup); //Last paren
|
|||
|
|
var elements = noParens.Split(separater); //Comma
|
|||
|
|
uint[] a = new uint[elements.Length];
|
|||
|
|
var index = 0;
|
|||
|
|
foreach (var element in elements)
|
|||
|
|
{
|
|||
|
|
a[index] = Convert.ToUInt32(element);
|
|||
|
|
index++;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return a;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// returns an array of unsigned ints
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="s">string to convert to an array of unsigned ints</param>
|
|||
|
|
/// <returns>an array of unsigned ints</returns>
|
|||
|
|
public static uint[] StringToUIntArray(string s)
|
|||
|
|
{
|
|||
|
|
return StringToUIntArray(s, LeftGroupChar, SeparaterChar, RightGroupChar);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|