code_junkie

C#: Test if string is a guid without throwing exceptions?

Nov 11th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. public static Boolean TryStrToGuid(String s, out Guid value)
  2. {
  3. try
  4. {
  5. value = new Guid(s);
  6. return true;
  7. }
  8. catch (FormatException)
  9. {
  10. value = Guid.Empty;
  11. return false;
  12. }
  13. }
  14.  
  15. c:Program Files
  16. c:Program Files.old
  17. c:Users
  18. c:Users.old
  19. c:UserManager.{CE7F5AA5-6832-43FE-BAE1-80D14CD8F666}
  20. c:Windows
  21. c:Windows.old
  22.  
  23. Catch FormatException:
  24. 10,000 good: 63,668 ticks
  25. 10,000 bad: 6,435,609 ticks
  26.  
  27. Regex Pre-Screen with try-catch:
  28. 10,000 good: 637,633 ticks
  29. 10,000 bad: 717,894 ticks
  30.  
  31. COM Interop CLSIDFromString
  32. 10,000 good: 126,120 ticks
  33. 10,000 bad: 23,134 ticks
  34.  
  35. Catch exception:
  36. 10,000 good: 63,668 ticks
  37. 10,000 bad: 6,435,609 ticks
  38.  
  39. Regex Pre-Screen:
  40. 10,000 good: 637,633 ticks
  41. 10,000 bad: 717,894 ticks
  42.  
  43. COM Interop CLSIDFromString
  44. 10,000 good: 126,120 ticks
  45. 10,000 bad: 23,134 ticks
  46.  
  47. /// <summary>
  48. /// Attempts to convert a string to a guid.
  49. /// </summary>
  50. /// <param name="s">The string to try to convert</param>
  51. /// <param name="value">Upon return will contain the Guid</param>
  52. /// <returns>Returns true if successful, otherwise false</returns>
  53. public static Boolean TryStrToGuid(String s, out Guid value)
  54. {
  55. //ClsidFromString returns the empty guid for null strings
  56. if ((s == null) || (s == ""))
  57. {
  58. value = Guid.Empty;
  59. return false;
  60. }
  61.  
  62. int hresult = PInvoke.ObjBase.CLSIDFromString(s, out value);
  63. if (hresult >= 0)
  64. {
  65. return true;
  66. }
  67. else
  68. {
  69. value = Guid.Empty;
  70. return false;
  71. }
  72. }
  73.  
  74.  
  75. namespace PInvoke
  76. {
  77. class ObjBase
  78. {
  79. /// <summary>
  80. /// This function converts a string generated by the StringFromCLSID function back into the original class identifier.
  81. /// </summary>
  82. /// <param name="sz">String that represents the class identifier</param>
  83. /// <param name="clsid">On return will contain the class identifier</param>
  84. /// <returns>
  85. /// Positive or zero if class identifier was obtained successfully
  86. /// Negative if the call failed
  87. /// </returns>
  88. [DllImport("ole32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = true)]
  89. public static extern int CLSIDFromString(string sz, out Guid clsid);
  90. }
  91. }
  92.  
  93. new Guid(someString);
  94.  
  95. Exception: 26ms
  96. Interop: 1,201ms
  97.  
  98. Exception: 1,150ms
  99. Interop: 1,201ms
  100.  
  101. try
  102. {
  103. value = new Guid(s);
  104. return true;
  105. }
  106. catch (FormatException)
  107. {
  108. value = Guid.Empty;
  109. return false;
  110. }
  111.  
  112. ^[A-Fa-f0-9]{32}$|^({|\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\))?$|^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$
  113.  
  114. using System.Text.RegularExpressions;
  115.  
  116.  
  117. /// <summary>
  118. /// Validate that a string is a valid GUID
  119. /// </summary>
  120. /// <param name="GUIDCheck"></param>
  121. /// <returns></returns>
  122. private bool IsValidGUID(string GUIDCheck)
  123. {
  124. if (!string.IsNullOrEmpty(GUIDCheck))
  125. {
  126. return new Regex(@"^({{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}}{0,1})$").IsMatch(GUIDCheck);
  127. }
  128. return false;
  129. }
  130.  
  131. public static Boolean TryStrToGuid(String s, out Guid value)
  132. {
  133.  
  134. // this is before the overhead of setting up the try/catch block.
  135. if(value == null || value.Length != 36)
  136. {
  137. value = Guid.Empty;
  138. return false;
  139. }
  140.  
  141. try
  142. {
  143. value = new Guid(s);
  144. return true;
  145. }
  146. catch (FormatException)
  147. {
  148. value = Guid.Empty;
  149. return false;
  150. }
  151. }
  152.  
  153. public static string IsValidGuid(string str)
  154. {
  155. Guid guid;
  156. return Guid.TryParse(str, out guid);
  157. }
  158.  
  159. bool IsProbablyGuid(string s)
  160. {
  161. int hexchars = 0;
  162. foreach(character c in string s)
  163. {
  164. if(IsValidHexChar(c))
  165. hexchars++;
  166. }
  167. return hexchars==32;
  168. }
  169.  
  170. Private Function IsGuidWithOptionalBraces(ByRef strValue As String) As Boolean
  171. If String.IsNullOrEmpty(strValue) Then
  172. Return False
  173. End If
  174.  
  175. Return System.Text.RegularExpressions.Regex.IsMatch(strValue, "^[{]?[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}[}]?$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
  176. End Function
  177.  
  178.  
  179. Private Function IsGuidWithoutBraces(ByRef strValue As String) As Boolean
  180. If String.IsNullOrEmpty(strValue) Then
  181. Return False
  182. End If
  183.  
  184. Return System.Text.RegularExpressions.Regex.IsMatch(strValue, "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
  185. End Function
  186.  
  187.  
  188. Private Function IsGuidWithBraces(ByRef strValue As String) As Boolean
  189. If String.IsNullOrEmpty(strValue) Then
  190. Return False
  191. End If
  192.  
  193. Return System.Text.RegularExpressions.Regex.IsMatch(strValue, "^{[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}}$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
  194. End Function
Add Comment
Please, Sign In to add comment