public void Search() { var str = "This is a string to search in."; var substr = "is"; var searcher = new KmpSearcher(Encoding.ASCII.GetBytes(substr)); var offset = searcher.Search(Encoding.ASCII.GetBytes(str)); Assert.AreEqual(2, offset); }
private void btnFindText_Click(object sender, EventArgs e) { lstResults.Items.Clear(); if (txtSearchDirectory.Text.Trim() != string.Empty && Directory.Exists(txtSearchDirectory.Text.Trim())) { if (txtSearchText.Text.Trim() != string.Empty) { var W = (cmbEncoding.SelectedValue as Encoding)?.GetBytes(txtSearchText.Text); var files = Directory.GetFiles(txtSearchDirectory.Text.Trim(), "*.*", (chkSearchSubfolders.Checked ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)); var results = new List <SearchResult>(); var searcher = new KmpSearcher(W); foreach (var file in files.Where(o => new FileInfo(o).Length < (8 * 1024 * 1024))) { using (var br = new BinaryReaderX(File.OpenRead(file))) { var offset = searcher.Search(br); if (offset >= 0) { results.Add(new SearchResult { Filename = file, Offset = offset }); } } } lstResults.Items.AddRange(results.ToArray()); } } if (lstResults.Items.Count == 0) { MessageBox.Show($"Could not find \"{txtSearchText.Text}\".", Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } }
public ReturnObject Post(SearchQuery query) { try { List <SearchResult> result = new List <SearchResult>(); Searcher searcher; if (query.Pattern == null) { query.Pattern = ""; } query.Pattern = query.Pattern.Trim(); if (query.Pattern.Equals("")) { throw new ArgumentException("Pattern kosong."); } if (query.Id == 0) { searcher = new KmpSearcher(query.Pattern); } else if (query.Id == 1) { searcher = new BoyerMooreSearcher(query.Pattern); } else if (query.Id == 2) { searcher = new RegexSearcher(query.Pattern); } else { throw new NotImplementedException(); } PrepareNewsList(query.Source); foreach (News news in newsList) { SearchResult searchResult = new SearchResult() { Url = news.Url, Title = news.Title, ImageUrl = news.ImageUrl, PubDate = news.PubDate }; bool found = false; int indexMatchContent = searcher.CheckMatch(news.Content); if (indexMatchContent != -1) { found = true; searchResult.Match = SearchResult.StringToMatch(news.Content, query.Pattern, indexMatchContent, 200); } else { int indexMatchTitle = searcher.CheckMatch(news.Title); if (indexMatchTitle != -1) { found = true; searchResult.Match = SearchResult.StringToMatch(news.Title, query.Pattern, indexMatchTitle, 50) + " (Pada Judul)"; } } if (found) { result.Add(searchResult); } } return(new ReturnObject() { status = true, data = result }); } catch (Exception e) { return(new ReturnObject() { status = false, data = e.Message }); } }
public static int CheckMatch(string text, string pattern) { KmpSearcher kmp = new KmpSearcher(pattern); return(kmp.CheckMatch(text)); }