< 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)
 40821        {
 40822            _header = descriptor;
 40823            _value = value;
 40824        }
 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)
 496630        {
 496631            _header = descriptor;
 496632            _value = values;
 496633        }
 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>
 537454        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)
 537475            {
 537476                if (value is string s)
 40877                {
 40878                    _values = null;
 40879                    _current = s;
 40880                }
 81                else
 496682                {
 496683                    _values = value as string[];
 496684                    _current = null;
 496685                }
 86
 537487                _index = 0;
 537488            }
 89
 90            /// <inheritdoc/>
 91            public bool MoveNext()
 6714092            {
 6714093                int index = _index;
 6714094                if (index < 0)
 40895                {
 40896                    return false;
 97                }
 98
 6673299                string[]? values = _values;
 66732100                if (values != null)
 66324101                {
 66324102                    if ((uint)index < (uint)values.Length)
 61358103                    {
 61358104                        _index = index + 1;
 61358105                        _current = values[index];
 61358106                        return true;
 107                    }
 108
 4966109                    _index = -1;
 4966110                    return false;
 111                }
 112
 408113                _index = -1;
 408114                return _current != null;
 67140115            }
 116
 117            /// <inheritdoc/>
 61766118            public string Current => _current!;
 119
 120            /// <inheritdoc/>
 0121            object IEnumerator.Current => Current;
 122
 123            /// <inheritdoc/>
 10748124            public void Dispose() { }
 125
 126            /// <inheritdoc/>
 0127            void IEnumerator.Reset() => throw new NotSupportedException();
 128        }
 129    }
 130}