<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
namespace Implem.Libraries.Utilities
{
    public static class Types
    {
        public const string CsInt = "int";
        public const string CsLong = "long";
        public const string CsDecimal = "decimal";
        public const string CsSingle = "Single";
        public const string CsDouble = "double";
        public const string CsString = "string";
        public const string CsDateTime = "DateTime";
        public const string CsBool = "bool";
        public const string CsNumeric = "numeric";
        public const string CsBytes = "byte[]";

        public static string CsType(this string type) 
        {
            switch (type.ToLower().Trim())
            {
                case "char":
                case "varchar":
                case "nchar":
                case "nvarchar":
                case "text":
                case "string":
                    return Types.CsString;
                case "int":
                case "smallint":
                    return Types.CsInt;
                case "bigint":
                case "long":
                    return Types.CsLong;
                case "decimal":
                    return Types.CsDecimal;
                case "float":
                case "real":
                    return Types.CsDouble;
                case "datetime":
                case "datetime2":
                case "date":
                case "smalldatetime":
                    return Types.CsDateTime;
                case "bit":
                case "bool":
                    return Types.CsBool;
                case "image":
                    return Types.CsBytes;
                default:
                    return type;
            }
        }

        public static string CsTypeSummary(this string type) 
        {
            switch (type.ToLower().Trim())
            {
                case "string":
                case "char":
                case "varchar":
                case "nchar":
                case "nvarchar":
                case "text":
                    return Types.CsString;
                case "int":
                case "long":
                case "smallint":
                case "bigint":
                case "decimal":
                case "float":
                case "real":
                    return Types.CsNumeric;
                case "datetime":
                case "datetime2":
                case "date":
                case "smalldatetime":
                    return Types.CsDateTime;
                case "bit":
                case "bool":
                    return Types.CsBool;
                case "image":
                    return Types.CsBytes;
                default:
                    return string.Empty;
            }
        }

        public static string ToString(this decimal self, string intFormat, string decimalFormat)
        {
            return self.ToString(decimalFormat).RegexExists(@"\.0+$")
                ? self.ToString(intFormat)
                : self.ToString(decimalFormat);
        }

        public static string ToStr(this object self) 
        {
            return (self ?? string.Empty).ToString();
        }

        public static int ToInt(this int? self)
        {
            return self != null
                ? Convert.ToInt32(self)
                : 0;
        }

        public static int ToInt(this long? self)
        {
            return self != null
                ? Convert.ToInt32(self)
                : 0;
        }

        public static int ToInt(this long self)
        {
            return Convert.ToInt32(self);
        }

        public static int ToInt(this decimal self)
        {
            return Convert.ToInt32(self);
        }

        public static int ToInt(this decimal? self)
        {
            return self != null
                ? Convert.ToInt32(self)
                : 0;
        }

        public static int ToInt(this Single self)
        {
            return Convert.ToInt32(self);
        }

        public static int ToInt(this Single? self)
        {
            return self != null
                ? Convert.ToInt32(self)
                : 0;
        }

        public static int ToInt(this double self)
        {
            return Convert.ToInt32(self);
        }

        public static int ToInt(this double? self)
        {
            return self != null
                ? Convert.ToInt32(self)
                : 0;
        }

        public static int ToInt(this object self) 
        {
            if (self != null)
            {
                var data = 0;
                if (int.TryParse(self.ToString(), out data))
                {
                    return data;
                }
                if (self is Enum)
                {
                    return Convert.ToInt32(self);
                }
            }
            return 0;
        }

        public static long ToLong(this int self)
        {
            return Convert.ToInt64(self);
        }

        public static long ToLong(this int? self)
        {
            return self != null
                ? Convert.ToInt64(self)
                : 0;
        }

        public static long ToLong(this long? self)
        {
            return self != null
                ? Convert.ToInt64(self)
                : 0;
        }

        public static long ToLong(this decimal self)
        {
            return Convert.ToInt64(self);
        }

        public static long ToLong(this decimal? self)
        {
            return self != null
                ? Convert.ToInt64(self)
                : 0;
        }

        public static long ToLong(this Single self)
        {
            return Convert.ToInt64(self);
        }

        public static long ToLong(this Single? self)
        {
            return self != null
                ? Convert.ToInt64(self)
                : 0;
        }

        public static long ToLong(this double self)
        {
            return Convert.ToInt64(self);
        }

        public static long ToLong(this double? self)
        {
            return self != null
                ? Convert.ToInt64(self)
                : 0;
        }

