PageRenderTime 67ms CodeModel.GetById 58ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/search/RESearchMatcher.java

#
Java | 138 lines | 60 code | 14 blank | 64 comment | 14 complexity | 5cb8f21003318d0cd8b20e878bfd4d8f MD5 | raw file
  1/*
  2 * RESearchMatcher.java - Regular expression matcher
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999, 2003 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.search;
 24
 25//{{{ Imports
 26import bsh.BshMethod;
 27import bsh.NameSpace;
 28import gnu.regexp.*;
 29import org.gjt.sp.jedit.BeanShell;
 30import org.gjt.sp.jedit.MiscUtilities;
 31//}}}
 32
 33/**
 34 * A regular expression string matcher using {@link gnu.regexp}.
 35 * @author Slava Pestov
 36 * @version $Id: RESearchMatcher.java 4826 2003-07-14 23:00:54Z spestov $
 37 */
 38public class RESearchMatcher extends SearchMatcher
 39{
 40	/**
 41	 * Perl5 syntax with character classes enabled.
 42	 * @since jEdit 3.0pre5
 43	 */
 44	public static final RESyntax RE_SYNTAX_JEDIT
 45		= new RESyntax(RESyntax.RE_SYNTAX_PERL5)
 46		.set(RESyntax.RE_CHAR_CLASSES)
 47		.setLineSeparator("\n");
 48
 49	//{{{ RESearchMatcher constructor
 50	/**
 51	 * Creates a new regular expression string matcher.
 52	 * @since jEdit 4.2pre4
 53	 */
 54	public RESearchMatcher(String search, boolean ignoreCase)
 55		throws REException
 56	{
 57		re = new RE(search,(ignoreCase ? RE.REG_ICASE : 0)
 58			| RE.REG_MULTILINE,RE_SYNTAX_JEDIT);
 59		returnValue = new Match();
 60	} //}}}
 61
 62	//{{{ nextMatch() method
 63	/**
 64	 * Returns the offset of the first match of the specified text
 65	 * within this matcher.
 66	 * @param text The text to search in
 67	 * @param start True if the start of the segment is the beginning of the
 68	 * buffer
 69	 * @param end True if the end of the segment is the end of the buffer
 70	 * @param firstTime If false and the search string matched at the start
 71	 * offset with length zero, automatically find next match
 72	 * @param reverse If true, searching will be performed in a backward
 73	 * direction.
 74	 * @return an array where the first element is the start offset
 75	 * of the match, and the second element is the end offset of
 76	 * the match
 77	 * @since jEdit 4.2pre4
 78	 */
 79	public SearchMatcher.Match nextMatch(CharIndexed text, boolean start,
 80		boolean end, boolean firstTime, boolean reverse)
 81	{
 82		int flags = 0;
 83
 84		// unless we are matching from the start of the buffer,
 85		// ^ should not match on the beginning of the substring
 86		if(!start)
 87			flags |= RE.REG_NOTBOL;
 88		// unless we are matching to the end of the buffer,
 89		// $ should not match on the end of the substring
 90		if(!end)
 91			flags |= RE.REG_NOTEOL;
 92
 93		REMatch match = re.getMatch(text,0,flags);
 94		if(match == null)
 95			return null;
 96
 97		returnValue.substitutions = new String[
 98			re.getNumSubs() + 1];
 99		for(int i = 0; i < returnValue.substitutions.length; i++)
100		{
101			returnValue.substitutions[i] = match.toString(i);
102		}
103
104		int _start = match.getStartIndex();
105		int _end = match.getEndIndex();
106
107		// some regexps (eg ^ by itself) have a length == 0, so we
108		// implement this hack. if you don't understand what's going on
109		// here, then go back to watching MTV
110		if(!firstTime && _start == 0 && _end == 0)
111		{
112			text.move(1);
113
114			if(text.charAt(0) == CharIndexed.OUT_OF_BOUNDS)
115			{
116				// never mind
117				return null;
118			}
119
120			match = re.getMatch(text,0,flags | RE.REG_NOTBOL);
121			if(match == null)
122				return null;
123			else
124			{
125				_start = match.getStartIndex() + 1;
126				_end = match.getEndIndex() + 1;
127			}
128		}
129
130		returnValue.start = _start;
131		returnValue.end = _end;
132		return returnValue;
133	} //}}}
134
135	//{{{ Private members
136	private RE re;
137	//}}}
138}