private static int ParseHexEscapeSequence(string s, int startIndex, int limit, StringBuilder sb) { if (startIndex == limit) { throw new KeyValueParseException("hex escape sequence missing.", startIndex, 0); } var hexResult = CharUtils.CharToHexValue(s[startIndex++]); if (hexResult == -1) { throw new KeyValueParseException("Invalid hex escape sequence.", startIndex - 1, 1); } for (; startIndex < limit; startIndex++) { var hexDigit = CharUtils.CharToHexValue(s[startIndex]); if (hexDigit == -1) { break; } hexResult = (hexResult << 4) | hexDigit; } sb.Append((char)hexResult); return(startIndex); }
private static int ParseUtf32EscapeSequence(string s, int startIndex, int limit, StringBuilder sb) { if (limit - startIndex < 8) { throw new KeyValueParseException("UTF-32 escape sequence missing.", startIndex, 0); } var utf32 = CharUtils.CharToHexValue(s[startIndex++]) << 28 | CharUtils.CharToHexValue(s[startIndex++]) << 24 | CharUtils.CharToHexValue(s[startIndex++]) << 20 | CharUtils.CharToHexValue(s[startIndex++]) << 16 | CharUtils.CharToHexValue(s[startIndex++]) << 12 | CharUtils.CharToHexValue(s[startIndex++]) << 8 | CharUtils.CharToHexValue(s[startIndex++]) << 4 | CharUtils.CharToHexValue(s[startIndex++]) << 0; const int highSurrogateStart = 0xd800; const int highSurrogateEnd = 0xdbff; const int lowSurrogateStart = 0xdc00; const int lowSurrogateEnd = 0xdfff; if (utf32 < 0 || utf32 > 0x10ffff || (utf32 >= highSurrogateStart && utf32 <= lowSurrogateEnd)) { throw new KeyValueParseException("Invalid UTF-32 escape sequence.", startIndex - 8, 8); } if (utf32 < 0x10000) { sb.Append((char)utf32); } else { var highSurrogate = (char)(((utf32 >> 10) - 0x40) + highSurrogateStart); var lowSurrogate = (char)((utf32 & 0x3FF) + lowSurrogateStart); Debug.Assert(highSurrogate >= highSurrogateStart && highSurrogate <= highSurrogateEnd); Debug.Assert(lowSurrogate >= lowSurrogateStart && lowSurrogate <= lowSurrogateEnd); sb.Append(highSurrogate); sb.Append(lowSurrogate); } return(startIndex); }
private static int ParseUtf16EscapeSequence(string s, int startIndex, int limit, StringBuilder sb) { if (limit - startIndex < 4) { throw new KeyValueParseException("UTF-16 escape sequence missing.", startIndex, 0); } var utf16 = CharUtils.CharToHexValue(s[startIndex++]) << 12 | CharUtils.CharToHexValue(s[startIndex++]) << 8 | CharUtils.CharToHexValue(s[startIndex++]) << 4 | CharUtils.CharToHexValue(s[startIndex++]) << 0; if (utf16 < 0) { throw new KeyValueParseException("Invalid UTF-16 escape sequence.", startIndex - 4, 4); } sb.Append((char)utf16); return(startIndex); }
private static int InternalTryParseHexStringToByteArray(string s, int startIndex, int length, byte[] buffer, int offset, int count) { var lastChar = startIndex + length; var lastByte = offset + count; while (lastByte > offset) { if (lastChar <= startIndex) { break; } var lo = CharUtils.CharToHexValue(s[--lastChar]); if (lo < 0) { return(~lastChar); } if (lastChar <= startIndex) { buffer[--lastByte] = (byte)lo; break; } var hi = CharUtils.CharToHexValue(s[--lastChar]); if (hi < 0) { return(~lastChar); } buffer[--lastByte] = (byte)((hi << 4) | (lo << 0)); } return(offset + count - lastByte); }