OSDN Git Service

ReplaceAllも最適化した
authorkonekoneko <test2214@hotmail.co.jp>
Fri, 10 Jul 2015 20:12:19 +0000 (05:12 +0900)
committerkonekoneko <test2214@hotmail.co.jp>
Fri, 10 Jul 2015 20:12:19 +0000 (05:12 +0900)
Common/Document.cs
Common/StringBuffer.cs
Common/UndoCommands.cs
WPF/UnitTest/UnitTest2.cs

index 0e6a72e..38cd2a3 100644 (file)
@@ -671,7 +671,7 @@ namespace FooEditEngine
         {\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
index 44eca99..4ae0b8f 100644 (file)
@@ -13,6 +13,7 @@ using System.Collections.Generic;
 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
@@ -165,6 +166,24 @@ namespace FooEditEngine
             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
index 50d43c9..571b7bc 100644 (file)
@@ -88,12 +88,14 @@ namespace FooEditEngine
         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()
@@ -104,20 +106,7 @@ namespace FooEditEngine
         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)
index fbf3d42..d979eda 100644 (file)
@@ -342,6 +342,18 @@ namespace UnitTest
         }
 
         [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();