PageRenderTime 81ms CodeModel.GetById 76ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/gnu/regexp/RETokenStart.java

#
Java | 69 lines | 39 code | 9 blank | 21 comment | 13 complexity | ee80d04c88ac16f03bbb6550d160df72 MD5 | raw file
 1/*
 2 *  gnu/regexp/RETokenStart.java
 3 *  Copyright (C) 1998-2001 Wes Biggs
 4 *
 5 *  This library is free software; you can redistribute it and/or modify
 6 *  it under the terms of the GNU Lesser General Public License as published
 7 *  by the Free Software Foundation; either version 2.1 of the License, or
 8 *  (at your option) any later version.
 9 *
10 *  This library is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *  GNU Lesser General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Lesser General Public License
16 *  along with this program; if not, write to the Free Software
17 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20package gnu.regexp;
21
22class RETokenStart extends REToken {
23    private String newline; // matches after a newline
24    
25    RETokenStart(int subIndex, String newline) {
26	super(subIndex);
27	this.newline = newline;
28    }
29    
30    boolean match(CharIndexed input, REMatch mymatch) {
31	// charAt(index-n) may be unknown on a Reader/InputStream. FIXME
32	// Match after a newline if in multiline mode
33	
34	if (newline != null) {
35	    int len = newline.length();
36	    if (mymatch.offset >= len) {
37		boolean found = true;
38		char z;
39		int i = 0; // position in REToken.newline
40		char ch = input.charAt(mymatch.index - len);
41		do {
42		    z = newline.charAt(i);
43		    if (ch != z) {
44			found = false;
45			break;
46		    }
47		    ++i;
48		    ch = input.charAt(mymatch.index - len + i);
49		} while (i < len);
50	    
51		if (found) return next(input, mymatch);
52	    }
53	}
54	
55	// Don't match at all if REG_NOTBOL is set.
56	if ((mymatch.eflags & RE.REG_NOTBOL) > 0) return false;
57	
58	if ((mymatch.eflags & RE.REG_ANCHORINDEX) > 0)
59	    return (mymatch.anchor == mymatch.offset) ? 
60		next(input, mymatch) : false;
61	else
62	    return ((mymatch.index == 0) && (mymatch.offset == 0)) ?
63		next(input, mymatch) : false;
64    }
65    
66    void dump(StringBuffer os) {
67	os.append('^');
68    }
69}