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);
245 this.buf.RemoveRange(lineHeadIndex, lineLength);
246 this.buf.InsertRange(lineHeadIndex, output, output.Length);
250 this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, lineHeadIndex, lineLength, output.Length, i));
254 internal void ReplaceAll(LineToIndexTable layoutlines,string target, string pattern, bool ci = false)
256 TextSearch ts = new TextSearch(target, ci);
257 char[] pattern_chars = pattern.ToCharArray();
258 for(int i = 0; i < layoutlines.Count; i++)
260 int lineHeadIndex = layoutlines.GetIndexFromLineNumber(i), lineLength = layoutlines.GetLengthFromLineNumber(i);
261 int left = lineHeadIndex, right = lineHeadIndex;
262 int newLineLength = lineLength;
263 while ((right = ts.IndexOf(this.buf, left, lineHeadIndex + newLineLength)) != -1)
267 this.buf.RemoveRange(right, target.Length);
268 this.buf.InsertRange(right, pattern_chars, pattern.Length);
271 left = right + pattern.Length;
272 newLineLength += pattern.Length - target.Length;
276 this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, lineHeadIndex, lineLength, newLineLength, i));
280 internal int IndexOf(string target, int start,bool ci = false)
284 TextSearch ts = new TextSearch(target,ci);
285 int patternIndex = ts.IndexOf(this.buf, start,this.buf.Count);
295 internal void Clear()
298 this.Update(this, new DocumentUpdateEventArgs(UpdateType.Clear, 0, this.buf.Count,0));
301 internal IEnumerable<char> GetEnumerator(int start, int length)
303 for (int i = start; i < start + length; i++)
304 yield return this.buf[i];
307 #region IEnumerable<char> メンバー
309 public IEnumerator<char> GetEnumerator()
311 for (int i = 0; i < this.Length; i++)
312 yield return this.buf[i];
317 #region IEnumerable メンバー
319 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
321 for (int i = 0; i < this.Length; i++)
322 yield return this[i];
328 sealed class TextSearch
332 Dictionary<char, int> qsTable = new Dictionary<char, int>();
334 public TextSearch(string pattern,bool ci = false)
336 this.patternLength = pattern.Length;
337 this.caseInsenstive = ci;
340 this.CreateQSTable(pattern.ToLower());
341 this.CreateQSTable(pattern.ToUpper());
342 this.pattern = new char[pattern.Length];
343 for (int i = 0; i < pattern.Length; i++)
344 this.pattern[i] = CharTool.ToUpperFastIf(pattern[i]);
348 this.CreateQSTable(pattern);
349 this.pattern = pattern.ToCharArray();
352 void CreateQSTable(string pattern)
354 int len = pattern.Length;
355 for (int i = 0; i < len; i++)
357 if (!this.qsTable.ContainsKey(pattern[i]))
358 this.qsTable.Add(pattern[i], len - i);
360 this.qsTable[pattern[i]] = len - i;
363 public int IndexOf(GapBuffer<char> buf, int start,int end)
366 int buflen = buf.Count - 1;
367 int plen = this.patternLength;
369 int search_end = end - plen;
371 if (this.caseInsenstive)
373 while (i <= search_end)
378 if (CharTool.ToUpperFastIf(buf[i + j]) != this.pattern[j])
389 if (k <= buflen) //buffer以降にアクセスする可能性がある
392 if (this.qsTable.TryGetValue(buf[k], out moveDelta))
407 while (i <= search_end)
412 if (buf[i + j] != this.pattern[j])
423 if (k <= buflen) //buffer以降にアクセスする可能性がある
426 if (this.qsTable.TryGetValue(buf[k], out moveDelta))
441 static class CharTool
444 /// Converts characters to lowercase.
446 const string _lookupStringL =
447 "---------------------------------!-#$%&-()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[-]^_`abcdefghijklmnopqrstuvwxyz{|}~-";
450 /// Converts characters to uppercase.
452 const string _lookupStringU =
453 "---------------------------------!-#$%&-()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[-]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~-";
456 /// Get lowercase version of this ASCII character.
458 public static char ToLower(char c)
460 return _lookupStringL[c];
464 /// Get uppercase version of this ASCII character.
466 public static char ToUpper(char c)
468 return _lookupStringU[c];
472 /// Translate uppercase ASCII characters to lowercase.
474 public static char ToLowerFastIf(char c)
476 if (c >= 'A' && c <= 'Z')
478 return (char)(c + 32);
487 /// Translate lowercase ASCII characters to uppercase.
489 public static char ToUpperFastIf(char c)
491 if (c >= 'a' && c <= 'z')
493 return (char)(c - 32);