< Summary

Line coverage
48%
Covered lines: 40
Uncovered lines: 42
Coverable lines: 82
Total lines: 146
Line coverage: 48.7%
Branch coverage
55%
Covered branches: 11
Total branches: 20
Branch coverage: 55%
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%110%
.ctor(...)100%110%
Parse(...)100%110%
TryParse(...)0%220%
GetTransferCodingLength(...)83.33%121293.54%
ToString()100%11100%
Equals(...)0%440%
GetHashCode()100%110%
System.ICloneable.Clone()100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Net.Http/src/System/Net/Http/Headers/TransferCodingHeaderValue.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.Generic;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.IO;
 8using System.Text;
 9
 10namespace System.Net.Http.Headers
 11{
 12    public class TransferCodingHeaderValue : ICloneable
 13    {
 14        // Use UnvalidatedObjectCollection<T> since we may have multiple parameters with the same name.
 15        private UnvalidatedObjectCollection<NameValueHeaderValue>? _parameters;
 2566316        private string _value = null!; // empty constructor only used internally and value set with non null
 17
 018        public string Value => _value;
 19
 435620        public ICollection<NameValueHeaderValue> Parameters => _parameters ??= new UnvalidatedObjectCollection<NameValue
 21
 2566322        internal TransferCodingHeaderValue()
 2566323        {
 2566324        }
 25
 026        protected TransferCodingHeaderValue(TransferCodingHeaderValue source)
 027        {
 028            Debug.Assert(source != null);
 29
 030            _value = source._value;
 031            _parameters = source._parameters.Clone();
 032        }
 33
 034        public TransferCodingHeaderValue(string value)
 035        {
 036            HeaderUtilities.CheckValidToken(value);
 037            _value = value;
 038        }
 39
 40        public static TransferCodingHeaderValue Parse(string input)
 041        {
 042            int index = 0;
 043            return (TransferCodingHeaderValue)TransferCodingHeaderParser.SingleValueParser.ParseValue(
 044                input, null, ref index);
 045        }
 46
 47        public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out TransferCodingHeaderValue
 048        {
 049            int index = 0;
 050            parsedValue = null;
 51
 052            if (TransferCodingHeaderParser.SingleValueParser.TryParseValue(input, null, ref index, out object? output))
 053            {
 054                parsedValue = (TransferCodingHeaderValue)output!;
 055                return true;
 56            }
 057            return false;
 058        }
 59
 60        internal static int GetTransferCodingLength(string input, int startIndex,
 61            Func<TransferCodingHeaderValue> transferCodingCreator, out TransferCodingHeaderValue? parsedValue)
 2582962        {
 2582963            Debug.Assert(transferCodingCreator != null);
 2582964            Debug.Assert(startIndex >= 0);
 65
 2582966            parsedValue = null;
 67
 2582968            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
 069            {
 070                return 0;
 71            }
 72
 73            // Caller must remove leading whitespace. If not, we'll return 0.
 2582974            int valueLength = HttpRuleParser.GetTokenLength(input, startIndex);
 75
 2582976            if (valueLength == 0)
 16677            {
 16678                return 0;
 79            }
 80
 2566381            string value = input.Substring(startIndex, valueLength);
 2566382            int current = startIndex + valueLength;
 2566383            current += HttpRuleParser.GetWhitespaceLength(input, current);
 84            TransferCodingHeaderValue transferCodingHeader;
 85
 86            // If we're not done and we have a parameter delimiter, then we have a list of parameters.
 2566387            if ((current < input.Length) && (input[current] == ';'))
 435688            {
 435689                transferCodingHeader = transferCodingCreator();
 435690                transferCodingHeader._value = value;
 91
 435692                current++; // skip delimiter.
 435693                int parameterLength = NameValueHeaderValue.GetNameValueListLength(input, current, ';',
 435694                    (UnvalidatedObjectCollection<NameValueHeaderValue>)transferCodingHeader.Parameters);
 95
 435696                if (parameterLength == 0)
 15497                {
 15498                    return 0;
 99                }
 100
 4202101                parsedValue = transferCodingHeader;
 4202102                return current + parameterLength - startIndex;
 103            }
 104
 105            // We have a transfer coding without parameters.
 21307106            transferCodingHeader = transferCodingCreator();
 21307107            transferCodingHeader._value = value;
 21307108            parsedValue = transferCodingHeader;
 21307109            return current - startIndex;
 25829110        }
 111
 112        public override string ToString()
 35725113        {
 35725114            StringBuilder sb = StringBuilderCache.Acquire();
 35725115            sb.Append(_value);
 35725116            NameValueHeaderValue.ToString(_parameters, ';', true, sb);
 35725117            return StringBuilderCache.GetStringAndRelease(sb);
 35725118        }
 119
 120        public override bool Equals([NotNullWhen(true)] object? obj)
 0121        {
 0122            TransferCodingHeaderValue? other = obj as TransferCodingHeaderValue;
 123
 0124            if (other == null)
 0125            {
 0126                return false;
 127            }
 128
 0129            return string.Equals(_value, other._value, StringComparison.OrdinalIgnoreCase) &&
 0130                HeaderUtilities.AreEqualCollections(_parameters, other._parameters);
 0131        }
 132
 133        public override int GetHashCode()
 0134        {
 135            // The value string is case-insensitive.
 0136            return StringComparer.OrdinalIgnoreCase.GetHashCode(_value) ^ NameValueHeaderValue.GetHashCode(_parameters);
 0137        }
 138
 139        // Implement ICloneable explicitly to allow derived types to "override" the implementation.
 140        object ICloneable.Clone()
 0141        {
 0142            return new TransferCodingHeaderValue(this);
 0143        }
 144    }
 145}
 146