<commit_before>﻿#region auto-generated FILE INFORMATION

// ==============================================
// This file is distributed under the MIT License
// ==============================================
// 
// Filename: Comparison.cs
// Version:  2017-05-12 11:54
// 
// Copyright (c) 2017, Si13n7 Developments (r)
// All rights reserved.
// ______________________________________________

#endregion

namespace SilDev
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    ///     Provides static methods and base classes used for the comparison of two or more objects.
    /// </summary>
    public static class Comparison
    {
        /// <summary>
        ///     Searches for the specified one-dimensional array and returns the index of its first
        ///     occurrence in another one-dimensional array.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the elements of the array.
        /// </typeparam>
        /// <param name="source">
        ///     The one-dimensional array to search.
        /// </param>
        /// <param name="target">
        ///     The one-dimensional array to locate.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// </exception>
        /// <exception cref="OverflowException">
        /// </exception>
        public static IEnumerable<int> IndexOf<T>(T[] source, T[] target)
        {
            if (target == null || source.Length < target.Length)
                yield break;
            for (var i = 0; i < source.Length - target.Length + 1; i++)
                if (!target.Where((data, index) => !source[i + index].Equals(data)).Any())
                    yield return i;
        }

        /// <summary>
        ///     Determines whether the value of this object instance is between two specified values.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the object.
        /// </typeparam>
        /// <param name="item">
        ///     The object value to compare.
        /// </param>
        /// <param name="start">
        ///     The start index value.
        /// </param>
        /// <param name="end">
        ///     The end index value.
        /// </param>
        public static bool IsBetween<T>(this T item, T start, T end) where T : IComparable, IComparable<T>
        {
            try
            {
                var c = Comparer<T>.Default;
                var a = c.Compare(item, start);
                var b = c.Compare(item, end);
                return a >= 0 && b <= 0;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether a specified sequence of elements occurs within this sequence of
        ///     elements.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the elements of the array.
        /// </typeparam>
        /// <param name="source">
        ///     The sequence of elements to browse.
        /// </param>
        /// <param name="targets">
        ///     The sequence of elements to seek.
        /// </param>
        public static bool ContainsEx<T>(this IEnumerable<T> source, params T[][] targets)
        {
            try
            {
                var r = targets.Any(x => IndexOf(source.ToArray(), x).Any(y => y >= 0));
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether a specified string occurs within this sequence of strings. A
        ///     parameter specifies the culture, case, and sort rules used in the comparison.
        /// </summary>
        /// <param name="source">
        ///     The sequence to browse.
        /// </param>
        /// <param name="comparisonType">
        ///     One of the enumeration values that specifies the rules for the search.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to seek.
        /// </param>
        public static bool ContainsEx(this IEnumerable<string> source, StringComparison comparisonType, params string[] targets)
        {
            try
            {
                var r = source.Any(x => targets.Any(y => string.Equals(x, y, comparisonType)));
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether a specified string occurs within this sequence of strings. The
        ///     <see cref="StringComparison.OrdinalIgnoreCase"/> parameter is used for this
        ///     comparison.
        /// </summary>
        /// <param name="source">
        ///     The sequence to browse.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to seek.
        /// </param>
        public static bool ContainsEx(this IEnumerable<string> source, params string[] targets) =>
            source.ContainsEx(StringComparison.OrdinalIgnoreCase, targets);

        /// <summary>
        ///     Determines whether a specified substring occurs within this string. A parameter
        ///     specifies the culture, case, and sort rules used in the comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to browse.
        /// </param>
        /// <param name="comparisonType">
        ///     One of the enumeration values that specifies the rules for the search.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to seek.
        /// </param>
        public static bool ContainsEx(this string source, StringComparison comparisonType, params string[] targets)
        {
            try
            {
                var r = targets.Any(x => source.IndexOf(x, 0, comparisonType) != -1);
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether a specified substring occurs within this string. The
        ///     <see cref="StringComparison.OrdinalIgnoreCase"/> parameter is used for this
        ///     comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to browse.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to seek.
        /// </param>
        public static bool ContainsEx(this string source, params string[] targets) =>
            source.ContainsEx(StringComparison.OrdinalIgnoreCase, targets);

        /// <summary>
        ///     Determines whether a specified characters occurs within this string. A parameter
        ///     specifies the culture, case, and sort rules used in the comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to browse.
        /// </param>
        /// <param name="comparisonType">
        ///     One of the enumeration values that specifies the rules for the search.
        /// </param>
        /// <param name="targets">
        ///     The sequence of characters to seek.
        /// </param>
        public static bool ContainsEx(this string source, StringComparison comparisonType, params char[] targets)
        {
            try
            {
                var r = targets.Any(x => source.IndexOf(x.ToString(), 0, comparisonType) != -1);
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether a specified characters occurs within this string. The
        ///     <see cref="StringComparison.OrdinalIgnoreCase"/> parameter is used for this
        ///     comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to browse.
        /// </param>
        /// <param name="targets">
        ///     The sequence of characters to seek.
        /// </param>
        public static bool ContainsEx(this string source, params char[] targets) =>
            source.ContainsEx(StringComparison.OrdinalIgnoreCase, targets);

        /// <summary>
        ///     Determines whether the beginning of this string matches a string. A parameter
        ///     specifies the culture, case, and sort rules used in the comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to check.
        /// </param>
        /// <param name="comparisonType">
        ///     One of the enumeration values that specifies the rules for the search.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to compare seek.
        /// </param>
        public static bool StartsWithEx(this string source, StringComparison comparisonType, params string[] targets)
        {
            try
            {
                var r = targets.Any(b => source.StartsWith(b, comparisonType));
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether the beginning of this string matches a string. The
        ///     <see cref="StringComparison.OrdinalIgnoreCase"/> parameter is used for this
        ///     comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to check.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to compare seek.
        /// </param>
        public static bool StartsWithEx(this string source, params string[] targets) =>
            source.StartsWithEx(StringComparison.OrdinalIgnoreCase, targets);

        /// <summary>
        ///     Determines whether the end of this string matches a string. A parameter
        ///     specifies the culture, case, and sort rules used in the comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to check.
        /// </param>
        /// <param name="comparisonType">
        ///     One of the enumeration values that specifies the rules for the search.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to compare seek.
        /// </param>
        public static bool EndsWithEx(this string source, StringComparison comparisonType, params string[] targets)
        {
            try
            {
                var r = targets.Any(b => source.EndsWith(b, comparisonType));
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether the end of this string matches a string. The
        ///     <see cref="StringComparison.OrdinalIgnoreCase"/> parameter is used for this comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to check.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to compare seek.
        /// </param>
        public static bool EndsWithEx(this string source, params string[] targets) =>
            source.EndsWithEx(StringComparison.OrdinalIgnoreCase, targets);

        /// <summary>
        ///     Determines whether this string instance is the same as a string of the specified string
        ///     array. A parameter specifies the culture, case, and sort rules used in the comparison.
        /// </summary>
        /// <param name="source">
        ///     The first string to compare.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to compare with the first.
        /// </param>
        public static bool EqualsEx(this string source, StringComparison comparisonType, params string[] targets)
        {
            try
            {
                var r = targets.Any(b => string.Equals(source, b, comparisonType));
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether this string instance is the same as a string of the specified string
        ///     array. The <see cref="StringComparison.OrdinalIgnoreCase"/> parameter is used for this
        ///     comparison.
        /// </summary>
        /// <param name="source">
        ///     The first string to compare.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to compare with the first.
        /// </param>
        public static bool EqualsEx(this string source, params string[] targets) =>
            source.EqualsEx(StringComparison.OrdinalIgnoreCase, targets);

        /// <summary>
        ///     Provides a base class for comparison.
        /// </summary>
        public class AlphanumericComparer : IComparer<object>
        {
            private readonly bool _d;

            /// <summary>
            ///     Initilazies a new instance of the <see cref="AlphanumericComparer"/> class. A
            ///     parameter specifies whether the order is descended.
            /// </summary>
            /// <param name="descendent">
            ///     true to enable the descending order; otherwise, false.
            /// </param>
            public AlphanumericComparer(bool descendent = false)
            {
                _d = descendent;
            }

            /// <summary>
            ///     Compare two specified objects and returns an integer that indicates their relative
            ///     position in the sort order.
            /// </summary>
            /// <param name="a">
            ///     The first object to compare.<commit_msg>Add various comparison functions for byte arrays.<commit_after>﻿#region auto-generated FILE INFORMATION

// ==============================================
// This file is distributed under the MIT License
// ==============================================
// 
// Filename: Comparison.cs
// Version:  2017-05-16 08:41
// 
// Copyright (c) 2017, Si13n7 Developments (r)
// All rights reserved.
// ______________________________________________

#endregion

namespace SilDev
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    ///     Provides static methods and base classes used for the comparison of two or more objects.
    /// </summary>
    public static class Comparison
    {
        /// <summary>
        ///     Searches for the specified one-dimensional array and returns the index of its first
        ///     occurrence in another one-dimensional array.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the elements of the array.
        /// </typeparam>
        /// <param name="source">
        ///     The one-dimensional array to search.
        /// </param>
        /// <param name="target">
        ///     The one-dimensional array to locate.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// </exception>
        /// <exception cref="OverflowException">
        /// </exception>
        public static IEnumerable<int> IndexOf<T>(T[] source, T[] target)
        {
            if (target == null || source.Length < target.Length)
                yield break;
            for (var i = 0; i < source.Length - target.Length + 1; i++)
                if (!target.Where((data, index) => !source[i + index].Equals(data)).Any())
                    yield return i;
        }

        /// <summary>
        ///     Determines whether the value of this object instance is between two specified values.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the object.
        /// </typeparam>
        /// <param name="item">
        ///     The object value to compare.
        /// </param>
        /// <param name="start">
        ///     The start index value.
        /// </param>
        /// <param name="end">
        ///     The end index value.
        /// </param>
        public static bool IsBetween<T>(this T item, T start, T end) where T : IComparable, IComparable<T>
        {
            try
            {
                var c = Comparer<T>.Default;
                var a = c.Compare(item, start);
                var b = c.Compare(item, end);
                return a >= 0 && b <= 0;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether a specified sequence of elements occurs within this sequence of
        ///     elements.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the elements of the array.
        /// </typeparam>
        /// <param name="source">
        ///     The sequence of elements to browse.
        /// </param>
        /// <param name="targets">
        ///     The sequence of elements to seek.
        /// </param>
        public static bool ContainsEx<T>(this IEnumerable<T> source, params T[][] targets)
        {
            try
            {
                var r = targets.Any(x => IndexOf(source.ToArray(), x).Any(y => y >= 0));
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether a specified string occurs within this sequence of strings. A
        ///     parameter specifies the culture, case, and sort rules used in the comparison.
        /// </summary>
        /// <param name="source">
        ///     The sequence to browse.
        /// </param>
        /// <param name="comparisonType">
        ///     One of the enumeration values that specifies the rules for the search.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to seek.
        /// </param>
        public static bool ContainsEx(this IEnumerable<string> source, StringComparison comparisonType, params string[] targets)
        {
            try
            {
                var r = source.Any(x => targets.Any(y => string.Equals(x, y, comparisonType)));
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether a specified string occurs within this sequence of strings. The
        ///     <see cref="StringComparison.OrdinalIgnoreCase"/> parameter is used for this
        ///     comparison.
        /// </summary>
        /// <param name="source">
        ///     The sequence to browse.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to seek.
        /// </param>
        public static bool ContainsEx(this IEnumerable<string> source, params string[] targets) =>
            source.ContainsEx(StringComparison.OrdinalIgnoreCase, targets);

        /// <summary>
        ///     Determines whether a specified substring occurs within this string. A parameter
        ///     specifies the culture, case, and sort rules used in the comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to browse.
        /// </param>
        /// <param name="comparisonType">
        ///     One of the enumeration values that specifies the rules for the search.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to seek.
        /// </param>
        public static bool ContainsEx(this string source, StringComparison comparisonType, params string[] targets)
        {
            try
            {
                var r = targets.Any(x => source.IndexOf(x, 0, comparisonType) != -1);
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether a specified substring occurs within this string. The
        ///     <see cref="StringComparison.OrdinalIgnoreCase"/> parameter is used for this
        ///     comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to browse.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to seek.
        /// </param>
        public static bool ContainsEx(this string source, params string[] targets) =>
            source.ContainsEx(StringComparison.OrdinalIgnoreCase, targets);

        /// <summary>
        ///     Determines whether a specified characters occurs within this string. A parameter
        ///     specifies the culture, case, and sort rules used in the comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to browse.
        /// </param>
        /// <param name="comparisonType">
        ///     One of the enumeration values that specifies the rules for the search.
        /// </param>
        /// <param name="targets">
        ///     The sequence of characters to seek.
        /// </param>
        public static bool ContainsEx(this string source, StringComparison comparisonType, params char[] targets)
        {
            try
            {
                var r = targets.Any(x => source.IndexOf(x.ToString(), 0, comparisonType) != -1);
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether a specified characters occurs within this string. The
        ///     <see cref="StringComparison.OrdinalIgnoreCase"/> parameter is used for this
        ///     comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to browse.
        /// </param>
        /// <param name="targets">
        ///     The sequence of characters to seek.
        /// </param>
        public static bool ContainsEx(this string source, params char[] targets) =>
            source.ContainsEx(StringComparison.OrdinalIgnoreCase, targets);

        /// <summary>
        ///     Determines whether the beginning of this string matches a string. A parameter
        ///     specifies the culture, case, and sort rules used in the comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to check.
        /// </param>
        /// <param name="comparisonType">
        ///     One of the enumeration values that specifies the rules for the search.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to compare seek.
        /// </param>
        public static bool StartsWithEx(this string source, StringComparison comparisonType, params string[] targets)
        {
            try
            {
                var r = targets.Any(b => source.StartsWith(b, comparisonType));
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether the beginning of this string matches a string. The
        ///     <see cref="StringComparison.OrdinalIgnoreCase"/> parameter is used for this
        ///     comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to check.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to compare seek.
        /// </param>
        public static bool StartsWithEx(this string source, params string[] targets) =>
            source.StartsWithEx(StringComparison.OrdinalIgnoreCase, targets);

        /// <summary>
        ///     Determines whether the beginning of this sequence of bytes matches the specified
        ///     sequence of bytes
        /// </summary>
        /// <param name="source">
        ///     The first sequence of bytes to compare.
        /// </param>
        /// <param name="target">
        ///     The sequence of bytes to compare with the first.
        /// </param>
        public static bool StartWith(this IEnumerable<byte> source, params byte[] target)
        {
            var ba = source?.ToArray();
            if (ba == null || target == null || ba.Length < target.Length)
                return false;
            var r = !target.Where((t, i) => t != ba[i]).Any();
            return r;
        }

        /// <summary>
        ///     Determines whether the end of this string matches a string. A parameter specifies the
        ///     culture, case, and sort rules used in the comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to check.
        /// </param>
        /// <param name="comparisonType">
        ///     One of the enumeration values that specifies the rules for the search.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to compare seek.
        /// </param>
        public static bool EndsWithEx(this string source, StringComparison comparisonType, params string[] targets)
        {
            try
            {
                var r = targets.Any(b => source.EndsWith(b, comparisonType));
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether the end of this string matches a string. The
        ///     <see cref="StringComparison.OrdinalIgnoreCase"/> parameter is used forthis comparison.
        /// </summary>
        /// <param name="source">
        ///     The string to check.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to compare seek.
        /// </param>
        public static bool EndsWithEx(this string source, params string[] targets) =>
            source.EndsWithEx(StringComparison.OrdinalIgnoreCase, targets);

        /// <summary>
        ///     Determines whether the end of this sequence of bytes matches the specified sequence of
        ///     bytes.
        /// </summary>
        /// <param name="source">
        ///     The first sequence of bytes to compare.
        /// </param>
        /// <param name="target">
        ///     The sequence of bytes to compare with the first.
        /// </param>
        public static bool EndsWith(this IEnumerable<byte> source, params byte[] target)
        {
            var ba = source?.ToArray();
            if (ba == null || target == null || ba.Length < target.Length)
                return false;
            for (var i = target.Length - 1; i >= 0; i--)
                if (target[i] != ba[i])
                    return false;
            return true;
        }

        /// <summary>
        ///     Determines whether this string instance is the same as a string of the specified string
        ///     array. A parameter specifies the culture, case, and sort rules used in the comparison.
        /// </summary>
        /// <param name="source">
        ///     The first string to compare.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to compare with the first.
        /// </param>
        public static bool EqualsEx(this string source, StringComparison comparisonType, params string[] targets)
        {
            try
            {
                var r = targets.Any(b => string.Equals(source, b, comparisonType));
                return r;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///     Determines whether this string instance is the same as a string of the specified string
        ///     array. The <see cref="StringComparison.OrdinalIgnoreCase"/> parameter is used for this
        ///     comparison.
        /// </summary>
        /// <param name="source">
        ///     The first string to compare.
        /// </param>
        /// <param name="targets">
        ///     The sequence of strings to compare with the first.
        /// </param>
        public static bool EqualsEx(this string source, params string[] targets) =>
            source.EqualsEx(StringComparison.OrdinalIgnoreCase, targets);

        /// <summary>
        ///     Determines whether this sequence of bytes the same as the specified sequence of
        ///     bytes.
        /// </summary>
        /// <param name="source">
        ///     The first sequence of bytes to compare.
        /// </param>
        /// <param name="target">
        ///     The sequence of bytes to compare with the first.
        /// </param>
        public static bool EqualsEx(this IEnumerable<byte> source, params byte[] target)
        {
            var ba = source?.ToArray();
            if (ba == null && target == null)
                return true;
            if (ba == null || target == null || ba.Length != target.Length)
                return false;
            var r = !ba.Where((t, i) => t != target[i]).Any();
            return r;
        }

        /// <summary>
        ///     Determines whether this string instance contains only ASCII characters.
        /// </summary>
        /// <param name="source">
        ///     The string to compare.
        /// </param>
        public static bool IsAscii(this string source)
        {
            if (source == null)
                return false;
            var r = source.All(c => c <= sbyte.MaxValue);
            return r;
        }

        /// <summary>
        ///     Provides a base class for comparison.
        /// </summary>
        public class AlphanumericComparer : IComparer<object>
        {
            private readonly bool _d;

            /// <summary>
            ///     Initilazies a new instance of the <see cref="AlphanumericComparer"/> class. A
            ///     parameter specifies whether the order is descended.
            /// </summary>
            /// <param name="descendent">
            ///     true to enable the descending order; otherwise, false.
            /// </param>
            public AlphanumericComparer(bool descendent = false)
            {
                _d = descendent;
            }

            /// <summary>
            ///     Compare two specified objects and returns an integer that indicates their relative
            ///     position in the sort order.
            /// </summary>
            /// <param name="a">
            ///     The first object to compare.
{'fraction_non_alphanumeric': 0.12469812675412832, 'fraction_numerical': 0.001892826838979179, 'mean_word_length': 1.3848548525177056, 'pattern_counts': {'":': 0, '<': 317, '<?xml version=': 0, '>': 348, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1518443', 'n_tokens_mistral': 7426, 'n_tokens_neox': 7170, 'n_words': 2523}