Fixes for Find (wholeword + count) - thx André :)

This commit is contained in:
niksedk 2016-01-17 07:28:42 +01:00
parent dbe92f359f
commit 019673f6b2
4 changed files with 43 additions and 25 deletions

View File

@ -72,7 +72,7 @@ namespace Nikse.SubtitleEdit.Forms
public FindReplaceDialogHelper GetFindDialogHelper(int startLineIndex)
{
return new FindReplaceDialogHelper(FindType, FindText, _regEx, string.Empty, 200, 300, startLineIndex);
return new FindReplaceDialogHelper(FindType, checkBoxWholeWord.Checked, FindText, _regEx, string.Empty, 200, 300, startLineIndex);
}
private void FormFindDialog_KeyDown(object sender, KeyEventArgs e)
@ -147,6 +147,7 @@ namespace Nikse.SubtitleEdit.Forms
textBoxFind.ContextMenu = null;
comboBoxFind.ContextMenu = null;
}
checkBoxWholeWord.Enabled = !radioButtonRegEx.Checked;
}
internal void Initialize(string selectedText, FindReplaceDialogHelper findHelper)
@ -175,6 +176,8 @@ namespace Nikse.SubtitleEdit.Forms
if (findHelper != null)
{
FindType = findHelper.FindType;
checkBoxWholeWord.Checked = findHelper.MatchWholeWord;
checkBoxWholeWord.Enabled = FindType != FindType.RegEx;
}
}

View File

@ -3948,7 +3948,8 @@ namespace Nikse.SubtitleEdit.Forms
_findHelper = findDialog.GetFindDialogHelper(_subtitleListViewIndex);
if (!string.IsNullOrWhiteSpace(_findHelper.FindText))
{
Configuration.Settings.Tools.FindHistory.Insert(0, _findHelper.FindText);
if (Configuration.Settings.Tools.FindHistory.Count == 0 || Configuration.Settings.Tools.FindHistory[0] != _findHelper.FindText)
Configuration.Settings.Tools.FindHistory.Insert(0, _findHelper.FindText);
}
ShowStatus(string.Format(_language.SearchingForXFromLineY, _findHelper.FindText, _subtitleListViewIndex + 1));
if (tabControlSubtitle.SelectedIndex == TabControlListView)
@ -11827,7 +11828,7 @@ namespace Nikse.SubtitleEdit.Forms
_clearLastFindType = _findHelper.FindType;
_clearLastFindText = _findHelper.FindText;
}
_findHelper = new FindReplaceDialogHelper(FindType.RegEx, string.Format(_language.DoubleWordsViaRegEx, regex), regex, string.Empty, 0, 0, _subtitleListViewIndex);
_findHelper = new FindReplaceDialogHelper(FindType.RegEx, false, string.Format(_language.DoubleWordsViaRegEx, regex), regex, string.Empty, 0, 0, _subtitleListViewIndex);
ReloadFromSourceView();
FindNext();

View File

@ -49,7 +49,7 @@ namespace Nikse.SubtitleEdit.Forms
public FindReplaceDialogHelper GetFindDialogHelper(int startLineIndex)
{
return new FindReplaceDialogHelper(GetFindType(), textBoxFind.Text, _regEx, textBoxReplace.Text, _left, _top, startLineIndex);
return new FindReplaceDialogHelper(GetFindType(), false, textBoxFind.Text, _regEx, textBoxReplace.Text, _left, _top, startLineIndex);
}
private void FormReplaceDialog_KeyDown(object sender, KeyEventArgs e)

View File

@ -8,7 +8,7 @@ namespace Nikse.SubtitleEdit.Logic
{
public class FindReplaceDialogHelper
{
private const string StartChars = " >-\"”“['`´¶(♪¿¡.…—";
private const string StartChars = " >-\"”“['`´¶(♪¿¡.…—\r\n";
private const string EndChars = " -\"”“]'`´¶)♪.!?:…—\r\n";
private readonly string _findText = string.Empty;
private readonly string _replaceText = string.Empty;
@ -17,6 +17,7 @@ namespace Nikse.SubtitleEdit.Logic
public bool Success { get; set; }
public FindType FindType { get; set; }
public bool MatchWholeWord { get; set; }
public int SelectedIndex { get; set; }
public int SelectedPosition { get; set; }
public int WindowPositionLeft { get; set; }
@ -48,7 +49,7 @@ namespace Nikse.SubtitleEdit.Logic
}
}
public FindReplaceDialogHelper(FindType findType, string findText, Regex regEx, string replaceText, int left, int top, int startLineIndex)
public FindReplaceDialogHelper(FindType findType, bool matchWholeWord, string findText, Regex regEx, string replaceText, int left, int top, int startLineIndex)
{
FindType = findType;
_findText = findText;
@ -64,6 +65,7 @@ namespace Nikse.SubtitleEdit.Logic
WindowPositionLeft = left;
WindowPositionTop = top;
StartLineIndex = startLineIndex;
MatchWholeWord = matchWholeWord;
}
public bool Find(Subtitle subtitle, Subtitle originalSubtitle, int startIndex)
@ -81,28 +83,39 @@ namespace Nikse.SubtitleEdit.Logic
if (startIndex >= text.Length && !(FindType == FindType.RegEx && startIndex == 0))
return -1;
switch (FindType)
if (FindType == FindType.CaseSensitive || FindType == FindType.Normal)
{
case FindType.Normal:
return (text.IndexOf(_findText, startIndex, System.StringComparison.OrdinalIgnoreCase));
case FindType.CaseSensitive:
return (text.IndexOf(_findText, startIndex, System.StringComparison.Ordinal));
case FindType.RegEx:
var comparison = FindType == FindType.CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
var idx = text.IndexOf(_findText, startIndex, comparison);
while (idx >= 0)
{
if (MatchWholeWord)
{
Match match = _regEx.Match(text, startIndex);
if (match.Success)
{
string groupName = Utilities.GetRegExGroup(_findText);
if (groupName != null && match.Groups[groupName] != null && match.Groups[groupName].Success)
{
_findTextLenght = match.Groups[groupName].Length;
return match.Groups[groupName].Index;
}
_findTextLenght = match.Length;
return match.Index;
}
return -1;
var startOk = idx == 0 || StartChars.Contains(text[idx - 1]);
var endOk = idx + _findText.Length == text.Length || EndChars.Contains(text[idx + _findText.Length]);
if (startOk && endOk)
return idx;
}
else
{
return idx;
}
idx = text.IndexOf(_findText, idx + _findText.Length, comparison);
}
return -1;
}
var match = _regEx.Match(text, startIndex);
if (match.Success)
{
string groupName = Utilities.GetRegExGroup(_findText);
if (groupName != null && match.Groups[groupName] != null && match.Groups[groupName].Success)
{
_findTextLenght = match.Groups[groupName].Length;
return match.Groups[groupName].Index;
}
_findTextLenght = match.Length;
return match.Index;
}
return -1;
}
@ -294,5 +307,6 @@ namespace Nikse.SubtitleEdit.Logic
}
return count;
}
}
}