OSDN Git Service

コア部分を共通プロジェクト化した
[fooeditengine/FooEditEngine.git] / Core / Test / CharFoldingMethod.cs
1 /*
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.
5
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.
8
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/>.
10  */
11 using System;
12 using System.Collections.Generic;
13 using System.Linq;
14 using FooEditEngine;
15
16 namespace FooEditEngine.Test
17 {
18     /// <summary>
19     /// テスト用折り畳みメソッドの実装
20     /// </summary>
21     public class CharFoldingMethod : IFoldingStrategy
22     {
23         public CharFoldingMethod(char begin, char end)
24         {
25             this.BeginChar = begin;
26             this.EndChar = end;
27         }
28
29         /// <summary>
30         /// 折り畳みの開始文字を表す
31         /// </summary>
32         public char BeginChar
33         {
34             get;
35             set;
36         }
37
38         /// <summary>
39         /// 折り畳みの終了文字を表す
40         /// </summary>
41         public char EndChar
42         {
43             get;
44             set;
45         }
46
47         public IEnumerable<FoldingItem> AnalyzeDocument(Document doc, int start, int end)
48         {
49             Stack<int> BeginIndexColletion = new Stack<int>();
50             for (int i = start; i <= end; i++)
51             {
52                 if (doc[i] == this.BeginChar)
53                     BeginIndexColletion.Push(i);
54                 if (doc[i] == this.EndChar)
55                 {
56                     if (BeginIndexColletion.Count == 0)
57                         continue;
58                     int beginIndex = BeginIndexColletion.Pop();
59                     if (beginIndex < i)
60                     {
61                         yield return new FoldingItem(beginIndex, i);
62                     }
63                 }
64             }
65         }
66     }
67 }