001/*
002 *  gnu/regexp/RETokenChar.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
022final class RETokenChar extends REToken {
023  private char[] ch;
024  private boolean insens;
025
026  RETokenChar(int subIndex, char c, boolean ins) {
027    super(subIndex);
028    ch = new char [1];
029    ch[0] = (insens = ins) ? Character.toLowerCase(c) : c;
030  }
031
032  int getMinimumLength() {
033    return ch.length;
034  }
035  
036    boolean match(CharIndexed input, REMatch mymatch) {
037        int z = ch.length;
038        char c;
039        for (int i=0; i<z; i++) {
040            c = input.charAt(mymatch.index+i);
041            if (( (insens) ? Character.toLowerCase(c) : c ) != ch[i]) {
042                return false;
043            }
044        }
045        mymatch.index += z;
046
047        return next(input, mymatch);
048    }
049
050  // Overrides REToken.chain() to optimize for strings
051  boolean chain(REToken next) {
052    if (next instanceof RETokenChar) {
053      RETokenChar cnext = (RETokenChar) next;
054      // assume for now that next can only be one character
055      int newsize = ch.length + cnext.ch.length;
056      
057      char[] chTemp = new char [newsize];
058      
059      System.arraycopy(ch,0,chTemp,0,ch.length);
060      System.arraycopy(cnext.ch,0,chTemp,ch.length,cnext.ch.length);
061      
062      ch = chTemp;
063      return false;
064    } else return super.chain(next);
065  }
066
067  void dump(StringBuffer os) {
068    os.append(ch);
069  }
070}
071
072