Ejemplo n.º 1
0
        /// <summary>
        /// Returns true if it quoted.
        /// </summary>
        public static bool QuoteValue(string str, StringBuilder sb, bool force = false)
        {
            int  ich = sb.Length;
            bool f   = QuoteValueCore(str, sb, force);

#if DEBUG // Verify the result.
            string v = sb.ToString(ich, sb.Length - ich);

            var curs = new CharCursor(v);
            Contracts.Assert(f == (force || curs.ChCur == '{'));

            var lex = new CmdLexer(curs);
            var res = new StringBuilder();

            // If it was quoted, gathering curly contents should get us the original. Otherwise,
            // the result should be equivalent to the original and should be a single token.
            if (f)
            {
                Contracts.Assert(v.StartsWith("{") && v.EndsWith("}"));
                lex.GatherCurlyContents(res);
            }
            else
            {
                lex.GetToken(res);
            }

            Contracts.Assert(!lex.Error);
            Contracts.Assert(curs.Eof);
            Contracts.Assert(str == res.ToString());
#endif

            return(f);
        }
Ejemplo n.º 2
0
        // Try to quote by just slapping curlies around the string. This will normally be sufficient
        // and produces a much more aesthetic result than escaping everything.
        private static bool TryNaiveQuoting(string str, StringBuilder sb)
        {
            Contracts.AssertNonEmpty(str);

            var curs = new CharCursor("{" + str + "}");
            var lex  = new CmdLexer(curs);
            var res  = new StringBuilder();

            lex.GatherCurlyContents(res);
            if (lex.Error || !curs.Eof || res.Length != str.Length || res.ToString() != str)
            {
                return(false);
            }

            sb.Append("{");
            sb.Append(str);
            sb.Append("}");
            return(true);
        }
Ejemplo n.º 3
0
        public static string UnquoteValue(string str)
        {
            if (!str.StartsWith("{") || !str.EndsWith("}"))
            {
                return(str);
            }

            CharCursor curs = new CharCursor(str);
            CmdLexer   lex  = new CmdLexer(curs);

            // Gather the curly group contents and make sure it consumes everything.
            StringBuilder sb = new StringBuilder();

            lex.GatherCurlyContents(sb);
            if (lex._error || !curs.Eof)
            {
                return(str);
            }

            return(sb.ToString());
        }