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;
31 using System.Collections.Generic;
33 namespace System.Net.Http.Headers
37 public static class Token
39 public static bool TryParse (string input, out string result)
41 if (input != null && Lexer.IsValidToken (input)) {
50 public static void Check (string s)
53 throw new ArgumentNullException ();
55 if (!Lexer.IsValidToken (s)) {
57 throw new ArgumentException ();
59 throw new FormatException (s);
63 public static bool TryCheck (string s)
68 return Lexer.IsValidToken (s);
71 public static void CheckQuotedString (string s)
74 throw new ArgumentNullException ();
76 var lexer = new Lexer (s);
77 if (lexer.Scan () == Headers.Token.Type.QuotedString && lexer.Scan () == Headers.Token.Type.End)
81 throw new ArgumentException ();
83 throw new FormatException (s);
86 public static void CheckComment (string s)
89 throw new ArgumentNullException ();
91 var lexer = new Lexer (s);
94 if (!lexer.ScanCommentOptional (out temp)) {
96 throw new ArgumentException ();
98 throw new FormatException (s);
103 public static class DateTime
105 public new static readonly Func<object, string> ToString = l => ((DateTimeOffset) l).ToString ("r", CultureInfo.InvariantCulture);
107 public static bool TryParse (string input, out DateTimeOffset result)
109 return Lexer.TryGetDateValue (input, out result);
113 public static class EmailAddress
115 public static bool TryParse (string input, out string result)
118 new MailAddress (input);
128 public static class Int
130 public static bool TryParse (string input, out int result)
132 return int.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
136 public static class Long
138 public static bool TryParse (string input, out long result)
140 return long.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
144 public static class MD5
146 public static bool TryParse (string input, out byte[] result)
149 result = Convert.FromBase64String (input);
158 public static class TimeSpanSeconds
160 public static bool TryParse (string input, out TimeSpan result)
163 if (Int.TryParse (input, out value)) {
164 result = TimeSpan.FromSeconds (value);
168 result = TimeSpan.Zero;
173 public static class Uri
175 public static bool TryParse (string input, out System.Uri result)
177 return System.Uri.TryCreate (input, UriKind.RelativeOrAbsolute, out result);
180 public static void Check (string s)
183 throw new ArgumentNullException ();
186 if (!TryParse (s, out uri)) {
188 throw new ArgumentException ();
190 throw new FormatException (s);