<commit_before>﻿namespace Nancy.ViewEngines
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Nancy.Bootstrapper;

    /// <summary>
    /// Contains the functionality for locating a view that has been embedded into an assembly resource.
    /// </summary>
    public class ResourceViewLocationProviders : IViewLocationProvider
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ResourceViewLocationProviders"/> class.
        /// </summary>
        public ResourceViewLocationProviders()
        {
        }

        /// <summary>
        /// Returns an <see cref="ViewLocationResult"/> instance for all the views that could be located by the provider.
        /// </summary>
        /// <param name="supportedViewExtensions">An <see cref="IEnumerable{T}"/> instance, containing the view engine file extensions that is supported by the running instance of Nancy.</param>
        /// <returns>An <see cref="IEnumerable{T}"/> instance, containing <see cref="ViewLocationResult"/> instances for the located views.</returns>
        /// <remarks>If no views could be located, this method should return an empty enumerable, never <see langword="null"/>.</remarks>
        public IEnumerable<ViewLocationResult> GetLocatedViews(IEnumerable<string> supportedViewExtensions)
        {
            if (supportedViewExtensions == null)
            {
                return null;
            }

            var resourceStreamMatches =
                GetResourceStreamMatches(supportedViewExtensions);

            return !resourceStreamMatches.Any() ?
                Enumerable.Empty<ViewLocationResult>() :
                GetViewLocationsWithQualifiedLocations(resourceStreamMatches);
        }

        private static IEnumerable<ViewLocationResult> GetViewLocationsWithQualifiedLocations(IEnumerable<Tuple<string, Stream>> resources)
        {
            var commonResourceNamespace =
                ExtractCommonResourceNamespace(resources.Select(x => x.Item1));

            return
                from resource in resources
                let resourceFileName = GetResourceFileName(resource.Item1)
                select new ViewLocationResult(
                    GetEncodedResouceName(commonResourceNamespace, resource.Item1, resourceFileName),
                    resourceFileName,
                    GetResourceNameExtension(resource.Item1),
                    () => new StreamReader(resource.Item2));
        }

        private static string GetEncodedResouceName(string commonResourceNamespace, string location, string viewName)
        {
            if (commonResourceNamespace.Equals(viewName, StringComparison.OrdinalIgnoreCase))
            {
                return viewName;
            }

            var locationWithoutViewName =
                location.Replace(viewName, string.Empty);

            var commonResourceNamespaceWithoutViewName =
                commonResourceNamespace.Replace(viewName, string.Empty);

            var encodedLocation = locationWithoutViewName
                .Replace(commonResourceNamespaceWithoutViewName, string.Empty)
                .TrimEnd(new[] { '.' })
                .Replace(".", @"\");

            if (encodedLocation.Length != 0)
            {
                encodedLocation = string.Concat(encodedLocation, @"\");
            }

            return string.Concat(encodedLocation, viewName);
        }

        private static string ExtractCommonResourceNamespace(IEnumerable<string> resources)
        {
            if (resources.Count() == 1)
            {
                return resources.First();
            }

            var commonPathSegments = resources.Select(s => new { parts = s.Split('.') })
                .Aggregate((previous, current) => new { parts = current.parts.TakeWhile((step, index) => step == previous.parts.ElementAtOrDefault(index)).ToArray() });

            var commonResourceNamespace =
                string.Join(".", commonPathSegments.parts) + ".";

            return commonResourceNamespace;
        }

        private static string GetResourceFileName(string resourceName)
        {
            var nameSegments =
                resourceName.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);

            var segmentCount =
                nameSegments.Count();

            return (segmentCount < 2) ?
                string.Empty :
                string.Format("{0}.{1}", nameSegments.ElementAt(segmentCount - 2), nameSegments.Last());
        }

        private static string GetResourceNameExtension(string resourceName)
        {
            var extension =
                Path.GetExtension(resourceName);

            return string.IsNullOrEmpty(extension) ? string.Empty : extension.TrimStart('.');
        }

        private static IEnumerable<Tuple<string, Stream>> GetResourceStreamMatches(IEnumerable<string> supportedViewEngineExtensions)
        {
            var resourceStreams =
                from assembly in AppDomainAssemblyTypeScanner.Assemblies
                where !assembly.FullName.StartsWith(@"System.")
                from resourceName in assembly.GetManifestResourceNames()
                from viewEngineExtension in supportedViewEngineExtensions
                where resourceName.EndsWith(viewEngineExtension, StringComparison.OrdinalIgnoreCase)
                select new Tuple<string, Stream>(resourceName, assembly.GetManifestResourceStream(resourceName));

            return resourceStreams.ToList();
        }
    }
}<commit_msg>Stripped extension from view name in ViewLocationResults returned by the ResourceViewLocationProvider<commit_after>﻿namespace Nancy.ViewEngines
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Nancy.Bootstrapper;

    /// <summary>
    /// Contains the functionality for locating a view that has been embedded into an assembly resource.
    /// </summary>
    public class ResourceViewLocationProviders : IViewLocationProvider
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ResourceViewLocationProviders"/> class.
        /// </summary>
        public ResourceViewLocationProviders()
        {
        }

        /// <summary>
        /// Returns an <see cref="ViewLocationResult"/> instance for all the views that could be located by the provider.
        /// </summary>
        /// <param name="supportedViewExtensions">An <see cref="IEnumerable{T}"/> instance, containing the view engine file extensions that is supported by the running instance of Nancy.</param>
        /// <returns>An <see cref="IEnumerable{T}"/> instance, containing <see cref="ViewLocationResult"/> instances for the located views.</returns>
        /// <remarks>If no views could be located, this method should return an empty enumerable, never <see langword="null"/>.</remarks>
        public IEnumerable<ViewLocationResult> GetLocatedViews(IEnumerable<string> supportedViewExtensions)
        {
            if (supportedViewExtensions == null)
            {
                return null;
            }

            var resourceStreamMatches =
                GetResourceStreamMatches(supportedViewExtensions);

            return !resourceStreamMatches.Any() ?
                Enumerable.Empty<ViewLocationResult>() :
                GetViewLocationsWithQualifiedLocations(resourceStreamMatches);
        }

        private static IEnumerable<ViewLocationResult> GetViewLocationsWithQualifiedLocations(IEnumerable<Tuple<string, Stream>> resources)
        {
            var commonResourceNamespace =
                ExtractCommonResourceNamespace(resources.Select(x => x.Item1));

            return
                from resource in resources
                let resourceFileName = GetResourceFileName(resource.Item1)
                select new ViewLocationResult(
                    GetEncodedResouceName(commonResourceNamespace, resource.Item1, resourceFileName),
                    resourceFileName,
                    GetResourceNameExtension(resource.Item1),
                    () => new StreamReader(resource.Item2));
        }

        private static string GetEncodedResouceName(string commonResourceNamespace, string location, string viewName)
        {
            if (commonResourceNamespace.Equals(viewName, StringComparison.OrdinalIgnoreCase))
            {
                return viewName;
            }

            var locationWithoutViewName =
                location.Replace(viewName, string.Empty);

            var commonResourceNamespaceWithoutViewName =
                commonResourceNamespace.Replace(viewName, string.Empty);

            var encodedLocation = locationWithoutViewName
                .Replace(commonResourceNamespaceWithoutViewName, string.Empty)
                .TrimEnd(new[] { '.' })
                .Replace(".", @"\");

            if (encodedLocation.Length != 0)
            {
                encodedLocation = string.Concat(encodedLocation, @"\");
            }

            return string.Concat(encodedLocation, viewName);
        }

        private static string ExtractCommonResourceNamespace(IEnumerable<string> resources)
        {
            if (resources.Count() == 1)
            {
                return resources.First();
            }

            var commonPathSegments = resources.Select(s => new { parts = s.Split('.') })
                .Aggregate((previous, current) => new { parts = current.parts.TakeWhile((step, index) => step == previous.parts.ElementAtOrDefault(index)).ToArray() });

            var commonResourceNamespace =
                string.Join(".", commonPathSegments.parts) + ".";

            return commonResourceNamespace;
        }

        private static string GetResourceFileName(string resourceName)
        {
            var nameSegments =
                resourceName.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);

            var segmentCount =
                nameSegments.Count();

            return (segmentCount < 2) ?
                string.Empty :
                nameSegments.ElementAt(segmentCount - 2);
        }

        private static string GetResourceNameExtension(string resourceName)
        {
            var extension =
                Path.GetExtension(resourceName);

            return string.IsNullOrEmpty(extension) ? string.Empty : extension.TrimStart('.');
        }

        private static IEnumerable<Tuple<string, Stream>> GetResourceStreamMatches(IEnumerable<string> supportedViewEngineExtensions)
        {
            var resourceStreams =
                from assembly in AppDomainAssemblyTypeScanner.Assemblies
                where !assembly.FullName.StartsWith(@"System.")
                from resourceName in assembly.GetManifestResourceNames()
                from viewEngineExtension in supportedViewEngineExtensions
                where resourceName.EndsWith(viewEngineExtension, StringComparison.OrdinalIgnoreCase)
                select new Tuple<string, Stream>(resourceName, assembly.GetManifestResourceStream(resourceName));

            return resourceStreams.ToList();
        }
    }
}
{'fraction_non_alphanumeric': 0.08517130154302154, 'fraction_numerical': 0.001743527155435446, 'mean_word_length': 2.0706638115631693, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2271220', 'n_tokens_mistral': 2690, 'n_tokens_neox': 2482, 'n_words': 582}