/** 注册替换文字工具 */ public void registReplaceTextTool(int type, ReplaceTextTool tool) { _replaceTools[type] = tool; }
/** 替换字符串(颜色,文字标记) */ public string replaceText(int type, string text, object obj) { ReplaceTextTool tool = _replaceTools[type]; if (tool == null) { Ctrl.throwError("不该找不到替换文字工具"); return(text); } int pos = 0; bool isColoring = false; bool isMatching = false; StringBuilder sb = null; while (true) { int index = text.IndexOfAny(_replaceMarkArr, pos); if (index != -1) { if (sb == null) { sb = StringBuilderPool.create(); } //颜色 if (text[index] == CommonSetting.replaceTextColorMark) { sb.Append(text.slice(pos, index)); //结束 if (isColoring) { isColoring = false; addColorEnd(sb); pos = index + 1; } //开始 else { if (text.Length <= index + CommonSetting.replaceTextColorLength) { Ctrl.throwError("替换文本的字符串缺少"); } isColoring = true; addColorFront(sb, text.Substring(index + 1, CommonSetting.replaceTextColorLength)); pos = index + 1 + CommonSetting.replaceTextColorLength; } } else { //结束 if (isMatching) { isMatching = false; sb.Append(tool.replace(text.slice(pos, index), obj)); pos = index + 1; } //开始 else { sb.Append(text.slice(pos, index)); isMatching = true; pos = index + 1; } } } else { if (sb != null) { sb.Append(text.Substring(pos)); } break; } } //无需替换 if (sb == null) { return(text); } if (isColoring || isMatching) { StringBuilderPool.release(sb); Ctrl.throwError("替换文本的字符串未配置完整"); return(text); } return(StringBuilderPool.releaseStr(sb)); }