wasSharp – Diff between revs 17 and 27
?pathlinks?
Rev 17 | Rev 27 | |||
---|---|---|---|---|
Line 1... | Line 1... | |||
1 | // The MIT License (MIT) |
1 | // The MIT License (MIT) |
|
2 | // |
2 | // |
|
3 | // Copyright (c) 2015 Dave Transom |
3 | // Copyright (c) 2015 Dave Transom |
|
4 | // |
4 | // |
|
5 | // Permission is hereby granted, free of charge, to any person obtaining a |
5 | // Permission is hereby granted, free of charge, to any person obtaining a |
|
6 | // copy of this software and associated documentation files (the |
6 | // copy of this software and associated documentation files (the |
|
7 | // "Software"), to deal in the Software without restriction, including |
7 | // "Software"), to deal in the Software without restriction, including |
|
8 | // without limitation the rights to use, copy, modify, merge, publish, |
8 | // without limitation the rights to use, copy, modify, merge, publish, |
|
9 | // distribute, sublicense, and/or sell copies of the Software, and to |
9 | // distribute, sublicense, and/or sell copies of the Software, and to |
|
10 | // permit persons to whom the Software is furnished to do so, subject to |
10 | // permit persons to whom the Software is furnished to do so, subject to |
|
11 | // the following conditions: |
11 | // the following conditions: |
|
12 | // |
12 | // |
|
13 | // The above copyright notice and this permission notice shall be included |
13 | // The above copyright notice and this permission notice shall be included |
|
14 | // in all copies or substantial portions of the Software. |
14 | // in all copies or substantial portions of the Software. |
|
15 | // |
15 | // |
|
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
|
17 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
18 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
18 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
19 | // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
19 | // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|
20 | // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
20 | // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
|
Line 46... | Line 46... | |||
46 | /// Accept-Language: en-us,en;q=0.5 |
46 | /// Accept-Language: en-us,en;q=0.5 |
|
47 | /// </example> |
47 | /// </example> |
|
48 | [DebuggerDisplay("QValue[{Name}, {Weight}]")] |
48 | [DebuggerDisplay("QValue[{Name}, {Weight}]")] |
|
49 | public struct QValue : IComparable<QValue> |
49 | public struct QValue : IComparable<QValue> |
|
50 | { |
50 | { |
|
51 | private static readonly char[] delimiters = {';', '='}; |
51 | private static readonly char[] delimiters = { ';', '=' }; |
|
52 | private const float defaultWeight = 1; |
52 | private const float defaultWeight = 1; |
|
Line 53... | Line 53... | |||
53 | |
53 | |
|
Line 54... | Line 54... | |||
54 | #region Fields |
54 | #region Fields |
|
55 | |
55 | |
|
Line 56... | Line 56... | |||
56 | private float _weight; |
56 | private float _weight; |
|
Line 57... | Line 57... | |||
57 | private int _ordinal; |
57 | private int _ordinal; |
|
Line 58... | Line 58... | |||
58 | |
58 | |
|
59 | #endregion |
59 | #endregion Fields |
|
Line 87... | Line 87... | |||
87 | _ordinal = ordinal; |
87 | _ordinal = ordinal; |
|
Line 88... | Line 88... | |||
88 | |
88 | |
|
89 | ParseInternal(ref this, value); |
89 | ParseInternal(ref this, value); |
|
Line 90... | Line 90... | |||
90 | } |
90 | } |
|
Line 91... | Line 91... | |||
91 | |
91 | |
|
Line 92... | Line 92... | |||
92 | #endregion |
92 | #endregion Constructors |
|
93 | |
93 | |
|
Line 112... | Line 112... | |||
112 | /// <summary> |
112 | /// <summary> |
|
113 | /// Whether the value is empty (i.e. has no name) |
113 | /// Whether the value is empty (i.e. has no name) |
|
114 | /// </summary> |
114 | /// </summary> |
|
115 | public bool IsEmpty => string.IsNullOrEmpty(Name); |
115 | public bool IsEmpty => string.IsNullOrEmpty(Name); |
|
Line 116... | Line 116... | |||
116 | |
116 | |
|
Line 117... | Line 117... | |||
117 | #endregion |
117 | #endregion Properties |
|
Line 118... | Line 118... | |||
118 | |
118 | |
|
119 | #region Methods |
119 | #region Methods |
|
Line 163... | Line 163... | |||
163 | float.TryParse(parts[2], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture.NumberFormat, |
163 | float.TryParse(parts[2], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture.NumberFormat, |
|
164 | out target._weight); |
164 | out target._weight); |
|
165 | } |
165 | } |
|
166 | } |
166 | } |
|
Line 167... | Line 167... | |||
167 | |
167 | |
|
Line 168... | Line 168... | |||
168 | #endregion |
168 | #endregion Methods |
|
Line 169... | Line 169... | |||
169 | |
169 | |
|
170 | #region IComparable<QValue> Members |
170 | #region IComparable<QValue> Members |
|
Line 184... | Line 184... | |||
184 | value = ord.CompareTo(-other._ordinal); |
184 | value = ord.CompareTo(-other._ordinal); |
|
185 | } |
185 | } |
|
186 | return value; |
186 | return value; |
|
187 | } |
187 | } |
|
Line 188... | Line 188... | |||
188 | |
188 | |
|
Line 189... | Line 189... | |||
189 | #endregion |
189 | #endregion IComparable<QValue> Members |
|
Line 190... | Line 190... | |||
190 | |
190 | |
|
191 | #region CompareByWeight |
191 | #region CompareByWeight |
|
Line 210... | Line 210... | |||
210 | public static int CompareByWeightDesc(QValue x, QValue y) |
210 | public static int CompareByWeightDesc(QValue x, QValue y) |
|
211 | { |
211 | { |
|
212 | return -x.CompareTo(y); |
212 | return -x.CompareTo(y); |
|
213 | } |
213 | } |
|
Line 214... | Line 214... | |||
214 | |
214 | |
|
215 | #endregion |
215 | #endregion CompareByWeight |
|
Line 216... | Line 216... | |||
216 | } |
216 | } |
|
217 | |
217 | |
|
218 | /// <summary> |
218 | /// <summary> |
|
Line 223... | Line 223... | |||
223 | /// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html |
223 | /// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html |
|
224 | /// </remarks> |
224 | /// </remarks> |
|
225 | [DebuggerDisplay("QValue[{Count}, {AcceptWildcard}]")] |
225 | [DebuggerDisplay("QValue[{Count}, {AcceptWildcard}]")] |
|
226 | public sealed class QValueList : List<QValue> |
226 | public sealed class QValueList : List<QValue> |
|
227 | { |
227 | { |
|
228 | private static readonly char[] delimiters = {','}; |
228 | private static readonly char[] delimiters = { ',' }; |
|
Line 229... | Line 229... | |||
229 | |
229 | |
|
Line 230... | Line 230... | |||
230 | #region Add |
230 | #region Add |
|
231 | |
231 | |
|
Line 239... | Line 239... | |||
239 | base.Add(item); |
239 | base.Add(item); |
|
Line 240... | Line 240... | |||
240 | |
240 | |
|
241 | applyAutoSort(); |
241 | applyAutoSort(); |
|
Line 242... | Line 242... | |||
242 | } |
242 | } |
|
Line 243... | Line 243... | |||
243 | |
243 | |
|
Line 244... | Line 244... | |||
244 | #endregion |
244 | #endregion Add |
|
245 | |
245 | |
|
Line 259... | Line 259... | |||
259 | |
259 | |
|
260 | AutoSort = state; |
260 | AutoSort = state; |
|
261 | applyAutoSort(); |
261 | applyAutoSort(); |
|
Line 262... | Line 262... | |||
262 | } |
262 | } |
|
Line 263... | Line 263... | |||
263 | |
263 | |
|
Line 264... | Line 264... | |||
264 | #endregion |
264 | #endregion AddRange |
|
265 | |
265 | |
|
Line 275... | Line 275... | |||
275 | Predicate<QValue> criteria = |
275 | Predicate<QValue> criteria = |
|
276 | item => item.Name.Equals(name, StringComparison.OrdinalIgnoreCase); |
276 | item => item.Name.Equals(name, StringComparison.OrdinalIgnoreCase); |
|
277 | return Find(criteria); |
277 | return Find(criteria); |
|
278 | } |
278 | } |
|
Line 279... | Line 279... | |||
279 | |
279 | |
|
Line 280... | Line 280... | |||
280 | #endregion |
280 | #endregion Find |
|
Line 281... | Line 281... | |||
281 | |
281 | |
|
282 | #region FindHighestWeight |
282 | #region FindHighestWeight |
|
Line 295... | Line 295... | |||
295 | { |
295 | { |
|
296 | Predicate<QValue> criteria = item => isCandidate(item.Name, candidates); |
296 | Predicate<QValue> criteria = item => isCandidate(item.Name, candidates); |
|
297 | return Find(criteria); |
297 | return Find(criteria); |
|
298 | } |
298 | } |
|
Line 299... | Line 299... | |||
299 | |
299 | |
|
Line 300... | Line 300... | |||
300 | #endregion |
300 | #endregion FindHighestWeight |
|
Line 301... | Line 301... | |||
301 | |
301 | |
|
302 | #region FindPreferred |
302 | #region FindPreferred |
|
Line 316... | Line 316... | |||
316 | Predicate<QValue> criteria = |
316 | Predicate<QValue> criteria = |
|
317 | item => isCandidate(item.Name, candidates) && item.CanAccept; |
317 | item => isCandidate(item.Name, candidates) && item.CanAccept; |
|
318 | return Find(criteria); |
318 | return Find(criteria); |
|
319 | } |
319 | } |
|
Line 320... | Line 320... | |||
320 | |
320 | |
|
Line 321... | Line 321... | |||
321 | #endregion |
321 | #endregion FindPreferred |
|
Line 322... | Line 322... | |||
322 | |
322 | |
|
323 | #region DefaultSort |
323 | #region DefaultSort |
|
Line 329... | Line 329... | |||
329 | public void DefaultSort() |
329 | public void DefaultSort() |
|
330 | { |
330 | { |
|
331 | Sort(QValue.CompareByWeightDesc); |
331 | Sort(QValue.CompareByWeightDesc); |
|
332 | } |
332 | } |
|
Line 333... | Line 333... | |||
333 | |
333 | |
|
Line 334... | Line 334... | |||
334 | #endregion |
334 | #endregion DefaultSort |
|
Line 335... | Line 335... | |||
335 | |
335 | |
|
336 | #region applyAutoSort |
336 | #region applyAutoSort |
|
Line 343... | Line 343... | |||
343 | { |
343 | { |
|
344 | if (AutoSort) |
344 | if (AutoSort) |
|
345 | DefaultSort(); |
345 | DefaultSort(); |
|
346 | } |
346 | } |
|
Line 347... | Line 347... | |||
347 | |
347 | |
|
Line 348... | Line 348... | |||
348 | #endregion |
348 | #endregion applyAutoSort |
|
Line 349... | Line 349... | |||
349 | |
349 | |
|
350 | #region isCandidate |
350 | #region isCandidate |
|
Line 364... | Line 364... | |||
364 | return true; |
364 | return true; |
|
365 | } |
365 | } |
|
366 | return false; |
366 | return false; |
|
367 | } |
367 | } |
|
Line 368... | Line 368... | |||
368 | |
368 | |
|
369 | #endregion |
- | ||
370 | |
- | ||
371 | #region Fields |
- | ||
372 | |
- | ||
Line 373... | Line 369... | |||
373 | #endregion |
369 | #endregion isCandidate |
|
Line 374... | Line 370... | |||
374 | |
370 | |
|
375 | #region Constructors |
371 | #region Constructors |
|
Line 411... | Line 407... | |||
411 | /// methods like FindPreferred to work correctly |
407 | /// methods like FindPreferred to work correctly |
|
412 | DefaultSort(); |
408 | DefaultSort(); |
|
413 | AutoSort = true; |
409 | AutoSort = true; |
|
414 | } |
410 | } |
|
Line 415... | Line 411... | |||
415 | |
411 | |
|
Line 416... | Line 412... | |||
416 | #endregion |
412 | #endregion Constructors |
|
Line 417... | Line 413... | |||
417 | |
413 | |
|
418 | #region Properties |
414 | #region Properties |
|
Line 432... | Line 428... | |||
432 | /// </summary> |
428 | /// </summary> |
|
433 | /// <param name="candidates">The preferred order in which to return an encoding</param> |
429 | /// <param name="candidates">The preferred order in which to return an encoding</param> |
|
434 | /// <returns>An QValue based on weight, or null</returns> |
430 | /// <returns>An QValue based on weight, or null</returns> |
|
435 | public QValue this[params string[] candidates] => FindPreferred(candidates); |
431 | public QValue this[params string[] candidates] => FindPreferred(candidates); |
|
Line 436... | Line 432... | |||
436 | |
432 | |
|
437 | #endregion |
433 | #endregion Properties |
|
438 | } |
- | ||
439 | } |
434 | } |
|
- | 435 | } |