test_searchandfind_v2_test_.../Test_SearchAndFind/Search.cs
2020-11-26 17:40:38 +01:00

172 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Test_SearchAndFind
{
/// <summary>
/// Searches the specified string in a text
/// </summary>
class Search
{
public void StartSearch(string text, string searchString, bool noCaseSensitve, bool onlyFullWord)
{
Text = text;
SearchString = searchString;
NoCaseSensitve = noCaseSensitve;
OnlyFullWord = onlyFullWord;
SearchEntities();
if (!Error)
{
CurrentCursorPosition = AllPositions[0];
Index = 0;
}
else
{
//Suchstring wurde nicht gefunden
throw new StringNotMatchException();
}
}
private string Text { set; get; }
private string SearchString { set; get; }
private bool NoCaseSensitve { set; get; }
private bool OnlyFullWord { set; get; }
public int Index { private set; get; }
/// <summary>
/// Contains the current cursor position
/// </summary>
public int CurrentCursorPosition { private set; get; }
/// <summary>
/// Contains a list with all found positions
/// </summary>
public List<int> AllPositions { private set; get; }
/// <summary>
/// Is true if an error has occurred
/// </summary>
public bool Error { private set; get; }
private void SearchEntities()
{
Error = false;
string text = Text;
string searchString = SearchString;
if (NoCaseSensitve)
{
//Groß-/Kleinschreibung ignorieren
text = text.ToLower();
searchString = searchString.ToLower();
}
if (!String.IsNullOrEmpty(searchString) && text.Contains(searchString))
{
AllPositions = Positions(text, searchString);
}
else
{
Error = true;
}
}
private List<int> Positions(string text, string searchString)
{
List<int> positions = new List<int>();
if (String.IsNullOrEmpty(text) || String.IsNullOrEmpty(searchString))
{
return positions;
}
int position = 0;
int currentPos = 0;
while (text.Contains(searchString))
{
if (OnlyFullWord)
{
//Nur ganzes Wort suchen
currentPos = IndexOfWord(text, searchString);
}
else
{
//Suchstring suchen
currentPos = text.IndexOf(searchString);
}
position += currentPos;
positions.Add(position);
position += searchString.Length;
text = text.Remove(0, currentPos + searchString.Length);
}
return positions;
}
private Regex myRegex;
private int IndexOfWord(string myString, string myWord)
{
myRegex = new Regex("\\b" + Regex.Escape(myWord) + "\\b", RegexOptions.Compiled);
Match mtch = myRegex.Match(myString);
if (mtch.Success)
return mtch.Index;
else
return -1;
}
/// <summary>
/// Jumps to the next occurrence position
/// </summary>
public void NextPosition()
{
if (Index + 1 < AllPositions.Count)
{
Index++;
}
else
{
Index = 0;
}
CurrentCursorPosition = AllPositions[Index];
}
/// <summary>
/// Jumps to previous occurrence position
/// </summary>
public void PreviousPosition()
{
if (Index > 0)
{
Index--;
}
else
{
Index = AllPositions.Count - 1;
}
CurrentCursorPosition = AllPositions[Index];
}
}
#region Exceptions
public class StringNotMatchException : Exception
{
public StringNotMatchException() { }
public StringNotMatchException(string message)
: base(message) { }
public StringNotMatchException(string message, Exception inner)
: base(message, inner) { }
}
#endregion
}