Code Coverage Statistics for Source File

c:\Tools\SD3\src\Libraries\ICSharpCode.TextEditor\Project\Src\Util\LookupTable.cs

Sequence Point Coverage
N/A
0 of 0
Branch Coverage
N/A
0 of 0
Lines
140
Highlight: Uncovered Code Covered Code
L V Source
1
// <file>
2
//     <copyright see="prj:///doc/copyright.txt"/>
3
//     <license see="prj:///doc/license.txt"/>
4
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
5
//     <version>$Revision: 1965 $</version>
6
// </file>
7
8
using System;
9
using ICSharpCode.TextEditor.Document;
10
11
namespace ICSharpCode.TextEditor.Util
12
{
13
	/// <summary>
14
	/// This class implements a keyword map. It implements a digital search trees (tries) to find
15
	/// a word.
16
	/// </summary>
17
	public class LookupTable
18
	{
19
		Node root = new Node(null, null);
20
		bool casesensitive;
21
		int  length;
22
		
23
		/// <value>
24
		/// The number of elements in the table
25
		/// </value>
26
		public int Count {
27
			get {
28
				return length;
29
			}
30
		}
31
		
32
		/// <summary>
33
		/// Get the object, which was inserted under the keyword (line, at offset, with length length),
34
		/// returns null, if no such keyword was inserted.
35
		/// </summary>
36
		public object this[IDocument document, LineSegment line, int offset, int length] {
37
			get {
38
				if(length == 0) {
39
					return null;
40
				}
41
				Node next = root;
42
				
43
				int wordOffset = line.Offset + offset;
44
				if (casesensitive) {
45
					for (int i = 0; i < length; ++i) {
46
						int index = ((int)document.GetCharAt(wordOffset + i)) % 256;
47
						next = next.leaf[index];
48
						
49
						if (next == null) {
50
							return null;
51
						}
52
						
53
						if (next.color != null && TextUtility.RegionMatches(document, wordOffset, length, next.word)) {
54
							return next.color;
55
						}
56
					}
57
				} else {
58
					for (int i = 0; i < length; ++i) {
59
						int index = ((int)Char.ToUpper(document.GetCharAt(wordOffset + i))) % 256;
60
						
61
						next = next.leaf[index];
62
						
63
						if (next == null) {
64
							return null;
65
						}
66
						
67
						if (next.color != null && TextUtility.RegionMatches(document, casesensitive, wordOffset, length, next.word)) {
68
							return next.color;
69
						}
70
					}
71
				}
72
				return null;
73
			}
74
		}
75
		
76
		/// <summary>
77
		/// Inserts an object in the tree, under keyword
78
		/// </summary>
79
		public object this[string keyword] {
80
			set {
81
				Node node = root;
82
				Node next = root;
83
				if (!casesensitive) {
84
					keyword = keyword.ToUpper();
85
				}
86
				++length;
87
				
88
				// insert word into the tree
89
				for (int i = 0; i < keyword.Length; ++i) {
90
					int index = ((int)keyword[i]) % 256; // index of curchar
91
					bool d = keyword[i] == '\\';
92
					
93
					next = next.leaf[index];             // get node to this index
94
					
95
					if (next == null) { // no node created -> insert word here
96
						node.leaf[index] = new Node(value, keyword);
97
						break;
98
					}
99
					
100
					if (next.word != null && next.word.Length != i) { // node there, take node content and insert them again
101
						string tmpword  = next.word;                  // this word will be inserted 1 level deeper (better, don't need too much 
102
						object tmpcolor = next.color;                 // string comparisons for finding.)
103
						next.color = next.word = null;
104
						this[tmpword] = tmpcolor;
105
					}
106
					
107
					if (i == keyword.Length - 1) { // end of keyword reached, insert node there, if a node was here it was
108
						next.word = keyword;       // reinserted, if it has the same length (keyword EQUALS this word) it will be overwritten
109
						next.color = value;
110
						break;
111
					}
112
					
113
					node = next;
114
				}
115
			}
116
		}
117
		
118
		/// <summary>
119
		/// Creates a new instance of <see cref="LookupTable"/>
120
		/// </summary>
121
		public LookupTable(bool casesensitive)
122
		{
123
			this.casesensitive = casesensitive;
124
		}
125
		
126
		class Node
127
		{
128
			public Node(object color, string word)
129
			{
130
				this.word  = word;
131
				this.color = color;
132
			}
133
			
134
			public string word;
135
			public object color;
136
			
137
			public Node[] leaf = new Node[256];
138
		}
139
	}
140
}