SboxSuperuser/Core/Commands/ChatCommandPrefix.cs

Represents a chat command prefix for in-game commands. It builds the prefix from a provided root name, checks whether a chat message is owned by this prefix, and parses/tokenizes the remainder of the message into command tokens.

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 ) );
	}
}