        public static long ToLong(this object self) 
        {
            if (self != null)
            {
                long data;
                if (long.TryParse(self.ToString(), out data))
                {
                    return data;
                }
                if (self is Enum)
                {
                    return Convert.ToInt64(self);
                }
            }
            return 0;
        }

        public static Decimal ToDecimal(this object self) 
        {
            Decimal data;
            if (self != null && Decimal.TryParse(self.ToString(), out data))
            {
                return data;
            }
            else
            {
                return 0;
            }
        }

        public static Single ToSingle(this object self) 
        {
            Single data;
            return self != null && Single.TryParse(self.ToString(), out data) ? data : 0;
        }

        public static double ToDouble(this object obj) 
        {
            double data;
            return obj != null && double.TryParse(obj.ToString(), out data) ? data : 0;
        }

        public static float ToFloat(this object obj)
        {
            float data;
            return obj != null && float.TryParse(obj.ToString(), out data) ? data : 0;
        }

        public static DateTime ToDateTime(this object obj)
        {
            DateTime data;
            var str = obj.ToString();
            return obj != null && DateTime.TryParse(str, out data)
                ? data
                : obj.ToLong() > 0
                    ? DateTime.FromOADate(double.Parse(str))
                    : DateTime.FromOADate(0);
        }

        public static bool ToBool(this object obj) 
        {
            switch (obj?.GetType().Name)
            {
                case "Boolean":
                    return Convert.ToBoolean(obj);
                case "String":
                    return
                        obj.ToString().Trim() == "1" ||
                        obj.ToString().Trim() == "-1" ||
                        obj.ToString().Trim().ToLower() == "true" ||
                        obj.ToString().Trim().ToLower() == "on";
                case "Int":
                case "Long":
                case "Single":
                case "Double":
                    return
                        Convert.ToInt32(obj) == 1 ||
                        Convert.ToInt32(obj) == -1;
                default:
                    return false;
            }
        }

        public static bool IsCollection(this object self)
        {
            return self != null && !(self is string) && self.GetType().GetInterfaces().Any(o =>
                o.IsGenericType && o.GetGenericTypeDefinition() == typeof(IEnumerable<>));
        }
    }
}
<commit_msg>Bug fixed: Int32型がBool値に変換できない問題を解消。<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
namespace Implem.Libraries.Utilities
{
    public static class Types
    {
        public const string CsInt = "int";
        public const string CsLong = "long";
        public const string CsDecimal = "decimal";
        public const string CsSingle = "Single";
        public const string CsDouble = "double";
        public const string CsString = "string";
        public const string CsDateTime = "DateTime";
        public const string CsBool = "bool";
        public const string CsNumeric = "numeric";
        public const string CsBytes = "byte[]";

        public static string CsType(this string type) 
        {
            switch (type.ToLower().Trim())
            {
                case "char":
                case "varchar":
                case "nchar":
                case "nvarchar":
                case "text":
                case "string":
                    return Types.CsString;
                case "int":
                case "smallint":
                    return Types.CsInt;
                case "bigint":
                case "long":
                    return Types.CsLong;
                case "decimal":
                    return Types.CsDecimal;
                case "float":
                case "real":
                    return Types.CsDouble;
                case "datetime":
                case "datetime2":
                case "date":
                case "smalldatetime":
                    return Types.CsDateTime;
                case "bit":
                case "bool":
                    return Types.CsBool;
                case "image":
                    return Types.CsBytes;
                default:
                    return type;
            }
        }

        public static string CsTypeSummary(this string type) 
        {
            switch (type.ToLower().Trim())
            {
                case "string":
                case "char":
                case "varchar":
                case "nchar":
                case "nvarchar":
                case "text":
                    return Types.CsString;
                case "int":
                case "long":
                case "smallint":
                case "bigint":
                case "decimal":
                case "float":
                case "real":
                    return Types.CsNumeric;
                case "datetime":
                case "datetime2":
                case "date":
                case "smalldatetime":
                    return Types.CsDateTime;
                case "bit":
                case "bool":
                    return Types.CsBool;
                case "image":
                    return Types.CsBytes;
                default:
                    return string.Empty;
            }
        }

        public static string ToString(this decimal self, string intFormat, string decimalFormat)
        {
            return self.ToString(decimalFormat).RegexExists(@"\.0+$")
                ? self.ToString(intFormat)
                : self.ToString(decimalFormat);
        }

        public static string ToStr(this object self) 
        {
            return (self ?? string.Empty).ToString();
        }

        public static int ToInt(this int? self)
        {
            return self != null
                ? Convert.ToInt32(self)
                : 0;
        }

        public static int ToInt(this long? self)
        {
            return self != null
                ? Convert.ToInt32(self)
                : 0;
        }

