Home » openjdk-7 » net.bonzoun » cocodonkey » [javadoc | source]

    1   //
    2   //  StringParser.java
    3   //  CocoDonkey
    4   //	$Id: StringParser.java,v 1.5 2003/01/01 17:35:01 fortun Exp $
    5   //
    6   //  Created by Fred Bonzoun on Tue Jul 09 2002.
    7   //  Copyright (c) 2002 Bonzoun. All rights reserved.
    8   //
    9   //  This library is free software; you can redistribute it and/or modify
   10   //  it under the terms of the GNU Lesser General Public License as published
   11   //  by the Free Software Foundation; either version 2.1 of the License, or
   12   //  (at your option) any later version.
   13   //
   14   //  This library is distributed in the hope that it will be useful,
   15   //  but WITHOUT ANY WARRANTY; without even the implied warranty of
   16   //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17   //  GNU Lesser General Public License for more details.
   18   //
   19   //  You should have received a copy of the GNU Lesser General Public License
   20   //  along with this program; if not, write to the Free Software
   21   //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   22   //
   23   
   24   package net.bonzoun.cocodonkey;
   25   
   26   public class StringParser {
   27   
   28       public static final String WHITESPACE = " \t\r\n";
   29       public static final String INTEGER = "0123456789";
   30       public static final String HEXA = "0123456789abcdefABCDEF";
   31       public static final String FLOAT = INTEGER  + ".";
   32       
   33       private String data;
   34       private int start;
   35       private int end;
   36   
   37       public StringParser(String s) {
   38           data = s;
   39           start = 0;
   40           end = s.length()-1;
   41       }
   42   
   43       public static boolean stringContainsOnly(String s, String chars) {
   44           int nb = s.length();
   45           for(int i=0; i<nb; i++) {
   46               if (chars.indexOf(s.charAt(i))<0)
   47                   return false;
   48           }
   49           return true;
   50       }
   51       
   52       /** Skips the next empty chars **/
   53       private void skipNextEmpty() {
   54           for(int i=start; i<=end; i++) {
   55               if (WHITESPACE.indexOf(data.charAt(i))<0) {
   56                   start = i;
   57                   return;
   58               }
   59           }
   60           start = end+1;
   61       }
   62   
   63       /** Skips the last empty chars **/
   64       private void skipLastEmpty() {
   65           for(int i=end; i>=start; i--) {
   66               if (WHITESPACE.indexOf(data.charAt(i))<0) {
   67                   end = i;
   68                   return;
   69               }
   70           }
   71           end = start-1;
   72       }
   73   
   74       public boolean hasMoreData() {
   75           return start<=end;
   76       }
   77       
   78       /** Returns the next non empty chars **/
   79       public String getNextString() {
   80           return getNextStringUntil(WHITESPACE);
   81       }
   82   
   83       public int getNextInt(int def) {
   84           return parseInt(getNextString(), def);
   85       }
   86   
   87       public float getNextFloat(int def) {
   88           return parseFloat(getNextString(), def);
   89       }
   90       
   91       /** Returns the next non empty chars until any char in delim is met, skipping the head withespaces **/
   92       public String getNextStringUntil(String delim) {
   93           skipNextEmpty();
   94           for(int i=start; i<=end; i++) {
   95               if (delim.indexOf(data.charAt(i))>=0) {
   96                   String s = (i>start ? data.substring(start, i) : "");
   97                   start = i;
   98                   return s;
   99               }
  100           }
  101           String s = (start<=end ? data.substring(start, end+1) : "");
  102           start = end+1;
  103           return s;
  104       }
  105       
  106       public char getNextChar() {
  107           char c = testNextChar();
  108           start++;
  109           return c;
  110       }
  111   
  112       public char testNextChar() {
  113           skipNextEmpty();
  114           if (start<=end)
  115               return data.charAt(start);
  116           else
  117               return '\0';
  118       }
  119       
  120       public int getNextIntUntil(String delim, int def) {
  121           return parseInt(getNextStringUntil(delim + WHITESPACE), def);
  122       }
  123   
  124       /** Skips the next chars until delim is met **/
  125       public boolean goAfter(char delim) {
  126           for(int i=start; i<=end; i++) {
  127               if (delim == data.charAt(i)) {
  128                   start = i+1;
  129                   return true;
  130               }
  131           }
  132           start = end+1;
  133           return false;
  134       }
  135   
  136       /** Skips the next chars until delim is met **/
  137       public boolean goBefore(char delim) {
  138           for(int i=end; i>=start; i--) {
  139               if (delim == data.charAt(i)) {
  140                   end = i-1;
  141                   return true;
  142               }
  143           }
  144           end = start-1;
  145           return false;
  146       }
  147       
  148       /** Returns the last non empty chars **/
  149       public String getLastString() {
  150           return getLastStringUntil(WHITESPACE);
  151       }
  152   
  153       public int getLastInt(int def) {
  154           return parseInt(getLastString(), def);
  155       }
  156   
  157       public float getLastFloat(int def) {
  158           return parseFloat(getLastString(), def);
  159       }
  160       
  161       /** Returns the last non empty chars until any char in delim is met, skipping the head withespaces **/
  162       public String getLastStringUntil(String delim) {
  163           skipLastEmpty();
  164           for(int i=end; i>=start; i--) {
  165               if (delim.indexOf(data.charAt(i))>=0) {
  166                   String s = (i<end ? data.substring(i+1, end+1) : "");
  167                   end = i;
  168                   return s;
  169               }
  170           }
  171           String s = (start<=end ? data.substring(start, end+1) : "");
  172           end = start-1;
  173           return s;
  174       }
  175   
  176       public char getLastChar() {
  177           char c = testLastChar();
  178           end--;
  179           return c;
  180       }
  181   
  182       public char testLastChar() {
  183           skipLastEmpty();
  184           if (start<=end)
  185               return data.charAt(end);
  186           else
  187               return '\0';
  188       }
  189       
  190       public int getLastIntUntil(String delim, int def) {
  191           return parseInt(getLastStringUntil(delim + WHITESPACE), def);
  192       }
  193   
  194       public String getRemainingString() {
  195           skipNextEmpty();
  196           skipLastEmpty();
  197           if (start<=end) {
  198               return data.substring(start, end+1);
  199           } else {
  200               return "";
  201           }
  202       }
  203       
  204       /** Converts a String to an int, without failing **/
  205       public static int parseInt(String s, int def) {
  206           try {
  207               return Integer.parseInt(s);
  208           }
  209           catch(Exception e) {
  210               return def;
  211           }
  212       }
  213   
  214       /** Converts a String to an float, without failing **/
  215       public static float parseFloat(String s, int def) {
  216           try {
  217               return Float.parseFloat(s);
  218           }
  219           catch(Exception e) {
  220               return def;
  221           }
  222       }
  223   }
  224   
  225   // $Log: StringParser.java,v $
  226   // Revision 1.5  2003/01/01 17:35:01  fortun
  227   // Added stringContainsOnly()
  228   //
  229   // Revision 1.4  2002/08/21 21:49:34  fortun
  230   // NUMBER becomes INTEGER
  231   //
  232   // Revision 1.3  2002/07/22 00:30:03  fortun
  233   // testLastChar() method added
  234   //
  235   // Revision 1.2  2002/07/21 18:40:49  fortun
  236   // New getNextChar/getLastChar and testNextChar functions
  237   //
  238   // Revision 1.1  2002/07/09 22:14:34  fortun
  239   // Fast parser
  240   //

Home » openjdk-7 » net.bonzoun » cocodonkey » [javadoc | source]