code_junkie

More efficient way to determine if a string starts with a token from a set of tokens

Nov 13th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. public CommandType GetCommandTypeFromCommandString(String command)
  2. {
  3. if(command.StartsWith(CommandConstants.Acknowledge))
  4. return CommandType.Acknowledge;
  5. else if (command.StartsWith(CommandConstants.Status))
  6. return CommandType.Status;
  7. else if (command.StartsWith(CommandConstants.Echo))
  8. return CommandType.Echo;
  9. else if (command.StartsWith(CommandConstants.Warning))
  10. return CommandType.Warning;
  11. // and so on
  12.  
  13. return CommandType.None;
  14. }
  15.  
  16. if(command.StartsWith(CommandConstants.Acknowledge, StringComparison.Ordinal))
  17. return CommandType.Acknowledge;
  18.  
  19. var a = "foo bar foo";
  20. var b = "foo";
  21.  
  22. int numTimes = 1000000;
  23.  
  24. Benchmark.Time(() => a.StartsWith(b, StringComparison.Ordinal), "ordinal", numTimes);
  25. Benchmark.Time(() => a.StartsWith(b), "culture sensitive", numTimes);
  26.  
  27. static Regex reCommands = new Regex("^(cmd1|cmd2|cmd3|cmd4)", RegexOptions.Compiled);
  28. static Dictionary<string, CommandType> Commands = new Dictionary<string, CommandType>();
  29. private static InitDictionary()
  30. {
  31. Commands.Add("cmd1", cmdType1);
  32. Commands.Add("cmd2", cmdType2);
  33. Commands.Add("cmd3", cmdType3);
  34. Commands.Add("cmd4", cmdType4);
  35. }
  36. public CommandType GetCommandTypeFromCommandString(String command)
  37. {
  38. Match m = reCommands.Match(command);
  39. if (m.Success)
  40. {
  41. return Commands[m.Groups[1].Value];
  42. }
  43. return CommandType.None; // no command
  44. }
  45.  
  46. static Regex reCommands = new Regex("^(cmd1|cmd2|cmd3|cmd4)", RegexOptions.Compiled);
  47. static Dictionary<string, CommandType> Commands = new Dictionary<string, CommandType>();
  48. private static InitDictionary()
  49. {
  50. Commands.Add("cmd1", cmdType1);
  51. Commands.Add("cmd2", cmdType2);
  52. Commands.Add("cmd3", cmdType3);
  53. Commands.Add("cmd4", cmdType4);
  54. }
  55. public CommandType GetCommandTypeFromCommandString(String command)
  56. {
  57. Match m = reCommands.Match(command);
  58. if (m.Success)
  59. {
  60. return Commands[m.Groups[1].Value];
  61. }
  62. return CommandType.None; // no command
  63. }
  64.  
  65. public CommandType GetCommandTypeFromCommandString(String command)
  66. {
  67. for(String currentCommand : allCommands) {
  68. if(command.StartsWith(currentCommand))
  69. return currentCommand;
  70. }
  71. return CommandType.None;
  72. }
  73.  
  74. public enum CommandType
  75. {
  76. Acknowledge,
  77. Status,
  78. Echo,
  79. Warning
  80. }
  81. static public CommandType? GetCommandTypeFromString(String command)
  82. {
  83. var CommandTypes = String.Join("|", Enum.GetNames(typeof(CommandType)));
  84. var PassedConstant = Regex.Match(command, String.Format("(?i:^({0}))", CommandTypes)).Value;
  85. if (PassedConstant != String.Empty)
  86. return (CommandType)Enum.Parse(typeof(CommandType), PassedConstant, true);
  87. return null;
  88. }
  89.  
  90. static void Main(string[] args)
  91. {
  92. Console.WriteLine(GetCommandTypeFromString("Acknowledge that I am great!").ToString());
  93. }
  94.  
  95. IDictionary<string,CommandType>
  96.  
  97. public static bool StartsWith(this string s, params string[] candidate)
  98. {
  99. string match = candidate.FirstOrDefault(t => s.StartsWith(t));
  100. return match != default(string);
  101. }
  102.  
  103. public static int PrefixIndex(this string s, params string[] candidate)
  104. {
  105. int index = -1;
  106. string match = candidate.FirstOrDefault(t => { index++; return s.StartsWith(t); });
  107. return match == default(string) ? -1 : index;
  108. }
  109.  
  110. int index = command.PrefixIndex(tokenStrings);
  111.  
  112. if (index >= 0) {
  113. // convert to your enum
  114. }
  115.  
  116. try
  117. {
  118. return (CommandType)Enum.Parse(typeof(CommandType), strCmdName, true);
  119. }
  120. catch (Exception)
  121. {
  122. return CommandType.None;
  123. }
Add Comment
Please, Sign In to add comment