001/*
002 *  gnu/regexp/RETokenStart.java
003 *  Copyright (C) 1998-2001 Wes Biggs
004 *
005 *  This library is free software; you can redistribute it and/or modify
006 *  it under the terms of the GNU Lesser General Public License as published
007 *  by the Free Software Foundation; either version 2.1 of the License, or
008 *  (at your option) any later version.
009 *
010 *  This library is distributed in the hope that it will be useful,
011 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
012 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 *  GNU Lesser General Public License for more details.
014 *
015 *  You should have received a copy of the GNU Lesser General Public License
016 *  along with this program; if not, write to the Free Software
017 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018 */
019
020package gnu.regexp;
021
022class RETokenStart extends REToken {
023    private String newline; // matches after a newline
024    
025    RETokenStart(int subIndex, String newline) {
026        super(subIndex);
027        this.newline = newline;
028    }
029    
030    boolean match(CharIndexed input, REMatch mymatch) {
031        // charAt(index-n) may be unknown on a Reader/InputStream. FIXME
032        // Match after a newline if in multiline mode
033        
034        if (newline != null) {
035            int len = newline.length();
036            if (mymatch.offset >= len) {
037                boolean found = true;
038                char z;
039                int i = 0; // position in REToken.newline
040                char ch = input.charAt(mymatch.index - len);
041                do {
042                    z = newline.charAt(i);
043                    if (ch != z) {
044                        found = false;
045                        break;
046                    }
047                    ++i;
048                    ch = input.charAt(mymatch.index - len + i);
049                } while (i < len);
050            
051                if (found) return next(input, mymatch);
052            }
053        }
054        
055        // Don't match at all if REG_NOTBOL is set.
056        if ((mymatch.eflags & RE.REG_NOTBOL) > 0) return false;
057        
058        if ((mymatch.eflags & RE.REG_ANCHORINDEX) > 0)
059            return (mymatch.anchor == mymatch.offset) ? 
060                next(input, mymatch) : false;
061        else
062            return ((mymatch.index == 0) && (mymatch.offset == 0)) ?
063                next(input, mymatch) : false;
064    }
065    
066    void dump(StringBuffer os) {
067        os.append('^');
068    }
069}