Example #1
0
        public static string Escape(string input, bool inCharGroup = false)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            for (int i = 0; i < input.Length; i++)
            {
                CharEscapeMode mode = GetEscapeMode(input[i], inCharGroup);

                if (mode != CharEscapeMode.None)
                {
                    var  sb = new StringBuilder();
                    char ch = input[i];
                    int  lastPos;
                    sb.Append(input, 0, i);

                    do
                    {
                        switch (mode)
                        {
                        case CharEscapeMode.AsciiHexadecimal:
                        {
                            sb.Append(@"\x");
                            sb.Append(((int)ch).ToString("X2", CultureInfo.InvariantCulture));
                            break;
                        }

                        case CharEscapeMode.Backslash:
                        {
                            sb.Append('\\');
                            sb.Append(ch);
                            break;
                        }

                        case CharEscapeMode.Bell:
                        {
                            sb.Append(@"\a");
                            break;
                        }

                        case CharEscapeMode.CarriageReturn:
                        {
                            sb.Append(@"\r");
                            break;
                        }

                        case CharEscapeMode.Escape:
                        {
                            sb.Append(@"\e");
                            break;
                        }

                        case CharEscapeMode.FormFeed:
                        {
                            sb.Append(@"\f");
                            break;
                        }

                        case CharEscapeMode.Linefeed:
                        {
                            sb.Append(@"\n");
                            break;
                        }

                        case CharEscapeMode.VerticalTab:
                        {
                            sb.Append(@"\v");
                            break;
                        }

                        case CharEscapeMode.Tab:
                        {
                            sb.Append(@"\t");
                            break;
                        }
                        }

                        i++;
                        lastPos = i;

                        while (i < input.Length)
                        {
                            ch   = input[i];
                            mode = GetEscapeMode(ch, inCharGroup);

                            if (mode != CharEscapeMode.None)
                            {
                                break;
                            }

                            i++;
                        }

                        sb.Append(input, lastPos, i - lastPos);
                    } while (i < input.Length);

                    return(sb.ToString());
                }
            }

            return(input);
        }
 private void Append(char ch, CharEscapeMode mode)
 {
     switch (mode)
     {
         case CharEscapeMode.AsciiHexadecimal:
             AppendAsciiHexadecimal((int)ch);
             break;
         case CharEscapeMode.Backslash:
             AppendBackslash(ch);
             break;
         case CharEscapeMode.Bell:
             AppendBackslash('a');
             break;
         case CharEscapeMode.CarriageReturn:
             AppendBackslash('r');
             break;
         case CharEscapeMode.Escape:
             AppendBackslash('e');
             break;
         case CharEscapeMode.FormFeed:
             AppendBackslash('f');
             break;
         case CharEscapeMode.Linefeed:
             AppendBackslash('n');
             break;
         case CharEscapeMode.VerticalTab:
             AppendBackslash('v');
             break;
         case CharEscapeMode.Tab:
             AppendBackslash('t');
             break;
     }
 }