{\r
if (this.regex == null)\r
throw new InvalidOperationException();\r
- ReplaceAllCommand cmd = new ReplaceAllCommand(this.buffer, this.regex, replacePattern, groupReplace);\r
+ ReplaceAllCommand cmd = new ReplaceAllCommand(this.buffer, this.LayoutLines, this.regex, replacePattern, groupReplace);\r
this.UndoManager.push(cmd);\r
cmd.redo();\r
}\r
using System.Globalization;\r
using System.Linq;\r
using System.Text;\r
+using System.Text.RegularExpressions;\r
using System.Threading;\r
using System.Threading.Tasks;\r
using Slusser.Collections.Generic;\r
this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, 0, 0, buf.Count));\r
}\r
\r
+ internal void ReplaceRegexAll(LineToIndexTable layoutlines, Regex regex, string pattern, bool groupReplace)\r
+ {\r
+ for (int i = 0; i < layoutlines.Count; i++)\r
+ {\r
+ int lineHeadIndex = layoutlines.GetIndexFromLineNumber(i), lineLength = layoutlines.GetLengthFromLineNumber(i);\r
+ int left = lineHeadIndex, right = lineHeadIndex;\r
+ string output = regex.Replace(layoutlines[i], (m) => {\r
+ if (groupReplace)\r
+ return m.Result(pattern);\r
+ else\r
+ return pattern;\r
+ });\r
+ this.buf.RemoveRange(lineHeadIndex, lineLength);\r
+ this.buf.InsertRange(lineHeadIndex, output, output.Length);\r
+ this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, lineHeadIndex, lineLength, output.Length, i));\r
+ }\r
+ }\r
+\r
internal void ReplaceAll(LineToIndexTable layoutlines,string target, string pattern, bool ci = false)\r
{\r
TextSearch ts = new TextSearch(target, ci);\r
StringBuffer oldBuffer;
string replacePattern;
bool groupReplace;
- public ReplaceAllCommand(StringBuffer buffer, Regex regex, string replacePattern,bool groupReplace)
+ LineToIndexTable layoutLines;
+ public ReplaceAllCommand(StringBuffer buffer, LineToIndexTable layoutlines, Regex regex, string replacePattern,bool groupReplace)
{
this.buffer = buffer;
this.regex = regex;
this.replacePattern = replacePattern;
this.groupReplace = groupReplace;
+ this.layoutLines = layoutlines;
}
public void undo()
public void redo()
{
this.oldBuffer = new StringBuffer(this.buffer);
-
- GapBuffer<char> result = new GapBuffer<char>();
- foreach (string line in this.buffer.GetLines(0, this.buffer.Length - 1))
- {
- string output = this.regex.Replace(line, (m) =>{
- if (this.groupReplace)
- return m.Result(this.replacePattern);
- else
- return this.replacePattern;
- });
- result.AddRange(Util.GetEnumrator(output),output.Length);
- }
-
- this.buffer.Replace(result);
+ this.buffer.ReplaceRegexAll(this.layoutLines, this.regex, this.replacePattern, this.groupReplace);
}
public bool marge(ICommand a)
}
[TestMethod]
+ public void ReplaceRegexAllTest()
+ {
+ DummyRender render = new DummyRender();
+ Document doc = new Document();
+ doc.LayoutLines.Render = render;
+ doc.Append("this is a pen");
+ doc.SetFindParam("is", false, RegexOptions.None);
+ doc.ReplaceAll("aaa", false);
+ Assert.IsTrue(doc.ToString(0) == "thaaa aaa a pen");
+ }
+
+ [TestMethod]
public void ReplaceAllTest()
{
DummyRender render = new DummyRender();