109 lines
3.5 KiB
C#
109 lines
3.5 KiB
C#
using Sarand.Net.Standard.Base.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Sarand.Net.Standard.Base.Core
|
|
{
|
|
//=======================================================================
|
|
public class StringPair : Tuple<String, String>
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Definitions.
|
|
public enum Value
|
|
{
|
|
Value1,
|
|
Value2,
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public StringPair (String value1, String value2) :
|
|
base(StringUtil.EmptyOnNull(value1), StringUtil.EmptyOnNull(value2))
|
|
{
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Concatenation.
|
|
public String Concat (String delimiter, bool delimBlanks = true)
|
|
{
|
|
String value = StringUtil.EmptyOnNull(Item1);
|
|
if (!String.IsNullOrEmpty(Item2) || delimBlanks)
|
|
value += StringUtil.EmptyOnNull(delimiter);
|
|
value += StringUtil.EmptyOnNull(Item2);
|
|
return value;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Operators.
|
|
public static implicit operator StringPair (String value1)
|
|
{
|
|
return new StringPair(value1, String.Empty);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
public static class StringPairList
|
|
{
|
|
//-------------------------------------------------------------------
|
|
public static List<StringPair> CreateFrom (List<String> valuesToAdd, bool duplicateAsS2 = false)
|
|
{
|
|
List<StringPair> values = new List<StringPair>();
|
|
foreach (String valueToAdd in valuesToAdd)
|
|
values.Add(new StringPair(valueToAdd, (duplicateAsS2 ? valueToAdd : "")));
|
|
return values;
|
|
}
|
|
public static List<StringPair> CreateFrom (List<String> s1Values, List<String> s2Values)
|
|
{
|
|
List<StringPair> values = new List<StringPair>();
|
|
for (int valueIdx = 0; valueIdx < (Math.Min(s1Values.Count,s2Values.Count)); valueIdx++)
|
|
{
|
|
values.Add(new StringPair(s1Values[valueIdx], s2Values[valueIdx]));
|
|
}
|
|
return values;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
public static void Add (this List<StringPair> values, String s1, String s2 = "")
|
|
{
|
|
values.Add(new StringPair(s1, s2));
|
|
}
|
|
public static void AddRange (this List<StringPair> values, List<String> valuesToAdd, bool duplicateAsS2 = false)
|
|
{
|
|
values.AddRange(CreateFrom(valuesToAdd, duplicateAsS2));
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
public static List<String> Concat (this List<StringPair> values, String delimiter, bool delimitBlanks = true)
|
|
{
|
|
List<String> data = new List<string>();
|
|
foreach (var pair in values)
|
|
{
|
|
String value = pair.Item1;
|
|
if (!String.IsNullOrEmpty(pair.Item2) || delimitBlanks)
|
|
value += delimiter;
|
|
value += pair.Item2;
|
|
data.Add(value);
|
|
}
|
|
return data;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
public static List<String> GetValues (this List<StringPair> values, StringPair.Value valueType = StringPair.Value.Value1)
|
|
{
|
|
List<String> data = new List<string>();
|
|
foreach (var pair in values)
|
|
{
|
|
String value = pair.Item1;
|
|
if (valueType == StringPair.Value.Value2)
|
|
value = pair.Item2;
|
|
data.Add(value);
|
|
}
|
|
return data;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|