Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ public Task StartAsync(CancellationToken cancellationToken = default)
{
Logger?.LogDebug("Configuring HTTP prefix: {Prefix}", prefix.Value);

if (string.IsNullOrWhiteSpace(prefix.Value))
continue;

if (!httpListener.Prefixes.Contains(prefix.Value!))
httpListener.Prefixes.Add(prefix.Value!);
if (!string.IsNullOrWhiteSpace(prefix.Value) && !httpListener.Prefixes.Contains(prefix.Value))
httpListener.Prefixes.Add(prefix.Value);
}
}

Expand Down
19 changes: 5 additions & 14 deletions src/Commands/Commands.Parsing/Parsers/ColorParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@ public ColorParser()
{
_colors = new(StringComparer.OrdinalIgnoreCase);

var properties = typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static);

foreach (var property in properties)
foreach (var property in typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static).Where(static p => p.PropertyType == typeof(Color)))
{
if (property.PropertyType == typeof(Color))
{
var color = (Color)property.GetValue(null)!;
_colors[property.Name] = color;
}
_colors[property.Name] = (Color)property.GetValue(null)!;
}
}

Expand Down Expand Up @@ -61,8 +55,6 @@ public override ValueTask<ParseResult> Parse(IContext context, ICommandParameter

private bool TryParseRgb(string value, out Color result)
{
result = new();

var separation = value.Split(',');

if (separation.Length == 3)
Expand All @@ -76,6 +68,7 @@ private bool TryParseRgb(string value, out Color result)
}
}

result = new();
return false;
}

Expand Down Expand Up @@ -108,8 +101,6 @@ private bool TryParseHex(string value, out Color result)

private bool TryParseUint(string value, out Color result)
{
result = new();

if (uint.TryParse(value, out var rgb))
{
var r = (byte)((rgb & 0xFF0000) >> 16);
Expand All @@ -121,20 +112,20 @@ private bool TryParseUint(string value, out Color result)
return true;
}

result = new();
return false;
}

private bool TryParseNamed(string value, out Color result)
{
result = new();

if (_colors.TryGetValue(value, out var color))
{
result = color;

return true;
}

result = new();
return false;
}
}
20 changes: 8 additions & 12 deletions src/Commands/Commands/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,17 @@ public static void NotNullOrInvalid(IEnumerable<string> values, Regex? regex, st
{
NotNull(values, nameof(values));

if (regex != null)
{
var valueCount = 0;
foreach (var value in values)
{
valueCount++;
if (regex == null) return;

NotNullOrEmpty(value, argumentExpression);
if (!values.Any())
throw new ArgumentException($"The argument '{argumentExpression}' must not be empty.", argumentExpression);

if (!regex.IsMatch(value))
throw new ArgumentException($"The argument '{argumentExpression}' must match the validation expression '{regex}'", argumentExpression);
}
foreach (var value in values)
{
NotNullOrEmpty(value, argumentExpression);

if (valueCount == 0)
throw new ArgumentException($"The argument '{argumentExpression}' must not be empty.", argumentExpression);
if (!regex.IsMatch(value))
throw new ArgumentException($"The argument '{argumentExpression}' must match the validation expression '{regex}'", argumentExpression);
}
}
}
22 changes: 5 additions & 17 deletions src/Commands/Commands/Components/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,9 @@ private Command(IActivator activator, ComponentOptions options)

(MinLength, MaxLength) = Utilities.GetLength(parameters);

for (var i = 0; i < parameters.Length; i++)
for (int i = parameters.Length - 1; i < -1; i--)
{
var parameter = parameters[i];

if (parameter.IsRemainder && i != parameters.Length - 1)
throw new ComponentFormatException("Remainder-marked parameters must be the last parameter in the parameter list of a command.");
if (parameters[i].IsRemainder) throw new ComponentFormatException("Remainder-marked parameters must be the last parameter in the parameter list of a command.");
}

Parameters = parameters;
Expand Down Expand Up @@ -283,7 +280,7 @@ static TestResult Compare(ITest test, TestResultType targetType, Exception excep
/// <param name="includeArguments">Defines if the arguments of the command should be included in the output.</param>
public string GetFullName(bool includeArguments)
{
var sb = new StringBuilder();
var sb = new ValueStringBuilder(stackalloc char[64]);

if (Parent?.Name != null)
{
Expand Down Expand Up @@ -314,18 +311,9 @@ public string GetFullName(bool includeArguments)
public string GetFullName()
=> GetFullName(true);

/// <inheritdoc />
public float GetScore()
{
var score = 1.0f;

foreach (var parameter in Parameters)
score += parameter.GetScore();

score += Attributes.FirstOrDefault<PriorityAttribute>()?.Priority ?? 0;

return score;
}
/// <inheritdoc />
public float GetScore() => 1 + Parameters.Sum(p => p.GetScore()) + Attributes.FirstOrDefault<PriorityAttribute>()?.Priority ?? 0;

/// <inheritdoc />
public int CompareTo(IComponent? component)
Expand Down
8 changes: 5 additions & 3 deletions src/Commands/Commands/Components/CommandGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,18 @@ public IEnumerable<ExecuteConditionAttribute> GetConditions()
/// <inheritdoc />
public string GetFullName()
{
var sb = new StringBuilder();
var sb = new ValueStringBuilder(stackalloc char[64]);

if (Parent?.Name != null)
{
sb.Append(Parent.GetFullName());

if (Name != null)
{
sb.Append(' ');
sb.Append(Name);
}

sb.Append(Name);
}
else if (Name != null)
sb.Append(Name);
Expand Down Expand Up @@ -236,7 +238,7 @@ public override IComponent[] Find(Arguments args)
/// <inheritdoc />
public override string ToString()
{
var sb = new StringBuilder();
var sb = new ValueStringBuilder(stackalloc char[64]);

if (Parent != null)
{
Expand Down
31 changes: 18 additions & 13 deletions src/Commands/Commands/Components/ComponentSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public IEnumerable<Command> GetCommands(Func<Command, bool> predicate, bool brow
if (component is Command command && predicate(command))
discovered.Add(command);

if (component is CommandGroup grp)
else if (component is CommandGroup grp)
discovered.AddRange(grp.GetCommands(predicate, browseNestedComponents));
}

Expand Down Expand Up @@ -166,35 +166,40 @@ internal SpanStateEnumerator GetSpanEnumerator()
// This method is used to remove a range of components from the array of components with low allocation overhead.
internal int UnbindRange(IEnumerable<IComponent> components)
{
IComponent[] copy = new IComponent[_items.Length];

lock (_items)
{
var mutations = 0;

var copy = new List<IComponent>(_items);
int mutations = 0;

foreach (var component in components)
{
Assert.NotNull(component, nameof(component));

var output = copy.Remove(component);

if (output)
for (int i = 0; i < _items.Length; i++)
{
mutations += 1;
component.Unbind();
if (_items[i] == component)
{
component.Unbind();
mutations++;
break;
}
else
{
copy[i - mutations] = _items[i];
}
}
}

if (mutations > 0)
{
Array.Resize(ref copy, _items.Length - mutations);
_items = copy;

_mutateTree?.Invoke(components, true);
_items = [.. copy];
}

return mutations;
}
}

// This method is used to add a range of components to the array of components with low allocation overhead.
internal int BindRange(IEnumerable<IComponent> components, bool extracted)
{
Expand Down
Loading