Skip to content

Commit

Permalink
modified the .Net regex converter to support multiple steps (i.e. mul…
Browse files Browse the repository at this point in the history
…tiple regular expressions processed one after the other).
  • Loading branch information
bobeaton committed Oct 5, 2024
1 parent a2d5919 commit 85f6ccd
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 30 deletions.
4 changes: 4 additions & 0 deletions src/EncCnvtrs/NetRegexAutoConfigDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 56 additions & 8 deletions src/EncCnvtrs/NetRegexAutoConfigDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ namespace SilEncConverters40
{
public partial class NetRegexAutoConfigDialog : SilEncConverters40.AutoConfigDialog
{
public const int ColumnIndexEnabled = 0;
public const int ColumnIndexFindWhat = 1;
public const int ColumnIndexReplaceWith = 2;
public const int ColumnIndexRegexOptions = 3;
public const int ColumnIndexEnabled = 0;
public const int ColumnIndexFindWhat = 1;
public const int ColumnIndexReplaceWith = 2;
public const int ColumnIndexRegexOptions = 3;

public NetRegexAutoConfigDialog (
public NetRegexAutoConfigDialog (
IEncConverters aECs,
string strDisplayName,
string strFriendlyName,
Expand All @@ -41,11 +41,11 @@ public NetRegexAutoConfigDialog (
#if VERBOSE_DEBUGGING
Console.WriteLine("NetRegexAutoConfigDialog ctor BEGIN");
#endif
InitializeComponent();
InitializeComponent();
#if VERBOSE_DEBUGGING
Console.WriteLine("Initialized NetRegexAutoConfigDialog component.");
#endif
base.Initialize (
base.Initialize (
aECs,
NetRegexEncConverter.strHtmlFilename,
strDisplayName,
Expand Down Expand Up @@ -380,6 +380,54 @@ private void linkLabelOpenQuickReference_LinkClicked(object sender, LinkLabelLin
{
System.Diagnostics.Process.Start(Properties.Resources.NetRegexQuickReferenceLink);
}
}

private int rowIndexFromMouseDown;
private int rowIndexOfItemUnderMouseToDrop;

private void dataGridViewRegularExpressions_MouseDown(object sender, MouseEventArgs e)
{
// Get the index of the row to drag
rowIndexFromMouseDown = dataGridViewRegularExpressions.HitTest(e.X, e.Y).RowIndex;
if ((rowIndexFromMouseDown < 0) || (rowIndexFromMouseDown > dataGridViewRegularExpressions.Rows.Count - 2))
return;
System.Diagnostics.Debug.WriteLine($"Select row index: {rowIndexFromMouseDown}");

if (rowIndexFromMouseDown != -1)
{
dataGridViewRegularExpressions.DoDragDrop(dataGridViewRegularExpressions.Rows[rowIndexFromMouseDown], DragDropEffects.Move | DragDropEffects.Copy);
}
}

private void dataGridViewRegularExpressions_DragOver(object sender, DragEventArgs e)
{
// Provide visual feedback during the drag operation
Point clientPoint = dataGridViewRegularExpressions.PointToClient(new Point(e.X, e.Y));
rowIndexOfItemUnderMouseToDrop = dataGridViewRegularExpressions.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
if ((rowIndexOfItemUnderMouseToDrop < 0) || (rowIndexOfItemUnderMouseToDrop > dataGridViewRegularExpressions.Rows.Count - 2))
{
e.Effect = DragDropEffects.None;
return;
}

var row = dataGridViewRegularExpressions.Rows[rowIndexOfItemUnderMouseToDrop];
e.Effect = row.IsNewRow ? DragDropEffects.None : DragDropEffects.Move;
}

private void dataGridViewRegularExpressions_DragDrop(object sender, DragEventArgs e)
{
// Get the row index where the row will be dropped
Point clientPoint = dataGridViewRegularExpressions.PointToClient(new Point(e.X, e.Y));
rowIndexOfItemUnderMouseToDrop = dataGridViewRegularExpressions.HitTest(clientPoint.X, clientPoint.Y).RowIndex;

if (rowIndexOfItemUnderMouseToDrop != -1 && rowIndexFromMouseDown != -1)
{
// Perform the row reordering
var rowToMove = dataGridViewRegularExpressions.Rows[rowIndexFromMouseDown];
dataGridViewRegularExpressions.Rows.RemoveAt(rowIndexFromMouseDown);
dataGridViewRegularExpressions.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
IsModified = true;
}
}
}
}

6 changes: 3 additions & 3 deletions src/EncCnvtrs/NetRegexAutoConfigDialog.resx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>635, 17</value>
<value>17, 165</value>
</metadata>
<metadata name="ColumnEnabled.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
Expand All @@ -139,10 +139,10 @@
<value>17, 17</value>
</metadata>
<metadata name="fontDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>338, 17</value>
<value>17, 91</value>
</metadata>
<metadata name="contextMenuRegexChars.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>448, 17</value>
<value>17, 128</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>68</value>
Expand Down
44 changes: 27 additions & 17 deletions src/EncCnvtrs/NetRegexEncConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using ECInterfaces; // for IEncConverter
using SilEncConverters40.Properties;

namespace SilEncConverters40
{
/// <summary>
/// Managed Net Regex EncConverter
/// </summary>
/// <summary>
/// Managed Net Regex EncConverter
/// </summary>
public class NetRegexEncConverter : EncConverter
{
#region Const Definitions
Expand Down Expand Up @@ -167,23 +164,36 @@ protected unsafe void Load(string converterIdentifier)
TransliteratorInitialized = true; // so we don't to do this with each call to Convert

Util.DebugWriteLine(this, "END");
}

// this is a bit of a kludge, but I'm not sure what better way to do it is
// Use the string in this setting variable to determine whether we want to
// unescape the regexString or not. Generally, we do for \n, \r, or \t but not
// for much else and I don't know if that's a complete list or not, so I'm
// making it a setting so that a user could add to it in a pinch
// This regular expression is saying any of \
protected string _unescapeClues = $@"(?<!\\)\\[{Properties.Settings.Default.RegexNetLettersToUnescape}]";

static unsafe string SafeUnescape(string regexString)
protected string SafeUnescape(string regexString)
{
try
{
try
// Look for escaped sequences (e.g. \n, \r) and unescape them,
// but only if they are not already escaped.
return Regex.Replace(regexString, _unescapeClues, match =>
{
// it'll throw an exception w/ something like \s+, but not \n
regexString = Regex.Unescape(regexString);
}
catch { }

return regexString;
// Convert the matched escape sequence into an actual unescaped string
return Regex.Unescape(match.Value);
});
}
catch { }

return regexString;
}

#endregion Misc helpers
#endregion Misc helpers

#region Abstract Base Class Overrides
#region Abstract Base Class Overrides
protected override void PreConvert
(
EncodingForm eInEncodingForm,
Expand Down Expand Up @@ -243,6 +253,6 @@ private static unsafe void StringToProperByteStar(string strOutput, byte* lpOutB
rnOutLen = nLen;
ECNormalizeData.StringToByteStar(strOutput, lpOutBuffer, rnOutLen, false);
}
#endregion Abstract Base Class Overrides
#endregion Abstract Base Class Overrides
}
}
11 changes: 10 additions & 1 deletion src/EncCnvtrs/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EncCnvtrs/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
&lt;string&gt;{[\u0027\u002a\u2018\u2019\u201c\u201d\u0303\u0304\u0310\u0314\u0325\u200c\u200d\uE03C\uFFFD]}-&amp;gt;{};&lt;/string&gt;
&lt;/ArrayOfString&gt;</Value>
</Setting>
<Setting Name="RegexNetLettersToUnescape" Type="System.String" Scope="Application">
<Value Profile="(Default)">rnt</Value>
</Setting>
</Settings>
</SettingsFile>
10 changes: 10 additions & 0 deletions src/EncCnvtrs/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SilEncConverters40.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SilEncConverters40.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
Expand All @@ -29,4 +32,11 @@
</setting>
</SilEncConverters40.Properties.Settings>
</userSettings>
<applicationSettings>
<SilEncConverters40.Properties.Settings>
<setting name="RegexNetLettersToUnescape" serializeAs="String">
<value>rnt</value>
</setting>
</SilEncConverters40.Properties.Settings>
</applicationSettings>
</configuration>
3 changes: 2 additions & 1 deletion src/TestEncCnvtrs/TestNetRegex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ या हम किसी दूसरे की प्रतीक्षा
")]
[TestCase("{a}->{b};0␞{b}->{A};0", "a", "A")]
[TestCase("{a}->{b};0␞{b}->{A};65535", "a", "b")] // 65535 means the 2nd regex is disabled
[TestCase("{a}->{b};0␞{b}->{A};0x8000", "a", "b")] // 0x8000 can also be used to mean the 2nd regex is disabled
[TestCase("{a}->{b};0␞{b}->{A};0x8000", "a", "b")] // 0x8000 can also be used to mean the 2nd regex is disabled
[TestCase(@"{ *\\\\rq .*?\\\\rq\*}->{};0", @"\\q2 राज करेगा। \\rq भजन 2:8-9 \\rq*", @"\\q2 राज करेगा।")] // make sure that while \r gets unescaped, \rq doesn't
public void TestRegexForUnicodeEscapeCharacters(string converterSpec, string strInput, string strOutput)
{
var rec = new NetRegexEncConverter();
Expand Down

0 comments on commit 85f6ccd

Please sign in to comment.