< Summary

Information
Class: System.Net.Http.HPack.IntegerEncoder
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\Common\src\System\Net\Http\aspnetcore\Http2\Hpack\IntegerEncoder.cs
Line coverage
60%
Covered lines: 21
Uncovered lines: 14
Coverable lines: 35
Total lines: 77
Line coverage: 60%
Branch coverage
50%
Covered branches: 6
Total branches: 12
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Encode(...)50%121258.82%
MaskHigh(...)100%11100%

File(s)

D:\runner\runtime\src\libraries\Common\src\System\Net\Http\aspnetcore\Http2\Hpack\IntegerEncoder.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.Diagnostics;
 5
 6namespace System.Net.Http.HPack
 7{
 8    internal static class IntegerEncoder
 9    {
 10        /// <summary>
 11        /// The maximum bytes required to encode a 32-bit int, regardless of prefix length.
 12        /// </summary>
 13        public const int MaxInt32EncodedLength = 6;
 14
 15        /// <summary>
 16        /// Encodes an integer into one or more bytes.
 17        /// </summary>
 18        /// <param name="value">The value to encode. Must not be negative.</param>
 19        /// <param name="numBits">The length of the prefix, in bits, to encode <paramref name="value"/> within. Must be 
 20        /// <param name="destination">The destination span to encode <paramref name="value"/> to.</param>
 21        /// <param name="bytesWritten">The number of bytes used to encode <paramref name="value"/>.</param>
 22        /// <returns>If <paramref name="destination"/> had enough storage to encode <paramref name="value"/>, true. Othe
 23        public static bool Encode(int value, int numBits, Span<byte> destination, out int bytesWritten)
 19624        {
 19625            Debug.Assert(value >= 0);
 19626            Debug.Assert(numBits >= 1 && numBits <= 8);
 27
 19628            if (destination.Length == 0)
 029            {
 030                bytesWritten = 0;
 031                return false;
 32            }
 33
 19634            destination[0] &= MaskHigh(8 - numBits);
 35
 19636            if (value < (1 << numBits) - 1)
 7237            {
 7238                destination[0] |= (byte)value;
 39
 7240                bytesWritten = 1;
 7241                return true;
 42            }
 43            else
 12444            {
 12445                destination[0] |= (byte)((1 << numBits) - 1);
 46
 12447                if (1 == destination.Length)
 048                {
 049                    bytesWritten = 0;
 050                    return false;
 51                }
 52
 12453                value -= ((1 << numBits) - 1);
 12454                int i = 1;
 55
 12456                while (value >= 128)
 057                {
 058                    destination[i++] = (byte)(value % 128 + 128);
 59
 060                    if (i >= destination.Length)
 061                    {
 062                        bytesWritten = 0;
 063                        return false;
 64                    }
 65
 066                    value /= 128;
 067                }
 12468                destination[i++] = (byte)value;
 69
 12470                bytesWritten = i;
 12471                return true;
 72            }
 19673        }
 74
 19675        private static byte MaskHigh(int n) => (byte)(sbyte.MinValue >> (n - 1));
 76    }
 77}