forked from SixLabors/ImageSharp.Web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListConverter{T}.cs
More file actions
53 lines (45 loc) · 1.47 KB
/
Copy pathListConverter{T}.cs
File metadata and controls
53 lines (45 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Globalization;
using System.Runtime.CompilerServices;
namespace SixLabors.ImageSharp.Web.Commands.Converters;
/// <summary>
/// Converts the value of a string to a generic list.
/// </summary>
/// <typeparam name="T">The type of result to return.</typeparam>
public sealed class ListConverter<T> : ICommandConverter<List<T>>
{
/// <inheritdoc/>
public Type Type => typeof(List<T>);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public List<T> ConvertFrom(
CommandParser parser,
CultureInfo culture,
string? value,
Type propertyType)
{
List<T> result = [];
if (string.IsNullOrWhiteSpace(value))
{
return result;
}
foreach (string pill in GetStringArray(value, culture))
{
T? item = parser.ParseValue<T>(pill, culture);
if (item is not null)
{
result.Add(item);
}
}
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string[] GetStringArray(string input, CultureInfo culture)
{
char separator = ConverterUtility.GetListSeparator(culture);
// TODO: Can we use StringSplit Enumerator here?
// https://github.com/dotnet/runtime/issues/934
return input.Split(separator).Select(s => s.Trim()).ToArray();
}
}