From 7099880170d3e4d90f9d159b016595da17e66ff3 Mon Sep 17 00:00:00 2001 From: niksedk Date: Tue, 2 Jul 2013 20:00:02 +0000 Subject: [PATCH] Fixed slow loading of regular expressions in "Multiple replace" - thx TeDDy :) git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@1905 99eadd0c-20b8-1223-b5c4-2a2b2df33de2 --- src/Forms/MeasurementConverter.Designer.cs | 1 + src/Forms/MeasurementConverter.cs | 19 +++++ src/Forms/MultipleReplace.Designer.cs | 86 +++++++++++----------- src/Forms/MultipleReplace.cs | 21 +++++- 4 files changed, 82 insertions(+), 45 deletions(-) diff --git a/src/Forms/MeasurementConverter.Designer.cs b/src/Forms/MeasurementConverter.Designer.cs index 35e095360..6b434fead 100644 --- a/src/Forms/MeasurementConverter.Designer.cs +++ b/src/Forms/MeasurementConverter.Designer.cs @@ -114,6 +114,7 @@ this.textBoxInput.Size = new System.Drawing.Size(179, 20); this.textBoxInput.TabIndex = 10; this.textBoxInput.TextChanged += new System.EventHandler(this.textBoxInput_TextChanged); + this.textBoxInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxInput_KeyPress); this.textBoxInput.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBoxInput_KeyUp); // // MeasurementConverter diff --git a/src/Forms/MeasurementConverter.cs b/src/Forms/MeasurementConverter.cs index da3b52afd..174e43b72 100644 --- a/src/Forms/MeasurementConverter.cs +++ b/src/Forms/MeasurementConverter.cs @@ -7,6 +7,7 @@ namespace Nikse.SubtitleEdit.Forms { public partial class MeasurementConverter : Form { + Color _defaultBackColor = Color.White; public MeasurementConverter() { InitializeComponent(); @@ -18,6 +19,9 @@ namespace Nikse.SubtitleEdit.Forms linkLabel1.Text = l.CopyToClipboard; buttonOK.Text = Configuration.Settings.Language.General.OK; + _defaultBackColor = textBoxInput.BackColor; + textBoxInput.Text = "1"; + comboBoxFrom.Items.Add(l.Fahrenheit); comboBoxFrom.Items.Add(l.Celsius); @@ -156,7 +160,11 @@ namespace Nikse.SubtitleEdit.Forms double d; if (!double.TryParse(textBoxInput.Text, out d)) + { + textBoxInput.BackColor = Configuration.Settings.Tools.ListViewSyntaxErrorColor; return; + } + textBoxInput.BackColor = _defaultBackColor; string text = comboBoxFrom.SelectedItem.ToString(); string textTo = comboBoxTo.SelectedItem.ToString(); @@ -324,5 +332,16 @@ namespace Nikse.SubtitleEdit.Forms textBoxInput_TextChanged(null, null); } + private void textBoxInput_KeyPress(object sender, KeyPressEventArgs e) + { + if (Char.IsDigit(e.KeyChar)) + return; + + if (e.KeyChar == Convert.ToChar(Keys.Back) || (e.KeyChar == '.') || (e.KeyChar == ',') || (e.KeyChar == '-')) + return; + + e.Handled = true; + } + } } diff --git a/src/Forms/MultipleReplace.Designer.cs b/src/Forms/MultipleReplace.Designer.cs index 4eacb806b..1cccbc9b6 100644 --- a/src/Forms/MultipleReplace.Designer.cs +++ b/src/Forms/MultipleReplace.Designer.cs @@ -38,6 +38,8 @@ this.columnHeader7 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader8 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.groupBoxReplaces = new System.Windows.Forms.GroupBox(); + this.radioButtonRegEx = new System.Windows.Forms.RadioButton(); + this.radioButtonCaseSensitive = new System.Windows.Forms.RadioButton(); this.buttonRemoveAll = new System.Windows.Forms.Button(); this.buttonExport = new System.Windows.Forms.Button(); this.buttonImport = new System.Windows.Forms.Button(); @@ -47,9 +49,6 @@ this.buttonAdd = new System.Windows.Forms.Button(); this.labelFindWhat = new System.Windows.Forms.Label(); this.labelReplaceWith = new System.Windows.Forms.Label(); - this.radioButtonRegEx = new System.Windows.Forms.RadioButton(); - this.radioButtonCaseSensitive = new System.Windows.Forms.RadioButton(); - this.radioButtonNormal = new System.Windows.Forms.RadioButton(); this.listViewReplaceList = new System.Windows.Forms.ListView(); this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); @@ -59,6 +58,7 @@ this.deleteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.moveUpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.moveDownToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.radioButtonNormal = new System.Windows.Forms.RadioButton(); this.buttonOK = new System.Windows.Forms.Button(); this.buttonCancel = new System.Windows.Forms.Button(); this.splitContainer1 = new System.Windows.Forms.SplitContainer(); @@ -175,6 +175,28 @@ this.groupBoxReplaces.TabIndex = 0; this.groupBoxReplaces.TabStop = false; // + // radioButtonRegEx + // + this.radioButtonRegEx.AutoSize = true; + this.radioButtonRegEx.Location = new System.Drawing.Point(286, 62); + this.radioButtonRegEx.Name = "radioButtonRegEx"; + this.radioButtonRegEx.Size = new System.Drawing.Size(56, 17); + this.radioButtonRegEx.TabIndex = 6; + this.radioButtonRegEx.Text = "RegEx"; + this.radioButtonRegEx.UseVisualStyleBackColor = true; + this.radioButtonRegEx.CheckedChanged += new System.EventHandler(this.RadioButtonCheckedChanged); + // + // radioButtonCaseSensitive + // + this.radioButtonCaseSensitive.AutoSize = true; + this.radioButtonCaseSensitive.Location = new System.Drawing.Point(146, 62); + this.radioButtonCaseSensitive.Name = "radioButtonCaseSensitive"; + this.radioButtonCaseSensitive.Size = new System.Drawing.Size(94, 17); + this.radioButtonCaseSensitive.TabIndex = 5; + this.radioButtonCaseSensitive.Text = "Case sensitive"; + this.radioButtonCaseSensitive.UseVisualStyleBackColor = true; + this.radioButtonCaseSensitive.CheckedChanged += new System.EventHandler(this.RadioButtonCheckedChanged); + // // buttonRemoveAll // this.buttonRemoveAll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); @@ -261,41 +283,6 @@ this.labelReplaceWith.TabIndex = 21; this.labelReplaceWith.Text = "Replace with:"; // - // radioButtonRegEx - // - this.radioButtonRegEx.AutoSize = true; - this.radioButtonRegEx.Location = new System.Drawing.Point(286, 62); - this.radioButtonRegEx.Name = "radioButtonRegEx"; - this.radioButtonRegEx.Size = new System.Drawing.Size(56, 17); - this.radioButtonRegEx.TabIndex = 6; - this.radioButtonRegEx.Text = "RegEx"; - this.radioButtonRegEx.UseVisualStyleBackColor = true; - this.radioButtonRegEx.CheckedChanged += new System.EventHandler(this.RadioButtonCheckedChanged); - // - // radioButtonCaseSensitive - // - this.radioButtonCaseSensitive.AutoSize = true; - this.radioButtonCaseSensitive.Location = new System.Drawing.Point(146, 62); - this.radioButtonCaseSensitive.Name = "radioButtonCaseSensitive"; - this.radioButtonCaseSensitive.Size = new System.Drawing.Size(94, 17); - this.radioButtonCaseSensitive.TabIndex = 5; - this.radioButtonCaseSensitive.Text = "Case sensitive"; - this.radioButtonCaseSensitive.UseVisualStyleBackColor = true; - this.radioButtonCaseSensitive.CheckedChanged += new System.EventHandler(this.RadioButtonCheckedChanged); - // - // radioButtonNormal - // - this.radioButtonNormal.AutoSize = true; - this.radioButtonNormal.Checked = true; - this.radioButtonNormal.Location = new System.Drawing.Point(22, 62); - this.radioButtonNormal.Name = "radioButtonNormal"; - this.radioButtonNormal.Size = new System.Drawing.Size(58, 17); - this.radioButtonNormal.TabIndex = 4; - this.radioButtonNormal.TabStop = true; - this.radioButtonNormal.Text = "Normal"; - this.radioButtonNormal.UseVisualStyleBackColor = true; - this.radioButtonNormal.CheckedChanged += new System.EventHandler(this.RadioButtonCheckedChanged); - // // listViewReplaceList // this.listViewReplaceList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -316,7 +303,6 @@ this.listViewReplaceList.TabIndex = 4; this.listViewReplaceList.UseCompatibleStateImageBehavior = false; this.listViewReplaceList.View = System.Windows.Forms.View.Details; - this.listViewReplaceList.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(this.ListViewReplaceListItemChecked); this.listViewReplaceList.SelectedIndexChanged += new System.EventHandler(this.ListViewReplaceListSelectedIndexChanged); this.listViewReplaceList.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ListViewReplaceListKeyDown); // @@ -347,30 +333,43 @@ this.moveUpToolStripMenuItem, this.moveDownToolStripMenuItem}); this.contextMenuStrip1.Name = "contextMenuStrip1"; - this.contextMenuStrip1.Size = new System.Drawing.Size(153, 92); + this.contextMenuStrip1.Size = new System.Drawing.Size(138, 70); this.contextMenuStrip1.Opening += new System.ComponentModel.CancelEventHandler(this.contextMenuStrip1_Opening); // // deleteToolStripMenuItem // this.deleteToolStripMenuItem.Name = "deleteToolStripMenuItem"; - this.deleteToolStripMenuItem.Size = new System.Drawing.Size(152, 22); + this.deleteToolStripMenuItem.Size = new System.Drawing.Size(137, 22); this.deleteToolStripMenuItem.Text = "Delete"; this.deleteToolStripMenuItem.Click += new System.EventHandler(this.DeleteToolStripMenuItemClick); // // moveUpToolStripMenuItem // this.moveUpToolStripMenuItem.Name = "moveUpToolStripMenuItem"; - this.moveUpToolStripMenuItem.Size = new System.Drawing.Size(152, 22); + this.moveUpToolStripMenuItem.Size = new System.Drawing.Size(137, 22); this.moveUpToolStripMenuItem.Text = "Move up"; this.moveUpToolStripMenuItem.Click += new System.EventHandler(this.moveUpToolStripMenuItem_Click); // // moveDownToolStripMenuItem // this.moveDownToolStripMenuItem.Name = "moveDownToolStripMenuItem"; - this.moveDownToolStripMenuItem.Size = new System.Drawing.Size(152, 22); + this.moveDownToolStripMenuItem.Size = new System.Drawing.Size(137, 22); this.moveDownToolStripMenuItem.Text = "Move down"; this.moveDownToolStripMenuItem.Click += new System.EventHandler(this.moveDownToolStripMenuItem_Click); // + // radioButtonNormal + // + this.radioButtonNormal.AutoSize = true; + this.radioButtonNormal.Checked = true; + this.radioButtonNormal.Location = new System.Drawing.Point(22, 62); + this.radioButtonNormal.Name = "radioButtonNormal"; + this.radioButtonNormal.Size = new System.Drawing.Size(58, 17); + this.radioButtonNormal.TabIndex = 4; + this.radioButtonNormal.TabStop = true; + this.radioButtonNormal.Text = "Normal"; + this.radioButtonNormal.UseVisualStyleBackColor = true; + this.radioButtonNormal.CheckedChanged += new System.EventHandler(this.RadioButtonCheckedChanged); + // // buttonOK // this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); @@ -432,6 +431,7 @@ this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Multiple replace"; + this.Shown += new System.EventHandler(this.MultipleReplace_Shown); this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MultipleReplace_KeyDown); this.groupBoxLinesFound.ResumeLayout(false); this.groupBoxReplaces.ResumeLayout(false); diff --git a/src/Forms/MultipleReplace.cs b/src/Forms/MultipleReplace.cs index 8e689d25c..e916c0546 100644 --- a/src/Forms/MultipleReplace.cs +++ b/src/Forms/MultipleReplace.cs @@ -13,7 +13,7 @@ namespace Nikse.SubtitleEdit.Forms public const string SearchTypeNormal = "Normal"; public const string SearchTypeCaseSensitive = "CaseSensitive"; public const string SearchTypeRegularExpression = "RegularExpression"; - + Dictionary _compiledRegExList = new Dictionary(); Subtitle _subtitle; public Subtitle FixedSubtitle { get; private set; } public int FixCount { get; private set; } @@ -161,7 +161,18 @@ namespace Nikse.SubtitleEdit.Forms } else if (searchType == Configuration.Settings.Language.MultipleReplace.RegularExpression) { - string result = Regex.Replace(newText, findWhat, replaceWith, RegexOptions.Multiline); + Regex r; + if (_compiledRegExList.ContainsKey(findWhat)) + { + r = _compiledRegExList[findWhat]; + } + else + { + r = new Regex(findWhat, RegexOptions.Compiled | RegexOptions.Multiline); + _compiledRegExList.Add(findWhat, r); + } + + string result = r.Replace(newText, replaceWith); if (result != newText) { hit = true; @@ -511,6 +522,12 @@ namespace Nikse.SubtitleEdit.Forms listViewReplaceList.Items.Clear(); } + private void MultipleReplace_Shown(object sender, EventArgs e) + { + listViewReplaceList.ItemChecked +=ListViewReplaceListItemChecked; + GeneratePreview(); + } + } }