Esempio n. 1
0
        /// <summary>
        /// Escapes a substring.
        /// </summary>
        static public string Escape(string inString, int inStartIndex, ICustomEscapeEvaluator inCustomEscape = null)
        {
            StringBuilder sb = new StringBuilder();

            Escape(inString, inStartIndex, sb, inCustomEscape);
            return(sb.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// Unescapes a string.
        /// </summary>
        static public string Unescape(string inString, ICustomEscapeEvaluator inCustomEscape = null)
        {
            StringBuilder sb = new StringBuilder();

            Unescape(inString, sb, inCustomEscape);
            return(sb.ToString());
        }
Esempio n. 3
0
        /// <summary>
        /// Escapes a string to the given string builder.
        /// </summary>
        static public void Escape(string inString, StringBuilder ioBuilder, ICustomEscapeEvaluator inCustomEscape = null)
        {
            if (inString == null)
            {
                return;
            }

            Escape(inString, 0, inString.Length, ioBuilder, inCustomEscape);
        }
Esempio n. 4
0
        /// <summary>
        /// Escapes a substring to the given string builder.
        /// </summary>
        static public void Escape(string inString, int inStartIndex, int inLength, StringBuilder ioBuilder, ICustomEscapeEvaluator inCustomEscape = null)
        {
            if (inString == null || inLength <= 0)
            {
                return;
            }

            if (inStartIndex < 0 || inStartIndex >= inString.Length)
            {
                throw new ArgumentOutOfRangeException("Starting index is out of range");
            }
            if (inStartIndex + inLength > inString.Length)
            {
                throw new ArgumentOutOfRangeException("Length extends beyond end of string");
            }

            for (int idx = 0; idx < inLength; ++idx)
            {
                int  realIdx = inStartIndex + idx;
                char c       = inString[realIdx];
                if (inCustomEscape != null)
                {
                    if (inCustomEscape.TryEscape(c, ioBuilder))
                    {
                        continue;
                    }
                }

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

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

                case '\0':
                    ioBuilder.Append("\\0");
                    break;

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

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

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

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

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

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

                default:
                    ioBuilder.Append(c);
                    break;
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Unescapes a substring to the given string builder.
        /// </summary>
        static public void Unescape(string inString, int inStartIndex, int inLength, StringBuilder ioBuilder, ICustomEscapeEvaluator inCustomEscape = null)
        {
            if (inString == null || inLength <= 0)
            {
                return;
            }

            if (inStartIndex < 0 || inStartIndex >= inString.Length)
            {
                throw new ArgumentOutOfRangeException("Starting index is out of range");
            }
            if (inStartIndex + inLength > inString.Length)
            {
                throw new ArgumentOutOfRangeException("Length extends beyond end of string");
            }

            for (int idx = 0; idx < inLength; ++idx)
            {
                int  realIdx = inStartIndex + idx;
                char c       = inString[realIdx];

                if (inCustomEscape != null)
                {
                    int advance = 0;
                    if (inCustomEscape.TryUnescape(c, inString, realIdx, out advance, ioBuilder))
                    {
                        idx += advance;
                        continue;
                    }
                }

                if (c == '\\')
                {
                    ++idx;
                    ++realIdx;
                    c = inString[realIdx];
                    switch (c)
                    {
                    case '0':
                        ioBuilder.Append('\0');
                        break;

                    case 'a':
                        ioBuilder.Append('\a');
                        break;

                    case 'v':
                        ioBuilder.Append('\v');
                        break;

                    case 't':
                        ioBuilder.Append('\t');
                        break;

                    case 'r':
                        ioBuilder.Append('\r');
                        break;

                    case 'n':
                        ioBuilder.Append('\n');
                        break;

                    case 'b':
                        ioBuilder.Append('\b');
                        break;

                    case 'f':
                        ioBuilder.Append('\f');
                        break;

                    case 'u':
                    {
                        string unicode = inString.Substring(realIdx + 1, 4);
                        char   code    = (char)int.Parse(unicode, NumberStyles.AllowHexSpecifier);
                        ioBuilder.Append(code);
                        idx += 4;
                        break;
                    }

                    default:
                        ioBuilder.Append(c);
                        break;
                    }
                }
                else
                {
                    ioBuilder.Append(c);
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Unescapes a substring to the given string builder.
        /// </summary>
        static public void Unescape(string inString, int inStartIndex, StringBuilder ioBuilder, ICustomEscapeEvaluator inCustomEscape = null)
        {
            if (inString == null)
            {
                return;
            }

            Unescape(inString, inStartIndex, inString.Length - inStartIndex, ioBuilder, inCustomEscape);
        }