<commit_before>﻿using IronVelocity.Runtime;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace IronVelocity.Compilation
{
    public static class VelocityExpressions
    {
        public static Expression BoxIfNeeded(Expression expression)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");

            return expression.Type.IsValueType && expression.Type != typeof(void)
                ? Expression.Convert(expression, typeof(object))
                : expression;
        }

        public static Expression ConvertIfNeeded(Expression expression, Type type)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");

            if (type == null)
                throw new ArgumentNullException("type");

            return ConvertIfNeeded(expression, expression.Type, type);
        }

        public static Expression ConvertIfNeeded(DynamicMetaObject obj)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");

            return ConvertIfNeeded(obj.Expression, obj.RuntimeType ?? typeof(object));
        }

        public static Expression ConvertIfNeeded(DynamicMetaObject obj, Type to)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");

            return ConvertIfNeeded(obj.Expression, obj.RuntimeType, to);
        }

        public static Expression ConvertIfNeeded(Expression expression, Type from, Type to)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");
            
            if (to == null)
                throw new ArgumentNullException("to");
            
            if (from == null)
                throw new ArgumentNullException("from");

            while (expression.NodeType == ExpressionType.Convert)
                expression = ((UnaryExpression)expression).Operand;

            if (to.IsValueType && !from.IsValueType && (from.IsInterface || from == typeof(object)))
                return Expression.Unbox(expression, to);
            if (expression.Type != from)
                expression = Expression.Convert(expression, from);
            if (from != to || expression.Type != to)
                expression = Expression.Convert(expression, to);

            return expression;
        }


        public static Expression CoerceToBoolean(Expression expression)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");

            expression = ConvertIfNeeded(expression, typeof(object));

            return Expression.Convert(expression, typeof(bool), MethodHelpers.BooleanCoercionMethodInfo);
        }

        public static Expression ConvertParameterIfNeeded(DynamicMetaObject target, ParameterInfo info)
        {
            if (target == null)
                throw new ArgumentNullException("target");

            if (info == null)<commit_msg>Small performance optimisation to avoid unnecessary conversation if an expression is of type bool.<commit_after>﻿using IronVelocity.Runtime;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace IronVelocity.Compilation
{
    public static class VelocityExpressions
    {
        public static Expression BoxIfNeeded(Expression expression)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");

            return expression.Type.IsValueType && expression.Type != typeof(void)
                ? Expression.Convert(expression, typeof(object))
                : expression;
        }

        public static Expression ConvertIfNeeded(Expression expression, Type type)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");

            if (type == null)
                throw new ArgumentNullException("type");

            return ConvertIfNeeded(expression, expression.Type, type);
        }

        public static Expression ConvertIfNeeded(DynamicMetaObject obj)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");

            return ConvertIfNeeded(obj.Expression, obj.RuntimeType ?? typeof(object));
        }

        public static Expression ConvertIfNeeded(DynamicMetaObject obj, Type to)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");

            return ConvertIfNeeded(obj.Expression, obj.RuntimeType, to);
        }

        public static Expression ConvertIfNeeded(Expression expression, Type from, Type to)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");
            
            if (to == null)
                throw new ArgumentNullException("to");
            
            if (from == null)
                throw new ArgumentNullException("from");

            while (expression.NodeType == ExpressionType.Convert)
                expression = ((UnaryExpression)expression).Operand;

            if (to.IsValueType && !from.IsValueType && (from.IsInterface || from == typeof(object)))
                return Expression.Unbox(expression, to);
            if (expression.Type != from)
                expression = Expression.Convert(expression, from);
            if (from != to || expression.Type != to)
                expression = Expression.Convert(expression, to);

            return expression;
        }


        public static Expression CoerceToBoolean(Expression expression)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");

            if (expression.Type == typeof(bool))
                return expression;

            expression = ConvertIfNeeded(expression, typeof(object));

            return Expression.Convert(expression, typeof(bool), MethodHelpers.BooleanCoercionMethodInfo);
        }

        public static Expression ConvertParameterIfNeeded(DynamicMetaObject target, ParameterInfo info)
        {
            if (target == null)
                throw new ArgumentNullException("target");

            if (info == null)
{'fraction_non_alphanumeric': 0.08526282853566959, 'fraction_numerical': 0.0, 'mean_word_length': 2.144613871126414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '926399', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1388, 'n_words': 385}