<commit_before>﻿// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Fx.Portability.Resources;
using System;
using System.Reflection;

namespace Microsoft.Fx.Portability
{
    public class ProductInformation
    {
        public ProductInformation(string name)
        {
            var version = GetVersionString().Replace("-", ".");

            if (!IsValid(name))
            {
                throw new ArgumentOutOfRangeException(nameof(name), LocalizedStrings.ProductInformationInvalidArgument);
            }

            if (!IsValid(version))
            {
                throw new ArgumentOutOfRangeException(nameof(version), LocalizedStrings.ProductInformationInvalidArgument);
            }

            Name = name;
            Version = version;
        }

        public string Name { get; }

        public string Version { get; }

        private static string GetVersionString()
        {
            var assembly = typeof(ProductInformation).GetTypeInfo().Assembly;
            var info = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();

            return info?.InformationalVersion ?? "unknown";
        }

        /// <summary>
        /// Verify strings/versions only contain letters, digits, '.', or '_'.  Otherwise, the user agent string may be created incorrectly
        /// </summary>
        private static bool IsValid(string str)
        {
            foreach (var s in str.ToCharArray())
            {
                if (!char.IsLetterOrDigit(s) && s != '.' && s != '_')
                {
                    return false;
                }
            }

            return true;
        }
    }
}<commit_msg>Add overload to ProductInformation to determine which assembly to use<commit_after>﻿// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Fx.Portability.Resources;
using System;
using System.Reflection;

namespace Microsoft.Fx.Portability
{
    public class ProductInformation
    {
        public ProductInformation(string name)
            : this(name, typeof(ProductInformation))
        { }

        public ProductInformation(string name, Type callerType)
        {
            var version = GetVersionString(callerType).Replace("-", ".");

            if (!IsValid(name))
            {
                throw new ArgumentOutOfRangeException(nameof(name), LocalizedStrings.ProductInformationInvalidArgument);
            }

            if (!IsValid(version))
            {
                throw new ArgumentOutOfRangeException(nameof(version), LocalizedStrings.ProductInformationInvalidArgument);
            }

            Name = name;
            Version = version;
        }

        public string Name { get; }

        public string Version { get; }

        private static string GetVersionString(Type callerType)
        {
            var assembly = callerType.GetTypeInfo().Assembly;
            var info = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();

            return info?.InformationalVersion ?? "unknown";
        }

        /// <summary>
        /// Verify strings/versions only contain letters, digits, '.', or '_'.  Otherwise, the user agent string may be created incorrectly
        /// </summary>
        private static bool IsValid(string str)
        {
            foreach (var s in str.ToCharArray())
            {
                if (!char.IsLetterOrDigit(s) && s != '.' && s != '_')
                {
                    return false;
                }
            }

            return true;
        }
    }
}
{'fraction_non_alphanumeric': 0.09177972865123703, 'fraction_numerical': 0.0, 'mean_word_length': 1.9147286821705427, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '1210534', 'n_tokens_mistral': 892, 'n_tokens_neox': 858, 'n_words': 243}