< 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
47%
Covered lines: 54
Uncovered lines: 59
Coverable lines: 113
Total lines: 182
Line coverage: 47.7%
Branch coverage
57%
Covered branches: 22
Total branches: 38
Branch coverage: 57.8%
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()100%1010100%
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
 106427        public ICollection<RangeItemHeaderValue> Ranges => _ranges ??= new UnvalidatedObjectCollection<RangeItemHeaderVa
 28
 107429        public RangeHeaderValue()
 107430        {
 107431            _unit = HeaderUtilities.BytesUnit;
 107432        }
 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()
 72956        {
 72957            var sb = new ValueStringBuilder(stackalloc char[256]);
 72958            sb.Append(_unit);
 72959            sb.Append('=');
 60
 72961            if (_ranges != null)
 72962            {
 72963                bool first = true;
 11000164                foreach (RangeItemHeaderValue range in _ranges)
 5390765                {
 5390766                    if (first)
 72967                    {
 72968                        first = false;
 72969                    }
 70                    else
 5317871                    {
 5317872                        sb.Append(", ");
 5317873                    }
 74
 10207275                    if (range.From.HasValue) sb.AppendSpanFormattable(range.From.GetValueOrDefault());
 5390776                    sb.Append('-');
 7061477                    if (range.To.HasValue) sb.AppendSpanFormattable(range.To.GetValueOrDefault());
 5390778                }
 72979            }
 80
 72981            return sb.ToString();
 72982        }
 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)
 1080132        {
 1080133            Debug.Assert(startIndex >= 0);
 134
 1080135            parsedValue = null;
 136
 1080137            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>'
 1080143            int unitLength = HttpRuleParser.GetTokenLength(input, startIndex);
 144
 1080145            if (unitLength == 0)
 6146            {
 6147                return 0;
 148            }
 149
 1074150            RangeHeaderValue result = new RangeHeaderValue();
 1074151            result._unit = input.Substring(startIndex, unitLength);
 1074152            int current = startIndex + unitLength;
 1074153            current += HttpRuleParser.GetWhitespaceLength(input, current);
 154
 1074155            if ((current == input.Length) || (input[current] != '='))
 10156            {
 10157                return 0;
 158            }
 159
 1064160            current++; // skip '=' separator
 1064161            current += HttpRuleParser.GetWhitespaceLength(input, current);
 162
 1064163            int rangesLength = RangeItemHeaderValue.GetRangeItemListLength(input, current, result.Ranges);
 164
 1064165            if (rangesLength == 0)
 578166            {
 578167                return 0;
 168            }
 169
 486170            current += rangesLength;
 486171            Debug.Assert(current == input.Length, "GetRangeItemListLength() should consume the whole string or fail.");
 172
 486173            parsedValue = result;
 486174            return current - startIndex;
 1080175        }
 176
 177        object ICloneable.Clone()
 0178        {
 0179            return new RangeHeaderValue(this);
 0180        }
 181    }
 182}