示例#1
0
        private void PrintStringCore(char quoteType, bool tripleQuoted, string text)
        {
            _out.Write(quoteType, false);
            if (tripleQuoted)
            {
                _out.Write(quoteType, false);
                _out.Write(quoteType, false);

                char a = '\0', b = '\0';
                foreach (char c in text)
                {
                    if (c == quoteType && b == quoteType && a == quoteType)
                    {
                        _out.Write(@"\\", false);
                    }
                    // prevent false escape sequences
                    if (a == '\\' && b == '\\' && (c == quoteType || c == 'n' || c == 'r' || c == '\\'))
                    {
                        _out.Write(@"\\", false);
                    }
                    _out.Write(c, false);
                    a = b; b = c;
                }

                _out.Write(quoteType, false);
                _out.Write(quoteType, false);
            }
            else
            {
                _out.Write(ParseHelpers.EscapeCStyle(text, EscapeC.Control, quoteType), false);
            }
            _out.Write(quoteType, true);
        }
示例#2
0
        public void PrintString(UString str)
        {
            int oldLen = S.S.Length;

            S.Append("\"");
            S.Append(ParseHelpers.EscapeCStyle(str, EscapeC.Minimal | EscapeC.DoubleQuotes | EscapeC.ABFV | EscapeC.Control));
            S.Append("\"");
        }
示例#3
0
 /// <summary>Prints a character as a string, e.g. <c>'a' -> "'a'"</c>, with
 /// the special value -1 representing EOF, so PrintChar(-1, ...) == "EOF".</summary>
 protected void PrintChar(int c, StringBuilder sb)
 {
     if (c == -1)
     {
         sb.Append("EOF");
     }
     else if (c >= 0 && c < 0xFFFC)
     {
         sb.Append('\'');
         ParseHelpers.EscapeCStyle((char)c, sb, EscapeC.Default | EscapeC.SingleQuotes);
         sb.Append('\'');
     }
     else
     {
         sb.Append(c);
     }
 }
示例#4
0
        private static string GetStringsNotEqualMessage(string a, string b)
        {
            // TODO: test this code
            if (a == null || b == null)
            {
                return(GetObjectMismatchMessage(a, b));
            }
            else
            {
                int i, c = System.Math.Min((a ?? "").Length, (b ?? "").Length);
                for (i = 0; i < c; i++)
                {
                    if (a[i] != b[i])
                    {
                        break;
                    }
                }
                StringBuilder msg = new StringBuilder();
                if (a.Length == b.Length)
                {
                    msg.AppendFormat("  String lengths are both {0}. Strings differ at index {1}.\n", c, i);
                }
                else
                {
                    msg.AppendFormat("  Expected string length {0} but was {1}. Strings differ at index {2}.\n", a.Length, b.Length, i);
                }
                int a_i = i, b_i = i, maxlen = System.Math.Max(a.Length, b.Length);
                msg.AppendFormat("  Expected: {0}\n", GetQuotedString(ref a, ref a_i, maxlen));
                msg.AppendFormat("  But was:  {0}\n", GetQuotedString(ref b, ref b_i, maxlen));

                int TailLength = "-----------".Length;
                var prefix     = b.Left(b_i);
                int i_adjusted = ParseHelpers.EscapeCStyle(prefix, EscapeC.Control | EscapeC.DoubleQuotes, '"').Length;
                msg.Append(' ', 2);
                msg.Append('-', TailLength + i_adjusted);
                msg.Append("^\n");
                return(msg.ToString());
            }
        }
示例#5
0
 private static void Append(StringBuilder sb, int c)
 {
     if (c < 32 || c == '\\' || c == ']')
     {
         if (c <= -1)
         {
             sb.Append(@"\$");
         }
         else
         {
             sb.Append(ParseHelpers.EscapeCStyle(((char)c).ToString(), EscapeC.Control | EscapeC.ABFV, ']'));
         }
     }
     else if (c == '-' || c == '^' && sb.Length == 1)
     {
         sb.Append('\\');
         sb.Append((char)c);
     }
     else
     {
         sb.Append((char)c);
     }
 }
