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-2024 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 <xercesc/util/XMLString.hpp>
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 latin1_to_utf8(std::string str);
55
57 static std::string convertUmlaute(std::string str);
58
60 static std::string replace(std::string str, const std::string& what, const std::string& by);
61
63 static std::string substituteEnvironment(const std::string& str, const std::chrono::time_point<std::chrono::system_clock>* const timeRef = nullptr);
64
66 static bool startsWith(const std::string& str, const std::string prefix);
67
69 static bool endsWith(const std::string& str, const std::string suffix);
70
72 static std::string padFront(const std::string& str, int length, char padding);
73
83 static std::string escapeXML(const std::string& orig, const bool maskDoubleHyphen = false);
84
88 static std::string escapeShell(const std::string& orig);
89
91 static std::string emptyString;
92
94 static std::string urlEncode(const std::string& url, const std::string encodeWhich = "");
95
97 static std::string urlDecode(const std::string& encoded);
98
100 static std::string charToHex(unsigned char c);
101
103 static unsigned char hexToChar(const std::string& str);
104
109 static int toInt(const std::string& sData);
110
113 static int toIntSecure(const std::string& sData, int def);
114
119 static long long int toLong(const std::string& sData);
120
125 static int hexToInt(const std::string& sData);
126
131 static double toDouble(const std::string& sData);
132
135 static double toDoubleSecure(const std::string& sData, const double def);
136
143 static bool toBool(const std::string& sData);
144
146 static MMVersion toVersion(const std::string& sData);
147
149 static double parseDist(const std::string& sData);
150
152 static double parseSpeed(const std::string& sData, const bool defaultKmph = true);
153
157 static inline std::string transcode(const XMLCh* const data) {
158 return transcode(data, (int)XERCES_CPP_NAMESPACE::XMLString::stringLen(data));
159 }
160
164 static std::string transcode(const XMLCh* const data, int length);
165
167 static std::string transcodeFromLocal(const std::string& localString);
168
170 static std::string transcodeToLocal(const std::string& utf8String);
171
173 static std::string trim_left(const std::string s, const std::string& t = " \t\n");
174
176 static std::string trim_right(const std::string s, const std::string& t = " \t\n");
177
179 static std::string trim(const std::string s, const std::string& t = " \t\n");
180
182 static std::string wrapText(const std::string s, int width);
183
185 static void resetTranscoder();
186
188 // variadic function
189 template<typename T, typename... Targs>
190 static const std::string format(const std::string& format, T value, Targs... Fargs) {
191 std::ostringstream os;
192 os << std::fixed << std::setprecision(gPrecision);
193 _format(format.c_str(), os, value, Fargs...);
194 return os.str();
195 }
196
197private:
198 static void _format(const char* format, std::ostringstream& os) {
199 os << format;
200 }
201
203 // variadic function
204 template<typename T, typename... Targs>
205 static void _format(const char* format, std::ostringstream& os, T value, Targs... Fargs) {
206 for (; *format != '\0'; format++) {
207 if (*format == '%') {
208 os << value;
209 _format(format + 1, os, Fargs...); // recursive call
210 return;
211 }
212 os << *format;
213 }
214 }
215
216 static XERCES_CPP_NAMESPACE::XMLLCPTranscoder* myLCPTranscoder;
217};
int gPrecision
the precision for floating point outputs
Definition StdDefs.cpp:26
std::pair< int, double > MMVersion
(M)ajor/(M)inor version for written networks and default version for loading
Definition StdDefs.h:67
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 MMVersion toVersion(const std::string &sData)
parse a (network) version string
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 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 void _format(const char *format, std::ostringstream &os, T value, Targs... Fargs)
adds a new formatted message
static void resetTranscoder()
must be called when shutting down the xml subsystem
static XERCES_CPP_NAMESPACE::XMLLCPTranscoder * myLCPTranscoder
static std::string trim_right(const std::string s, const std::string &t=" \t\n")
remove trailing whitespace from string
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 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 std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.
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 double parseDist(const std::string &sData)
parse a distance, length or width value with a unit
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 std::string emptyString
An empty string.
Definition StringUtils.h:91
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 std::string transcode(const XMLCh *const data)
converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8
static std::string transcodeToLocal(const std::string &utf8String)
convert a string from UTF-8 to the local codepage
static int toIntSecure(const std::string &sData, int def)
converts a string into the integer value described by it
static std::string transcodeFromLocal(const std::string &localString)
convert a string from the local codepage to UTF-8
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 toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter