コード例 #1
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;
                }
            }
        }