<commit_before>﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using GitHub.Extensions;

namespace GitHub.Primitives
{
    /// <summary>
    /// This class represents a URI given to us as a string and is implicitly 
    /// convertible to and from string.
    /// </summary>
    /// <remarks>
    /// This typically represents a URI from an external source such as user input, a 
    /// Git Repo Remote, or an API URL.  We try to preserve the original form and let 
    /// downstream clients validate the URL. This class doesn't validate the URL. It just 
    /// performs a best-effort to parse the URI into bits important to us. For example, 
    /// we need to know the HOST so we can compare against GitHub.com, GH:E instances, etc.
    /// </remarks>
    [SuppressMessage("Microsoft.Usage", "CA2240:ImplementISerializableCorrectly", Justification = "GetObjectData is implemented in the base class")]
    [Serializable]
    public class UriString : StringEquivalent<UriString>, IEquatable<UriString>
    {
        //static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
        static readonly Regex sshRegex = new Regex(@"^.+@(?<host>(\[.*?\]|[a-z0-9-.]+?))(:(?<owner>.*?))?(/(?<repo>.*)(\.git)?)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
        readonly Uri url;

        public UriString(string uriString) : base(NormalizePath(uriString))
        {
            if (uriString == null) throw new ArgumentNullException(nameof(uriString), "Cannot create a null UriString");
            if (uriString.Length == 0) return;
            if (Uri.TryCreate(uriString, UriKind.Absolute, out url))
            {
                if (!url.IsFile)
                    SetUri(url);
                else
                    SetFilePath(url);
            }
            else if (!ParseScpSyntax(uriString))
            {
                SetFilePath(uriString);
            }

            if (RepositoryName != null)
            {
                NameWithOwner = Owner != null 
                    ? string.Format(CultureInfo.InvariantCulture, "{0}/{1}", Owner, RepositoryName) 
                    : RepositoryName;
            }
        }

        public static UriString ToUriString(Uri uri)
        {
            return uri == null ? null : new UriString(uri.ToString());
        }

        public Uri ToUri()
        {
            if (url == null)
                throw new InvalidOperationException("This Uri String is not a valid Uri");
            return url;
        }

        void SetUri(Uri uri)
        {
            Host = uri.Host;
            if (uri.Segments.Any())
            {
                RepositoryName = GetRepositoryName(uri.Segments.Last());
            }
            
            if (uri.Segments.Length > 2)
            {
                Owner = (uri.Segments[uri.Segments.Length - 2] ?? "").TrimEnd('/').ToNullIfEmpty();
            }
            
            IsHypertextTransferProtocol = uri.IsHypertextTransferProtocol();
        }

        void SetFilePath(Uri uri)
        {
            Host = "";
            Owner = "";
            RepositoryName = GetRepositoryName(uri.Segments.Last());
            IsFileUri = true;
        }

        void SetFilePath(string path)
        {
            Host = "";
            Owner = "";
            RepositoryName = GetRepositoryName(path.Replace("/", @"\").RightAfterLast(@"\"));
            IsFileUri = true;
        }

        // For xml serialization
        protected UriString()
        {
        }

        bool ParseScpSyntax(string scpString)
        {
            var match = sshRegex.Match(scpString);
            if (match.Success)
            {
                Host = match.Groups["host"].Value.ToNullIfEmpty();
                Owner = match.Groups["owner"].Value.ToNullIfEmpty();
                RepositoryName = GetRepositoryName(match.Groups["repo"].Value);
                IsScpUri = true;
                return true;
            }
            return false;
        }

        public string Host { get; private set; }

        public string Owner { get; private set; }

        public string RepositoryName { get; private set; }

        public string NameWithOwner { get; private set; }

        public bool IsFileUri { get; private set; }

        public bool IsScpUri { get; private set; }

        public bool IsValidUri => url != null;

        /// <summary>
        /// Attempts a best-effort to convert the remote origin to a GitHub Repository URL.
        /// </summary>
        /// <returns></returns>
        public Uri ToRepositoryUrl()
        {
            // we only want to process urls that represent network resources
            if (!IsValidUri && !IsScpUri) return url;
            if (IsValidUri && IsFileUri) return url;

            var scheme = url != null && IsHypertextTransferProtocol<commit_msg>Simplify check<commit_after>﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using GitHub.Extensions;

namespace GitHub.Primitives
{
    /// <summary>
    /// This class represents a URI given to us as a string and is implicitly 
    /// convertible to and from string.
    /// </summary>
    /// <remarks>
    /// This typically represents a URI from an external source such as user input, a 
    /// Git Repo Remote, or an API URL.  We try to preserve the original form and let 
    /// downstream clients validate the URL. This class doesn't validate the URL. It just 
    /// performs a best-effort to parse the URI into bits important to us. For example, 
    /// we need to know the HOST so we can compare against GitHub.com, GH:E instances, etc.
    /// </remarks>
    [SuppressMessage("Microsoft.Usage", "CA2240:ImplementISerializableCorrectly", Justification = "GetObjectData is implemented in the base class")]
    [Serializable]
    public class UriString : StringEquivalent<UriString>, IEquatable<UriString>
    {
        //static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
        static readonly Regex sshRegex = new Regex(@"^.+@(?<host>(\[.*?\]|[a-z0-9-.]+?))(:(?<owner>.*?))?(/(?<repo>.*)(\.git)?)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
        readonly Uri url;

        public UriString(string uriString) : base(NormalizePath(uriString))
        {
            if (uriString == null) throw new ArgumentNullException(nameof(uriString), "Cannot create a null UriString");
            if (uriString.Length == 0) return;
            if (Uri.TryCreate(uriString, UriKind.Absolute, out url))
            {
                if (!url.IsFile)
                    SetUri(url);
                else
                    SetFilePath(url);
            }
            else if (!ParseScpSyntax(uriString))
            {
                SetFilePath(uriString);
            }

            if (RepositoryName != null)
            {
                NameWithOwner = Owner != null 
                    ? string.Format(CultureInfo.InvariantCulture, "{0}/{1}", Owner, RepositoryName) 
                    : RepositoryName;
            }
        }

        public static UriString ToUriString(Uri uri)
        {
            return uri == null ? null : new UriString(uri.ToString());
        }

        public Uri ToUri()
        {
            if (url == null)
                throw new InvalidOperationException("This Uri String is not a valid Uri");
            return url;
        }

        void SetUri(Uri uri)
        {
            Host = uri.Host;
            if (uri.Segments.Any())
            {
                RepositoryName = GetRepositoryName(uri.Segments.Last());
            }
            
            if (uri.Segments.Length > 2)
            {
                Owner = (uri.Segments[uri.Segments.Length - 2] ?? "").TrimEnd('/').ToNullIfEmpty();
            }
            
            IsHypertextTransferProtocol = uri.IsHypertextTransferProtocol();
        }

        void SetFilePath(Uri uri)
        {
            Host = "";
            Owner = "";
            RepositoryName = GetRepositoryName(uri.Segments.Last());
            IsFileUri = true;
        }

        void SetFilePath(string path)
        {
            Host = "";
            Owner = "";
            RepositoryName = GetRepositoryName(path.Replace("/", @"\").RightAfterLast(@"\"));
            IsFileUri = true;
        }

        // For xml serialization
        protected UriString()
        {
        }

        bool ParseScpSyntax(string scpString)
        {
            var match = sshRegex.Match(scpString);
            if (match.Success)
            {
                Host = match.Groups["host"].Value.ToNullIfEmpty();
                Owner = match.Groups["owner"].Value.ToNullIfEmpty();
                RepositoryName = GetRepositoryName(match.Groups["repo"].Value);
                IsScpUri = true;
                return true;
            }
            return false;
        }

        public string Host { get; private set; }

        public string Owner { get; private set; }

        public string RepositoryName { get; private set; }

        public string NameWithOwner { get; private set; }

        public bool IsFileUri { get; private set; }

        public bool IsScpUri { get; private set; }

        public bool IsValidUri => url != null;

        /// <summary>
        /// Attempts a best-effort to convert the remote origin to a GitHub Repository URL.
        /// </summary>
        /// <returns></returns>
        public Uri ToRepositoryUrl()
        {
            // we only want to process urls that represent network resources
            if (!IsScpUri && (!IsValidUri || IsFileUri)) return url;

            var scheme = url != null && IsHypertextTransferProtocol
{'fraction_non_alphanumeric': 0.10884284992420415, 'fraction_numerical': 0.002223345123799899, 'mean_word_length': 1.782902137232846, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6428031', 'n_tokens_mistral': 2677, 'n_tokens_neox': 2504, 'n_words': 724}