/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file StringTokenizer.h14/// @author Daniel Krajzewicz15/// @author Jakob Erdmann16/// @author Michael Behrisch17/// @date ?18///19// A java-style StringTokenizer for c++ (stl)20/****************************************************************************/21#pragma once22#include <config.h>23#include <string>24#include <vector>25#include <set>2627/**28* StringTokenizer29* A class similar to the StringTokenizer from Java. It splits a string at30* the given string or character (or one of the special cases NEWLINE or31* WHITECHAR) and allows to iterate over the so generated substrings.32*33* The normal usage is like this:34* <pre>35* StringTokenizer st(CString("This is a line"), ' ');36* while(st.hasNext())37* cout << st.next() << endl;38* </pre>39* This would generate the output:40* <pre>41* This42* is43* a44* line45* </pre>46*47* There is something to know about the behaviour:48* When using WHITECHAR, a list of whitechars occuring in the string to49* split is regarded as a single divider. All other parameter will use50* multiple occurrences of operators as a list of single divider and the51* string between them will have a length of zero.52*/53// ===========================================================================54// class definitions55// ===========================================================================56/**57*58*/59class StringTokenizer {60public:61/// @brief identifier for splitting the given string at all newline characters62static const int NEWLINE;6364/// @brief identifier for splitting the given string at all whitespace characters65static const int WHITECHARS;6667/// @brief the ascii index of the highest whitespace character68static const int SPACE;6970/// @brief the ascii index of the tab character71static const int TAB;7273public:74/// @brief default constructor75StringTokenizer();7677/**@brief constructor78* @param tosplit is the string to split into substrings. If the string between two split positions is empty, it will not be returned.79* @note same as StringTokenizer(tosplit, StringTokenizer.WHITECHARS)80*/81StringTokenizer(std::string tosplit);8283/**@brief constructor84* @note the first string will be split at the second string's occurrences.85If the optional third parameter is true, the string will be split whenever86a char from the second string occurs. If the string between two split87positions is empty, it will nevertheless be returned.88*/89StringTokenizer(std::string tosplit, std::string token, bool splitAtAllChars = false);9091/**@brief constructor92* @note When StringTokenizer.NEWLINE is used as second parameter, the string93will be split at all occurrences of a newline character (0x0d / 0x0a)94When StringTokenizer.WHITECHARS is used as second parameter, the95string will be split at all characters below 0x20 (SPACE)96All other ints specified as second parameter are casted int o a char97at which the string will be splitted.98*/99StringTokenizer(std::string tosplit, int special);100101/// @brief destructor102~StringTokenizer();103104/// @brief reinitialises the internal iterator105void reinit();106107/// @brief returns the information whether further substrings exist108bool hasNext();109110/// @brief returns the next substring when it exists. Otherwise the behaviour is undefined111std::string next();112113/// @brief returns the number of existing substrings114int size() const;115116/// @brief returns the first substring without moving the iterator117std::string front();118119/// @brief returns the item at the given position120std::string get(int pos) const;121122/// @brief return vector of strings123std::vector<std::string> getVector();124125/// @brief return set of strings126std::set<std::string> getSet();127128private:129/// @brief splits the first string at all occurrences of the second. If the third parameter is true split at all chars given in the second130void prepare(const std::string& tosplit, const std::string& token, bool splitAtAllChars);131132/// @brief @brief splits the first string at all occurrences of whitechars133void prepareWhitechar(const std::string& tosplit);134135private:136/// @brief a list of positions/lengths137typedef std::vector<int> SizeVector;138139/// @brief the string to split140std::string myTosplit;141142/// @brief the current position in the list of substrings143int myPos;144145/// @brief the list of substring starts146SizeVector myStarts;147148/// @brief the list of substring lengths149SizeVector myLengths;150};151152153