private ObservableCollection<SearchResultFile> extendedSearch(string searchTerm, string oldSearchTerm, Collection<SearchableFile> searchable, Collection<SearchResultFile> preResults) { if (searchable.Count != preResults.Count) return search(searchTerm, allFiles); // default - search everything for (int i = 0; i < searchable.Count; i++) { SearchableFile sf = searchable.ElementAt(i); SearchResultFile sr = preResults.ElementAt(i); SearchResultFile postResult = new SearchResultFile(sf); foreach (KeyValuePair<int, string[]> hit in sr.hits) { int index = hit.Key; // filename hit if (index == -1 && sf.FileName.ToLower().Contains(searchTerm.ToLower())) { int index2 = sf.FileName.ToLower().IndexOf(searchTerm.ToLower()); string[] surroundingText = new string[3]; surroundingText[0] = sf.FileName.Substring(0, index2); surroundingText[1] = sf.FileName.Substring(index2, searchTerm.Length); surroundingText[2] = sf.FileName.Substring(index2 + searchTerm.Length); postResult.AddHit(-1, surroundingText); } int end = Math.Min(searchTerm.Length, sf.FileText.Length - index); if (index > -1 && sf.FileText.Substring(index, end).ToLower().Equals(searchTerm.ToLower())) { string[] surroundingText = new string[3]; surroundingText[0] = hit.Value[0]; surroundingText[1] = hit.Value[1] + hit.Value[2].Substring(0, searchTerm.Length - oldSearchTerm.Length); surroundingText[2] = hit.Value[2].Substring(searchTerm.Length - oldSearchTerm.Length); postResult.AddHit(index, surroundingText); } } if (postResult.GetNumHits() > 0) { lastSearchResults[searchTerm].Key.Add(sf); lastSearchResults[searchTerm].Value.Add(postResult); } } return new ObservableCollection<SearchResultFile>(lastSearchResults[searchTerm].Value); }
private ObservableCollection<SearchResultFile> search(string searchTerm, Collection<SearchableFile> filesToSearch) { searchTerm = searchTerm.ToLower(); List<SearchResultFile> searchResults = new List<SearchResultFile>(); foreach (SearchableFile file in filesToSearch) { SearchResultFile result = null; // The filename match will be the first "hit" int index = file.FileName.ToLower().IndexOf(searchTerm); if (index != -1) { result = new SearchResultFile(file); int start = index + searchTerm.Length; string[] surroundingText = new string[3]; surroundingText[0] = file.FileName.Substring(0, index); surroundingText[1] = file.FileName.Substring(index, searchTerm.Length); surroundingText[2] = file.FileName.Substring(start, file.FileName.Length - start); result.AddHit(-1, surroundingText); // -1 is a default value. Not meant for display. } if ((bool)IsolatedStorageSettings.ApplicationSettings["SearchFileTextSetting"]) { index = file.FileText.IndexOf(searchTerm); while (index != -1) { if (result == null) result = new SearchResultFile(file); string[] surroundingText = GetSurroundingText(result, index, file.FileText, searchTerm); result.AddHit(index, surroundingText); index = file.FileText.IndexOf(searchTerm, index + 1); } } if (result != null) { searchResults.Add(result); lastSearchResults[searchTerm].Key.Add(file); lastSearchResults[searchTerm].Value.Add(result); } } searchResults.Sort(); return new ObservableCollection<SearchResultFile>(searchResults); }