Пример #1
0
        private static int ParseUtf32EscapeSequence(string s, int startIndex, int limit, StringBuilder sb)
        {
            if (limit - startIndex < 8)
            {
                throw new ParseException("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 ParseException("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);
        }
Пример #2
0
        private static int ParseUtf16EscapeSequence(string s, int startIndex, int limit, StringBuilder sb)
        {
            if (limit - startIndex < 4)
            {
                throw new ParseException("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 ParseException("Invalid UTF-16 escape sequence.", startIndex - 4, 4);
            }

            sb.Append((char)utf16);

            return(startIndex);
        }
Пример #3
0
        private static string InternalByteArrayToString(byte[] buffer, int offset, int count)
        {
            if (count == 0)
            {
                return(string.Empty);
            }
            if (count > int.MaxValue / 2)
            {
                throw new ArgumentOutOfRangeException(nameof(count), FormattableString.Invariant($"The specified length exceeds the maximum value of {int.MaxValue / 2}."));
            }

            var result      = new char[2 * count];
            var resultIndex = 0;
            var limit       = offset + count;

            for (; offset < limit; offset++)
            {
                result[resultIndex++] = CharUtils.HexValueToChar(buffer[offset] >> 4);
                result[resultIndex++] = CharUtils.HexValueToChar(buffer[offset] & 0xF);
            }

            return(new string(result));
        }
Пример #4
0
        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);
        }
Пример #5
0
        public static void EscapeString(string s, int startIndex, int limit, StringBuilder sb)
        {
            for (; startIndex < limit; startIndex++)
            {
                var c = s[startIndex];

                switch (c)
                {
                case '\0':
                    sb.Append("\\0");
                    break;

                case '\a':
                    sb.Append("\\a");
                    break;

                case '\b':
                    sb.Append("\\b");
                    break;

                case '\t':
                    sb.Append("\\t");
                    break;

                case '\n':
                    sb.Append("\\n");
                    break;

                case '\v':
                    sb.Append("\\v");
                    break;

                case '\f':
                    sb.Append("\\f");
                    break;

                case '\r':
                    sb.Append("\\r");
                    break;

                case '"':
                    sb.Append("\\\"");
                    break;

                case '\\':
                    sb.Append("\\\\");
                    break;

                default:
                    if (CharUtils.RequiresEscaping(c))
                    {
                        sb.Append("\\u");
                        sb.Append(CharUtils.HexValueToChar((c >> 12) & 0xF));
                        sb.Append(CharUtils.HexValueToChar((c >> 8) & 0xF));
                        sb.Append(CharUtils.HexValueToChar((c >> 4) & 0xF));
                        sb.Append(CharUtils.HexValueToChar((c >> 0) & 0xF));
                        continue;
                    }

                    sb.Append(c);
                    break;
                }
            }
        }