Esempio n. 1
0
        private static string GetFunctionBodyGetKnownMethodIndex(ulong mask)
        {
            var shifts = HttpUtilitiesGeneratorHelpers.GetShifts(mask);

            var maskHexString = HttpUtilitiesGeneratorHelpers.MaskToHexString(mask);

            string bodyString;

            if (shifts.Length > 0)
            {
                var bitsCount = HttpUtilitiesGeneratorHelpers.CountBits(mask);

                var tmpReturn = string.Empty;
                foreach (var item in shifts)
                {
                    if (tmpReturn.Length > 0)
                    {
                        tmpReturn += " | ";
                    }

                    tmpReturn += string.Format(CultureInfo.InvariantCulture, "(tmp >> {1})", HttpUtilitiesGeneratorHelpers.MaskToHexString(item.Mask), item.Shift);
                }

                var mask2 = (ulong)(Math.Pow(2, bitsCount) - 1);

                string returnString = string.Format(CultureInfo.InvariantCulture, "return ({0}) & {1};", tmpReturn, HttpUtilitiesGeneratorHelpers.MaskToHexString(mask2));

                bodyString = string.Format(CultureInfo.InvariantCulture, "            const int magicNumer = {0};\r\n            var tmp = (int)value & magicNumer;\r\n            {1}", HttpUtilitiesGeneratorHelpers.MaskToHexString(mask), returnString);
            }
            else
            {
                bodyString = string.Format(CultureInfo.InvariantCulture, "return (int)(value & {0});", maskHexString);
            }

            return(bodyString);
        }
Esempio n. 2
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, @"// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

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

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));
        }