| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | // Copied from https://github.com/aspnet/AspNetCore/tree/master/src/Http/WebUtilities/src |
| | | 5 | | |
| | | 6 | | using System; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | |
| | | 9 | | #pragma warning disable IDE0008 // Use explicit type |
| | | 10 | | #pragma warning disable IDE0018 // Inline declaration |
| | | 11 | | #pragma warning disable IDE0034 // default can be simplified |
| | | 12 | | |
| | | 13 | | namespace Azure.Core.Http.Multipart |
| | | 14 | | { |
| | | 15 | | internal struct KeyValueAccumulator |
| | | 16 | | { |
| | | 17 | | private Dictionary<string, StringValues> _accumulator; |
| | | 18 | | private Dictionary<string, List<string>> _expandingAccumulator; |
| | | 19 | | |
| | | 20 | | public void Append(string key, string value) |
| | | 21 | | { |
| | 2540 | 22 | | if (_accumulator == null) |
| | | 23 | | { |
| | 1272 | 24 | | _accumulator = new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | StringValues values; |
| | 2540 | 28 | | if (_accumulator.TryGetValue(key, out values)) |
| | | 29 | | { |
| | 0 | 30 | | if (values.Count == 0) |
| | | 31 | | { |
| | | 32 | | // Marker entry for this key to indicate entry already in expanding list dictionary |
| | 0 | 33 | | _expandingAccumulator[key].Add(value); |
| | | 34 | | } |
| | 0 | 35 | | else if (values.Count == 1) |
| | | 36 | | { |
| | | 37 | | // Second value for this key |
| | 0 | 38 | | _accumulator[key] = new string[] { values[0], value }; |
| | | 39 | | } |
| | | 40 | | else |
| | | 41 | | { |
| | | 42 | | // Third value for this key |
| | | 43 | | // Add zero count entry and move to data to expanding list dictionary |
| | 0 | 44 | | _accumulator[key] = default(StringValues); |
| | | 45 | | |
| | 0 | 46 | | if (_expandingAccumulator == null) |
| | | 47 | | { |
| | 0 | 48 | | _expandingAccumulator = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | // Already 3 entries so use starting allocated as 8; then use List's expansion mechanism for more |
| | 0 | 52 | | var list = new List<string>(8); |
| | 0 | 53 | | var array = values.ToArray(); |
| | | 54 | | |
| | 0 | 55 | | list.Add(array[0]); |
| | 0 | 56 | | list.Add(array[1]); |
| | 0 | 57 | | list.Add(value); |
| | | 58 | | |
| | 0 | 59 | | _expandingAccumulator[key] = list; |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | else |
| | | 63 | | { |
| | | 64 | | // First value for this key |
| | 2540 | 65 | | _accumulator[key] = new StringValues(value); |
| | | 66 | | } |
| | | 67 | | |
| | 2540 | 68 | | ValueCount++; |
| | 2540 | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | public bool HasValues => ValueCount > 0; |
| | | 72 | | |
| | 2540 | 73 | | public int KeyCount => _accumulator?.Count ?? 0; |
| | | 74 | | |
| | 5080 | 75 | | public int ValueCount { get; private set; } |
| | | 76 | | |
| | | 77 | | public Dictionary<string, StringValues> GetResults() |
| | | 78 | | { |
| | 1272 | 79 | | if (_expandingAccumulator != null) |
| | | 80 | | { |
| | | 81 | | // Coalesce count 3+ multi-value entries into _accumulator dictionary |
| | 0 | 82 | | foreach (var entry in _expandingAccumulator) |
| | | 83 | | { |
| | 0 | 84 | | _accumulator[entry.Key] = new StringValues(entry.Value.ToArray()); |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | |
| | 1272 | 88 | | return _accumulator ?? new Dictionary<string, StringValues>(0, StringComparer.OrdinalIgnoreCase); |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | } |