Parser.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // Parser.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Net.Mail;
  29. using System.Globalization;
  30. using System.Collections.Generic;
  31. namespace System.Net.Http.Headers
  32. {
  33. static class Parser
  34. {
  35. public static class Token
  36. {
  37. public static bool TryParse (string input, out string result)
  38. {
  39. if (input != null && Lexer.IsValidToken (input)) {
  40. result = input;
  41. return true;
  42. }
  43. result = null;
  44. return false;
  45. }
  46. public static void Check (string s)
  47. {
  48. if (s == null)
  49. throw new ArgumentNullException ();
  50. if (!Lexer.IsValidToken (s)) {
  51. if (s.Length == 0)
  52. throw new ArgumentException ();
  53. throw new FormatException (s);
  54. }
  55. }
  56. public static bool TryCheck (string s)
  57. {
  58. if (s == null)
  59. return false;
  60. return Lexer.IsValidToken (s);
  61. }
  62. public static void CheckQuotedString (string s)
  63. {
  64. if (s == null)
  65. throw new ArgumentNullException ();
  66. var lexer = new Lexer (s);
  67. if (lexer.Scan () == Headers.Token.Type.QuotedString && lexer.Scan () == Headers.Token.Type.End)
  68. return;
  69. if (s.Length == 0)
  70. throw new ArgumentException ();
  71. throw new FormatException (s);
  72. }
  73. public static void CheckComment (string s)
  74. {
  75. if (s == null)
  76. throw new ArgumentNullException ();
  77. var lexer = new Lexer (s);
  78. string temp;
  79. if (!lexer.ScanCommentOptional (out temp)) {
  80. if (s.Length == 0)
  81. throw new ArgumentException ();
  82. throw new FormatException (s);
  83. }
  84. }
  85. }
  86. public static class DateTime
  87. {
  88. public new static readonly Func<object, string> ToString = l => ((DateTimeOffset) l).ToString ("r", CultureInfo.InvariantCulture);
  89. public static bool TryParse (string input, out DateTimeOffset result)
  90. {
  91. return Lexer.TryGetDateValue (input, out result);
  92. }
  93. }
  94. public static class EmailAddress
  95. {
  96. public static bool TryParse (string input, out string result)
  97. {
  98. try {
  99. new MailAddress (input);
  100. result = input;
  101. return true;
  102. } catch {
  103. result = null;
  104. return false;
  105. }
  106. }
  107. }
  108. public static class Host
  109. {
  110. public static bool TryParse (string input, out string result)
  111. {
  112. result = input;
  113. System.Uri dummy;
  114. return System.Uri.TryCreate ("http://u@" + input + "/", UriKind.Absolute, out dummy);
  115. }
  116. }
  117. public static class Int
  118. {
  119. public static bool TryParse (string input, out int result)
  120. {
  121. return int.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
  122. }
  123. }
  124. public static class Long
  125. {
  126. public static bool TryParse (string input, out long result)
  127. {
  128. return long.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
  129. }
  130. }
  131. public static class MD5
  132. {
  133. public new static readonly Func<object, string> ToString = l => Convert.ToBase64String ((byte[]) l);
  134. public static bool TryParse (string input, out byte[] result)
  135. {
  136. try {
  137. result = Convert.FromBase64String (input);
  138. return true;
  139. } catch {
  140. result = null;
  141. return false;
  142. }
  143. }
  144. }
  145. public static class TimeSpanSeconds
  146. {
  147. public static bool TryParse (string input, out TimeSpan result)
  148. {
  149. int value;
  150. if (Int.TryParse (input, out value)) {
  151. result = TimeSpan.FromSeconds (value);
  152. return true;
  153. }
  154. result = TimeSpan.Zero;
  155. return false;
  156. }
  157. }
  158. public static class Uri
  159. {
  160. public static bool TryParse (string input, out System.Uri result)
  161. {
  162. return System.Uri.TryCreate (input, UriKind.RelativeOrAbsolute, out result);
  163. }
  164. public static void Check (string s)
  165. {
  166. if (s == null)
  167. throw new ArgumentNullException ();
  168. System.Uri uri;
  169. if (!TryParse (s, out uri)) {
  170. if (s.Length == 0)
  171. throw new ArgumentException ();
  172. throw new FormatException (s);
  173. }
  174. }
  175. }
  176. }
  177. }