/// <summary> /// Tries to convert the given text into this parser's expected type. Conversion only, formatting should already have been removed. /// </summary> /// <param name="text">The text to convert.</param> /// <param name="targetValue">The target value.</param> /// <returns> /// Whether or not conversion was successful. /// </returns> internal override bool TryConvert(string text, out object targetValue) { DebugUtils.CheckNoExternalCallers(); // must be of even length. if ((text.Length % 2) != 0) { targetValue = null; return(false); } byte[] result = new byte[text.Length / 2]; int resultIndex = 0; int textIndex = 0; while (resultIndex < result.Length) { char ch0 = text[textIndex]; char ch1 = text[textIndex + 1]; if (!UriPrimitiveTypeParser.IsCharHexDigit(ch0) || !UriPrimitiveTypeParser.IsCharHexDigit(ch1)) { targetValue = null; return(false); } result[resultIndex] = (byte)((byte)(HexCharToNibble(ch0) << 4) + HexCharToNibble(ch1)); textIndex += 2; resultIndex++; } targetValue = result; return(true); }
/// <summary>Parses a token that starts with a digit.</summary> /// <returns>The kind of token recognized.</returns> private ExpressionTokenKind ParseFromDigit() { Debug.Assert(this.IsValidDigit, "this.IsValidDigit"); ExpressionTokenKind result; char startChar = this.ch.Value; this.NextChar(); if (startChar == '0' && this.ch == 'x' || this.ch == 'X') { result = ExpressionTokenKind.BinaryLiteral; do { this.NextChar(); }while (this.ch.HasValue && UriPrimitiveTypeParser.IsCharHexDigit(this.ch.Value)); } else { result = ExpressionTokenKind.IntegerLiteral; while (this.IsValidDigit) { this.NextChar(); } if (this.ch == '.') { result = ExpressionTokenKind.DoubleLiteral; this.NextChar(); this.ValidateDigit(); do { this.NextChar(); }while (this.IsValidDigit); } if (this.ch == 'E' || this.ch == 'e') { result = ExpressionTokenKind.DoubleLiteral; this.NextChar(); if (this.ch == '+' || this.ch == '-') { this.NextChar(); } this.ValidateDigit(); do { this.NextChar(); }while (this.IsValidDigit); } if (this.ch == 'M' || this.ch == 'm') { result = ExpressionTokenKind.DecimalLiteral; this.NextChar(); } else if (this.ch == 'd' || this.ch == 'D') { result = ExpressionTokenKind.DoubleLiteral; this.NextChar(); } else if (this.ch == 'L' || this.ch == 'l') { result = ExpressionTokenKind.Int64Literal; this.NextChar(); } else if (this.ch == 'f' || this.ch == 'F') { result = ExpressionTokenKind.SingleLiteral; this.NextChar(); } } return(result); }
/// <summary>Returns the 4 bits that correspond to the specified character.</summary> /// <param name="c">Character in the 0-F range to be converted.</param> /// <returns>The 4 bits that correspond to the specified character.</returns> /// <exception cref="FormatException">Thrown when 'c' is not in the '0'-'9','a'-'f' range.</exception> private static byte HexCharToNibble(char c) { Debug.Assert(UriPrimitiveTypeParser.IsCharHexDigit(c), String.Format(CultureInfo.InvariantCulture, "{0} is not a hex digit.", c)); switch (c) { case '0': return(0); case '1': return(1); case '2': return(2); case '3': return(3); case '4': return(4); case '5': return(5); case '6': return(6); case '7': return(7); case '8': return(8); case '9': return(9); case 'a': case 'A': return(10); case 'b': case 'B': return(11); case 'c': case 'C': return(12); case 'd': case 'D': return(13); case 'e': case 'E': return(14); case 'f': case 'F': return(15); default: throw new InvalidOperationException(); } }
private ExpressionTokenKind ParseFromDigit() { ExpressionTokenKind integerLiteral; char ch = this.ch; this.NextChar(); if (((ch == '0') && (this.ch == 'x')) || (this.ch == 'X')) { integerLiteral = ExpressionTokenKind.BinaryLiteral; do { this.NextChar(); }while (UriPrimitiveTypeParser.IsCharHexDigit(this.ch)); return(integerLiteral); } integerLiteral = ExpressionTokenKind.IntegerLiteral; while (char.IsDigit(this.ch)) { this.NextChar(); } if (this.ch == '.') { integerLiteral = ExpressionTokenKind.DoubleLiteral; this.NextChar(); this.ValidateDigit(); do { this.NextChar(); }while (char.IsDigit(this.ch)); } if ((this.ch == 'E') || (this.ch == 'e')) { integerLiteral = ExpressionTokenKind.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 = ExpressionTokenKind.DecimalLiteral; this.NextChar(); return(integerLiteral); } if ((this.ch == 'd') || (this.ch == 'D')) { integerLiteral = ExpressionTokenKind.DoubleLiteral; this.NextChar(); return(integerLiteral); } if ((this.ch == 'L') || (this.ch == 'l')) { integerLiteral = ExpressionTokenKind.Int64Literal; this.NextChar(); return(integerLiteral); } if ((this.ch == 'f') || (this.ch == 'F')) { integerLiteral = ExpressionTokenKind.SingleLiteral; this.NextChar(); } return(integerLiteral); }