< Summary

Information
Class: System.Net.Http.Headers.HeaderStringValues
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\HeaderStringValues.cs
Line coverage
71%
Covered lines: 41
Uncovered lines: 16
Coverable lines: 57
Total lines: 130
Line coverage: 71.9%
Branch coverage
50%
Covered branches: 8
Total branches: 16
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
ToString()0%440%
GetEnumerator()100%11100%
System.Collections.Generic.IEnumerable<System.String>.GetEnumerator()100%110%
System.Collections.IEnumerable.GetEnumerator()100%110%
.ctor(...)100%22100%
MoveNext()100%66100%
Dispose()100%11100%
System.Collections.IEnumerator.Reset()100%110%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\HeaderStringValues.cs

#LineLine coverage
 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
 4using System.Collections;
 5using System.Collections.Generic;
 6
 7namespace 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)
 208521        {
 208522            _header = descriptor;
 208523            _value = value;
 208524        }
 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)
 1165930        {
 1165931            _header = descriptor;
 1165932            _value = values;
 1165933        }
 34
 35        /// <summary>Gets the number of header values in the collection.</summary>
 036        public int Count => _value switch
 037        {
 038            string => 1,
 039            string[] values => values.Length,
 040            _ => 0
 041        };
 42
 43        /// <summary>Gets a string containing all the headers in the collection.</summary>
 44        /// <returns></returns>
 045        public override string ToString() => _value switch
 046        {
 047            string value => value,
 048            string[] values => string.Join(_header.Separator, values),
 049            _ => string.Empty,
 050        };
 51
 52        /// <summary>Gets an enumerator for all of the strings in the collection.</summary>
 53        /// <returns></returns>
 1374454        public Enumerator GetEnumerator() => new Enumerator(_value);
 55
 56        /// <inheritdoc/>
 057        IEnumerator<string> IEnumerable<string>.GetEnumerator() => GetEnumerator();
 58
 59        /// <inheritdoc/>
 060        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)
 1374475            {
 1374476                if (value is string s)
 208577                {
 208578                    _values = null;
 208579                    _current = s;
 208580                }
 81                else
 1165982                {
 1165983                    _values = value as string[];
 1165984                    _current = null;
 1165985                }
 86
 1374487                _index = 0;
 1374488            }
 89
 90            /// <inheritdoc/>
 91            public bool MoveNext()
 51033992            {
 51033993                int index = _index;
 51033994                if (index < 0)
 208595                {
 208596                    return false;
 97                }
 98
 50825499                string[]? values = _values;
 508254100                if (values != null)
 506169101                {
 506169102                    if ((uint)index < (uint)values.Length)
 494510103                    {
 494510104                        _index = index + 1;
 494510105                        _current = values[index];
 494510106                        return true;
 107                    }
 108
 11659109                    _index = -1;
 11659110                    return false;
 111                }
 112
 2085113                _index = -1;
 2085114                return _current != null;
 510339115            }
 116
 117            /// <inheritdoc/>
 496595118            public string Current => _current!;
 119
 120            /// <inheritdoc/>
 0121            object IEnumerator.Current => Current;
 122
 123            /// <inheritdoc/>
 27488124            public void Dispose() { }
 125
 126            /// <inheritdoc/>
 0127            void IEnumerator.Reset() => throw new NotSupportedException();
 128        }
 129    }
 130}