Add dumb special casing to DialogSearchReplace so that replacing '$' with 'str' with regular expressions enabled results in 'str' being appended once rather than 1000 times.

Originally committed to SVN as r6062.
This commit is contained in:
Thomas Goyne 2011-12-22 21:19:12 +00:00
parent 3ecc731ff3
commit 0201b8ea9b
2 changed files with 43 additions and 46 deletions

View file

@ -257,9 +257,11 @@ void SearchReplaceEngine::ReplaceNext(bool DoReplace) {
// Search for it // Search for it
while (!found) { while (!found) {
Text = GetText(curLine,field); Text = GetText(context->subsGrid->GetDialogue(curLine), field);
if (DoReplace && LastWasFind) tempPos = pos; if (DoReplace && LastWasFind)
else tempPos = pos+replaceLen; tempPos = pos;
else
tempPos = pos+replaceLen;
// RegExp // RegExp
if (isReg) { if (isReg) {
@ -347,55 +349,55 @@ void SearchReplaceEngine::ReplaceNext(bool DoReplace) {
/// @brief Replace all instances /// @brief Replace all instances
void SearchReplaceEngine::ReplaceAll() { void SearchReplaceEngine::ReplaceAll() {
wxString *Text;
int nrows = context->subsGrid->GetRows();
size_t count = 0; size_t count = 0;
int regFlags = wxRE_ADVANCED; int regFlags = wxRE_ADVANCED;
if (!matchCase) { if (!matchCase)
if (isReg) regFlags |= wxRE_ICASE; regFlags |= wxRE_ICASE;
//else LookFor.MakeLower(); wxRegEx reg;
} if (isReg)
bool replaced; reg.Compile(LookFor, regFlags);
context->subsGrid->BeginBatch();
// Selection // Selection
bool hasSelection = false; std::set<AssDialogue*> sel = context->selectionController->GetSelectedSet();
wxArrayInt sels = context->subsGrid->GetSelection(); bool hasSelection = !sel.empty();
if (sels.Count() > 0) hasSelection = true; bool inSel = affect == 1;
bool inSel = false;
if (affect == 1) inSel = true;
// Scan // Scan
for (int i=0;i<nrows;i++) { for (std::list<AssEntry*>::iterator it = context->ass->Line.begin(); it != context->ass->Line.end(); ++it) {
// Check if row is selected AssDialogue *diag = dynamic_cast<AssDialogue*>(*it);
if (inSel && hasSelection && sels.Index(i) == wxNOT_FOUND) { if (!diag) continue;
continue;
}
// Prepare // Check if row is selected
replaced = false; if (inSel && hasSelection && !sel.count(diag))
Text = GetText(i,field); continue;
wxString *Text = GetText(diag, field);
// Regular expressions // Regular expressions
if (isReg) { if (isReg) {
wxRegEx reg(LookFor,regFlags); if (reg.Matches(*Text)) {
if (reg.IsValid()) { size_t start, len;
size_t reps = reg.Replace(Text,ReplaceWith,1000); reg.GetMatch(&start, &len);
if (reps > 0) replaced = true;
count += reps; // A zero length match (such as '$') will always be replaced
// maxMatches times, which is almost certainly not what the user
// wanted, so limit it to one replacement in that situation
count += reg.Replace(Text, ReplaceWith, len > 0 ? 1000 : 1);
} }
} }
// Normal replace // Normal replace
else { else {
if (!Search.matchCase) { if (!Search.matchCase) {
wxString Left = "", Right = *Text; bool replaced = false;
int pos = 0; wxString Left, Right = *Text;
Left.Alloc(Right.Len()); size_t pos = 0;
while (pos <= (int)(Right.Len() - LookFor.Len())) { Left.reserve(Right.size());
if (Right.Mid(pos, LookFor.Len()).CmpNoCase(LookFor) == 0) { while (pos + LookFor.size() <= Right.size()) {
Left.Append(Right.Mid(0,pos)).Append(ReplaceWith); if (Right.Mid(pos, LookFor.size()).CmpNoCase(LookFor) == 0) {
Right = Right.Mid(pos+LookFor.Len()); Left.Append(Right.Left(pos)).Append(ReplaceWith);
count++; Right = Right.Mid(pos + LookFor.Len());
++count;
replaced = true; replaced = true;
pos = 0; pos = 0;
} }
@ -407,11 +409,8 @@ void SearchReplaceEngine::ReplaceAll() {
*Text = Left + Right; *Text = Left + Right;
} }
} }
else { else if(Text->Contains(LookFor)) {
if(Text->Contains(LookFor)) { count += Text->Replace(LookFor, ReplaceWith);
count += Text->Replace(LookFor, ReplaceWith);
replaced = true;
}
} }
} }
} }
@ -423,7 +422,6 @@ void SearchReplaceEngine::ReplaceAll() {
else { else {
wxMessageBox(_("No matches found.")); wxMessageBox(_("No matches found."));
} }
context->subsGrid->EndBatch();
LastWasFind = false; LastWasFind = false;
} }
@ -462,8 +460,7 @@ void SearchReplaceEngine::OpenDialog (bool replace) {
hasReplace = replace; hasReplace = replace;
} }
wxString *SearchReplaceEngine::GetText(int n,int field) { wxString *SearchReplaceEngine::GetText(AssDialogue *cur, int field) {
AssDialogue *cur = context->subsGrid->GetDialogue(n);
if (field == 0) return &cur->Text; if (field == 0) return &cur->Text;
else if (field == 1) return &cur->Style; else if (field == 1) return &cur->Style;
else if (field == 2) return &cur->Actor; else if (field == 2) return &cur->Actor;

View file

@ -101,7 +101,7 @@ class SearchReplaceEngine {
wxString ReplaceWith; wxString ReplaceWith;
wxString *GetText(int n,int field); wxString *GetText(AssDialogue *cur, int field);
public: public:
/// DOCME /// DOCME