Exemplo n.º 1
0
        /// <summary>
        /// Attempts to parse the string slice into a color.
        /// </summary>
        static public bool TryParseColor(StringSlice inSlice, out Color outColor)
        {
            StringSlice colorData = inSlice;
            StringSlice alphaData = StringSlice.Empty;

            int dotIdx = inSlice.IndexOf('.');

            if (dotIdx >= 0)
            {
                colorData = inSlice.Substring(0, dotIdx);
                alphaData = inSlice.Substring(dotIdx + 1);
            }

            Color color   = default(Color);
            bool  bParsed = false;

            if (colorData.StartsWith('#'))
            {
                ulong       hex;
                StringSlice hexString = colorData.Substring(1);
                if (hexString.Length <= 6 && TryParseHex(colorData, 6, out hex))
                {
                    color   = Colors.RGBA((uint)hex << 8);
                    bParsed = true;
                }
                else if (TryParseHex(colorData, 8, out hex))
                {
                    color   = Colors.RGBA((uint)hex);
                    bParsed = true;
                }
            }

            if (!bParsed)
            {
                bParsed = ColorUtility.TryParseHtmlString(colorData.ToString(), out color);
                if (!bParsed)
                {
                    outColor = default(Color);
                    return(false);
                }
            }

            if (!alphaData.IsEmpty)
            {
                float alphaMult;
                if (!TryParseFloat(alphaData, out alphaMult))
                {
                    outColor = default(Color);
                    return(false);
                }

                color.a *= alphaMult / 100f;
            }

            outColor = color;
            return(bParsed);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attempts to parse a string slice into a float.
        /// </summary>
        static public bool TryParseFloat(StringSlice inSlice, out float outFloat)
        {
            inSlice = inSlice.Trim();

            if (inSlice.Length == 0)
            {
                outFloat = 0;
                return(false);
            }

            if (inSlice.Equals("Infinity"))
            {
                outFloat = float.PositiveInfinity;
                return(true);
            }

            if (inSlice.Equals("-Infinity"))
            {
                outFloat = float.NegativeInfinity;
                return(true);
            }

            if (inSlice.Equals("NaN"))
            {
                outFloat = float.NaN;
                return(true);
            }

            int evalMode = EvaluateFloatMode(inSlice);

            if (evalMode == DoNotRead)
            {
                outFloat = 0;
                return(false);
            }
            else if (evalMode == ReadAsInteger)
            {
                long l;
                if (TryParseLongInternal(inSlice, false, out l))
                {
                    outFloat = (float)l;
                    return(true);
                }
            }
            else if (evalMode == ReadAsDecimalPlace)
            {
                double d;
                if (TryParseLongDouble(inSlice, out d))
                {
                    outFloat = (float)d;
                    return(true);
                }
            }

            return(float.TryParse(inSlice.ToString(), NumberStyles.Float, CultureInfo.InvariantCulture, out outFloat));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Attempts to convert a string slice into a value of the given type.
        /// </summary>
        static public bool TryConvertTo(StringSlice inSlice, Type inType, out object outValue)
        {
            if (inType.IsEnum)
            {
                try
                {
                    outValue = Enum.Parse(inType, inSlice.ToString(), false);
                    return(true);
                }
                catch
                {
                    outValue = null;
                    return(false);
                }
            }

            TypeCode tc = Type.GetTypeCode(inType);

            switch (tc)
            {
            case TypeCode.Boolean:
            {
                bool b;
                if (TryParseBool(inSlice, out b))
                {
                    outValue = b;
                    return(true);
                }

                break;
            }

            case TypeCode.Byte:
            {
                byte b;
                if (TryParseByte(inSlice, out b))
                {
                    outValue = b;
                    return(true);
                }

                break;
            }

            case TypeCode.Char:
            {
                if (inSlice.Length > 0)
                {
                    outValue = inSlice[0];
                    return(true);
                }

                break;
            }

            case TypeCode.DateTime:
            {
                try
                {
                    outValue = Convert.ToDateTime(inSlice.ToString());
                    return(true);
                }
                catch
                {
                    outValue = null;
                    return(false);
                }
            }

            case TypeCode.Decimal:
            {
                double d;
                if (TryParseDouble(inSlice, out d))
                {
                    outValue = (decimal)d;
                    return(true);
                }

                break;
            }

            case TypeCode.Double:
            {
                double d;
                if (TryParseDouble(inSlice, out d))
                {
                    outValue = d;
                    return(true);
                }

                break;
            }

            case TypeCode.Int16:
            {
                short s;
                if (TryParseShort(inSlice, out s))
                {
                    outValue = s;
                    return(true);
                }

                break;
            }

            case TypeCode.Int32:
            {
                int i;
                if (TryParseInt(inSlice, out i))
                {
                    outValue = i;
                    return(true);
                }

                break;
            }

            case TypeCode.Int64:
            {
                long l;
                if (TryParseLong(inSlice, out l))
                {
                    outValue = l;
                    return(true);
                }

                break;
            }

            case TypeCode.Object:
            {
                if (inType == typeof(StringSlice) || inType == typeof(object))
                {
                    outValue = inSlice;
                    return(true);
                }
                if (inType == typeof(StringHash32))
                {
                    StringHash32 hash;
                    if (StringHash32.TryParse(inSlice, out hash))
                    {
                        outValue = hash;
                        return(true);
                    }
                }
                if (inType == typeof(StringHash64))
                {
                    StringHash64 hash;
                    if (StringHash64.TryParse(inSlice, out hash))
                    {
                        outValue = hash;
                        return(true);
                    }
                }
                else if (inType == typeof(Variant))
                {
                    Variant v;
                    if (Variant.TryParse(inSlice, true, out v))
                    {
                        outValue = v;
                        return(true);
                    }
                }

                break;
            }

            case TypeCode.SByte:
            {
                sbyte s;
                if (TryParseSByte(inSlice, out s))
                {
                    outValue = s;
                    return(true);
                }

                break;
            }

            case TypeCode.Single:
            {
                float f;
                if (TryParseFloat(inSlice, out f))
                {
                    outValue = f;
                    return(true);
                }

                break;
            }

            case TypeCode.String:
            {
                outValue = inSlice.ToString();
                return(true);
            }

            case TypeCode.UInt16:
            {
                ushort u;
                if (TryParseUShort(inSlice, out u))
                {
                    outValue = u;
                    return(true);
                }

                break;
            }

            case TypeCode.UInt32:
            {
                uint u;
                if (TryParseUInt(inSlice, out u))
                {
                    outValue = u;
                    return(true);
                }

                break;
            }

            case TypeCode.UInt64:
            {
                ulong u;
                if (TryParseULong(inSlice, out u))
                {
                    outValue = u;
                    return(true);
                }

                break;
            }
            }

            outValue = null;
            return(false);
        }