Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public CommandType GetCommandTypeFromCommandString(String command)
- {
- if(command.StartsWith(CommandConstants.Acknowledge))
- return CommandType.Acknowledge;
- else if (command.StartsWith(CommandConstants.Status))
- return CommandType.Status;
- else if (command.StartsWith(CommandConstants.Echo))
- return CommandType.Echo;
- else if (command.StartsWith(CommandConstants.Warning))
- return CommandType.Warning;
- // and so on
- return CommandType.None;
- }
- if(command.StartsWith(CommandConstants.Acknowledge, StringComparison.Ordinal))
- return CommandType.Acknowledge;
- var a = "foo bar foo";
- var b = "foo";
- int numTimes = 1000000;
- Benchmark.Time(() => a.StartsWith(b, StringComparison.Ordinal), "ordinal", numTimes);
- Benchmark.Time(() => a.StartsWith(b), "culture sensitive", numTimes);
- static Regex reCommands = new Regex("^(cmd1|cmd2|cmd3|cmd4)", RegexOptions.Compiled);
- static Dictionary<string, CommandType> Commands = new Dictionary<string, CommandType>();
- private static InitDictionary()
- {
- Commands.Add("cmd1", cmdType1);
- Commands.Add("cmd2", cmdType2);
- Commands.Add("cmd3", cmdType3);
- Commands.Add("cmd4", cmdType4);
- }
- public CommandType GetCommandTypeFromCommandString(String command)
- {
- Match m = reCommands.Match(command);
- if (m.Success)
- {
- return Commands[m.Groups[1].Value];
- }
- return CommandType.None; // no command
- }
- static Regex reCommands = new Regex("^(cmd1|cmd2|cmd3|cmd4)", RegexOptions.Compiled);
- static Dictionary<string, CommandType> Commands = new Dictionary<string, CommandType>();
- private static InitDictionary()
- {
- Commands.Add("cmd1", cmdType1);
- Commands.Add("cmd2", cmdType2);
- Commands.Add("cmd3", cmdType3);
- Commands.Add("cmd4", cmdType4);
- }
- public CommandType GetCommandTypeFromCommandString(String command)
- {
- Match m = reCommands.Match(command);
- if (m.Success)
- {
- return Commands[m.Groups[1].Value];
- }
- return CommandType.None; // no command
- }
- public CommandType GetCommandTypeFromCommandString(String command)
- {
- for(String currentCommand : allCommands) {
- if(command.StartsWith(currentCommand))
- return currentCommand;
- }
- return CommandType.None;
- }
- public enum CommandType
- {
- Acknowledge,
- Status,
- Echo,
- Warning
- }
- static public CommandType? GetCommandTypeFromString(String command)
- {
- var CommandTypes = String.Join("|", Enum.GetNames(typeof(CommandType)));
- var PassedConstant = Regex.Match(command, String.Format("(?i:^({0}))", CommandTypes)).Value;
- if (PassedConstant != String.Empty)
- return (CommandType)Enum.Parse(typeof(CommandType), PassedConstant, true);
- return null;
- }
- static void Main(string[] args)
- {
- Console.WriteLine(GetCommandTypeFromString("Acknowledge that I am great!").ToString());
- }
- IDictionary<string,CommandType>
- public static bool StartsWith(this string s, params string[] candidate)
- {
- string match = candidate.FirstOrDefault(t => s.StartsWith(t));
- return match != default(string);
- }
- public static int PrefixIndex(this string s, params string[] candidate)
- {
- int index = -1;
- string match = candidate.FirstOrDefault(t => { index++; return s.StartsWith(t); });
- return match == default(string) ? -1 : index;
- }
- int index = command.PrefixIndex(tokenStrings);
- if (index >= 0) {
- // convert to your enum
- }
- try
- {
- return (CommandType)Enum.Parse(typeof(CommandType), strCmdName, true);
- }
- catch (Exception)
- {
- return CommandType.None;
- }
Add Comment
Please, Sign In to add comment