示例#6
0
        static string GetQuotedString(ref string s, ref int dif_i, int len)
        {
            int maxw = TruncateStringsLongerThan;

            if (len > maxw)
            {
                if (dif_i < maxw / 2)
                {
                    s = s.Left(maxw - 3) + "...";                     // "beginning..."
                }
                else if (len - dif_i < maxw / 2)
                {
                    s      = "..." + s.SafeSubstring(len - (maxw - 3));                // "...ending"
                    dif_i -= len - maxw;
                }
                else
                {
                    s     = "..." + s.SafeSubstring(dif_i - maxw / 2 + 3, maxw - 6) + "...";
                    dif_i = maxw / 2;                     // "...middle..."
                }
            }
            return("\"" + ParseHelpers.EscapeCStyle(s, EscapeC.Default, '"') + "\"");
        }
示例#7
0
        /// <summary>Parses a normal or triple-quoted string whose starting quotes
        /// have been stripped out. If triple-quote parsing was requested, stops
        /// parsing at three quote marks; otherwise, stops parsing at a single
        /// end-quote or newline.</summary>
        /// <returns>true if parsing stopped at one or three quote marks, or false
        /// if parsing stopped at the end of the input string or at a newline (in
        /// a string that is not triple-quoted).</returns>
        /// <remarks>This method recognizes LES and EC#-style string syntax.</remarks>
        public static bool UnescapeString(ref UString sourceText, char quoteType, bool isTripleQuoted, Action <int, string> onError, StringBuilder sb, UString indentation = default(UString), bool ecsTQIndents = false)
        {
            Debug.Assert(quoteType == '"' || quoteType == '\'' || quoteType == '`');
            bool fail;

            for (;;)
            {
                if (sourceText.IsEmpty)
                {
                    return(false);
                }
                int i0 = sourceText.InternalStart;
                if (!isTripleQuoted)
                {
                    char c = ParseHelpers.UnescapeChar(ref sourceText);
                    if ((c == quoteType || c == '\n') && sourceText.InternalStart == i0 + 1)
                    {
                        return(c == quoteType);                        // end of string
                    }
                    if (c == '\\' && sourceText.InternalStart == i0 + 1)
                    {
                        // This backslash was ignored by UnescapeChar
                        onError(i0, Localize.Localized(@"Unrecognized escape sequence '\{0}' in string", ParseHelpers.EscapeCStyle(sourceText[0, ' '].ToString(), EscapeC.Control)));
                    }
                    sb.Append(c);
                }
                else
                {
                    // Inside triple-quoted string
                    int c;
                    if (sourceText[2, '\0'] == '/')
                    {
                        // Detect escape sequence
                        c = ParseHelpers.UnescapeChar(ref sourceText);
                        if (sourceText.InternalStart > i0 + 1)
                        {
                            G.Verify(sourceText.PopFront(out fail) == '/');
                        }
                    }
                    else
                    {
                        c = sourceText.PopFront(out fail);
                        if (fail)
                        {
                            return(false);
                        }
                        if (c == quoteType)
                        {
                            if (sourceText[0, '\0'] == quoteType &&
                                sourceText[1, '\0'] == quoteType)
                            {
                                sourceText = sourceText.Substring(2);
                                // end of string
                                return(true);
                            }
                        }
                        if (c == '\r' || c == '\n')
                        {
                            // To ensure platform independency of source code, CR and
                            // CR-LF become LF.
                            if (c == '\r')
                            {
                                c = '\n';
                                var copy = sourceText.Clone();
                                if (sourceText.PopFront(out fail) != '\n')
                                {
                                    sourceText = copy;
                                }
                            }
                            // Inside a triple-quoted string, the indentation following a newline
                            // is ignored, as long as it matches the indentation of the first line.
                            UString src = sourceText, ind = indentation;
                            int     sp;
                            while ((sp = src.PopFront(out fail)) == ind.PopFront(out fail) && !fail)
                            {
                                sourceText = src;
                            }
                            if (ecsTQIndents)
                            {
                                // Allow an additional one tab or three spaces
                                if (sp == '\t')
                                {
                                    sourceText = src;
                                }
                                else if (sp == ' ')
                                {
                                    sourceText = src;
                                    if (src.PopFront(out fail) == ' ')
                                    {
                                        sourceText = src;
                                    }
                                    if (src.PopFront(out fail) == ' ')
                                    {
                                        sourceText = src;
                                    }
                                }
                            }
                        }
                    }

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