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/>.
14 using System.Collections.Generic;
15 using System.Globalization;
18 using System.Text.RegularExpressions;
19 using System.Threading;
20 using System.Threading.Tasks;
21 using Slusser.Collections.Generic;
23 namespace FooEditEngine
26 /// ランダムアクセス可能な列挙子を提供するインターフェイス
28 /// <typeparam name="T"></typeparam>
29 public interface IRandomEnumrator<T>
34 /// <param name="index">インデックス</param>
35 /// <returns>Tを返す</returns>
36 T this[int index]{get;}
39 sealed class StringBuffer : IEnumerable<char>, IRandomEnumrator<char>
41 GapBuffer<char> buf = new GapBuffer<char>();
42 const int MaxSemaphoreCount = 1;
43 SemaphoreSlim Semaphore = new SemaphoreSlim(MaxSemaphoreCount);
47 this.Update = (s, e) => { };
51 /// ロック中なら真を返し、そうでないなら偽を返す
57 return this.Semaphore.CurrentCount == 0;
66 this.Semaphore.Release();
74 this.Semaphore.Wait();
80 /// <returns>Taskオブジェクト</returns>
81 public Task LockAsync()
83 return this.Semaphore.WaitAsync();
86 public StringBuffer(StringBuffer buffer)
89 buf.AddRange(buffer.buf, buffer.Length);
93 public char this[int index]
102 public string ToString(int index, int length)
106 StringBuilder temp = new StringBuilder();
108 for (int i = index; i < index + length; i++)
113 return temp.ToString();
116 public IEnumerable<string> GetLines(int startIndex, int endIndex, int maxCharCount = -1)
118 foreach (Tuple<int, int> range in this.ForEachLines(startIndex, endIndex, maxCharCount))
120 StringBuilder temp = new StringBuilder();
122 int lineEndIndex = range.Item1;
124 lineEndIndex += range.Item2 - 1;
125 for (int i = range.Item1; i <= lineEndIndex; i++)
127 yield return temp.ToString();
131 public IEnumerable<Tuple<int,int>> ForEachLines(int startIndex, int endIndex, int maxCharCount = -1)
133 int currentLineHeadIndex = startIndex;
134 int currentLineLength = 0;
136 for (int i = startIndex; i <= endIndex; i++)
139 char c = this.buf[i];
140 if (c == Document.NewLine ||
141 (maxCharCount != -1 && currentLineLength >= maxCharCount))
143 UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(c);
144 if (uc != UnicodeCategory.NonSpacingMark &&
145 uc != UnicodeCategory.SpacingCombiningMark &&
146 uc != UnicodeCategory.EnclosingMark &&
147 uc != UnicodeCategory.Surrogate)
149 yield return new Tuple<int,int>(currentLineHeadIndex, currentLineLength);
150 currentLineHeadIndex += currentLineLength;
151 currentLineLength = 0;
155 if (currentLineLength > 0)
156 yield return new Tuple<int, int>(currentLineHeadIndex, currentLineLength);
161 get { return this.buf.Count; }
164 internal DocumentUpdateEventHandler Update;
166 internal void Replace(StringBuffer buf)
168 this.Replace(buf.buf);
171 internal void Replace(GapBuffer<char> buf)
180 this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, 0, 0, buf.Count));
183 internal void Replace(int index, int length, IEnumerable<char> chars,int count)
188 this.buf.RemoveRange(index, length);
189 this.buf.InsertRange(index, chars,count);
193 this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, index, length, count));
196 internal async Task LoadAsync(TextReader fs, CancellationTokenSource tokenSource = null)
198 char[] str = new char[1024 * 256];
202 int index = this.Length;
206 readCount = await fs.ReadAsync(str, 0, str.Length).ConfigureAwait(false);
209 var internal_str = from s in str where s != '\r' && s != '\0' select s;
211 await this.LockAsync().ConfigureAwait(false);
213 //str.lengthは事前に確保しておくために使用するので影響はない
214 this.buf.InsertRange(index, internal_str, str.Length);
218 if (tokenSource != null)
219 tokenSource.Token.ThrowIfCancellationRequested();
221 System.Diagnostics.Debug.WriteLine("waiting now");
222 await Task.Delay(100).ConfigureAwait(false);
224 Array.Clear(str, 0, str.Length);
225 } while (readCount > 0);
227 this.Update(this, new DocumentUpdateEventArgs(UpdateType.Clear, -1, -1, -1));
228 this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, 0, 0, buf.Count));
231 internal void ReplaceRegexAll(LineToIndexTable layoutlines, Regex regex, string pattern, bool groupReplace)
233 for (int i = 0; i < layoutlines.Count; i++)
235 int lineHeadIndex = layoutlines.GetIndexFromLineNumber(i), lineLength = layoutlines.GetLengthFromLineNumber(i);
236 int left = lineHeadIndex, right = lineHeadIndex;
237 string output = regex.Replace(layoutlines[i], (m) => {
239 return m.Result(pattern);
246 if(lineHeadIndex < this.buf.Count )
247 this.buf.RemoveRange(lineHeadIndex, lineLength);
248 this.buf.InsertRange(lineHeadIndex, output, output.Length);
252 this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, lineHeadIndex, lineLength, output.Length, i));
256 internal void ReplaceAll(LineToIndexTable layoutlines,string target, string pattern, bool ci = false)
258 TextSearch ts = new TextSearch(target, ci);
259 char[] pattern_chars = pattern.ToCharArray();
260 for(int i = 0; i < layoutlines.Count; i++)
262 int lineHeadIndex = layoutlines.GetIndexFromLineNumber(i), lineLength = layoutlines.GetLengthFromLineNumber(i);
263 int left = lineHeadIndex, right = lineHeadIndex;
264 int newLineLength = lineLength;
265 while ((right = ts.IndexOf(this.buf, left, lineHeadIndex + newLineLength)) != -1)
269 this.buf.RemoveRange(right, target.Length);
270 this.buf.InsertRange(right, pattern_chars, pattern.Length);
273 left = right + pattern.Length;
274 newLineLength += pattern.Length - target.Length;
278 this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, lineHeadIndex, lineLength, newLineLength, i));
282 internal int IndexOf(string target, int start,bool ci = false)
286 TextSearch ts = new TextSearch(target,ci);
287 int patternIndex = ts.IndexOf(this.buf, start,this.buf.Count);
297 internal void Clear()
300 this.Update(this, new DocumentUpdateEventArgs(UpdateType.Clear, 0, this.buf.Count,0));
303 internal IEnumerable<char> GetEnumerator(int start, int length)
305 for (int i = start; i < start + length; i++)
306 yield return this.buf[i];
309 #region IEnumerable<char> メンバー
311 public IEnumerator<char> GetEnumerator()
313 for (int i = 0; i < this.Length; i++)
314 yield return this.buf[i];
319 #region IEnumerable メンバー
321 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
323 for (int i = 0; i < this.Length; i++)
324 yield return this[i];
330 sealed class TextSearch
334 Dictionary<char, int> qsTable = new Dictionary<char, int>();
336 public TextSearch(string pattern,bool ci = false)
338 this.patternLength = pattern.Length;
339 this.caseInsenstive = ci;
342 this.CreateQSTable(pattern.ToLower());
343 this.CreateQSTable(pattern.ToUpper());
344 this.pattern = new char[pattern.Length];
345 for (int i = 0; i < pattern.Length; i++)
346 this.pattern[i] = CharTool.ToUpperFastIf(pattern[i]);
350 this.CreateQSTable(pattern);
351 this.pattern = pattern.ToCharArray();
354 void CreateQSTable(string pattern)
356 int len = pattern.Length;
357 for (int i = 0; i < len; i++)
359 if (!this.qsTable.ContainsKey(pattern[i]))
360 this.qsTable.Add(pattern[i], len - i);
362 this.qsTable[pattern[i]] = len - i;
365 public int IndexOf(GapBuffer<char> buf, int start,int end)
368 int buflen = buf.Count - 1;
369 int plen = this.patternLength;
371 int search_end = end - plen;
373 if (this.caseInsenstive)
375 while (i <= search_end)
380 if (CharTool.ToUpperFastIf(buf[i + j]) != this.pattern[j])
391 if (k <= buflen) //buffer以降にアクセスする可能性がある
394 if (this.qsTable.TryGetValue(buf[k], out moveDelta))
409 while (i <= search_end)
414 if (buf[i + j] != this.pattern[j])
425 if (k <= buflen) //buffer以降にアクセスする可能性がある
428 if (this.qsTable.TryGetValue(buf[k], out moveDelta))
443 static class CharTool
446 /// Converts characters to lowercase.
448 const string _lookupStringL =
449 "---------------------------------!-#$%&-()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[-]^_`abcdefghijklmnopqrstuvwxyz{|}~-";
452 /// Converts characters to uppercase.
454 const string _lookupStringU =
455 "---------------------------------!-#$%&-()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[-]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~-";
458 /// Get lowercase version of this ASCII character.
460 public static char ToLower(char c)
462 return _lookupStringL[c];
466 /// Get uppercase version of this ASCII character.
468 public static char ToUpper(char c)
470 return _lookupStringU[c];
474 /// Translate uppercase ASCII characters to lowercase.
476 public static char ToLowerFastIf(char c)
478 if (c >= 'A' && c <= 'Z')
480 return (char)(c + 32);
489 /// Translate lowercase ASCII characters to uppercase.
491 public static char ToUpperFastIf(char c)
493 if (c >= 'a' && c <= 'z')
495 return (char)(c - 32);