Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/common/StringTokenizer.h
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file StringTokenizer.h
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @date ?
19
///
20
// A java-style StringTokenizer for c++ (stl)
21
/****************************************************************************/
22
#pragma once
23
#include <config.h>
24
#include <string>
25
#include <vector>
26
#include <set>
27
28
/**
29
* StringTokenizer
30
* A class similar to the StringTokenizer from Java. It splits a string at
31
* the given string or character (or one of the special cases NEWLINE or
32
* WHITECHAR) and allows to iterate over the so generated substrings.
33
*
34
* The normal usage is like this:
35
* <pre>
36
* StringTokenizer st(CString("This is a line"), ' ');
37
* while(st.hasNext())
38
* cout << st.next() << endl;
39
* </pre>
40
* This would generate the output:
41
* <pre>
42
* This
43
* is
44
* a
45
* line
46
* </pre>
47
*
48
* There is something to know about the behaviour:
49
* When using WHITECHAR, a list of whitechars occuring in the string to
50
* split is regarded as a single divider. All other parameter will use
51
* multiple occurrences of operators as a list of single divider and the
52
* string between them will have a length of zero.
53
*/
54
// ===========================================================================
55
// class definitions
56
// ===========================================================================
57
/**
58
*
59
*/
60
class StringTokenizer {
61
public:
62
/// @brief identifier for splitting the given string at all newline characters
63
static const int NEWLINE;
64
65
/// @brief identifier for splitting the given string at all whitespace characters
66
static const int WHITECHARS;
67
68
/// @brief the ascii index of the highest whitespace character
69
static const int SPACE;
70
71
/// @brief the ascii index of the tab character
72
static const int TAB;
73
74
public:
75
/// @brief default constructor
76
StringTokenizer();
77
78
/**@brief constructor
79
* @param tosplit is the string to split into substrings. If the string between two split positions is empty, it will not be returned.
80
* @note same as StringTokenizer(tosplit, StringTokenizer.WHITECHARS)
81
*/
82
StringTokenizer(std::string tosplit);
83
84
/**@brief constructor
85
* @note the first string will be split at the second string's occurrences.
86
If the optional third parameter is true, the string will be split whenever
87
a char from the second string occurs. If the string between two split
88
positions is empty, it will nevertheless be returned.
89
*/
90
StringTokenizer(std::string tosplit, std::string token, bool splitAtAllChars = false);
91
92
/**@brief constructor
93
* @note When StringTokenizer.NEWLINE is used as second parameter, the string
94
will be split at all occurrences of a newline character (0x0d / 0x0a)
95
When StringTokenizer.WHITECHARS is used as second parameter, the
96
string will be split at all characters below 0x20 (SPACE)
97
All other ints specified as second parameter are casted int o a char
98
at which the string will be splitted.
99
*/
100
StringTokenizer(std::string tosplit, int special);
101
102
/// @brief destructor
103
~StringTokenizer();
104
105
/// @brief reinitialises the internal iterator
106
void reinit();
107
108
/// @brief returns the information whether further substrings exist
109
bool hasNext();
110
111
/// @brief returns the next substring when it exists. Otherwise the behaviour is undefined
112
std::string next();
113
114
/// @brief returns the number of existing substrings
115
int size() const;
116
117
/// @brief returns the first substring without moving the iterator
118
std::string front();
119
120
/// @brief returns the item at the given position
121
std::string get(int pos) const;
122
123
/// @brief return vector of strings
124
std::vector<std::string> getVector();
125
126
/// @brief return set of strings
127
std::set<std::string> getSet();
128
129
private:
130
/// @brief splits the first string at all occurrences of the second. If the third parameter is true split at all chars given in the second
131
void prepare(const std::string& tosplit, const std::string& token, bool splitAtAllChars);
132
133
/// @brief @brief splits the first string at all occurrences of whitechars
134
void prepareWhitechar(const std::string& tosplit);
135
136
private:
137
/// @brief a list of positions/lengths
138
typedef std::vector<int> SizeVector;
139
140
/// @brief the string to split
141
std::string myTosplit;
142
143
/// @brief the current position in the list of substrings
144
int myPos;
145
146
/// @brief the list of substring starts
147
SizeVector myStarts;
148
149
/// @brief the list of substring lengths
150
SizeVector myLengths;
151
};
152
153