using System.Text;
using System;
namespace DTS.Common.Utilities
{
///
/// encapsulates converting array or IEnumerables to a string
///
public static class ArrayToString
{
///
/// separator between elements
///
/// This will change the separator for all callers
public static string Separater { get; set; } = ", ";
///
/// separator between elements with no space
///
/// This will change the separator for all callers
public static string SeparaterNoSpace { get; set; } = ",";
///
/// symbol for the start of a group
///
/// this will change the symbol for all callers
public static string LeftGroup { get; set; } = "(";
///
/// symbol for the end of a group
///
/// this will change the symbol for all callers
public static string RightGroup { get; set; } = ")";
///
/// separator character between elements
///
public static char[] SeparaterChar { get; set; } = new char[1] { ',' };
///
/// symbol for the opening paren
///
public static char[] LeftGroupChar { get; set; } = new char[1] { '(' };
///
/// symbol for the closing paren
///
public static char[] RightGroupChar { get; set; } = new char[1] { ')' };
///
/// 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
///
/// array to serialize to string
/// symbol to put at the start of the string
/// symbol to use between elements
/// symbol to use at the end of the string
/// string representation of array
public static string ArrayObjectToString(object arr, string leftStr, string sepStr, string rightStr)
{
if (arr == null)
return "";
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();
}
///
/// returns a string representation of an array
///
/// array to serialize to string
/// string representation of array
public static string ArrayObjectToString(object arr)
{
return ArrayObjectToString(arr, LeftGroup, Separater, RightGroup);
}
///
/// constructs an array of unsigned ints out of a string
///
///
/// symbol to remove at the start
/// symbol used to separate elements
/// symbol to remove at the end
///
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;
}
///
/// returns an array of unsigned ints
///
/// string to convert to an array of unsigned ints
/// an array of unsigned ints
public static uint[] StringToUIntArray(string s)
{
return StringToUIntArray(s, LeftGroupChar, SeparaterChar, RightGroupChar);
}
}
}