コード例 #1
0
        /// <summary>
        /// Truncate the string from the first index of the given character, also removing the indexed character.
        /// </summary>
        /// <param name="str">String to truncate.</param>
        /// <param name="c">Character to locate the index of.</param>
        /// <returns>Truncated string.</returns>
        public static string TruncateFromIndexOf(this string str, char c)
        {
            EnsureArgument.NotNull(str, nameof(str));

            int last = str.IndexOf(c);

            if (last > -1)
            {
                return(str.Substring(0, last));
            }

            return(str);
        }
コード例 #2
0
        /// <summary>
        /// Trim all characters at the start of the string until the last index of the given string,
        /// also removing the indexed character.
        /// </summary>
        /// <param name="str">String to trim.</param>
        /// <param name="value">String to locate the index of.</param>
        /// <param name="comparisonType">Comparison rule for locating the string.</param>
        /// <returns>Trimmed string.</returns>
        public static string TrimUntilLastIndexOf(this string str, string value, StringComparison comparisonType = StringComparison.Ordinal)
        {
            EnsureArgument.NotNull(str, nameof(str));

            int first = str.LastIndexOf(value, comparisonType);

            if (first > -1)
            {
                return(str.Substring(first + value.Length, str.Length - first - value.Length));
            }

            return(str);
        }
コード例 #3
0
        /// <summary>
        /// Trim all characters at the start of the string until the last index of the given character,
        /// also removing the indexed character.
        /// </summary>
        /// <param name="str">String to trim.</param>
        /// <param name="c">Character to locate the index of.</param>
        /// <returns>Trimmed string.</returns>
        public static string TrimUntilLastIndexOf(this string str, char c)
        {
            EnsureArgument.NotNull(str, nameof(str));

            int first = str.LastIndexOf(c);

            if (first > -1)
            {
                return(str.Substring(first + 1, str.Length - first - 1));
            }

            return(str);
        }
コード例 #4
0
        private void Init(Uri remoteUri, Uri proxyUri, bool isCertificateVerificationEnabled, string oAuthConsumerKey, string oAuthConsumerSecret, string traceConfig)
        {
            EnsureArgument.NotNull(remoteUri, nameof(remoteUri));
            RemoteUri = remoteUri;

            ProxyUri = proxyUri;

            IsCertificateVerificationEnabled = isCertificateVerificationEnabled;

            OAuthConsumerKey    = oAuthConsumerKey;
            OAuthConsumerSecret = oAuthConsumerSecret;

            Trace = traceConfig;
        }
コード例 #5
0
        public static IEnumerable <string> GetGitConfigurationScopes(this Uri uri)
        {
            EnsureArgument.NotNull(uri, nameof(uri));

            string schemeAndDelim = $"{uri.Scheme}{Uri.SchemeDelimiter}";
            string host           = uri.Host.TrimEnd('/');
            string path           = uri.AbsolutePath.Trim('/');
            string port           = uri.AbsoluteUri.Contains($":{uri.Port}") ? $":{uri.Port}" : string.Empty;

            // Unfold the path by component, right-to-left
            while (!string.IsNullOrWhiteSpace(path))
            {
                yield return($"{schemeAndDelim}{host}{port}/{path}");

                // Trim off the last path component
                if (!TryTrimString(path, StringExtensions.TruncateFromLastIndexOf, '/', out path))
                {
                    break;
                }
            }

            // Unfold the host by sub-domain, left-to-right
            while (!string.IsNullOrWhiteSpace(host))
            {
                if (host.Contains(".") ||
                    host.Equals("localhost", StringComparison.InvariantCultureIgnoreCase))    // Do not emit just the TLD
                {
                    yield return($"{schemeAndDelim}{host}{port}");
                }

                // Trim off the left-most sub-domain
                if (!TryTrimString(host, StringExtensions.TrimUntilIndexOf, '.', out host))
                {
                    break;
                }
            }
        }
コード例 #6
0
        private static string FormatText(string message, string filePath, int lineNumber, string memberName)
        {
            const int sourceColumnMaxWidth = 23;

            EnsureArgument.NotNull(message, nameof(message));
            EnsureArgument.NotNull(filePath, nameof(filePath));
            EnsureArgument.PositiveOrZero(lineNumber, nameof(lineNumber));
            EnsureArgument.NotNull(memberName, nameof(memberName));

            // Source column format is file:line
            string source = $"{filePath}:{lineNumber}";

            if (source.Length > sourceColumnMaxWidth)
            {
                int idx    = 0;
                int maxlen = sourceColumnMaxWidth - 3;
                int srclen = source.Length;

                while (idx >= 0 && (srclen - idx) > maxlen)
                {
                    idx = source.IndexOf('\\', idx + 1);
                }

                // If we cannot find a path separator which allows the path to be long enough, just truncate the file name
                if (idx < 0)
                {
                    idx = srclen - maxlen;
                }

                source = "..." + source.Substring(idx);
            }

            // Git's trace format is "{timestamp,-15} {source,-23} trace: {details}"
            string text = $"{DateTime.Now:HH:mm:ss.ffffff} {source,-23} trace: [{memberName}] {message}";

            return(text);
        }