Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
StringWithQualityHeaderValue.cs
Go to the documentation of this file.
4
6
8{
9 private readonly string _value;
10
11 private readonly double? _quality;
12
13 public string Value => _value;
14
15 public double? Quality => _quality;
16
22
23 public StringWithQualityHeaderValue(string value, double quality)
24 {
26 if (quality < 0.0 || quality > 1.0)
27 {
28 throw new ArgumentOutOfRangeException("quality");
29 }
30 _value = value;
31 _quality = quality;
32 }
33
39
40 public override string ToString()
41 {
42 if (_quality.HasValue)
43 {
45 IFormatProvider provider = invariantCulture;
46 Span<char> initialBuffer = stackalloc char[128];
47 DefaultInterpolatedStringHandler handler = new DefaultInterpolatedStringHandler(4, 2, invariantCulture, initialBuffer);
48 handler.AppendFormatted(_value);
49 handler.AppendLiteral("; q=");
50 handler.AppendFormatted(_quality.Value, "0.0##");
51 return string.Create(provider, initialBuffer, ref handler);
52 }
53 return _value;
54 }
55
56 public override bool Equals([NotNullWhen(true)] object? obj)
57 {
58 if (!(obj is StringWithQualityHeaderValue stringWithQualityHeaderValue))
59 {
60 return false;
61 }
62 if (!string.Equals(_value, stringWithQualityHeaderValue._value, StringComparison.OrdinalIgnoreCase))
63 {
64 return false;
65 }
66 if (_quality.HasValue)
67 {
68 if (stringWithQualityHeaderValue._quality.HasValue)
69 {
70 return _quality.Value == stringWithQualityHeaderValue._quality.Value;
71 }
72 return false;
73 }
74 return !stringWithQualityHeaderValue._quality.HasValue;
75 }
76
77 public override int GetHashCode()
78 {
79 int num = StringComparer.OrdinalIgnoreCase.GetHashCode(_value);
80 if (_quality.HasValue)
81 {
82 num ^= _quality.Value.GetHashCode();
83 }
84 return num;
85 }
86
92
93 public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out StringWithQualityHeaderValue? parsedValue)
94 {
95 int index = 0;
96 parsedValue = null;
97 if (GenericHeaderParser.SingleValueStringWithQualityParser.TryParseValue(input, null, ref index, out var parsedValue2))
98 {
99 parsedValue = (StringWithQualityHeaderValue)parsedValue2;
100 return true;
101 }
102 return false;
103 }
104
105 internal static int GetStringWithQualityLength(string input, int startIndex, out object parsedValue)
106 {
107 parsedValue = null;
108 if (string.IsNullOrEmpty(input) || startIndex >= input.Length)
109 {
110 return 0;
111 }
112 int tokenLength = HttpRuleParser.GetTokenLength(input, startIndex);
113 if (tokenLength == 0)
114 {
115 return 0;
116 }
117 string value = input.Substring(startIndex, tokenLength);
118 int num = startIndex + tokenLength;
120 if (num == input.Length || input[num] != ';')
121 {
122 parsedValue = new StringWithQualityHeaderValue(value);
123 return num - startIndex;
124 }
125 num++;
127 if (!TryReadQuality(input, out var quality, ref num))
128 {
129 return 0;
130 }
131 parsedValue = new StringWithQualityHeaderValue(value, quality);
132 return num - startIndex;
133 }
134
135 private static bool TryReadQuality(string input, out double quality, ref int index)
136 {
137 int num = index;
138 quality = 0.0;
139 if (num == input.Length || (input[num] != 'q' && input[num] != 'Q'))
140 {
141 return false;
142 }
143 num++;
145 if (num == input.Length || input[num] != '=')
146 {
147 return false;
148 }
149 num++;
151 if (num == input.Length)
152 {
153 return false;
154 }
155 int numberLength = HttpRuleParser.GetNumberLength(input, num, allowDecimal: true);
156 if (numberLength == 0)
157 {
158 return false;
159 }
160 if (!double.TryParse(input.AsSpan(num, numberLength), NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo, out quality))
161 {
162 return false;
163 }
164 if (quality < 0.0 || quality > 1.0)
165 {
166 return false;
167 }
168 num += numberLength;
170 index = num;
171 return true;
172 }
173
175 {
176 return new StringWithQualityHeaderValue(this);
177 }
178}
static CultureInfo InvariantCulture
static readonly GenericHeaderParser SingleValueStringWithQualityParser
static void CheckValidToken(string value, string parameterName)
static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out StringWithQualityHeaderValue? parsedValue)
override bool Equals([NotNullWhen(true)] object? obj)
static int GetStringWithQualityLength(string input, int startIndex, out object parsedValue)
static StringWithQualityHeaderValue Parse(string? input)
static bool TryReadQuality(string input, out double quality, ref int index)
static int GetTokenLength(string input, int startIndex)
static int GetWhitespaceLength(string input, int startIndex)
static int GetNumberLength(string input, int startIndex, bool allowDecimal)
static StringComparer OrdinalIgnoreCase