Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
StringTokenizer.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 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/****************************************************************************/
20// A java-style StringTokenizer for c++ (stl)
21/****************************************************************************/
22#include <config.h>
23
24#include <string>
25#include <vector>
26#include <iostream> // !!! debug only
27
28#include "StringTokenizer.h"
29
30
31// ===========================================================================
32// variable definitions
33// ===========================================================================
34const int StringTokenizer::NEWLINE = -256;
35const int StringTokenizer::WHITECHARS = -257;
36const int StringTokenizer::SPACE = 32;
37const int StringTokenizer::TAB = 9;
38
39
40// ===========================================================================
41// method definitions
42// ===========================================================================
43
47
48
50 myTosplit(tosplit), myPos(0) {
51 prepareWhitechar(tosplit);
52}
53
54
55StringTokenizer::StringTokenizer(std::string tosplit, std::string token, bool splitAtAllChars) :
56 myTosplit(tosplit), myPos(0) {
57 prepare(tosplit, token, splitAtAllChars);
58}
59
60
61StringTokenizer::StringTokenizer(std::string tosplit, int special) :
62 myTosplit(tosplit), myPos(0) {
63 switch (special) {
64 case NEWLINE:
65 prepare(tosplit, "\r\n", true);
66 break;
67 case TAB:
68 prepare(tosplit, "\t", true);
69 break;
70 case WHITECHARS:
71 prepareWhitechar(tosplit);
72 break;
73 default:
74 char* buf = new char[2];
75 buf[0] = (char) special;
76 buf[1] = 0;
77 prepare(tosplit, buf, false);
78 delete[] buf;
79 break;
80 }
81}
82
83
85
86
88 myPos = 0;
89}
90
91
93 return myPos != (int)myStarts.size();
94}
95
96
97std::string StringTokenizer::next() {
98 if (myPos >= (int)myStarts.size()) {
100 }
101 if (myLengths[myPos] == 0) {
102 myPos++;
103 return "";
104 }
105 int start = myStarts[myPos];
106 int length = myLengths[myPos++];
107 return myTosplit.substr(start, length);
108}
109
110
112 if (myStarts.size() == 0) {
113 throw OutOfBoundsException();
114 }
115 if (myLengths[0] == 0) {
116 return "";
117 }
118 return myTosplit.substr(myStarts[0], myLengths[0]);
119}
120
121
122std::string StringTokenizer::get(int pos) const {
123 if (pos >= (int)myStarts.size()) {
124 throw OutOfBoundsException();
125 }
126 if (myLengths[pos] == 0) {
127 return "";
128 }
129 int start = myStarts[pos];
130 int length = myLengths[pos];
131 return myTosplit.substr(start, length);
132}
133
134
136 return (int)myStarts.size();
137}
138
139
140void StringTokenizer::prepare(const std::string& tosplit, const std::string& token, bool splitAtAllChars) {
141 int beg = 0;
142 int len = (int)token.length();
143 if (splitAtAllChars) {
144 len = 1;
145 }
146 while (beg < (int)tosplit.length()) {
147 std::string::size_type end;
148 if (splitAtAllChars) {
149 end = tosplit.find_first_of(token, beg);
150 } else {
151 end = tosplit.find(token, beg);
152 }
153 if (end == std::string::npos) {
154 end = tosplit.length();
155 }
156 myStarts.push_back(beg);
157 myLengths.push_back((int)end - beg);
158 beg = (int)end + len;
159 if (beg == (int)tosplit.length()) {
160 myStarts.push_back(beg - 1);
161 myLengths.push_back(0);
162 }
163 }
164}
165
166
167void StringTokenizer::prepareWhitechar(const std::string& tosplit) {
168 std::string::size_type len = tosplit.length();
169 std::string::size_type beg = 0;
170 while (beg < len && (unsigned char)tosplit[beg] <= SPACE) {
171 beg++;
172 }
173 while (beg != std::string::npos && beg < len) {
174 std::string::size_type end = beg;
175 while (end < len && (unsigned char)tosplit[end] > SPACE) {
176 end++;
177 }
178 myStarts.push_back((int)beg);
179 myLengths.push_back((int)end - (int)beg);
180 beg = end;
181 while (beg < len && (unsigned char)tosplit[beg] <= SPACE) {
182 beg++;
183 }
184 }
185}
186
187
188std::vector<std::string>
190 std::vector<std::string> ret;
191 ret.reserve(size());
192 while (hasNext()) {
193 ret.push_back(next());
194 }
195 reinit();
196 return ret;
197}
198
199
200std::set<std::string>
202 std::vector<std::string> v = getVector();
203 return std::set<std::string>(v.begin(), v.end());
204}
205
206
207/****************************************************************************/
static const int NEWLINE
identifier for splitting the given string at all newline characters
std::string front()
returns the first substring without moving the iterator
std::set< std::string > getSet()
return set of strings
~StringTokenizer()
destructor
int size() const
returns the number of existing substrings
SizeVector myStarts
the list of substring starts
void reinit()
reinitialises the internal iterator
static const int WHITECHARS
identifier for splitting the given string at all whitespace characters
std::string get(int pos) const
returns the item at the given position
std::vector< std::string > getVector()
return vector of strings
bool hasNext()
returns the information whether further substrings exist
static const int TAB
the ascii index of the tab character
static const int SPACE
the ascii index of the highest whitespace character
SizeVector myLengths
the list of substring lengths
StringTokenizer()
default constructor
int myPos
the current position in the list of substrings
void prepare(const std::string &tosplit, const std::string &token, bool splitAtAllChars)
splits the first string at all occurrences of the second. If the third parameter is true split at all...
std::string myTosplit
the string to split
void prepareWhitechar(const std::string &tosplit)
splits the first string at all occurrences of whitechars
std::string next()
returns the next substring when it exists. Otherwise the behaviour is undefined