2 // StringWithQualityHeaderValue.cs
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.Globalization;
30 namespace System.Net.Http.Headers
32 public class StringWithQualityHeaderValue : ICloneable
34 public StringWithQualityHeaderValue (string value)
36 Parser.Token.Check (value);
40 public StringWithQualityHeaderValue (string value, double quality)
43 if (quality < 0 || quality > 1)
44 throw new ArgumentOutOfRangeException ("quality");
49 private StringWithQualityHeaderValue ()
53 public double? Quality { get; private set; }
54 public string Value { get; private set; }
56 object ICloneable.Clone ()
58 return MemberwiseClone ();
61 public override bool Equals (object obj)
63 var source = obj as StringWithQualityHeaderValue;
64 return source != null &&
65 string.Equals (source.Value, Value, StringComparison.OrdinalIgnoreCase) &&
66 source.Quality == Quality;
69 public override int GetHashCode ()
71 return Value.ToLowerInvariant ().GetHashCode () ^ Quality.GetHashCode ();
74 public static StringWithQualityHeaderValue Parse (string input)
76 StringWithQualityHeaderValue value;
77 if (TryParse (input, out value))
80 throw new FormatException (input);
83 public static bool TryParse (string input, out StringWithQualityHeaderValue parsedValue)
87 var lexer = new Lexer (input);
88 var t = lexer.Scan ();
89 if (t != Token.Type.Token)
92 var value = new StringWithQualityHeaderValue ();
93 value.Value = lexer.GetStringValue (t);
96 if (t == Token.Type.SeparatorSemicolon) {
98 if (t != Token.Type.Token)
101 var s = lexer.GetStringValue (t);
102 if (s != "q" && s != "Q")
106 if (t != Token.Type.SeparatorEqual)
112 if (!lexer.TryGetDoubleValue (t, out d))
123 if (t != Token.Type.End)
130 public override string ToString ()
133 return Value + "; q=" + Quality.Value.ToString ("0.0##", CultureInfo.InvariantCulture);