1 package org.jedit.syntax; 2 3 12 13 import javax.swing.text.Segment ; 14 15 25 public class KeywordMap 26 { 27 31 public KeywordMap(boolean ignoreCase) 32 { 33 this(ignoreCase, 52); 34 this.ignoreCase = ignoreCase; 35 } 36 37 43 public KeywordMap(boolean ignoreCase, int mapLength) 44 { 45 this.mapLength = mapLength; 46 this.ignoreCase = ignoreCase; 47 map = new Keyword[mapLength]; 48 } 49 50 56 public byte lookup(Segment text, int offset, int length) 57 { 58 if(length == 0) 59 return Token.NULL; 60 Keyword k = map[getSegmentMapKey(text, offset, length)]; 61 while(k != null) 62 { 63 if(length != k.keyword.length) 64 { 65 k = k.next; 66 continue; 67 } 68 if(SyntaxUtilities.regionMatches(ignoreCase,text,offset, 69 k.keyword)) 70 return k.id; 71 k = k.next; 72 } 73 return Token.NULL; 74 } 75 76 81 public void add(String keyword, byte id) 82 { 83 int key = getStringMapKey(keyword); 84 map[key] = new Keyword(keyword.toCharArray(),id,map[key]); 85 } 86 87 91 public boolean getIgnoreCase() 92 { 93 return ignoreCase; 94 } 95 96 101 public void setIgnoreCase(boolean ignoreCase) 102 { 103 this.ignoreCase = ignoreCase; 104 } 105 106 protected int mapLength; 108 109 protected int getStringMapKey(String s) 110 { 111 return (Character.toUpperCase(s.charAt(0)) + 112 Character.toUpperCase(s.charAt(s.length()-1))) 113 % mapLength; 114 } 115 116 protected int getSegmentMapKey(Segment s, int off, int len) 117 { 118 return (Character.toUpperCase(s.array[off]) + 119 Character.toUpperCase(s.array[off + len - 1])) 120 % mapLength; 121 } 122 123 class Keyword 125 { 126 public Keyword(char[] keyword, byte id, Keyword next) 127 { 128 this.keyword = keyword; 129 this.id = id; 130 this.next = next; 131 } 132 133 public char[] keyword; 134 public byte id; 135 public Keyword next; 136 } 137 138 private Keyword[] map; 139 private boolean ignoreCase; 140 } 141 | Popular Tags |