예제 #1
0
        /// <summary>
        /// C转码字符串,根据 模式 参数.
        /// </summary>
        /// <param name="src">源字符串.</param>
        /// <param name="mode">模式.</param>
        /// <returns>返回该字符串的转义.</returns>
        public static string EscapeString(string src, StringEscapeCMode mode)
        {
            if (string.IsNullOrEmpty(src))
            {
                return(src);
            }
            if ((mode & StringEscapeCMode.QuoteMask) == StringEscapeCMode.NoEscape)
            {
                return(src);
            }
            StringBuilder sb        = new StringBuilder();
            bool          hasescape = false;

            foreach (char ch in src)
            {
                string s = EscapeChar_OnlyS(ch);
                if (null != s)
                {
                    sb.Append(s);
                    hasescape = true;
                }
                else
                {
                    sb.Append(ch);
                }
            }
            bool hasquote = ((mode & StringEscapeCMode.QuoteMask) == StringEscapeCMode.QuoteYes);

            if ((mode & StringEscapeCMode.QuoteMask) == StringEscapeCMode.QuoteAuto && hasescape)
            {
                hasquote = true;
            }
            if (!hasescape && !hasquote)
            {
                return(src);
            }
            if (hasquote)
            {
                sb.Insert(0, "\"");
                sb.Append('"');
            }
            return(sb.ToString());
        }
예제 #2
0
        /// <summary>
        /// C转码字符,根据 模式 参数.
        /// </summary>
        /// <param name="ch">字符.</param>
        /// <param name="mode">模式.</param>
        /// <returns>返回该字符的转义.</returns>
        public static string EscapeChar(char ch, StringEscapeCMode mode)
        {
            if ((mode & StringEscapeCMode.QuoteMask) == StringEscapeCMode.NoEscape)
            {
                return(ch.ToString());
            }
            string rt       = EscapeChar_Only(ch);
            bool   hasquote = ((mode & StringEscapeCMode.QuoteMask) == StringEscapeCMode.QuoteYes);

            if ((mode & StringEscapeCMode.QuoteMask) == StringEscapeCMode.QuoteAuto && null != rt)
            {
                hasquote = true;
            }
            if (null == rt)
            {
                rt = ch.ToString();
            }
            if (hasquote)
            {
                rt = string.Format("'{0}'", rt);
            }
            return(rt);
        }
예제 #3
0
        /// <summary>
        /// 解转义字符串.
        /// </summary>
        /// <param name="src">源字符串.</param>
        /// <param name="mode">模式.</param>
        /// <returns>返回该字符串的解转义.</returns>
        public static string UnescapeString(string src, StringEscapeCMode mode)
        {
            if (string.IsNullOrEmpty(src))
            {
                return(src);
            }
            Debug.Assert(mode == StringEscapeCMode.Default, "Now only support StringEscapeCMode.Default!");             //TODO: 支持更多的C转义的解码.
            StringBuilder sb = new StringBuilder(src.Length);

            char[] arr          = src.ToCharArray();
            bool   bQuoteIn     = false;        // 已经进入括号内.
            bool   bBackslash   = false;        // 前一个是反斜杠.
            long   posBackslash = 0;            // 反斜杠的位置.

            for (int i = 0; i < arr.Length; ++i)
            {
                bool bOutChar = true;                   // 输出当前字符.
                char ch       = arr[i];
                if (bQuoteIn)
                {
                    if (bBackslash)
                    {
                        switch (ch)
                        {
                        case '\\':
                        case '\"':
                        case '\'':
                            // 输出ch.
                            break;

                        case '0': ch = '\0'; break;

                        case 'a':
                        case 'A': ch = '\a'; break;

                        case 'b':
                        case 'B': ch = '\b'; break;

                        case 'f':
                        case 'F': ch = '\f'; break;

                        case 'n':
                        case 'N': ch = '\n'; break;

                        case 'r':
                        case 'R': ch = '\r'; break;

                        case 't':
                        case 'T': ch = '\t'; break;

                        case 'v':
                        case 'V': ch = '\v'; break;

                        case 'u':
                        case 'U':
                            Debug.Assert(false, "Now not support.");                                        //TODO: 实现unicode转码.
                            sb.Append('\\');
                            break;

                        default:
                            // 无效转义,补上前面的反斜杠.
                            sb.Append('\\');
                            break;
                        }
                        if (bOutChar)
                        {
                            bBackslash = false;
                        }
                    }
                    else
                    {
                        switch (ch)
                        {
                        case '\"':
                            bQuoteIn   = false;
                            bBackslash = false;
                            bOutChar   = false;
                            break;

                        case '\\':
                            bBackslash   = true;
                            posBackslash = i;
                            bOutChar     = false;
                            break;

                        default:
                            //bOutChar = true;
                            break;
                        }
                    }
                }
                else
                {
                    if ('"' == ch)
                    {
                        bQuoteIn   = true;
                        bBackslash = false;
                        bOutChar   = false;
                    }
                    else
                    {
                        //bOutChar = true;
                    }
                }
                if (bOutChar)
                {
                    sb.Append(ch);
                }
            }
            if (bBackslash)
            {
                sb.Append(src.Substring((int)posBackslash));
            }
            return(sb.ToString());
        }