PageRenderTime 138ms CodeModel.GetById 125ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/syntax/KeywordMap.java

#
Java | 202 lines | 113 code | 19 blank | 70 comment | 13 complexity | fd70b65718920b2decbf55292317bd0d MD5 | raw file
  1/*
  2 * KeywordMap.java - Fast keyword->id map
  3 * Copyright (C) 1998, 1999 Slava Pestov
  4 * Copyright (C) 1999 Mike Dillon
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License
  8 * as published by the Free Software Foundation; either version 2
  9 * of the License, or any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program; if not, write to the Free Software
 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 19 */
 20package org.gjt.sp.jedit.syntax;
 21
 22import javax.swing.text.Segment;
 23import java.util.Vector;
 24import org.gjt.sp.jedit.TextUtilities;
 25
 26/**
 27 * A <code>KeywordMap</code> is similar to a hashtable in that it maps keys
 28 * to values. However, the `keys' are Swing segments. This allows lookups of
 29 * text substrings without the overhead of creating a new string object.
 30 *
 31 * @author Slava Pestov, Mike Dillon
 32 * @version $Id: KeywordMap.java 3931 2001-12-02 11:40:51Z spestov $
 33 */
 34public class KeywordMap
 35{
 36	/**
 37	 * Creates a new <code>KeywordMap</code>.
 38	 * @param ignoreCase True if keys are case insensitive
 39	 */
 40	public KeywordMap(boolean ignoreCase)
 41	{
 42		this(ignoreCase, 52);
 43		this.ignoreCase = ignoreCase;
 44		noWordSep = new StringBuffer();
 45	}
 46
 47	/**
 48	 * Creates a new <code>KeywordMap</code>.
 49	 * @param ignoreCase True if the keys are case insensitive
 50	 * @param mapLength The number of `buckets' to create.
 51	 * A value of 52 will give good performance for most maps.
 52	 */
 53	public KeywordMap(boolean ignoreCase, int mapLength)
 54	{
 55		this.mapLength = mapLength;
 56		this.ignoreCase = ignoreCase;
 57		map = new Keyword[mapLength];
 58	}
 59
 60	/**
 61	 * Looks up a key.
 62	 * @param text The text segment
 63	 * @param offset The offset of the substring within the text segment
 64	 * @param length The length of the substring
 65	 */
 66	public byte lookup(Segment text, int offset, int length)
 67	{
 68		if(length == 0)
 69			return Token.NULL;
 70		Keyword k = map[getSegmentMapKey(text, offset, length)];
 71		while(k != null)
 72		{
 73			if(length != k.keyword.length)
 74			{
 75				k = k.next;
 76				continue;
 77			}
 78			if(TextUtilities.regionMatches(ignoreCase,text,offset,
 79				k.keyword))
 80				return k.id;
 81			k = k.next;
 82		}
 83		return Token.NULL;
 84	}
 85
 86	/**
 87	 * Adds a key-value mapping.
 88	 * @param keyword The key
 89	 * @param id The value
 90	 */
 91	public void add(String keyword, byte id)
 92	{
 93		int key = getStringMapKey(keyword);
 94
 95		char[] chars = keyword.toCharArray();
 96
 97		// complete-word command needs a list of all non-alphanumeric
 98		// characters used in a keyword map.
 99loop:		for(int i = 0; i < chars.length; i++)
100		{
101			char ch = chars[i];
102			if(!Character.isLetterOrDigit(ch))
103			{
104				for(int j = 0; j < noWordSep.length(); j++)
105				{
106					if(noWordSep.charAt(j) == ch)
107						continue loop;
108				}
109
110				noWordSep.append(ch);
111			}
112		}
113
114		map[key] = new Keyword(chars,id,map[key]);
115	}
116
117	/**
118	 * Returns all non-alphanumeric characters that appear in the
119	 * keywords of this keyword map.
120	 * @since jEdit 4.0pre3
121	 */
122	public String getNonAlphaNumericChars()
123	{
124		return noWordSep.toString();
125	}
126
127	/**
128	 * Returns an array containing all keywords in this keyword map.
129	 * @since jEdit 4.0pre3
130	 */
131	public String[] getKeywords()
132	{
133		Vector vector = new Vector(100);
134		for(int i = 0; i < map.length; i++)
135		{
136			Keyword keyword = map[i];
137			while(keyword != null)
138			{
139				vector.addElement(new String(keyword.keyword));
140				keyword = keyword.next;
141			}
142		}
143		String[] retVal = new String[vector.size()];
144		vector.copyInto(retVal);
145		return retVal;
146	}
147
148	/**
149	 * Returns true if the keyword map is set to be case insensitive,
150	 * false otherwise.
151	 */
152	public boolean getIgnoreCase()
153	{
154		return ignoreCase;
155	}
156
157	/**
158	 * Sets if the keyword map should be case insensitive.
159	 * @param ignoreCase True if the keyword map should be case
160	 * insensitive, false otherwise
161	 */
162	public void setIgnoreCase(boolean ignoreCase)
163	{
164		this.ignoreCase = ignoreCase;
165	}
166
167	// protected members
168	protected int mapLength;
169
170	protected int getStringMapKey(String s)
171	{
172		return (Character.toUpperCase(s.charAt(0)) +
173				Character.toUpperCase(s.charAt(s.length()-1)))
174				% mapLength;
175	}
176
177	protected int getSegmentMapKey(Segment s, int off, int len)
178	{
179		return (Character.toUpperCase(s.array[off]) +
180				Character.toUpperCase(s.array[off + len - 1]))
181				% mapLength;
182	}
183
184	// private members
185	class Keyword
186	{
187		public Keyword(char[] keyword, byte id, Keyword next)
188		{
189			this.keyword = keyword;
190			this.id = id;
191			this.next = next;
192		}
193
194		public char[] keyword;
195		public byte id;
196		public Keyword next;
197	}
198
199	private Keyword[] map;
200	private boolean ignoreCase;
201	private StringBuffer noWordSep;
202}