| 0: | // LookupTable.cs | |
| 1: | // Copyright (c) 2000 Mike Krueger | |
| 2: | // | |
| 3: | // This program is free software; you can redistribute it and/or modify | |
| 4: | // it under the terms of the GNU General Public License as published by | |
| 5: | // the Free Software Foundation; either version 2 of the License, or | |
| 6: | // (at your option) any later version. | |
| 7: | // | |
| 8: | // This program is distributed in the hope that it will be useful, | |
| 9: | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10: | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11: | // GNU General Public License for more details. | |
| 12: | // | |
| 13: | // You should have received a copy of the GNU General Public License | |
| 14: | // along with this program; if not, write to the Free Software | |
| 15: | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 16: | ||
| 17: | using System; | |
| 18: | using System.Collections; | |
| 19: | using System.Drawing; | |
| 20: | ||
| 21: | namespace SharpDevelop.Internal.Text { | |
| 22: | ||
| 23: | /// <summary> | |
| 24: | /// This class implements a keyword map. It implements a digital search trees (tries) to find | |
| 25: | /// a word. | |
| 26: | /// </summary> | |
| 27: | public class LookupTable | |
| 28: | { | |
| 29: | Node root = new Node(null, null); | |
| 30: | bool casesensitive; | |
| 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[string line, int offset, int length] { | |
| 37: | get { | |
| 38: | if(length == 0) | |
| 39: | return null; | |
| 40: | ||
| 41: | Node next = root; | |
| 42: | if (casesensitive) { | |
| 43: | for (int i = 0; i < length; ++i) { | |
| 44: | int index = ((int)line[offset + i]) % 256; | |
| 45: | next = next.leaf[index]; | |
| 46: | ||
| 47: | if (next == null) { | |
| 48: | return null; | |
| 49: | } | |
| 50: | ||
| 51: | if (next.color != null && SharpDevelop.Tool.Text.TextUtility.RegionMatches(line, offset, length, next.word)) { | |
| 52: | return next.color; | |
| 53: | } | |
| 54: | } | |
| 55: | } else { | |
| 56: | for (int i = 0; i < length; ++i) { | |
| 57: | int index = ((int)Char.ToUpper(line[offset + i])) % 256; | |
| 58: | next = next.leaf[index]; | |
| 59: | ||
| 60: | if (next == null) { | |
| 61: | return null; | |
| 62: | } | |
| 63: | if (next.color != null && SharpDevelop.Tool.Text.TextUtility.RegionMatches(casesensitive, line, offset, length, next.word)) { | |
| 64: | return next.color; | |
| 65: | } | |
| 66: | } | |
| 67: | } | |
| 68: | return null; | |
| 69: | } | |
| 70: | } | |
| 71: | ||
| 72: | /// <summary> | |
| 73: | /// Inserts an object in the tree, under keyword | |
| 74: | /// </summary> | |
| 75: | public object this[string keyword] { | |
| 76: | set { | |
| 77: | Node node = root; | |
| 78: | Node next = root; | |
| 79: | if (!casesensitive) | |
| 80: | keyword = keyword.ToUpper(); | |
| 81: | ||
| 82: | // insert word into the tree | |
| 83: | for (int i = 0; i < keyword.Length; ++i) { | |
| 84: | int index = ((int)keyword[i]) % 256; // index of curchar | |
| 85: | bool d = keyword[i] == '\\'; | |
| 86: | ||
| 87: | next = next.leaf[index]; // get node to this index | |
| 88: | ||
| 89: | if (next == null) { // no node created -> insert word here | |
| 90: | node.leaf[index] = new Node(value, keyword); | |
| 91: | break; | |
| 92: | } | |
| 93: | ||
| 94: | if (next.word != null && next.word.Length != i) { // node there, take node content and insert them again | |
| 95: | string tmpword = next.word; // this word will be inserted 1 level deeper (better, don't need too much | |
| 96: | object tmpcolor = next.color; // string comparisons for finding.) | |
| 97: | next.color = next.word = null; | |
| 98: | this[tmpword] = tmpcolor; | |
| 99: | } | |
| 100: | ||
| 101: | if (i == keyword.Length - 1) { // end of keyword reached, insert node there, if a node was here it was | |
| 102: | next.word = keyword; // reinserted, if it has the same length (keyword EQUALS this word) it will be overwritten | |
| 103: | next.color = value; | |
| 104: | break; | |
| 105: | } | |
| 106: | ||
| 107: | node = next; | |
| 108: | } | |
| 109: | } | |
| 110: | } | |
| 111: | ||
| 112: | public LookupTable(bool casesensitive) | |
| 113: | { | |
| 114: | this.casesensitive = casesensitive; | |
| 115: | } | |
| 116: | ||
| 117: | class Node | |
| 118: | { | |
| 119: | public Node(object color, string word) | |
| 120: | { | |
| 121: | this.word = word; | |
| 122: | this.color = color; | |
| 123: | } | |
| 124: | ||
| 125: | public string word; | |
| 126: | public object color; | |
| 127: | ||
| 128: | public Node[] leaf = new Node[256]; | |
| 129: | } | |
| 130: | } | |
| 131: | } |
This page was automatically generated by SharpDevelop.