Example #1
0
        /// <summary>
        /// Unscape all symbols for given type
        /// </summary>
        /// <param name="str"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static String Unescape(this String str, EscapingType type)
        {
            IEscapeProvider d   = EscapingDataFactory.Get(type);
            StringBuilder   res = new StringBuilder(str.Length);

            int offset = 0;

            foreach (var i in d.GetFirst())
            {
                if (str.StartsWith(i.Value))
                {
                    res.Append(i.Key);
                    offset = i.Value.Length;
                    break;
                }
            }

            for (int i = offset; i < str.Length; i++)
            {
                String s = CheckSuffix(str, offset, i, d);
                if (s != null)
                {
                    res.Append(s);
                    offset = i + 1;
                }
            }

            return(res + str.Substring(offset));
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="c"></param>
        /// <param name="type"></param>
        /// <param name="first"></param>
        /// <returns></returns>
        public static bool IsLiteral(this char c, EscapingType type, bool first = false)
        {
            IEscapeProvider d = EscapingDataFactory.Get(type);

            return(!(first && d.GetFirst().ContainsKey(c) ||
                     d.GetCommon().ContainsKey(c) ||
                     d.NeedEscapeUnicode(c)));
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="str"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsLiteral(this String str, EscapingType type)
        {
            IEscapeProvider d = EscapingDataFactory.Get(type);

            if (d.GetFirst().ContainsKey(str[0]))
            {
                return(false);
            }
            foreach (char c in str)
            {
                if (d.GetCommon().ContainsKey(c))
                {
                    return(false);
                }
            }
            return(true);
        }
Example #4
0
        /// <summary>
        /// Escape all symbols for given type
        /// </summary>
        /// <param name="str"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static String Escape(this String str, EscapingType type)
        {
            if (type == EscapingType.BxlSinglelineString || type == EscapingType.BxlMultilineString)
            {
                if (string.IsNullOrEmpty(str))
                {
                    return("\"\"");
                }
                if (type == EscapingType.BxlSinglelineString)
                {
                    return(ToBxlSingleLineString(str));
                }
                if (type == EscapingType.BxlMultilineString)
                {
                    return(ToBxlMultiLineString(str));
                }
            }
            else if (type == EscapingType.BxlStringOrLiteral)
            {
                if (string.IsNullOrEmpty(str))
                {
                    return("\"\"");
                }
                if (str.IsLiteral(EscapingType.BxlLiteral) && !str.Contains("\\") && !str.Contains("#"))
                {
                    return(str);
                }

                return(ToBxlSingleLineString(str));
            }
            else if (type == EscapingType.JsonValue)
            {
                //идентично
                return(ToBxlSingleLineString(str, false));
            }
            if (string.IsNullOrEmpty(str))
            {
                return(string.Empty);
            }

            IEscapeProvider d  = EscapingDataFactory.Get(type);
            var             sb = new StringBuilder(str.Length);

            if (!IsLiteral(str[0], type, true))
            {
                sb.Append(EscapeFirst(str[0], d));
            }
            else
            {
                sb.Append(str[0]);
            }

            for (int i = 1; i < str.Length; i++)
            {
                if (!IsLiteral(str[i], type))
                {
                    sb.Append(EscapeCommon(str[i], d));
                }
                else
                {
                    sb.Append(str[i]);
                }
            }

            return(sb.ToString());
        }