// 搜索 private List <LangLine> Search(GetMatchTextDelegate GetMatchText, bool useRegex, string keyword, bool ignoreCase) { if (useRegex) { return(SearchRegex(GetMatchText, keyword, ignoreCase)); } List <LangLine> results = new List <LangLine>(); foreach (LangLine langLine in langLines) { // 如果找到就添加 if (IsMatch(GetMatchText(langLine), keyword, ignoreCase)) { results.Add(langLine); } if (results.Count >= MAX_RESULT_NUM) { results.Add(overflow); break; } } return(results); }
// 正则搜索 private List <LangLine> SearchRegex(GetMatchTextDelegate GetMatchText, string pattern, bool ignoreCase) { RegexOptions option = RegexOptions.Compiled; if (ignoreCase) { option |= RegexOptions.IgnoreCase; } Regex r = new Regex(pattern, option); // 第一遍查找 // 因为正则查找太慢,所以先大致找一次 string[] keywords = pattern .Trim('$') .Trim('^') .Split(new string[] { ".*" }, StringSplitOptions.RemoveEmptyEntries); List <LangLine> firstPassResults = new List <LangLine>(); foreach (LangLine langLine in langLines) { foreach (string keyword in keywords) { if (IsMatch(GetMatchText(langLine), keyword, ignoreCase)) { firstPassResults.Add(langLine); break; } } } // 第二遍查找 List <LangLine> results = new List <LangLine>(); foreach (LangLine langLine in firstPassResults) { // 如果找到就添加 if (IsMatchRegex(GetMatchText(langLine), r)) { results.Add(langLine); } if (results.Count >= MAX_RESULT_NUM) { results.Add(overflow); break; } } return(results); }