Example #1
0
        /// <summary>
        /// Replace characters above 127 by entities.
        /// </summary>
        /// <param name="text">The source text.</param>
        /// <param name="useNames">If set to false, the function will not use known entities name. Default is true.</param>
        /// <param name="entitizeQuotAmpAndLtGt">If set to true, the [quote], [ampersand], [lower than] and [greather than] characters will be entitized.</param>
        /// <returns>The result text</returns>
        public static string Entitize(string text, bool useNames, bool entitizeQuotAmpAndLtGt)
//		_entityValue.Add("quot", 34);	// quotation mark = APL quote, U+0022 ISOnum
//		_entityName.Add(34, "quot");
//		_entityValue.Add("amp", 38);	// ampersand, U+0026 ISOnum
//		_entityName.Add(38, "amp");
//		_entityValue.Add("lt", 60);	// less-than sign, U+003C ISOnum
//		_entityName.Add(60, "lt");
//		_entityValue.Add("gt", 62);	// greater-than sign, U+003E ISOnum
//		_entityName.Add(62, "gt");
        {
            if (text == null)
            {
                return(null);
            }

            if (text.Length == 0)
            {
                return(text);
            }

            StringBuilder sb = new StringBuilder(text.Length);

            for (int i = 0; i < text.Length; i++)
            {
                int code = text[i];
                if ((code > 127) ||
                    (entitizeQuotAmpAndLtGt && ((code == 34) || (code == 38) || (code == 60) || (code == 62))))
                {
                    string entity;
                    EntityName.TryGetValue(code, out entity);

                    if ((entity == null) || (!useNames))
                    {
                        sb.Append("&#" + code + ";");
                    }
                    else
                    {
                        sb.Append("&" + entity + ";");
                    }
                }
                else
                {
                    sb.Append(text[i]);
                }
            }

            return(sb.ToString());
        }
Example #2
0
        /// <summary>
        /// Replace characters above 127 by entities.
        /// </summary>
        /// <param name="text">The source text.</param>
        /// <param name="useNames">If set to false, the function will not use known entities name. Default is true.</param>
        /// <param name="entitizeQuotAmpAndLtGt">If set to true, the [quote], [ampersand], [lower than] and [greather than] characters will be entitized.</param>
        /// <returns>The result text</returns>
        public static string Entitize(string text, bool useNames, bool entitizeQuotAmpAndLtGt)
//        _entityValue.Add("quot", 34);    // quotation mark = APL quote, U+0022 ISOnum
//        _entityName.Add(34, "quot");
//        _entityValue.Add("amp", 38);    // ampersand, U+0026 ISOnum
//        _entityName.Add(38, "amp");
//        _entityValue.Add("lt", 60);    // less-than sign, U+003C ISOnum
//        _entityName.Add(60, "lt");
//        _entityValue.Add("gt", 62);    // greater-than sign, U+003E ISOnum
//        _entityName.Add(62, "gt");
        {
            if (text == null)
            {
                return(null);
            }

            if (text.Length == 0)
            {
                return(text);
            }

            StringBuilder sb = new StringBuilder(text.Length);

#if !FX20 && !FX35
            if (UseWebUtility)
            {
                TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator(text);
                while (enumerator.MoveNext())
                {
                    sb.Append(System.Net.WebUtility.HtmlEncode(enumerator.GetTextElement()));
                }
            }
            else
            {
#endif
            for (int i = 0; i < text.Length; i++)
            {
                int code = text[i];
                if ((code > 127) ||
                    (entitizeQuotAmpAndLtGt && ((code == 34) || (code == 38) || (code == 60) || (code == 62))))
                {
                    string entity = null;

                    if (useNames)
                    {
                        EntityName.TryGetValue(code, out entity);
                    }

                    if (entity == null)
                    {
                        sb.Append("&#" + code + ";");
                    }
                    else
                    {
                        sb.Append("&" + entity + ";");
                    }
                }
                else
                {
                    sb.Append(text[i]);
                }
            }
#if !FX20 && !FX35
        }
#endif

            return(sb.ToString());
        }