Line data Source code
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 : /****************************************************************************/ 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 from Latin 1 (ISO-8859-1) to UTF-8 54 : static std::string latin1_to_utf8(std::string str); 55 : 56 : /// @brief Converts german "Umlaute" to their latin-version 57 : static std::string convertUmlaute(std::string str); 58 : 59 : /// @brief Replaces all occurrences of the second string by the third string within the first string 60 : static std::string replace(std::string str, const std::string& what, const std::string& by); 61 : 62 : /// @brief Replaces an environment variable with its value (similar to bash); syntax for a variable is ${NAME} 63 : static std::string substituteEnvironment(const std::string& str, const std::chrono::time_point<std::chrono::system_clock>* const timeRef = nullptr); 64 : 65 : ///@brief Checks whether a given string starts with the prefix 66 : static bool startsWith(const std::string& str, const std::string prefix); 67 : 68 : /// @brief Checks whether a given string ends with the suffix 69 : static bool endsWith(const std::string& str, const std::string suffix); 70 : 71 : //// @brief pads the given string with padding character up to the given total length 72 : static std::string padFront(const std::string& str, int length, char padding); 73 : 74 : /** 75 : * @brief Replaces the standard escapes by their XML entities. 76 : * 77 : * The strings &, <, >, ", and ' are replaced by &, <, >, ", and ' 78 : * 79 : * @param[in] orig The original string 80 : * @param[in] maskDoubleHyphen Whether -- in input shall be converted to -- (semantically equivalent but allowed in XML comments) 81 : * @return the string with the escaped sequences 82 : */ 83 : static std::string escapeXML(const std::string& orig, const bool maskDoubleHyphen = false); 84 : 85 : /// @brief An empty string 86 : static std::string emptyString; 87 : 88 : /// @brief encode url (stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/) 89 : static std::string urlEncode(const std::string& url, const std::string encodeWhich = ""); 90 : 91 : /// @brief decode url (stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/) 92 : static std::string urlDecode(const std::string& encoded); 93 : 94 : /// @brief char to hexadecimal 95 : static std::string charToHex(unsigned char c); 96 : 97 : /// @brief hexadecimal to char 98 : static unsigned char hexToChar(const std::string& str); 99 : 100 : /**@brief converts a string into the integer value described by it by calling the char-type converter, which 101 : * @throw an EmptyData - exception if the given string is empty 102 : * @throw NumberFormatException - exception when the string does not contain an integer 103 : */ 104 : static int toInt(const std::string& sData); 105 : 106 : /// @brief converts a string into the integer value described by it 107 : /// @return the default value if the data is empty 108 : static int toIntSecure(const std::string& sData, int def); 109 : 110 : /**@brief converts a string into the long value described by it by calling the char-type converter, which 111 : * @throw an EmptyData - exception if the given string is empty 112 : * @throw NumberFormatException - exception when the string does not contain a long integer 113 : */ 114 : static long long int toLong(const std::string& sData); 115 : 116 : /**@brief converts a string with a hex value into the integer value described by it by calling the char-type converter 117 : * @throw an EmptyData - exception if the given string is empty 118 : * @throw a NumberFormatException - exception when the string does not contain an integer 119 : */ 120 : static int hexToInt(const std::string& sData); 121 : 122 : /**@brief converts a string into the double value described by it by calling the char-type converter 123 : * @throw an EmptyData - exception if the given string is empty 124 : * @throw a NumberFormatException - exception when the string does not contain a double 125 : */ 126 : static double toDouble(const std::string& sData); 127 : 128 : /// @brief converts a string into the integer value described by it 129 : /// @return the default value if the data is empty 130 : static double toDoubleSecure(const std::string& sData, const double def); 131 : 132 : /**@brief converts a string into the bool value described by it by calling the char-type converter 133 : * @return true if the data* is one of the following (case insensitive): '1', 'x', 'true', 'yes', 'on', 't' 134 : * @return false if the data* is one of the following (case insensitive): '0', '-', 'false', 'no', 'off', 'f' 135 : * @throw EmptyData - exception if the given string is empty or 0 pointer 136 : * @throw BoolFormatException in any other case 137 : */ 138 : static bool toBool(const std::string& sData); 139 : 140 : /// @brief to version 141 : static MMVersion toVersion(const std::string& sData); 142 : 143 : /**@brief converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8 144 : * @throw an EmptyData - exception if the given pointer is 0 145 : */ 146 94028038 : static inline std::string transcode(const XMLCh* const data) { 147 94028038 : return transcode(data, (int)XERCES_CPP_NAMESPACE::XMLString::stringLen(data)); 148 : } 149 : 150 : /**@brief converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8 considering the given length 151 : * @throw EmptyData if the given pointer is 0 152 : */ 153 : static std::string transcode(const XMLCh* const data, int length); 154 : 155 : /// @brief convert a string from the local codepage to UTF-8 156 : static std::string transcodeFromLocal(const std::string& localString); 157 : 158 : /// @brief convert a string from UTF-8 to the local codepage 159 : static std::string transcodeToLocal(const std::string& utf8String); 160 : 161 : /// @brief remove leading whitespace from string 162 : static std::string trim_left(const std::string s, const std::string& t = " \t\n"); 163 : 164 : /// @brief remove trailing whitespace from string 165 : static std::string trim_right(const std::string s, const std::string& t = " \t\n"); 166 : 167 : /// @brief remove leading and trailing whitespace 168 : static std::string trim(const std::string s, const std::string& t = " \t\n"); 169 : 170 : /// @brief must be called when shutting down the xml subsystem 171 : static void resetTranscoder(); 172 : 173 : /// @brief adds a new formatted message 174 : // variadic function 175 : template<typename T, typename... Targs> 176 617789 : static const std::string format(const std::string& format, T value, Targs... Fargs) { 177 617789 : std::ostringstream os; 178 617789 : os << std::fixed << std::setprecision(gPrecision); 179 1281939 : _format(format.c_str(), os, value, Fargs...); 180 617789 : return os.str(); 181 617789 : } 182 : 183 : private: 184 : static void _format(const char* format, std::ostringstream& os) { 185 617789 : os << format; 186 : } 187 : 188 : /// @brief adds a new formatted message 189 : // variadic function 190 : template<typename T, typename... Targs> 191 1392120 : static void _format(const char* format, std::ostringstream& os, T value, Targs... Fargs) { 192 21927931 : for (; *format != '\0'; format++) { 193 21927931 : if (*format == '%') { 194 1392120 : os << value; 195 1632455 : _format(format + 1, os, Fargs...); // recursive call 196 1392120 : return; 197 : } 198 20535811 : os << *format; 199 : } 200 : } 201 : 202 : static XERCES_CPP_NAMESPACE::XMLLCPTranscoder* myLCPTranscoder; 203 : };