/// <summary>Converts a string to a primitive value.</summary> /// <param name="text">String text to convert.</param> /// <param name="targetType">Type to convert string to.</param> /// <param name="targetValue">After invocation, converted value.</param> /// <returns>true if the value was converted; false otherwise.</returns> internal static bool TryKeyStringToPrimitive(string text, Type targetType, out object targetValue) { Debug.Assert(text != null, "text != null"); Debug.Assert(targetType != null, "targetType != null"); targetType = Nullable.GetUnderlyingType(targetType) ?? targetType; byte[] byteArrayValue; bool binaryResult = TryKeyStringToByteArray(text, out byteArrayValue); if (targetType == typeof(byte[]) || targetType == typeof(System.Data.Linq.Binary)) { // The object cast is required because otherwise the compiler uses the implicit byte[] // to Binary conversion and always returns Binary. targetValue = (byteArrayValue != null && targetType == typeof(System.Data.Linq.Binary)) ? (object)new System.Data.Linq.Binary(byteArrayValue) : (object)byteArrayValue; return(binaryResult); } else if (binaryResult) { string keyValue = Encoding.UTF8.GetString(byteArrayValue); return(TryKeyStringToPrimitive(keyValue, targetType, out targetValue)); } // These have separate handlers for convenience - reuse them. else if (targetType == typeof(Guid)) { Guid guidValue; bool result = TryKeyStringToGuid(text, out guidValue); targetValue = guidValue; return(result); } else if (targetType == typeof(DateTime)) { DateTime dateTimeValue; bool result = TryKeyStringToDateTime(text, out dateTimeValue); targetValue = dateTimeValue; return(result); } bool quoted = WebConvert.IsKeyTypeQuoted(targetType); if (quoted != WebConvert.IsKeyValueQuoted(text)) { targetValue = null; return(false); } if (quoted) { Debug.Assert(IsKeyValueQuoted(text), "IsKeyValueQuoted(text) - otherwise caller didn't check this before"); text = RemoveQuotes(text); } try { if (typeof(String) == targetType) { targetValue = text; } else if (typeof(Boolean) == targetType) { targetValue = XmlConvert.ToBoolean(text); } else if (typeof(Byte) == targetType) { targetValue = XmlConvert.ToByte(text); } else if (typeof(SByte) == targetType) { targetValue = XmlConvert.ToSByte(text); } else if (typeof(Int16) == targetType) { targetValue = XmlConvert.ToInt16(text); } else if (typeof(Int32) == targetType) { targetValue = XmlConvert.ToInt32(text); } else if (typeof(Int64) == targetType) { if (TryRemoveLiteralSuffix(XmlConstants.XmlInt64LiteralSuffix, ref text)) { targetValue = XmlConvert.ToInt64(text); } else { targetValue = default(Int64); return(false); } } else if (typeof(Single) == targetType) { if (TryRemoveLiteralSuffix(XmlConstants.XmlSingleLiteralSuffix, ref text)) { targetValue = XmlConvert.ToSingle(text); } else { targetValue = default(Single); return(false); } } else if (typeof(Double) == targetType) { TryRemoveLiteralSuffix(XmlConstants.XmlDoubleLiteralSuffix, ref text); targetValue = XmlConvert.ToDouble(text); } else if (typeof(Decimal) == targetType) { if (TryRemoveLiteralSuffix(XmlConstants.XmlDecimalLiteralSuffix, ref text)) { try { targetValue = XmlConvert.ToDecimal(text); } catch (FormatException) { // we need to support exponential format for decimals since we used to support them in V1 decimal result; if (Decimal.TryParse(text, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out result)) { targetValue = result; } else { targetValue = default(Decimal); return(false); } } } else { targetValue = default(Decimal); return(false); } } else { Debug.Assert(typeof(System.Xml.Linq.XElement) == targetType, "XElement == " + targetType); targetValue = System.Xml.Linq.XElement.Parse(text, System.Xml.Linq.LoadOptions.PreserveWhitespace); } return(true); } catch (FormatException) { targetValue = null; return(false); } }
private TokenId ParseFromDigit() { TokenId integerLiteral; char ch = this.ch; this.NextChar(); if (((ch == '0') && (this.ch == 'x')) || (this.ch == 'X')) { integerLiteral = TokenId.BinaryLiteral; do { this.NextChar(); }while (WebConvert.IsCharHexDigit(this.ch)); return(integerLiteral); } integerLiteral = TokenId.IntegerLiteral; while (char.IsDigit(this.ch)) { this.NextChar(); } if (this.ch == '.') { integerLiteral = TokenId.DoubleLiteral; this.NextChar(); this.ValidateDigit(); do { this.NextChar(); }while (char.IsDigit(this.ch)); } if ((this.ch == 'E') || (this.ch == 'e')) { integerLiteral = TokenId.DoubleLiteral; this.NextChar(); if ((this.ch == '+') || (this.ch == '-')) { this.NextChar(); } this.ValidateDigit(); do { this.NextChar(); }while (char.IsDigit(this.ch)); } if ((this.ch == 'M') || (this.ch == 'm')) { integerLiteral = TokenId.DecimalLiteral; this.NextChar(); return(integerLiteral); } if ((this.ch == 'd') || (this.ch == 'D')) { integerLiteral = TokenId.DoubleLiteral; this.NextChar(); return(integerLiteral); } if ((this.ch == 'L') || (this.ch == 'l')) { integerLiteral = TokenId.Int64Literal; this.NextChar(); return(integerLiteral); } if ((this.ch == 'f') || (this.ch == 'F')) { integerLiteral = TokenId.SingleLiteral; this.NextChar(); } return(integerLiteral); }
/// <summary>Parses a token that starts with a digit.</summary> /// <returns>The kind of token recognized.</returns> private TokenId ParseFromDigit() { Debug.Assert(Char.IsDigit(this.ch), "Char.IsDigit(this.ch)"); TokenId result; char startChar = this.ch; this.NextChar(); if (startChar == '0' && this.ch == 'x' || this.ch == 'X') { result = TokenId.BinaryLiteral; do { this.NextChar(); }while (WebConvert.IsCharHexDigit(this.ch)); } else { result = TokenId.IntegerLiteral; while (Char.IsDigit(this.ch)) { this.NextChar(); } if (this.ch == '.') { result = TokenId.DoubleLiteral; this.NextChar(); this.ValidateDigit(); do { this.NextChar(); }while (Char.IsDigit(this.ch)); } if (this.ch == 'E' || this.ch == 'e') { result = TokenId.DoubleLiteral; this.NextChar(); if (this.ch == '+' || this.ch == '-') { this.NextChar(); } this.ValidateDigit(); do { this.NextChar(); }while (Char.IsDigit(this.ch)); } if (this.ch == 'M' || this.ch == 'm') { result = TokenId.DecimalLiteral; this.NextChar(); } else if (this.ch == 'd' || this.ch == 'D') { result = TokenId.DoubleLiteral; this.NextChar(); } else if (this.ch == 'L' || this.ch == 'l') { result = TokenId.Int64Literal; this.NextChar(); } else if (this.ch == 'f' || this.ch == 'F') { result = TokenId.SingleLiteral; this.NextChar(); } } return(result); }