Code/SboxSuperuser/Core/Commands/ChatCommandPrefix.cs

Represents a chat command prefix for the game's chat commands. It stores the prefix (root plus leading slash), checks whether a chat message is owned by this prefix, and parses/tokenizes the command portion after the prefix.

namespace SboxSuperuser.Core.Commands;

internal sealed class ChatCommandPrefix
{
	internal string Value { get; }

	internal ChatCommandPrefix( string commandRoot ) => Value = "/" + CommandRootName.Validate( commandRoot );

	internal bool Owns( string message ) => message is not null && message.StartsWith( Value, StringComparison.Ordinal )
		&& (message.Length == Value.Length || char.IsWhiteSpace( message[ Value.Length ] ));

	internal Tokenization Parse( string message )
	{
		if ( !Owns( message ) || message.Length > CommandTokenizer.MaximumTextLength )
		{
			return new Tokenization( Array.Empty<string>(), "Chat command is missing, unowned or oversized." );
		}
		return CommandTokenizer.Tokenize( message.Substring( Value.Length ) );
	}
}