Esempio n. 1
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));
        }
Esempio n. 2
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;
                }
            }
        }