< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 26
Coverable lines: 26
Total lines: 61
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
DecodeUtf8(...)0%440%
DecodeUtf8Prefixed(...)0%220%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/EncodingHelper.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.Buffers;
 5using System.Diagnostics;
 6using System.Reflection.Metadata;
 7using System.Runtime.InteropServices;
 8
 9namespace System.Reflection.Internal
 10{
 11    /// <summary>
 12    /// Provides helpers to decode strings from unmanaged memory to System.String while avoiding
 13    /// intermediate allocation.
 14    /// </summary>
 15    internal static unsafe class EncodingHelper
 16    {
 17        public static string DecodeUtf8(byte* bytes, int byteCount, byte[]? prefix, MetadataStringDecoder utf8Decoder)
 018        {
 019            Debug.Assert(utf8Decoder != null);
 20
 021            if (prefix != null)
 022            {
 023                return DecodeUtf8Prefixed(bytes, byteCount, prefix, utf8Decoder);
 24            }
 25
 026            if (byteCount == 0)
 027            {
 028                return string.Empty;
 29            }
 30
 031            return utf8Decoder.GetString(bytes, byteCount);
 032        }
 33
 34        private static string DecodeUtf8Prefixed(byte* bytes, int byteCount, byte[] prefix, MetadataStringDecoder utf8De
 035        {
 036            Debug.Assert(utf8Decoder != null);
 37
 038            int prefixedByteCount = byteCount + prefix.Length;
 39
 040            if (prefixedByteCount == 0)
 041            {
 042                return string.Empty;
 43            }
 44
 045            byte[] buffer = ArrayPool<byte>.Shared.Rent(prefixedByteCount);
 46
 047            prefix.CopyTo(buffer, 0);
 048            Marshal.Copy((IntPtr)bytes, buffer, prefix.Length, byteCount);
 49
 50            string result;
 051            fixed (byte* prefixedBytes = &buffer[0])
 052            {
 053                result = utf8Decoder.GetString(prefixedBytes, prefixedByteCount);
 054            }
 55
 056            ArrayPool<byte>.Shared.Return(buffer);
 057            return result;
 058        }
 59    }
 60}
 61