1 30 31 package org.syntax.jedit; 32 33 import org.syntax.jedit.tokenmarker.*; 34 import javax.swing.text.Segment ; 35 36 46 public class KeywordMap 47 { 48 52 public KeywordMap(boolean ignoreCase) 53 { 54 this(ignoreCase, 52); 55 this.ignoreCase = ignoreCase; 56 } 57 58 64 public KeywordMap(boolean ignoreCase, int mapLength) 65 { 66 this.mapLength = mapLength; 67 this.ignoreCase = ignoreCase; 68 map = new Keyword[mapLength]; 69 } 70 71 77 public byte lookup(Segment text, int offset, int length) 78 { 79 if(length == 0) 80 return Token.NULL; 81 Keyword k = map[getSegmentMapKey(text, offset, length)]; 82 while(k != null) 83 { 84 if(length != k.keyword.length) 85 { 86 k = k.next; 87 continue; 88 } 89 if(SyntaxUtilities.regionMatches(ignoreCase,text,offset, 90 k.keyword)) 91 return k.id; 92 k = k.next; 93 } 94 return Token.NULL; 95 } 96 97 102 public void add(String keyword, byte id) 103 { 104 int key = getStringMapKey(keyword); 105 map[key] = new Keyword(keyword.toCharArray(),id,map[key]); 106 } 107 108 112 public boolean getIgnoreCase() 113 { 114 return ignoreCase; 115 } 116 117 122 public void setIgnoreCase(boolean ignoreCase) 123 { 124 this.ignoreCase = ignoreCase; 125 } 126 127 protected int mapLength; 129 130 protected int getStringMapKey(String s) 131 { 132 return (Character.toUpperCase(s.charAt(0)) + 133 Character.toUpperCase(s.charAt(s.length()-1))) 134 % mapLength; 135 } 136 137 protected int getSegmentMapKey(Segment s, int off, int len) 138 { 139 return (Character.toUpperCase(s.array[off]) + 140 Character.toUpperCase(s.array[off + len - 1])) 141 % mapLength; 142 } 143 144 class Keyword 146 { 147 public Keyword(char[] keyword, byte id, Keyword next) 148 { 149 this.keyword = keyword; 150 this.id = id; 151 this.next = next; 152 } 153 154 public char[] keyword; 155 public byte id; 156 public Keyword next; 157 } 158 159 private Keyword[] map; 160 private boolean ignoreCase; 161 } 162 | Popular Tags |