        public static int ToInt(this long self)
        {
            return Convert.ToInt32(self);
        }

        public static int ToInt(this decimal self)
        {
            return Convert.ToInt32(self);
        }

        public static int ToInt(this decimal? self)
        {
            return self != null
                ? Convert.ToInt32(self)
                : 0;
        }

        public static int ToInt(this Single self)
        {
            return Convert.ToInt32(self);
        }

        public static int ToInt(this Single? self)
        {
            return self != null
                ? Convert.ToInt32(self)
                : 0;
        }

        public static int ToInt(this double self)
        {
            return Convert.ToInt32(self);
        }

        public static int ToInt(this double? self)
        {
            return self != null
                ? Convert.ToInt32(self)
                : 0;
        }

        public static int ToInt(this object self) 
        {
            if (self != null)
            {
                var data = 0;
                if (int.TryParse(self.ToString(), out data))
                {
                    return data;
                }
                if (self is Enum)
                {
                    return Convert.ToInt32(self);
                }
            }
            return 0;
        }

        public static long ToLong(this int self)
        {
            return Convert.ToInt64(self);
        }

        public static long ToLong(this int? self)
        {
            return self != null
                ? Convert.ToInt64(self)
                : 0;
        }

        public static long ToLong(this long? self)
        {
            return self != null
                ? Convert.ToInt64(self)
                : 0;
        }

        public static long ToLong(this decimal self)
        {
            return Convert.ToInt64(self);
        }

        public static long ToLong(this decimal? self)
        {
            return self != null
                ? Convert.ToInt64(self)
                : 0;
        }

        public static long ToLong(this Single self)
        {
            return Convert.ToInt64(self);
        }

        public static long ToLong(this Single? self)
        {
            return self != null
                ? Convert.ToInt64(self)
                : 0;
        }

        public static long ToLong(this double self)
        {
            return Convert.ToInt64(self);
        }

        public static long ToLong(this double? self)
        {
            return self != null
                ? Convert.ToInt64(self)
                : 0;
        }

        public static long ToLong(this object self) 
        {
            if (self != null)
            {
                long data;
                if (long.TryParse(self.ToString(), out data))
                {
                    return data;
                }
                if (self is Enum)
                {
                    return Convert.ToInt64(self);
                }
            }
            return 0;
        }

        public static Decimal ToDecimal(this object self) 
        {
            Decimal data;
            if (self != null && Decimal.TryParse(self.ToString(), out data))
            {
                return data;
            }
            else
            {
                return 0;
            }
        }

        public static Single ToSingle(this object self) 
        {
            Single data;
            return self != null && Single.TryParse(self.ToString(), out data) ? data : 0;
        }

        public static double ToDouble(this object obj) 
        {
            double data;
            return obj != null && double.TryParse(obj.ToString(), out data) ? data : 0;
        }

        public static float ToFloat(this object obj)
        {
            float data;
            return obj != null && float.TryParse(obj.ToString(), out data) ? data : 0;
        }

        public static DateTime ToDateTime(this object obj)
        {
            DateTime data;
            var str = obj.ToString();
            return obj != null && DateTime.TryParse(str, out data)
                ? data
                : obj.ToLong() > 0
                    ? DateTime.FromOADate(double.Parse(str))
                    : DateTime.FromOADate(0);
        }

        public static bool ToBool(this object obj) 
        {
            switch (obj?.GetType().Name)
            {
                case "Boolean":
                    return Convert.ToBoolean(obj);
                case "String":
                    return
                        obj.ToString().Trim() == "1" ||
                        obj.ToString().Trim() == "-1" ||
                        obj.ToString().Trim().ToLower() == "true" ||
                        obj.ToString().Trim().ToLower() == "on";
                case "Int":
                case "Int32":
                case "Long":
                case "Single":
                case "Double":
                    return
                        Convert.ToInt32(obj) == 1 ||
                        Convert.ToInt32(obj) == -1;
                default:
                    return false;
            }
        }

        public static bool IsCollection(this object self)
        {
            return self != null && !(self is string) && self.GetType().GetInterfaces().Any(o =>
                o.IsGenericType && o.GetGenericTypeDefinition() == typeof(IEnumerable<>));
        }
    }
}

{'fraction_non_alphanumeric': 0.08779066571381453, 'fraction_numerical': 0.007916002418778517, 'mean_word_length': 1.037862663828834, 'pattern_counts': {'":': 93, '<': 5, '<?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': '6419845', 'n_tokens_mistral': 4707, 'n_tokens_neox': 4242, 'n_words': 1168}