コード例 #1
0
        private static bool ReadFloatingPoint(Type expectedType, StringIterator inputIterator, out object result)
        {
            //Prepare a simple regex
            var regex = new Regex(@"^[+-]?\d+(\.\d+)?");

            //Send it to our iterator
            var number = inputIterator.ReadRegex(regex);

            //Setup the style and culture for parsing the float
            var style  = NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint;
            var format = CultureInfo.InvariantCulture.NumberFormat;

            //Get the appropriate parser based on type
            switch (Type.GetTypeCode(expectedType))
            {
            case TypeCode.Single:  { bool e = Single.TryParse(number, style, format, out Single tmp); result = tmp; return(e); }

            case TypeCode.Double:  { bool e = Double.TryParse(number, style, format, out Double tmp); result = tmp; return(e); }

            case TypeCode.Decimal: { bool e = Decimal.TryParse(number, style, format, out Decimal tmp); result = tmp; return(e); }
            }

            //Should never reach this
            throw new Exception("Hit unreachable code.");
        }
コード例 #2
0
        private static bool ReadInteger(Type expectedType, StringIterator inputIterator, out object result)
        {
            //Prepare a simple regex
            var regex = new Regex(@"^[+-]?\d+");

            //Send it to our iterator
            var number = inputIterator.ReadRegex(regex);

            //Setup the style and culture for parsing the float
            var style  = NumberStyles.AllowLeadingSign;
            var format = CultureInfo.InvariantCulture.NumberFormat;

            //Get the appropriate parser based on type
            switch (Type.GetTypeCode(expectedType))
            {
            case TypeCode.Byte:   { bool e = Byte.TryParse(number, style, format, out Byte tmp); result = tmp; return(e); }

            case TypeCode.SByte:  { bool e = SByte.TryParse(number, style, format, out SByte tmp); result = tmp; return(e); }

            case TypeCode.UInt16: { bool e = UInt16.TryParse(number, style, format, out UInt16 tmp); result = tmp; return(e); }

            case TypeCode.UInt32: { bool e = UInt32.TryParse(number, style, format, out UInt32 tmp); result = tmp; return(e); }

            case TypeCode.UInt64: { bool e = UInt64.TryParse(number, style, format, out UInt64 tmp); result = tmp; return(e); }

            case TypeCode.Int16:  { bool e = Int16.TryParse(number, style, format, out Int16 tmp); result = tmp; return(e); }

            case TypeCode.Int32:  { bool e = Int32.TryParse(number, style, format, out Int32 tmp); result = tmp; return(e); }

            case TypeCode.Int64:  { bool e = Int64.TryParse(number, style, format, out Int64 tmp); result = tmp; return(e); }
            }

            //Should never reach this
            throw new Exception("Hit unreachable code.");
        }
コード例 #3
0
        private static bool ReadString(string pattern, StringIterator inputIterator, int limit, out object result)
        {
            //Prepare the regex
            var regex = new Regex(pattern);

            //Send it to our iterator
            var tmp = inputIterator.ReadRegex(regex, limit);

            result = tmp;

            //Check that it's not null or empty
            return(!string.IsNullOrWhiteSpace(tmp));
        }