Exemplo n.º 1
0
 public long ToInt64()
 {
     return(NumberHelper.InstanceByRadix(radix).ToInt64(this));
 }
Exemplo n.º 2
0
 public decimal ToDecimal()
 {
     return(NumberHelper.ToDecimal(this));
 }
Exemplo n.º 3
0
 public double ToDouble()
 {
     return(NumberHelper.InstanceByRadix(radix).ToDouble(this));
 }
Exemplo n.º 4
0
        /// <summary>
        /// 尝试将字符串解析为枚举值。
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="str">字符串</param>
        /// <param name="value">返回枚举值</param>
        /// <returns>返回是否解析成功</returns>
        public static unsafe bool TryParseEnum <T>(Ps <char> str, out T value) where T : struct, Enum
        {
            const char NumberMin = '0';
            const char NumberMax = '9';

            if (str.Length < 0)
            {
                goto False;
            }

            switch (str.Pointer[0])
            {
            case NumberHelper.NegativeSign:
            case NumberHelper.PositiveSign:
            case var digit when digit >= NumberMin && digit <= NumberMax:
                goto Number;
            }

            ulong val = 0;

            var spliter = str.Split(EnumParsingSeperator);

Continue:

            while (spliter.MoveNext())
            {
                var item = spliter.Current;

                item = StringHelper.Trim(item.Pointer, item.Length);

                foreach (var(bits, name) in EnumInterface <T> .Items)
                {
                    if (StringHelper.EqualsWithIgnoreCase(item, name))
                    {
                        val |= bits;

                        goto Continue;
                    }
                }

                if (val != 0)
                {
                    goto False;
                }
                else
                {
                    goto Number;
                }
            }

            value = AsEnum <T>(val);

            return(true);

Number:

            var numberInfo = NumberHelper.GetNumberInfo(str.Pointer, str.Length, 16);

            if (numberInfo.IsNumber && !numberInfo.HaveFractional && !numberInfo.HaveExponent && numberInfo.End == str.Length && numberInfo.IsCommonRadix(out var radix))
            {
                value = numberInfo.IsNegative
                    ? AsEnum <T>((ulong)numberInfo.ToInt64(radix))
                    : AsEnum <T>(numberInfo.ToUInt64(radix));

                return(true);
            }

False:

            value = default;

            return(false);
        }
Exemplo n.º 5
0
 public static Guid ToGuid(string value) => NumberHelper.TryParse(value, out Guid guid) ? guid : new Guid(value);
Exemplo n.º 6
0
 public static string ToString(Guid value) => NumberHelper.ToString(value);