<commit_before>﻿using System.Runtime.CompilerServices;
using System.Runtime.WootzJs;

namespace System
{
    public class Convert
    {
        /// <summary>
        /// Returns an object of the specified type and whose value is equivalent to the specified object.
        /// </summary>
        /// 
        /// <returns>
        /// An object whose type is <paramref name="conversionType"/> and whose value is equivalent to <paramref name="value"/>.-or-A null reference (Nothing in Visual Basic), if <paramref name="value"/> is null and <paramref name="conversionType"/> is not a value type.
        /// </returns>
        /// <param name="value">An object that implements the <see cref="T:System.IConvertible"/> interface. </param><param name="conversionType">The type of object to return. </param><exception cref="T:System.InvalidCastException">This conversion is not supported.  -or-<paramref name="value"/> is null and <paramref name="conversionType"/> is a value type.-or-<paramref name="value"/> does not implement the <see cref="T:System.IConvertible"/> interface.</exception><exception cref="T:System.FormatException"><paramref name="value"/> is not in a format recognized by <paramref name="conversionType"/>.</exception><exception cref="T:System.OverflowException"><paramref name="value"/> represents a number that is out of the range of <paramref name="conversionType"/>.</exception><exception cref="T:System.ArgumentNullException"><paramref name="conversionType"/> is null.</exception><filterpriority>1</filterpriority>
        public static object ChangeType(object value, Type conversionType)
        {
            if (value == null)
            {
                if (conversionType.IsValueType)
                    throw new Exception("Null object cannot be converted to a value type");
                return null;
            }
            if (conversionType.IsNullableValueType())
            {
                conversionType = conversionType.GetGenericArguments()[0];
            }
            if (value.GetType() == typeof(Number))
            {
                if (conversionType == typeof(int))
                    return (int)value.As<float>();
                if (conversionType == typeof(long))
                    return (long)value.As<double>();
                if (conversionType == typeof(string))
                    return value.As<JsObject>().toString();
            }
            if (value is DateTime)
            {
                if (conversionType == typeof(string))
                    return ((DateTime)value).ToString("yyyy-MM-dd");
            }
            if (value.GetType() == conversionType)
            {
                return value;
            }
            if (value is string)
            {
                var s = (string)value;
                if (conversionType == typeof(long)) 
                    return long.Parse(s);
                if (conversionType == typeof(int))
                    return int.Parse(s);
                if (conversionType == typeof(short))
                    return short.Parse(s);
                if (conversionType == typeof(DateTime))
                    return DateTime.ParseExact(s, "yyyy-MM-dd");
            }
            throw new Exception("Could not convert " + value + " (" + value.GetType().FullName + ") to " + conversionType.FullName);
        }
    }
}<commit_msg>Fix bug in convert class that wasn't handling nullable types when checking for null<commit_after>﻿using System.Runtime.CompilerServices;
using System.Runtime.WootzJs;

namespace System
{
    public class Convert
    {
        /// <summary>
        /// Returns an object of the specified type and whose value is equivalent to the specified object.
        /// </summary>
        /// 
        /// <returns>
        /// An object whose type is <paramref name="conversionType"/> and whose value is equivalent to <paramref name="value"/>.-or-A null reference (Nothing in Visual Basic), if <paramref name="value"/> is null and <paramref name="conversionType"/> is not a value type.
        /// </returns>
        /// <param name="value">An object that implements the <see cref="T:System.IConvertible"/> interface. </param><param name="conversionType">The type of object to return. </param><exception cref="T:System.InvalidCastException">This conversion is not supported.  -or-<paramref name="value"/> is null and <paramref name="conversionType"/> is a value type.-or-<paramref name="value"/> does not implement the <see cref="T:System.IConvertible"/> interface.</exception><exception cref="T:System.FormatException"><paramref name="value"/> is not in a format recognized by <paramref name="conversionType"/>.</exception><exception cref="T:System.OverflowException"><paramref name="value"/> represents a number that is out of the range of <paramref name="conversionType"/>.</exception><exception cref="T:System.ArgumentNullException"><paramref name="conversionType"/> is null.</exception><filterpriority>1</filterpriority>
        public static object ChangeType(object value, Type conversionType)
        {
            if (value == null)
            {
                if (conversionType.IsValueType && !conversionType.IsNullableValueType())
                    throw new Exception("Null object cannot be converted to a value type");
                return null;
            }
            if (conversionType.IsNullableValueType())
            {
                conversionType = conversionType.GetGenericArguments()[0];
            }
            if (value.GetType() == typeof(Number))
            {
                if (conversionType == typeof(int))
                    return (int)value.As<float>();
                if (conversionType == typeof(long))
                    return (long)value.As<double>();
                if (conversionType == typeof(string))
                    return value.As<JsObject>().toString();
            }
            if (value is DateTime)
            {
                if (conversionType == typeof(string))
                    return ((DateTime)value).ToString("yyyy-MM-dd");
            }
            if (value.GetType() == conversionType)
            {
                return value;
            }
            if (value is string)
            {
                var s = (string)value;
                if (conversionType == typeof(long)) 
                    return long.Parse(s);
                if (conversionType == typeof(int))
                    return int.Parse(s);
                if (conversionType == typeof(short))
                    return short.Parse(s);
                if (conversionType == typeof(DateTime))
                    return DateTime.ParseExact(s, "yyyy-MM-dd");
            }
            throw new Exception("Could not convert " + value + " (" + value.GetType().FullName + ") to " + conversionType.FullName);
        }
    }
}
{'fraction_non_alphanumeric': 0.12121655212750403, 'fraction_numerical': 0.0005848808305307793, 'mean_word_length': 2.1020408163265305, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 73, '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': '4979986', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1589, 'n_words': 478}