Line data Source code
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 : /****************************************************************************/
14 : /// @file StringUtils.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @author Robert Hilbrich
19 : /// @date unknown
20 : ///
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>
30 : #include <utils/common/StdDefs.h>
31 :
32 :
33 : // ===========================================================================
34 : // class definitions
35 : // ===========================================================================
36 : /**
37 : * @class StringUtils
38 : * @brief Some static methods for string processing
39 : */
40 : class StringUtils {
41 :
42 : public:
43 :
44 : /// @brief Removes trailing and leading whitechars
45 : static std::string prune(const std::string& str);
46 :
47 : /// @brief Removes trailing zeros (at most 'max')
48 : static std::string pruneZeros(const std::string& str, int max);
49 :
50 : /// @brief Transfers the content to lower case
51 : static std::string to_lower_case(const std::string& str);
52 :
53 : /// @brief Transfers the content to upper case
54 : static std::string to_upper_case(const std::string& str);
55 :
56 : /// @brief Transfers from Latin 1 (ISO-8859-1) to UTF-8
57 : static std::string latin1_to_utf8(std::string str);
58 :
59 : /// @brief Converts german "Umlaute" to their latin-version
60 : static std::string convertUmlaute(std::string str);
61 :
62 : /// @brief Replaces all occurrences of the second string by the third string within the first string
63 : static std::string replace(std::string str, const std::string& what, const std::string& by);
64 :
65 : /// @brief Replaces an environment variable with its value (similar to bash); syntax for a variable is ${NAME}
66 : static std::string substituteEnvironment(const std::string& str, const std::chrono::time_point<std::chrono::system_clock>* const timeRef = nullptr);
67 :
68 : /// @brief Returns an ISO8601 formatted time string with microsecond precision
69 : static std::string isoTimeString(const std::chrono::time_point<std::chrono::system_clock>* const timeRef = nullptr);
70 :
71 : ///@brief Checks whether a given string starts with the prefix
72 : static bool startsWith(const std::string& str, const std::string prefix);
73 :
74 : /// @brief Checks whether a given string ends with the suffix
75 : static bool endsWith(const std::string& str, const std::string suffix);
76 :
77 : //// @brief pads the given string with padding character up to the given total length
78 : static std::string padFront(const std::string& str, int length, char padding);
79 :
80 : /**
81 : * @brief Replaces the standard escapes by their XML entities.
82 : *
83 : * The strings &, <, >, ", and ' are replaced by &, <, >, ", and '
84 : *
85 : * @param[in] orig The original string
86 : * @param[in] maskDoubleHyphen Whether -- in input shall be converted to -- (semantically equivalent but allowed in XML comments)
87 : * @return the string with the escaped sequences
88 : */
89 : static std::string escapeXML(const std::string& orig, const bool maskDoubleHyphen = false);
90 :
91 : /**
92 : * @brief Escape special characters with backslash
93 : */
94 : static std::string escapeShell(const std::string& orig);
95 :
96 : /// @brief An empty string
97 : static std::string emptyString;
98 :
99 : /// @brief encode url (stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/)
100 : static std::string urlEncode(const std::string& url, const std::string encodeWhich = "");
101 :
102 : /// @brief decode url (stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/)
103 : static std::string urlDecode(const std::string& encoded);
104 :
105 : /// @brief char to hexadecimal
106 : static std::string charToHex(unsigned char c);
107 :
108 : /// @brief hexadecimal to char
109 : static unsigned char hexToChar(const std::string& str);
110 :
111 : /**@brief converts a string into the integer value described by it by calling the char-type converter, which
112 : * @throw an EmptyData - exception if the given string is empty
113 : * @throw NumberFormatException - exception when the string does not contain an integer
114 : */
115 : static int toInt(const std::string& sData);
116 :
117 : /// @brief check if the given sData can be converted to int
118 : static bool isInt(const std::string& sData);
119 :
120 : /// @brief converts a string into the integer value described by it
121 : /// @return the default value if the data is empty
122 : static int toIntSecure(const std::string& sData, int def);
123 :
124 : /**@brief converts a string into the long value described by it by calling the char-type converter, which
125 : * @throw an EmptyData - exception if the given string is empty
126 : * @throw NumberFormatException - exception when the string does not contain a long integer
127 : */
128 : static long long int toLong(const std::string& sData);
129 :
130 : /// @brief Check if the given sData can be converted to long
131 : static bool isLong(const std::string& sData);
132 :
133 : /**@brief converts a string with a hex value into the integer value described by it by calling the char-type converter
134 : * @throw an EmptyData - exception if the given string is empty
135 : * @throw a NumberFormatException - exception when the string does not contain an integer
136 : */
137 : static int hexToInt(const std::string& sData);
138 :
139 : /// @brief check if the given string can be converted to hex
140 : static bool isHex(std::string sData);
141 :
142 : /**@brief converts a string into the double value described by it by calling the char-type converter
143 : * @throw an EmptyData - exception if the given string is empty
144 : * @throw a NumberFormatException - exception when the string does not contain a double
145 : */
146 : static double toDouble(const std::string& sData);
147 :
148 : /// @brief check if the given sData can be conveted to double
149 : static bool isDouble(const std::string& sData);
150 :
151 : /// @brief converts a string into the integer value described by it
152 : /// @return the default value if the data is empty
153 : static double toDoubleSecure(const std::string& sData, const double def);
154 :
155 : /**@brief converts a string into the bool value described by it by calling the char-type converter
156 : * @return true if the sData is one of the following (case insensitive): '1', 'x', 'true', 'yes', 'on', 't'
157 : * @return false if the sData is one of the following (case insensitive): '0', '-', 'false', 'no', 'off', 'f'
158 : * @throw EmptyData - exception if the given string is empty
159 : * @throw BoolFormatException in any other case
160 : */
161 : static bool toBool(const std::string& sData);
162 :
163 : /// @brief check if the given value can be converted to bool
164 : static bool isBool(const std::string& sData);
165 :
166 : /// @brief parse a (network) version string
167 : static MMVersion toVersion(const std::string& sData);
168 :
169 : /// @brief parse a distance, length or width value with a unit
170 : static double parseDist(const std::string& sData);
171 :
172 : /// @brief parse a speed value with a unit
173 : static double parseSpeed(const std::string& sData, const bool defaultKmph = true);
174 :
175 : /**@brief converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8
176 : * @throw an EmptyData - exception if the given pointer is 0
177 : */
178 101499488 : static inline std::string transcode(const XMLCh* const data) {
179 101499488 : return transcode(data, (int)XERCES_CPP_NAMESPACE::XMLString::stringLen(data));
180 : }
181 :
182 : /**@brief converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8 considering the given length
183 : * @throw EmptyData if the given pointer is 0
184 : */
185 : static std::string transcode(const XMLCh* const data, int length);
186 :
187 : /// @brief convert a string from the local codepage to UTF-8
188 : static std::string transcodeFromLocal(const std::string& localString);
189 :
190 : /// @brief convert a string from UTF-8 to the local codepage
191 : static std::string transcodeToLocal(const std::string& utf8String);
192 :
193 : /// @brief remove leading whitespace from string
194 : static std::string trim_left(const std::string s, const std::string& t = " \t\n");
195 :
196 : /// @brief remove trailing whitespace from string
197 : static std::string trim_right(const std::string s, const std::string& t = " \t\n");
198 :
199 : /// @brief remove leading and trailing whitespace
200 : static std::string trim(const std::string s, const std::string& t = " \t\n");
201 :
202 : /// @brief remove leading and trailing whitespace
203 : static std::string wrapText(const std::string s, int width);
204 :
205 : /// @brief write with maximum precision if needed but remove trailing zeros
206 : static std::string adjustDecimalValue(double value, int precision);
207 :
208 : /// @brief must be called when shutting down the xml subsystem
209 : static void resetTranscoder();
210 :
211 : /// @brief adds a new formatted message
212 : // variadic function
213 : template<typename T, typename... Targs>
214 3135779 : static const std::string format(const std::string& format, T value, Targs... Fargs) {
215 3135779 : std::ostringstream os;
216 3135779 : os << std::fixed << std::setprecision(gPrecision);
217 6270471 : _format(format.c_str(), os, value, Fargs...);
218 3135779 : return os.str();
219 3135779 : }
220 :
221 : private:
222 : static void _format(const char* format, std::ostringstream& os) {
223 3135779 : os << format;
224 : }
225 :
226 : /// @brief adds a new formatted message
227 : // variadic function
228 : template<typename T, typename... Targs>
229 8585681 : static void _format(const char* format, std::ostringstream& os, T value, Targs... Fargs) {
230 133416861 : for (; *format != '\0'; format++) {
231 133416861 : if (*format == '%') {
232 8585681 : os << value;
233 11194406 : _format(format + 1, os, Fargs...); // recursive call
234 8585681 : return;
235 : }
236 124831180 : os << *format;
237 : }
238 : }
239 :
240 : static XERCES_CPP_NAMESPACE::XMLLCPTranscoder* myLCPTranscoder;
241 : };
|