| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http.Headers |
| | | 8 | | { |
| | | 9 | | /// <summary>Provides a collection of header string values.</summary> |
| | | 10 | | public readonly struct HeaderStringValues : IReadOnlyCollection<string> |
| | | 11 | | { |
| | | 12 | | /// <summary>The associated header. This is used only for producing a string from <see cref="_value"/> when it' |
| | | 13 | | private readonly HeaderDescriptor _header; |
| | | 14 | | /// <summary>A string or string array (or null if the instance is default).</summary> |
| | | 15 | | private readonly object _value; |
| | | 16 | | |
| | | 17 | | /// <summary>Initializes the instance.</summary> |
| | | 18 | | /// <param name="descriptor">The header descriptor associated with the header value.</param> |
| | | 19 | | /// <param name="value">The header value.</param> |
| | | 20 | | internal HeaderStringValues(HeaderDescriptor descriptor, string value) |
| | 2085 | 21 | | { |
| | 2085 | 22 | | _header = descriptor; |
| | 2085 | 23 | | _value = value; |
| | 2085 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary>Initializes the instance.</summary> |
| | | 27 | | /// <param name="descriptor">The header descriptor associated with the header values.</param> |
| | | 28 | | /// <param name="values">The header values.</param> |
| | | 29 | | internal HeaderStringValues(HeaderDescriptor descriptor, string[] values) |
| | 11659 | 30 | | { |
| | 11659 | 31 | | _header = descriptor; |
| | 11659 | 32 | | _value = values; |
| | 11659 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary>Gets the number of header values in the collection.</summary> |
| | 0 | 36 | | public int Count => _value switch |
| | 0 | 37 | | { |
| | 0 | 38 | | string => 1, |
| | 0 | 39 | | string[] values => values.Length, |
| | 0 | 40 | | _ => 0 |
| | 0 | 41 | | }; |
| | | 42 | | |
| | | 43 | | /// <summary>Gets a string containing all the headers in the collection.</summary> |
| | | 44 | | /// <returns></returns> |
| | 0 | 45 | | public override string ToString() => _value switch |
| | 0 | 46 | | { |
| | 0 | 47 | | string value => value, |
| | 0 | 48 | | string[] values => string.Join(_header.Separator, values), |
| | 0 | 49 | | _ => string.Empty, |
| | 0 | 50 | | }; |
| | | 51 | | |
| | | 52 | | /// <summary>Gets an enumerator for all of the strings in the collection.</summary> |
| | | 53 | | /// <returns></returns> |
| | 13744 | 54 | | public Enumerator GetEnumerator() => new Enumerator(_value); |
| | | 55 | | |
| | | 56 | | /// <inheritdoc/> |
| | 0 | 57 | | IEnumerator<string> IEnumerable<string>.GetEnumerator() => GetEnumerator(); |
| | | 58 | | |
| | | 59 | | /// <inheritdoc/> |
| | 0 | 60 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 61 | | |
| | | 62 | | /// <summary>Enumerates the elements of a <see cref="HeaderStringValues"/>.</summary> |
| | | 63 | | public struct Enumerator : IEnumerator<string> |
| | | 64 | | { |
| | | 65 | | /// <summary>If this wraps a string[], that array. Otherwise, null.</summary> |
| | | 66 | | private readonly string[]? _values; |
| | | 67 | | /// <summary>The current string header value. If this wraps a single string, that string.</summary> |
| | | 68 | | private string? _current; |
| | | 69 | | /// <summary>Current state of the iteration.</summary> |
| | | 70 | | private int _index; |
| | | 71 | | |
| | | 72 | | /// <summary>Initializes the enumerator with a string or string[].</summary> |
| | | 73 | | /// <param name="value">The string or string[] value, or null if this collection is empty.</param> |
| | | 74 | | internal Enumerator(object value) |
| | 13744 | 75 | | { |
| | 13744 | 76 | | if (value is string s) |
| | 2085 | 77 | | { |
| | 2085 | 78 | | _values = null; |
| | 2085 | 79 | | _current = s; |
| | 2085 | 80 | | } |
| | | 81 | | else |
| | 11659 | 82 | | { |
| | 11659 | 83 | | _values = value as string[]; |
| | 11659 | 84 | | _current = null; |
| | 11659 | 85 | | } |
| | | 86 | | |
| | 13744 | 87 | | _index = 0; |
| | 13744 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <inheritdoc/> |
| | | 91 | | public bool MoveNext() |
| | 510339 | 92 | | { |
| | 510339 | 93 | | int index = _index; |
| | 510339 | 94 | | if (index < 0) |
| | 2085 | 95 | | { |
| | 2085 | 96 | | return false; |
| | | 97 | | } |
| | | 98 | | |
| | 508254 | 99 | | string[]? values = _values; |
| | 508254 | 100 | | if (values != null) |
| | 506169 | 101 | | { |
| | 506169 | 102 | | if ((uint)index < (uint)values.Length) |
| | 494510 | 103 | | { |
| | 494510 | 104 | | _index = index + 1; |
| | 494510 | 105 | | _current = values[index]; |
| | 494510 | 106 | | return true; |
| | | 107 | | } |
| | | 108 | | |
| | 11659 | 109 | | _index = -1; |
| | 11659 | 110 | | return false; |
| | | 111 | | } |
| | | 112 | | |
| | 2085 | 113 | | _index = -1; |
| | 2085 | 114 | | return _current != null; |
| | 510339 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <inheritdoc/> |
| | 496595 | 118 | | public string Current => _current!; |
| | | 119 | | |
| | | 120 | | /// <inheritdoc/> |
| | 0 | 121 | | object IEnumerator.Current => Current; |
| | | 122 | | |
| | | 123 | | /// <inheritdoc/> |
| | 27488 | 124 | | public void Dispose() { } |
| | | 125 | | |
| | | 126 | | /// <inheritdoc/> |
| | 0 | 127 | | void IEnumerator.Reset() => throw new NotSupportedException(); |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | } |