2 * Copyright (C) 2013 FooProject
3 * * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
6 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
13 using System.Text.RegularExpressions;
14 using Slusser.Collections.Generic;
16 namespace FooEditEngine
18 sealed class ReplaceCommand : ICommand
21 TextRange ReplacementRange, ReplacedRange;
22 GapBuffer<char> replacement, replaced; //置き換え後の文字列、置き換え前の文字列
24 public ReplaceCommand(StringBuffer buf, int start, int length, string str)
27 this.ReplacementRange = new TextRange(start,str.Length);
28 this.replacement = new GapBuffer<char>();
29 this.replacement.AddRange(Util.GetEnumrator(str));
30 this.ReplacedRange = new TextRange(start,length);
31 this.replaced = new GapBuffer<char>();
32 this.replaced.AddRange(this.Buffer.GetEnumerator(start, length));
39 this.Buffer.Replace(this.ReplacementRange.Index, this.replacement.Count,this.replaced,this.replaced.Count);
44 this.Buffer.Replace(this.ReplacedRange.Index, this.replaced.Count, this.replacement, this.replacement.Count);
47 public bool marge(ICommand a)
49 ReplaceCommand cmd = a as ReplaceCommand;
53 if (this.ReplacedRange.Length == 0 &&
54 cmd.ReplacementRange.Index >= this.ReplacementRange.Index &&
55 cmd.ReplacementRange.Index + cmd.ReplacementRange.Length <= this.ReplacementRange.Index + this.ReplacementRange.Length)
57 int bufferIndex = cmd.ReplacedRange.Index - this.ReplacementRange.Index;
58 if(bufferIndex < this.replacement.Count)
59 this.replacement.RemoveRange(bufferIndex, cmd.ReplacedRange.Length);
60 this.replacement.InsertRange(bufferIndex, cmd.replacement);
64 if (this.ReplacedRange.Index + this.ReplacementRange.Length == cmd.ReplacedRange.Index &&
65 this.ReplacedRange.Index == this.ReplacementRange.Index)
67 this.replaced.AddRange(cmd.replaced);
68 this.replacement.AddRange(cmd.replacement);
69 this.ReplacedRange.Length += cmd.ReplacedRange.Length;
70 this.ReplacementRange.Length += cmd.ReplacementRange.Length;
78 return this.replaced.Count == 0 && this.replacement.Count == 0;
84 sealed class ReplaceAllCommand : ICommand
88 StringBuffer oldBuffer;
89 string replacePattern;
91 LineToIndexTable layoutLines;
92 public ReplaceAllCommand(StringBuffer buffer, LineToIndexTable layoutlines, Regex regex, string replacePattern,bool groupReplace)
96 this.replacePattern = replacePattern;
97 this.groupReplace = groupReplace;
98 this.layoutLines = layoutlines;
103 this.buffer.Replace(this.oldBuffer);
108 this.oldBuffer = new StringBuffer(this.buffer);
109 this.buffer.ReplaceRegexAll(this.layoutLines, this.regex, this.replacePattern, this.groupReplace);
112 public bool marge(ICommand a)
117 public bool isempty()
122 sealed class FastReplaceAllCommand : ICommand
125 StringBuffer oldBuffer;
126 string targetPattern;
127 string replacePattern;
129 LineToIndexTable layoutLines;
130 public FastReplaceAllCommand(StringBuffer buffer,LineToIndexTable layoutlines,string targetPattern, string replacePattern,bool ci)
132 this.buffer = buffer;
133 this.replacePattern = replacePattern;
134 this.targetPattern = targetPattern;
135 this.caseInsensitve = ci;
136 this.layoutLines = layoutlines;
141 this.buffer.Replace(this.oldBuffer);
146 this.oldBuffer = new StringBuffer(this.buffer);
147 this.buffer.ReplaceAll(this.layoutLines, this.targetPattern, this.replacePattern,this.caseInsensitve);
150 public bool marge(ICommand a)
155 public bool isempty()