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.Components/Utilities/BindingConverters/RangeConverter.cs

62 lines
1.5 KiB

2 years ago
namespace Connected
{
public class RangeConverter<T> : Converter<Components.Range<T>>
{
readonly DefaultConverter<T> _converter;
public RangeConverter()
{
_converter = new DefaultConverter<T>();
SetFunc = OnSet;
GetFunc = OnGet;
}
private Components.Range<T> OnGet(string value)
{
if (!Split(value, out var valueStart, out var valueEnd))
return null;
return new Components.Range<T>(_converter.Get(valueStart), _converter.Get(valueEnd));
}
private string OnSet(Components.Range<T> arg)
{
if (arg == null)
return string.Empty;
return Join(_converter.Set(arg.Start), _converter.Set(arg.End));
}
public static string Join(string valueStart, string valueEnd)
{
if (string.IsNullOrEmpty(valueStart) && string.IsNullOrEmpty(valueEnd))
return string.Empty;
return $"[{valueStart};{valueEnd}]";
}
public static bool Split(string value, out string valueStart, out string valueEnd)
{
valueStart = valueEnd = string.Empty;
if (string.IsNullOrEmpty(value) || value[0] != '[' || value[^1] != ']')
{
return false;
}
var idx = value.IndexOf(';');
if (idx < 1)
{
return false;
}
valueStart = value[1..idx];
valueEnd = value[(idx + 1)..^1];
return true;
}
}
}