// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: StringTokenizer.jlc,v 1.7 2003/09/27 08:10:29 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/util/StringTokenizer.h" using namespace jakelib::lang; using namespace jakelib::util; JAKELIB_IMPLEMENT_CLASS("jakelib.util.StringTokenizer", StringTokenizer, Object); /*****************************************************************************\ * StringTokenizer | *****************************************************************************/ StringTokenizer::StringTokenizer(String* str) { this->str = str; this->delim = `" \t\n\r\f"`; this->retDelims = false; this->pos = 0; } StringTokenizer::StringTokenizer(String* str, String* delim, jboolean returnDelims) { this->str = str; this->delim = delim; this->retDelims = returnDelims; this->pos = 0; } /*****************************************************************************\ * hasMoreTokens | *****************************************************************************/ jboolean StringTokenizer::hasMoreTokens() { if (!retDelims) { while (pos < str->length() && delim->indexOf(str->charAt(pos)) > -1) { pos++; } } return pos < str->length(); } /*****************************************************************************\ * nextToken | *****************************************************************************/ String* StringTokenizer::nextToken(String* delim) { this->delim = delim; return nextToken(); } /*****************************************************************************\ * nextToken | *****************************************************************************/ String* StringTokenizer::nextToken() { if (pos < str->length() && delim->indexOf(str->charAt(pos)) > -1) { if (retDelims) return str->substring(pos, ++pos); while (++pos < str->length() && delim->indexOf(str->charAt(pos)) > -1) { /* empty */ } } if (pos < str->length()) { int start = pos; while (++pos < str->length() && delim->indexOf(str->charAt(pos)) == -1) { /* empty */ } return str->substring(start, pos); } throw new NoSuchElementException(); } /*****************************************************************************\ * hasNext | *****************************************************************************/ jboolean StringTokenizer::hasNext() { return hasMoreTokens(); } /*****************************************************************************\ * next | *****************************************************************************/ Object* StringTokenizer::next() { return nextToken(); } /*****************************************************************************\ * countTokens | *****************************************************************************/ int StringTokenizer::countTokens() { int count = 0; int delimiterCount = 0; jboolean tokenFound = false; // Set when a non-delimiter is found int tmpPos = pos; // Note for efficiency, we count up the delimiters rather than check // retDelims every time we encounter one. That way, we can // just do the conditional once at the end of the method while (tmpPos < str->length()) { if (delim->indexOf(str->charAt(tmpPos++)) > -1) { if (tokenFound) { // Got to the end of a token count++; tokenFound = false; } delimiterCount++; // Increment for this delimiter } else { tokenFound = true; // Get to the end of the token while (tmpPos < str->length() && delim->indexOf(str->charAt(tmpPos)) == -1) ++tmpPos; } } // Make sure to count the last token if (tokenFound) count++; // if counting delmiters add them into the token count return retDelims ? count + delimiterCount : count; } /*****************************************************************************\ * remove | *****************************************************************************/ void StringTokenizer::remove() { throw new UnsupportedOperationException(`""` .. JAKELIB_AT2(`"jakelib.util.StringTokenizer.remove"`)); }