Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
StringUtils.h
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/****************************************************************************/
21// Some static methods for string processing
22/****************************************************************************/
23#pragma once
24#include <config.h>
25#include <string>
26#include <chrono>
27#include <sstream>
28#include <iomanip>
29#include <vector>
31
32
33// ===========================================================================
34// class definitions
35// ===========================================================================
41
42public:
43
45 static std::string prune(const std::string& str);
46
48 static std::string pruneZeros(const std::string& str, int max);
49
51 static std::string to_lower_case(const std::string& str);
52
54 static std::string to_upper_case(const std::string& str);
55
57 static std::string latin1_to_utf8(std::string str);
58
60 static std::string convertUmlaute(std::string str);
61
63 static std::string replace(std::string str, const std::string& what, const std::string& by);
64
66 static std::string substituteEnvironment(const std::string& str, const std::chrono::time_point<std::chrono::system_clock>* const timeRef = nullptr);
67
69 static std::string isoTimeString(const std::chrono::time_point<std::chrono::system_clock>* const timeRef = nullptr);
70
72 static bool startsWith(const std::string& str, const std::string prefix);
73
75 static bool endsWith(const std::string& str, const std::string suffix);
76
78 static std::string padFront(const std::string& str, int length, char padding);
79
89 static std::string escapeXML(const std::string& orig, const bool maskDoubleHyphen = false);
90
94 static std::string escapeShell(const std::string& orig);
95
104 static std::string escapeCSV(const std::string& orig, const char separator, const char quote = '"');
105
107 static std::string emptyString;
108
110 static std::string urlEncode(const std::string& url, const std::string encodeWhich = "");
111
113 static std::string urlDecode(const std::string& encoded);
114
116 static std::string charToHex(unsigned char c);
117
119 static unsigned char hexToChar(const std::string& str);
120
125 static int toInt(const std::string& sData);
126
128 static bool isInt(const std::string& sData);
129
132 static int toIntSecure(const std::string& sData, int def);
133
138 static long long int toLong(const std::string& sData);
139
141 static bool isLong(const std::string& sData);
142
147 static int hexToInt(const std::string& sData);
148
150 static bool isHex(std::string sData);
151
156 static double toDouble(const std::string& sData);
157
159 static bool isDouble(const std::string& sData);
160
163 static double toDoubleSecure(const std::string& sData, const double def);
164
169 static double toDoubleAllowTime(const std::string& sData);
170
177 static bool toBool(const std::string& sData);
178
180 static bool isBool(const std::string& sData);
181
183 static MMVersion toVersion(const std::string& sData);
184
186 static double parseDist(const std::string& sData);
187
189 static double parseSpeed(const std::string& sData, const bool defaultKmph = true);
190
192 static std::string trim_left(const std::string s, const std::string& t = " \t\n");
193
195 static std::string trim_right(const std::string s, const std::string& t = " \t\n");
196
198 static std::string trim(const std::string s, const std::string& t = " \t\n");
199
201 static std::string wrapText(const std::string s, int width);
202
204 static std::string adjustDecimalValue(double value, int precision);
205
208
210 static bool isValidNetID(const std::string& value);
211
213 static bool isValidVehicleID(const std::string& value);
214
216 static bool isValidTypeID(const std::string& value);
217
219 static bool isValidAdditionalID(const std::string& value);
220
222 static bool isValidDetectorID(const std::string& value);
223
225 static bool isValidAttribute(const std::string& value);
226
228 static bool isValidFilename(const std::string& value);
229
231 static bool isValidListOfNetIDs(const std::string& value);
232
234 static bool isValidListOfTypeID(const std::string& value);
235
237 static bool isValidListOfTypeID(const std::vector<std::string>& typeIDs);
238
240 static bool isValidParameterKey(const std::string& value);
241
243 // Replaces all invalid characters for a netID by an underscore, this may violate uniqueness!
244 // Since the netID has the most restrictions, the resulting ID should be valid for the other use cases too
245 static std::string makeValidID(const std::string& value);
246
248
250 // variadic function
251 template<typename T, typename... Targs>
252 static const std::string format(const std::string& format, T value, Targs... Fargs) {
253 std::ostringstream os;
254 os << std::fixed << std::setprecision(gPrecision);
255 _format(format.c_str(), os, value, Fargs...);
256 return os.str();
257 }
258
262 static std::string transcodeLocal(const std::string& str, bool toLocal);
263
264private:
265 static void _format(const char* format, std::ostringstream& os) {
266 os << format;
267 }
268
270 // variadic function
271 template<typename T, typename... Targs>
272 static void _format(const char* format, std::ostringstream& os, T value, Targs... Fargs) {
273 for (; *format != '\0'; format++) {
274 if (*format == '%') {
275 os << value;
276 _format(format + 1, os, Fargs...); // recursive call
277 return;
278 }
279 os << *format;
280 }
281 }
282};
int gPrecision
the precision for floating point outputs
Definition StdDefs.cpp:27
std::pair< int, double > MMVersion
(M)ajor/(M)inor version for written networks and default version for loading
Definition StdDefs.h:71
Some static methods for string processing.
Definition StringUtils.h:40
static std::string pruneZeros(const std::string &str, int max)
Removes trailing zeros (at most 'max')
static std::string urlEncode(const std::string &url, const std::string encodeWhich="")
encode url (stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/)
static bool isDouble(const std::string &sData)
check if the given sData can be conveted to double
static MMVersion toVersion(const std::string &sData)
parse a (network) version string
static bool isBool(const std::string &sData)
check if the given value can be converted to bool
static std::string to_upper_case(const std::string &str)
Transfers the content to upper case.
static std::string transcodeLocal(const std::string &str, bool toLocal)
convert a string between UTF-8 and the local codepage (identity on non-Windows systems)
static std::string charToHex(unsigned char c)
char to hexadecimal
static std::string urlDecode(const std::string &encoded)
decode url (stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/)
static long long int toLong(const std::string &sData)
converts a string into the long value described by it by calling the char-type converter,...
static double toDoubleSecure(const std::string &sData, const double def)
converts a string into the integer value described by it
static std::string makeValidID(const std::string &value)
transform the given string into a valid id for a network element (or any other id)
static std::string trim(const std::string s, const std::string &t=" \t\n")
remove leading and trailing whitespace
static std::string to_lower_case(const std::string &str)
Transfers the content to lower case.
static bool isValidParameterKey(const std::string &value)
whether the given string is a valid key for a parameter
static void _format(const char *format, std::ostringstream &os, T value, Targs... Fargs)
adds a new formatted message
static std::string trim_right(const std::string s, const std::string &t=" \t\n")
remove trailing whitespace from string
static bool isValidNetID(const std::string &value)
whether the given string is a valid id for a network element
static void _format(const char *format, std::ostringstream &os)
static std::string trim_left(const std::string s, const std::string &t=" \t\n")
remove leading whitespace from string
static bool isValidListOfTypeID(const std::string &value)
whether the given string is a valid list of ids for an edge or vehicle type (empty aren't allowed)
static std::string escapeShell(const std::string &orig)
Escape special characters with backslash.
static std::string replace(std::string str, const std::string &what, const std::string &by)
Replaces all occurrences of the second string by the third string within the first string.
static int hexToInt(const std::string &sData)
converts a string with a hex value into the integer value described by it by calling the char-type co...
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static bool isValidVehicleID(const std::string &value)
whether the given string is a valid id for a vehicle or flow
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.
static bool isValidDetectorID(const std::string &value)
whether the given string is a valid id for an detector
static bool isHex(std::string sData)
check if the given string can be converted to hex
static bool isValidAdditionalID(const std::string &value)
whether the given string is a valid id for an additional object
static std::string latin1_to_utf8(std::string str)
Transfers from Latin 1 (ISO-8859-1) to UTF-8.
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
static std::string padFront(const std::string &str, int length, char padding)
static std::string convertUmlaute(std::string str)
Converts german "Umlaute" to their latin-version.
static bool isValidFilename(const std::string &value)
whether the given string is a valid attribute for a filename (for example, a name)
static double parseDist(const std::string &sData)
parse a distance, length or width value with a unit
static std::string adjustDecimalValue(double value, int precision)
write with maximum precision if needed but remove trailing zeros
static unsigned char hexToChar(const std::string &str)
hexadecimal to char
static const std::string format(const std::string &format, T value, Targs... Fargs)
adds a new formatted message
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
static std::string wrapText(const std::string s, int width)
remove leading and trailing whitespace
static double parseSpeed(const std::string &sData, const bool defaultKmph=true)
parse a speed value with a unit
static double toDoubleAllowTime(const std::string &sData)
converts a string into a double value. Values in human-readable-time format (see string2time) are per...
static std::string emptyString
An empty string.
static bool endsWith(const std::string &str, const std::string suffix)
Checks whether a given string ends with the suffix.
static std::string substituteEnvironment(const std::string &str, const std::chrono::time_point< std::chrono::system_clock > *const timeRef=nullptr)
Replaces an environment variable with its value (similar to bash); syntax for a variable is ${NAME}.
static bool isLong(const std::string &sData)
Check if the given sData can be converted to long.
static int toIntSecure(const std::string &sData, int def)
converts a string into the integer value described by it
static std::string isoTimeString(const std::chrono::time_point< std::chrono::system_clock > *const timeRef=nullptr)
Returns an ISO8601 formatted time string with microsecond precision.
static std::string escapeCSV(const std::string &orig, const char separator, const char quote='"')
Adds quotes to strings containing the separator or the quote char and escapes the quote char with a b...
static bool isValidListOfNetIDs(const std::string &value)
whether the given string is a valid list of id for a network (empty aren't allowed)
static bool isValidAttribute(const std::string &value)
whether the given string is a valid attribute for a certain key (for example, a name)
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
static bool isValidTypeID(const std::string &value)
whether the given string is a valid id for an edge or vehicle type
static bool isInt(const std::string &sData)
check if the given sData can be converted to int
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter