< Summary

Information
Class: System.Net.Http.Headers.RangeHeaderValue
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\RangeHeaderValue.cs
Line coverage
45%
Covered lines: 51
Uncovered lines: 62
Coverable lines: 113
Total lines: 182
Line coverage: 45.1%
Branch coverage
50%
Covered branches: 19
Total branches: 38
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%110%
.ctor(...)0%440%
ToString()80%101086.95%
Equals(...)0%440%
GetHashCode()0%440%
Parse(...)100%110%
TryParse(...)0%220%
GetRangeLength(...)83.33%121292.85%
System.ICloneable.Clone()100%110%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\RangeHeaderValue.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 RangeHeaderValue : ICloneable
 13    {
 14        private string _unit;
 15        private UnvalidatedObjectCollection<RangeItemHeaderValue>? _ranges;
 16
 17        public string Unit
 18        {
 019            get { return _unit; }
 20            set
 021            {
 022                HeaderUtilities.CheckValidToken(value);
 023                _unit = value;
 024            }
 25        }
 26
 9627        public ICollection<RangeItemHeaderValue> Ranges => _ranges ??= new UnvalidatedObjectCollection<RangeItemHeaderVa
 28
 10829        public RangeHeaderValue()
 10830        {
 10831            _unit = HeaderUtilities.BytesUnit;
 10832        }
 33
 034        public RangeHeaderValue(long? from, long? to)
 035        {
 36            // convenience ctor: "Range: bytes=from-to"
 037            _unit = HeaderUtilities.BytesUnit;
 038            Ranges.Add(new RangeItemHeaderValue(from, to));
 039        }
 40
 041        private RangeHeaderValue(RangeHeaderValue source)
 042        {
 043            Debug.Assert(source != null);
 44
 045            _unit = source._unit;
 046            if (source._ranges != null)
 047            {
 048                foreach (RangeItemHeaderValue range in source._ranges)
 049                {
 050                    this.Ranges.Add(new RangeItemHeaderValue(range));
 051                }
 052            }
 053        }
 54
 55        public override string ToString()
 3356        {
 3357            var sb = new ValueStringBuilder(stackalloc char[256]);
 3358            sb.Append(_unit);
 3359            sb.Append('=');
 60
 3361            if (_ranges != null)
 3362            {
 3363                bool first = true;
 16564                foreach (RangeItemHeaderValue range in _ranges)
 3365                {
 3366                    if (first)
 3367                    {
 3368                        first = false;
 3369                    }
 70                    else
 071                    {
 072                        sb.Append(", ");
 073                    }
 74
 4875                    if (range.From.HasValue) sb.AppendSpanFormattable(range.From.GetValueOrDefault());
 3376                    sb.Append('-');
 5777                    if (range.To.HasValue) sb.AppendSpanFormattable(range.To.GetValueOrDefault());
 3378                }
 3379            }
 80
 3381            return sb.ToString();
 3382        }
 83
 84        public override bool Equals([NotNullWhen(true)] object? obj)
 085        {
 086            RangeHeaderValue? other = obj as RangeHeaderValue;
 87
 088            if (other == null)
 089            {
 090                return false;
 91            }
 92
 093            return string.Equals(_unit, other._unit, StringComparison.OrdinalIgnoreCase) &&
 094                HeaderUtilities.AreEqualCollections(_ranges, other._ranges);
 095        }
 96
 97        public override int GetHashCode()
 098        {
 099            int result = StringComparer.OrdinalIgnoreCase.GetHashCode(_unit);
 100
 0101            if (_ranges != null)
 0102            {
 0103                foreach (RangeItemHeaderValue range in _ranges)
 0104                {
 0105                    result ^= range.GetHashCode();
 0106                }
 0107            }
 108
 0109            return result;
 0110        }
 111
 112        public static RangeHeaderValue Parse(string input)
 0113        {
 0114            int index = 0;
 0115            return (RangeHeaderValue)GenericHeaderParser.RangeParser.ParseValue(input, null, ref index);
 0116        }
 117
 118        public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out RangeHeaderValue? parsedV
 0119        {
 0120            int index = 0;
 0121            parsedValue = null;
 122
 0123            if (GenericHeaderParser.RangeParser.TryParseValue(input, null, ref index, out object? output))
 0124            {
 0125                parsedValue = (RangeHeaderValue)output!;
 0126                return true;
 127            }
 0128            return false;
 0129        }
 130
 131        internal static int GetRangeLength(string? input, int startIndex, out object? parsedValue)
 114132        {
 114133            Debug.Assert(startIndex >= 0);
 134
 114135            parsedValue = null;
 136
 114137            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
 0138            {
 0139                return 0;
 140            }
 141
 142            // Parse the unit string: <unit> in '<unit>=<from1>-<to1>, <from2>-<to2>'
 114143            int unitLength = HttpRuleParser.GetTokenLength(input, startIndex);
 144
 114145            if (unitLength == 0)
 6146            {
 6147                return 0;
 148            }
 149
 108150            RangeHeaderValue result = new RangeHeaderValue();
 108151            result._unit = input.Substring(startIndex, unitLength);
 108152            int current = startIndex + unitLength;
 108153            current += HttpRuleParser.GetWhitespaceLength(input, current);
 154
 108155            if ((current == input.Length) || (input[current] != '='))
 12156            {
 12157                return 0;
 158            }
 159
 96160            current++; // skip '=' separator
 96161            current += HttpRuleParser.GetWhitespaceLength(input, current);
 162
 96163            int rangesLength = RangeItemHeaderValue.GetRangeItemListLength(input, current, result.Ranges);
 164
 96165            if (rangesLength == 0)
 74166            {
 74167                return 0;
 168            }
 169
 22170            current += rangesLength;
 22171            Debug.Assert(current == input.Length, "GetRangeItemListLength() should consume the whole string or fail.");
 172
 22173            parsedValue = result;
 22174            return current - startIndex;
 114175        }
 176
 177        object ICloneable.Clone()
 0178        {
 0179            return new RangeHeaderValue(this);
 0180        }
 181    }
 182}