5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Net.Mail;
30 using System.Globalization;
32 namespace System.Net.Http.Headers
36 public static class Token
38 public static bool TryParse (string input, out string result)
40 if (input != null && Lexer.IsValidToken (input)) {
49 public static void Check (string s)
52 throw new ArgumentNullException ();
54 if (!Lexer.IsValidToken (s)) {
56 throw new ArgumentException ();
58 throw new FormatException (s);
62 public static bool TryCheck (string s)
67 if (!Lexer.IsValidToken (s)) {
77 public static void CheckQuotedString (string s)
80 throw new ArgumentNullException ();
82 var lexer = new Lexer (s);
83 if (lexer.Scan () == Headers.Token.Type.QuotedString && lexer.Scan () == Headers.Token.Type.End)
87 throw new ArgumentException ();
89 throw new FormatException (s);
92 public static void CheckComment (string s)
95 throw new ArgumentNullException ();
97 var lexer = new Lexer (s);
100 if (!lexer.ScanCommentOptional (out temp)) {
102 throw new ArgumentException ();
104 throw new FormatException (s);
109 public static class DateTime
111 public new static readonly Func<object, string> ToString = l => ((DateTimeOffset) l).ToString ("r", CultureInfo.InvariantCulture);
113 public static bool TryParse (string input, out DateTimeOffset result)
115 return Lexer.TryGetDateValue (input, out result);
119 public static class EmailAddress
121 public static bool TryParse (string input, out string result)
124 new MailAddress (input);
134 public static class Int
136 public static bool TryParse (string input, out int result)
138 return int.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
142 public static class Long
144 public static bool TryParse (string input, out long result)
146 return long.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
150 public static class MD5
152 public static bool TryParse (string input, out byte[] result)
155 result = Convert.FromBase64String (input);
164 public static class TimeSpanSeconds
166 public static bool TryParse (string input, out TimeSpan result)
169 if (Int.TryParse (input, out value)) {
170 result = TimeSpan.FromSeconds (value);
174 result = TimeSpan.Zero;
179 public static class Uri
181 public static bool TryParse (string input, out System.Uri result)
183 return System.Uri.TryCreate (input, UriKind.RelativeOrAbsolute, out result);
186 public static void Check (string s)
189 throw new ArgumentNullException ();
192 if (!TryParse (s, out uri)) {
194 throw new ArgumentException ();
196 throw new FormatException (s);