Exemplo n.º 1
0
        /// <summary>
        /// 对传入的字符串进行关键字替换,并返回还原特征字符串(如果传入的字符串为空,那么特征字符串也会为空,因为不需要对其进行版本控制)
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public string Replace(string value, out string newVersion, out string newRevertInfo)
        {
            InitRegex();

#if DEBUG
            //System.Web.HttpContext.Current.Response.Write("<font style=\"size:26px;color:red\">注意,发生了关键字替换,请确认这不是BUG</font><br />");
#endif

            if (string.IsNullOrEmpty(value) || m_ReplaceKeyWordRegex == null || m_ReplaceKeywords == null || m_ReplaceKeywords.Count == 0)
            {
                newVersion    = Version;
                newRevertInfo = string.Empty;
                return(value);
            }


            int indexOffset = 0;
            ReplacedWordCollection replacedWords = new ReplacedWordCollection();

            string result = m_ReplaceKeyWordRegex.Replace(value, delegate(Match match)
            {
                object obj = m_ReplaceKeywords[match.Value];

                if (obj != null)
                {
                    string newWord = (string)obj;
                    int newLength  = newWord.Length;
                    int newIndex   = match.Index + indexOffset;
                    indexOffset   += newLength - match.Length;

                    ReplacedWord word = new ReplacedWord();
                    word.Index        = newIndex;
                    word.Length       = newLength;
                    word.OriginalWord = match.Value;
                    replacedWords.Add(word);

                    return(newWord);
                }

                return(match.Value);
            });

            if (replacedWords.Count > 0)
            {
                newRevertInfo = replacedWords.ToString();
            }
            else
            {
                newRevertInfo = string.Empty;
            }

            newVersion = this.Version;

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据传入的已被替换过的内容,以及恢复特征信息,恢复原始的内容
        /// </summary>
        /// <param name="value"></param>
        /// <param name="revertInfo"></param>
        /// <returns></returns>
        public string Revert(string value, string version, string revertInfo)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(string.Empty);
            }

            if (string.IsNullOrEmpty(revertInfo) || string.IsNullOrEmpty(version))
            {
                return(value);
            }

            ReplacedWordCollection words = ReplacedWordCollection.Parse(revertInfo);

            if (words.Count == 0)
            {
                return(value);
            }

            int           indexOffset = 0;
            StringBuilder builder     = new StringBuilder(value);

            foreach (ReplacedWord item in words)
            {
                int newIndex = item.Index + indexOffset;

                if (newIndex + item.Length > builder.Length)
                {
                    break;
                }

                builder.Remove(newIndex, item.Length);
                builder.Insert(newIndex, item.OriginalWord);

                indexOffset += item.OriginalWord.Length - item.Length;
            }

            return(builder.ToString());
        }