示例#1
0
        public static void AnalyzeText(ref string text)
        {
            hrefInfos.Clear();
            if (text == "" || text.Length <= 3)
            {
                return;
            }
            if (!isInit)
            {
                Init();
            }
            //***********第一步:对整个字符串进行正则表达式替换********//
            //三种正则:
            //自定义的([x],[\x])    替换成##
            //系统A类型的(<x>,<\x>) 替换成##
            //系统B类型(<x/>)       替换成$$
            string beginFlagText = text;        //记录富文本的实际开始作用index
            string originalText  = text;

            for (int n = 0; n < handlers.Count; n++)
            {
                URichTextHandler_Base handler = handlers[n];
                int regexCount = 2;
                if (handler.IsSingleRegex())
                {
                    regexCount = 1;
                }
                for (int i = 0; i < regexCount; i++)
                {
                    s_TextBuilder.Length = 0;
                    Regex s_Regex = new Regex(handler.GetRegexInfo()[i], RegexOptions.Singleline);
                    int   isHead  = regexCount == 1 ? -1 : i;
                    foreach (Match match in s_Regex.Matches(originalText))
                    {
                        var      group      = match.Groups[1];
                        string[] richValues = new string[match.Groups.Count];
                        var      hrefInfo   = new URichTextParseInfo
                        {
                            sortIndex = match.Index,
                            type      = (RichType)(n),
                            values    = match.Groups,
                            isHead    = isHead
                        };
                        ////替换字符串,进替换匹配到的第一个
                        int length = match.Value.Length;
                        int index  = text.IndexOf(match.Value);

                        text          = ReplaceTheFirst(text, match.Value, headPlaceholder);
                        beginFlagText = ReplaceTheFirst(beginFlagText, match.Value, isHead == 1 ? "" : headPlaceholder);
                        //text = text.Replace(match.Value, hrefInfo.isHead==1 ? tailPlaceholder:headPlaceholder);  //重复replace,消耗性能
                        hrefInfos.Add(hrefInfo);
                    }
                    //Debug.Log(beginFlagText);
                    //Debug.Log(text);
                }
            }
            //***********第二步:对href进行排序********//
            hrefInfos.Sort(CompareByIndex);

            //***********第三步:对替换后的字符串进行分析********//
            int curTextIndex = 0;
            int curHrefIndex = 0;

            //遍历字符串
            while (true)
            {
                int addTextCount = 1;   //处理完后,会对字符串进行添加的字符数

                if (curTextIndex >= 0 && curTextIndex < text.Length && text[curTextIndex].ToString() == headPlaceholder && curHrefIndex < hrefInfos.Count)
                {
                    URichTextParseInfo hrefInfo = hrefInfos[curHrefIndex];
                    //Debug.Log("currrrr: " + hrefInfo.type + "  " + hrefInfo.sortIndex +"  "+ hrefInfo.isHead);
                    int hrefStartIndex = curTextIndex;     //富文本需要处理字符串的首字母index
                    int hrefEndIndex   = curTextIndex + 1; //富文本需要处理字符串的尾字母index
                    int hrefLength     = 0;                //富文本需要处理字符串的长度
                    int hrefInsideCout = 0;                //富文本需要处理字符串的中内嵌的占位符数量
                    //遍历起始占位符到结尾占位符之间的字符串
                    bool isFindEnd = false;
                    if (hrefInfo.isHead != -1)
                    {
                        while (true)
                        {
                            if (hrefEndIndex >= 0 && hrefEndIndex < text.Length && text[hrefEndIndex].ToString() == headPlaceholder)
                            {
                                hrefInsideCout++;
                                URichTextParseInfo nextHrefInfo = hrefInfos[curHrefIndex + hrefInsideCout];

                                //Debug.Log("nenenenenL: " + nextHrefInfo.type + "  " + nextHrefInfo.sortIndex + " " + nextHrefInfo.isHead);
                                //找到的末尾
                                if (nextHrefInfo.isHead == 1 && nextHrefInfo.type == hrefInfo.type)
                                {
                                    hrefInfos.Remove(nextHrefInfo);
                                    isFindEnd = true;
                                }
                            }

                            if (!isFindEnd)
                            {
                                hrefEndIndex++;
                            }
                            else
                            {
                                hrefInsideCout--;   //减去末尾的
                                                    //检查是否合法:
                                                    //...
                                break;
                            }
                            if (hrefEndIndex >= text.Length)
                            {
                                break;
                            }
                        }
                        if (isFindEnd)
                        {
                            //计算总长度
                            hrefLength = hrefEndIndex - hrefStartIndex - hrefInsideCout + 1 - 2;
                            hrefInfo.vertStartIndex = curTextIndex;
                            hrefInfo.realLength     = hrefLength; //保存作用长度
                            text = handlers[(int)hrefInfo.type].HandleText(ref addTextCount, text, hrefStartIndex, hrefEndIndex, hrefLength, hrefInfo.values);
                            curHrefIndex++;
                        }
                        else
                        {
                            curTextIndex = text.Length + 1;     //如果没找到那就是该字符串有问题,直接返回
                            Debug.LogWarning("regex Type[" + hrefInfo.type + "] has no end!!!");
                        }
                    }
                    else
                    {
                        hrefInfo.vertStartIndex = curTextIndex;
                        hrefInfo.realLength     = 0; //保存作用长度
                        text = handlers[(int)hrefInfo.type].HandleText(ref addTextCount, text, hrefStartIndex, hrefStartIndex, hrefLength, hrefInfo.values);
                        curHrefIndex++;
                    }
                    //Debug.Log("regex Type[" + hrefInfo.type + "]: " + text);
                }

                curTextIndex += addTextCount;
                if (curTextIndex >= text.Length)
                {
                    break;
                }
            }
            //***********第四步:得到HrefInfo的开始作用Index********//
            curTextIndex = 0;
            curHrefIndex = 0;
            //遍历beginFlagText,找到开始的index
            while (true)
            {
                if (beginFlagText[curTextIndex].ToString() == headPlaceholder)
                {
                    //Debug.Log(beginFlagText);
                    if (curHrefIndex >= hrefInfos.Count)
                    {
                        break;
                    }
                    URichTextParseInfo hrefInfo = hrefInfos[curHrefIndex];
                    hrefInfo.startIndex = curTextIndex;
                    beginFlagText       = beginFlagText.Remove(curTextIndex, PLACE_HOLDER_LENGTH);
                    curHrefIndex++;
                }
                else
                {
                    curTextIndex++;
                }
                if (curTextIndex >= beginFlagText.Length)
                {
                    break;
                }
            }
            //Debug.Log(beginFlagText);
        }
示例#2
0
 public static void AddHandler(URichTextHandler_Base handler)
 {
     handlers.Add(handler);
 }