Parser.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 Int
  109. {
  110. public static bool TryParse (string input, out int result)
  111. {
  112. return int.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
  113. }
  114. }
  115. public static class Long
  116. {
  117. public static bool TryParse (string input, out long result)
  118. {
  119. return long.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
  120. }
  121. }
  122. public static class MD5
  123. {
  124. public static bool TryParse (string input, out byte[] result)
  125. {
  126. try {
  127. result = Convert.FromBase64String (input);
  128. return true;
  129. } catch {
  130. result = null;
  131. return false;
  132. }
  133. }
  134. }
  135. public static class TimeSpanSeconds
  136. {
  137. public static bool TryParse (string input, out TimeSpan result)
  138. {
  139. int value;
  140. if (Int.TryParse (input, out value)) {
  141. result = TimeSpan.FromSeconds (value);
  142. return true;
  143. }
  144. result = TimeSpan.Zero;
  145. return false;
  146. }
  147. }
  148. public static class Uri
  149. {
  150. public static bool TryParse (string input, out System.Uri result)
  151. {
  152. return System.Uri.TryCreate (input, UriKind.RelativeOrAbsolute, out result);
  153. }
  154. public static void Check (string s)
  155. {
  156. if (s == null)
  157. throw new ArgumentNullException ();
  158. System.Uri uri;
  159. if (!TryParse (s, out uri)) {
  160. if (s.Length == 0)
  161. throw new ArgumentException ();
  162. throw new FormatException (s);
  163. }
  164. }
  165. }
  166. }
  167. }