internal void FindNextStringOnPage(string findText, bool backward, bool resetSearch, bool matchCase, bool useRegex)
{
if (this.currentTabIndex >= 0 && this.currentTabIndex < this.tab.Count)
{
bool thisSearchWasFromBegin;
int matchLength = 0;
repeatSearch:
if (backward)
{
// Search backwards
currentSearchIndex--;
if (resetSearch || currentSearchIndex < 0 || currentSearchIndex >= tab[this.currentTabIndex].TextEditor.Text.Length) currentSearchIndex = 0;
thisSearchWasFromBegin = (currentSearchIndex == 0);
currentSearchIndex = tab[this.currentTabIndex].TextEditor.Text.LastIndexOf(findText, this.currentSearchIndex, matchCase ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
matchLength = findText.Length;
}
else
{
// Search forwards
currentSearchIndex++;
if (resetSearch || currentSearchIndex < 0 || currentSearchIndex >= tab[this.currentTabIndex].TextEditor.Text.Length) currentSearchIndex = 0;
thisSearchWasFromBegin = (currentSearchIndex == 0);
if (useRegex)
{
try
{
System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(findText, matchCase ? System.Text.RegularExpressions.RegexOptions.None : System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Match regExMatch = regEx.Match(tab[this.currentTabIndex].TextEditor.Text, currentSearchIndex);
if (regExMatch.Success)
{
currentSearchIndex = regExMatch.Index;
matchLength = regExMatch.Length;
}
else
{
currentSearchIndex = -1;
}
}
catch
{
currentSearchIndex = -1;
}
}
else
{
currentSearchIndex = tab[this.currentTabIndex].TextEditor.Text.IndexOf(findText, this.currentSearchIndex, matchCase ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
matchLength = findText.Length;
}
}
if (!this.lastKeyWasEnter)
{
this.lastKeyWasEnter = true;
goto repeatSearch;
}
if (currentSearchIndex >= 0)
{
tab[this.currentTabIndex].TextEditor.Select(currentSearchIndex, matchLength, /*isFindOnPageSelection*/true);
// We are looping and changed to status text to "NO MORE MATCHES", change it back to the match count.
if (thisSearchWasFromBegin)
{
UpdateFind(findText, false, matchCase, useRegex);
}
}
else
{
if (thisSearchWasFromBegin)
{
this.SetStatusText("NO MATCHES FOUND");
}
else
{
this.SetStatusText("NO MORE MATCHES");
}
tab[this.currentTabIndex].TextEditor.Select(0, 0);
}
}
}