@@ -7,25 +7,25 @@ namespace Discord.Commands
{
internal class CommandMapNode
{
private static readonly char[] _whitespaceChars = new char[] { ' ', '\r', '\n' };
private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;
private readonly string _name;
private readonly object _lockObj = new object();
private readonly CommandService _service;
private ImmutableArray<CommandInfo> _commands;
public bool IsEmpty => _commands.Length == 0 && _nodes.Count == 0;
public CommandMapNode(string name)
public CommandMapNode(string name, CommandService service )
{
_name = name;
_nodes = new ConcurrentDictionary<string, CommandMapNode>();
_commands = ImmutableArray.Create<CommandInfo>();
_service = service;
}
public void AddCommand(string text, int index, CommandInfo command)
{
int nextSpace = NextWhitespace (text, index);
int nextSpace = NextSeparator (text, index);
string name;
lock (_lockObj)
@@ -43,14 +43,14 @@ namespace Discord.Commands
else
name = text.Substring(index, nextSpace - index);
var nextNode = _nodes.GetOrAdd(name, x => new CommandMapNode(x));
var nextNode = _nodes.GetOrAdd(name, x => new CommandMapNode(x, _service ));
nextNode.AddCommand(nextSpace == -1 ? "" : text, nextSpace + 1, command);
}
}
}
public void RemoveCommand(string text, int index, CommandInfo command)
{
int nextSpace = NextWhitespace (text, index);
int nextSpace = NextSeparator (text, index);
string name;
lock (_lockObj)
@@ -77,7 +77,7 @@ namespace Discord.Commands
public IEnumerable<CommandInfo> GetCommands(string text, int index)
{
int nextSpace = NextWhitespace (text, index);
int nextSpace = NextSeparator (text, index);
string name;
var commands = _commands;
@@ -100,15 +100,12 @@ namespace Discord.Commands
}
}
private static int NextWhitespace (string text, int startIndex)
private int NextSeparator (string text, int startIndex)
{
int lowest = int.MaxValue;
for (int i = 0; i < _whitespaceChars.Length; i++)
{
int index = text.IndexOf(_whitespaceChars[i], startIndex);
if (index != -1 && index < lowest)
int index = text.IndexOf(_service._nodeSeparator, startIndex);
if (index != -1 && index < lowest)
lowest = index;
}
return (lowest != int.MaxValue) ? lowest : -1;
}
}