<commit_before>﻿/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the LICENSE file or at
 * https://developers.google.com/open-source/licenses/bsd
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Google.Api.Gax.Grpc
{
    /// <summary>
    /// Helps build user agent strings for the X-Goog-Api-Client header.
    /// Our user agent is a space-separated list of name/value pairs, where the value
    /// should be a semantic version string.
    /// </summary>
    internal sealed class UserAgentBuilder
    {
        /// <summary>
        /// The name of the header to set in gRPC calls.
        /// </summary>
        internal const string HeaderName = "x-goog-api-client";
        private readonly List<string> _values = new List<string>();

        /// <summary>
        /// Appends the given name/version string to the list.
        /// </summary>
        internal UserAgentBuilder AppendVersion(string name, string version)
        {
            GaxPreconditions.CheckNotNull(name, nameof(name));
            GaxPreconditions.CheckNotNull(version, nameof(version));
            _values.Add($"{name}/{version}");
            return this;
        }

        /// <summary>
        /// Appends a name/version string, taking the version from the version of the assembly
        /// containing the given type.
        /// </summary>
        internal UserAgentBuilder AppendAssemblyVersion(string name, System.Type type)
            => AppendVersion(name, FormatAssemblyVersion(type));

        /// <summary>
        /// Appends the .NET environment information to the list.
        /// </summary>
        internal UserAgentBuilder AppendDotNetEnvironment()
#if NETSTANDARD1_5
            => AppendVersion("gl-dotnet", FormatVersion(Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.RuntimeFramework.Version));
#else
            => AppendVersion("gl-dotnet", FormatVersion(Environment.Version));
#endif

        private static string FormatAssemblyVersion(System.Type type)
        {
            // Prefer AssemblyInformationalVersion, then AssemblyFileVersion,
            // then AssemblyVersion.

            var assembly = type.GetTypeInfo().Assembly;
            var info = assembly.GetCustomAttributes<AssemblyInformationalVersionAttribute>().FirstOrDefault()?.InformationalVersion;
            if (info != null)
            {
                return info;
            }
            var file = assembly.GetCustomAttributes<AssemblyFileVersionAttribute>().FirstOrDefault()?.Version;
            if (file != null)
            {
                return string.Join(".", file.Split('.').Take(3));
            }
            return FormatVersion(assembly.GetName().Version);
        }

        private static string FormatVersion(Version version) =>
            $"{version.Major}.{version.Minor}.{(version.Build != -1 ? version.Build : 0)}";

        public override string ToString() => string.Join(" ", _values);
    }
}<commit_msg>Work around null return from PlatformServices.Default in F# (#148)<commit_after>﻿/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the LICENSE file or at
 * https://developers.google.com/open-source/licenses/bsd
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Google.Api.Gax.Grpc
{
    /// <summary>
    /// Helps build user agent strings for the X-Goog-Api-Client header.
    /// Our user agent is a space-separated list of name/value pairs, where the value
    /// should be a semantic version string.
    /// </summary>
    internal sealed class UserAgentBuilder
    {
        /// <summary>
        /// The name of the header to set in gRPC calls.
        /// </summary>
        internal const string HeaderName = "x-goog-api-client";
        private readonly List<string> _values = new List<string>();

        /// <summary>
        /// Appends the given name/version string to the list.
        /// </summary>
        internal UserAgentBuilder AppendVersion(string name, string version)
        {
            GaxPreconditions.CheckNotNull(name, nameof(name));
            GaxPreconditions.CheckNotNull(version, nameof(version));
            _values.Add($"{name}/{version}");
            return this;
        }

        /// <summary>
        /// Appends a name/version string, taking the version from the version of the assembly
        /// containing the given type.
        /// </summary>
        internal UserAgentBuilder AppendAssemblyVersion(string name, System.Type type)
            => AppendVersion(name, FormatAssemblyVersion(type));

        /// <summary>
        /// Appends the .NET environment information to the list.
        /// </summary>
        internal UserAgentBuilder AppendDotNetEnvironment()
#if NETSTANDARD1_5
            => AppendVersion("gl-dotnet", FormatVersion(Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default?.Application?.RuntimeFramework?.Version));
#else
            => AppendVersion("gl-dotnet", FormatVersion(Environment.Version));
#endif

        private static string FormatAssemblyVersion(System.Type type)
        {
            // Prefer AssemblyInformationalVersion, then AssemblyFileVersion,
            // then AssemblyVersion.

            var assembly = type.GetTypeInfo().Assembly;
            var info = assembly.GetCustomAttributes<AssemblyInformationalVersionAttribute>().FirstOrDefault()?.InformationalVersion;
            if (info != null)
            {
                return info;
            }
            var file = assembly.GetCustomAttributes<AssemblyFileVersionAttribute>().FirstOrDefault()?.Version;
            if (file != null)
            {
                return string.Join(".", file.Split('.').Take(3));
            }
            return FormatVersion(assembly.GetName().Version);
        }

        private static string FormatVersion(Version version) =>
            version != null ?
            $"{version.Major}.{version.Minor}.{(version.Build != -1 ? version.Build : 0)}" :
            ""; // Empty string means "unknown"

        public override string ToString() => string.Join(" ", _values);
    }
}
{'fraction_non_alphanumeric': 0.11151362419278626, 'fraction_numerical': 0.0033076074972436605, 'mean_word_length': 2.3438651922064246, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 41, 'https://': 2, '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': '6191566', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1560, 'n_words': 446}