Parser.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. namespace System.Net.Http.Headers
  31. {
  32. static class Parser
  33. {
  34. public static class Token
  35. {
  36. public static bool TryParse (string input, out string result)
  37. {
  38. if (input != null && Lexer.IsValidToken (input)) {
  39. result = input;
  40. return true;
  41. }
  42. result = null;
  43. return false;
  44. }
  45. public static void Check (string s)
  46. {
  47. if (s == null)
  48. throw new ArgumentNullException ();
  49. if (!Lexer.IsValidToken (s)) {
  50. if (s.Length == 0)
  51. throw new ArgumentException ();
  52. throw new FormatException (s);
  53. }
  54. }
  55. public static bool TryCheck (string s)
  56. {
  57. if (s == null)
  58. return false;
  59. if (!Lexer.IsValidToken (s)) {
  60. if (s.Length == 0)
  61. return false;
  62. return false;
  63. }
  64. return true;
  65. }
  66. public static void CheckQuotedString (string s)
  67. {
  68. if (s == null)
  69. throw new ArgumentNullException ();
  70. var lexer = new Lexer (s);
  71. if (lexer.Scan () == Headers.Token.Type.QuotedString && lexer.Scan () == Headers.Token.Type.End)
  72. return;
  73. if (s.Length == 0)
  74. throw new ArgumentException ();
  75. throw new FormatException (s);
  76. }
  77. public static void CheckComment (string s)
  78. {
  79. if (s == null)
  80. throw new ArgumentNullException ();
  81. var lexer = new Lexer (s);
  82. string temp;
  83. if (!lexer.ScanCommentOptional (out temp)) {
  84. if (s.Length == 0)
  85. throw new ArgumentException ();
  86. throw new FormatException (s);
  87. }
  88. }
  89. }
  90. public static class DateTime
  91. {
  92. public new static readonly Func<object, string> ToString = l => ((DateTimeOffset) l).ToString ("r", CultureInfo.InvariantCulture);
  93. public static bool TryParse (string input, out DateTimeOffset result)
  94. {
  95. return Lexer.TryGetDateValue (input, out result);
  96. }
  97. }
  98. public static class EmailAddress
  99. {
  100. public static bool TryParse (string input, out string result)
  101. {
  102. try {
  103. new MailAddress (input);
  104. result = input;
  105. return true;
  106. } catch {
  107. result = null;
  108. return false;
  109. }
  110. }
  111. }
  112. public static class Int
  113. {
  114. public static bool TryParse (string input, out int result)
  115. {
  116. return int.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
  117. }
  118. }
  119. public static class Long
  120. {
  121. public static bool TryParse (string input, out long result)
  122. {
  123. return long.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
  124. }
  125. }
  126. public static class MD5
  127. {
  128. public static bool TryParse (string input, out byte[] result)
  129. {
  130. try {
  131. result = Convert.FromBase64String (input);
  132. return true;
  133. } catch {
  134. result = null;
  135. return false;
  136. }
  137. }
  138. }
  139. public static class TimeSpanSeconds
  140. {
  141. public static bool TryParse (string input, out TimeSpan result)
  142. {
  143. int value;
  144. if (Int.TryParse (input, out value)) {
  145. result = TimeSpan.FromSeconds (value);
  146. return true;
  147. }
  148. result = TimeSpan.Zero;
  149. return false;
  150. }
  151. }
  152. public static class Uri
  153. {
  154. public static bool TryParse (string input, out System.Uri result)
  155. {
  156. return System.Uri.TryCreate (input, UriKind.RelativeOrAbsolute, out result);
  157. }
  158. public static void Check (string s)
  159. {
  160. if (s == null)
  161. throw new ArgumentNullException ();
  162. System.Uri uri;
  163. if (!TryParse (s, out uri)) {
  164. if (s.Length == 0)
  165. throw new ArgumentException ();
  166. throw new FormatException (s);
  167. }
  168. }
  169. }
  170. }
  171. }