Skip to content

Commit

Permalink
Use spans
Browse files Browse the repository at this point in the history
  • Loading branch information
MartyIX committed Apr 27, 2024
1 parent 18023c5 commit 8f5cd2f
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Controls/src/Core/GridLengthTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,34 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(

public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
var strValue = value?.ToString();
string? s = value?.ToString();

if (strValue == null)
if (s is null)
{
return null;
}

strValue = strValue.Trim();
if (string.Compare(strValue, "auto", StringComparison.OrdinalIgnoreCase) == 0)
#if NETSTANDARD2_0_OR_GREATER
string strValue = s.Trim();
#else
ReadOnlySpan<char> strValue = s.AsSpan().Trim();
#endif

if (strValue.Equals("auto", StringComparison.OrdinalIgnoreCase))
{
return GridLength.Auto;
}

if (string.Compare(strValue, "*", StringComparison.OrdinalIgnoreCase) == 0)
if (strValue.Equals("*", StringComparison.OrdinalIgnoreCase))
{
return new GridLength(1, GridUnitType.Star);
}

#if NETSTANDARD2_0_OR_GREATER
if (strValue.EndsWith("*", StringComparison.Ordinal) && double.TryParse(strValue.Substring(0, strValue.Length - 1), NumberStyles.Number, CultureInfo.InvariantCulture, out var length))
#else
if (strValue.EndsWith("*", StringComparison.Ordinal) && double.TryParse(strValue[0..(strValue.Length - 1)], NumberStyles.Number, CultureInfo.InvariantCulture, out var length))
#endif
{
return new GridLength(length, GridUnitType.Star);
}
Expand Down

0 comments on commit 8f5cd2f

Please sign in to comment.