You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Framework/Connected.Interop/Reflection/StringConcatenator.cs

74 lines
1.9 KiB

using System.Text;
namespace Connected.Interop.Reflection
{
internal class StringConcatenator : IStringConcatenator
{
private readonly string _separator;
private readonly string _nullValue;
private readonly ConcatenationOptions _concatenationOptions;
public StringConcatenator()
: this(TypeConverter.DefaultStringSeparator)
{
}
public StringConcatenator(string separator)
: this(separator, ConcatenationOptions.Default)
{
}
public StringConcatenator(string separator, ConcatenationOptions concatenationOptions)
: this(separator, TypeConverter.DefaultNullStringValue, concatenationOptions)
{
}
public StringConcatenator(string separator, string nullValue)
: this(separator, nullValue, ConcatenationOptions.Default)
{
}
public StringConcatenator(string separator, string nullValue, ConcatenationOptions concatenationOptions)
{
if (separator == null)
throw new ArgumentNullException("separator");
_separator = separator;
_nullValue = nullValue;
_concatenationOptions = concatenationOptions;
}
public string Concatenate(string[] values)
{
var valuesToConcatenate = values.Where(v => !IgnoreValue(v)).Select(value => value ?? _nullValue).ToArray();
return ConcatenateCore(valuesToConcatenate);
}
private bool IgnoreValue(string value)
{
if (value is null && (_concatenationOptions & ConcatenationOptions.IgnoreNull) == ConcatenationOptions.IgnoreNull)
return true;
if (string.IsNullOrEmpty(value) && (_concatenationOptions & ConcatenationOptions.IgnoreEmpty) == ConcatenationOptions.IgnoreEmpty)
return true;
return false;
}
protected virtual string ConcatenateCore(string[] values)
{
var result = new StringBuilder();
foreach (string value in values)
{
if (result.Length > 0)
result.Append(_separator);
result.Append(value);
}
return result.ToString();
}
}
}