Example #1
0
    private static string GenerateFile(Tuple <string, String>[] httpMethods)
    {
        var maskLength = (byte)Math.Ceiling(Math.Log(httpMethods.Length, 2));

        var methodsInfo = httpMethods.Select(GetMethodStringAndUlongAndMaskLength).ToList();

        var methodsInfoWithoutGet = methodsInfo.Where(m => m.HttpMethod != "Get".ToString()).ToList();

        var methodsAsciiStringAsLong = methodsInfo.Select(m => m.AsciiStringAsLong).ToArray();

        var mask = HttpUtilitiesGeneratorHelpers.SearchKeyByLookThroughMaskCombinations(methodsAsciiStringAsLong, 0, sizeof(ulong) * 8, maskLength);

        if (mask.HasValue == false)
        {
            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Generated {0} not found.", nameof(mask)));
        }

        var functionGetKnownMethodIndex = GetFunctionBodyGetKnownMethodIndex(mask.Value);

        var methodsSection = GetMethodsSection(methodsInfoWithoutGet);

        var masksSection = GetMasksSection(methodsInfoWithoutGet);

        var setKnownMethodSection = GetSetKnownMethodSection(methodsInfoWithoutGet);
        var methodNamesSection    = GetMethodNamesSection(methodsInfo);

        int knownMethodsArrayLength = (int)(Math.Pow(2, maskLength) + 1);
        int methodNamesArrayLength  = httpMethods.Length;

        return(string.Format(CultureInfo.InvariantCulture, @"// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;

#nullable enable

namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure
{{
    internal static partial class HttpUtilities
    {{
        // readonly primitive statics can be Jit'd to consts https://github.com/dotnet/coreclr/issues/1079
{0}

{1}
        private static readonly Tuple<ulong, ulong, HttpMethod, int>[] _knownMethods =
            new Tuple<ulong, ulong, HttpMethod, int>[{2}];

        private static readonly string[] _methodNames = new string[{3}];

        static HttpUtilities()
        {{
{4}
            FillKnownMethodsGaps();
{5}
        }}

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static int GetKnownMethodIndex(ulong value)
        {{
{6}
        }}
    }}
}}", methodsSection, masksSection, knownMethodsArrayLength, methodNamesArrayLength, setKnownMethodSection, methodNamesSection, functionGetKnownMethodIndex));
    }