Eclipse SUMO - Simulation of Urban MObility
StringUtils.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Some static methods for string processing
18 /****************************************************************************/
19 #ifndef StringUtils_h
20 #define StringUtils_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 
27 #include <string>
28 #include <xercesc/util/XMLString.hpp>
30 
31 
32 // ===========================================================================
33 // class definitions
34 // ===========================================================================
39 class StringUtils {
40 public:
42  static std::string prune(const std::string& str);
43 
45  static std::string to_lower_case(std::string str);
46 
48  static std::string latin1_to_utf8(std::string str);
49 
51  static std::string convertUmlaute(std::string str);
52 
55  static std::string replace(std::string str, const char* what,
56  const char* by);
57 
60  static std::string substituteEnvironment(std::string str);
61 
63  static std::string toTimeString(int time);
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 
80  static std::string escapeXML(const std::string& orig, const bool maskDoubleHyphen = false);
81 
83  static std::string emptyString;
84 
85  // the following methods stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/
86 
87  static std::string urlEncode(const std::string& url, const std::string encodeWhich = "");
88  static std::string urlDecode(const std::string& encoded);
89 
90  static std::string charToHex(unsigned char c);
91  static unsigned char hexToChar(const std::string& str);
92 
97  static int toInt(const std::string& sData);
98 
101  static int toIntSecure(const std::string& sData, int def);
102 
107  static long long int toLong(const std::string& sData);
108 
113  static int hexToInt(const std::string& sData);
114 
119  static double toDouble(const std::string& sData);
120 
123  static double toDoubleSecure(const std::string& sData, const double def);
124 
131  static bool toBool(const std::string& sData);
132 
136  static inline std::string transcode(const XMLCh* const data) {
137  return transcode(data, (int)XERCES_CPP_NAMESPACE::XMLString::stringLen(data));
138  }
139 
143  static std::string transcode(const XMLCh* const data, int length);
144 };
145 
146 
147 #endif
148 
149 /****************************************************************************/
150 
StringUtils::replace
static std::string replace(std::string str, const char *what, const char *by)
Definition: StringUtils.cpp:105
StringUtils::charToHex
static std::string charToHex(unsigned char c)
Definition: StringUtils.cpp:248
StringUtils::urlDecode
static std::string urlDecode(const std::string &encoded)
Definition: StringUtils.cpp:231
StringUtils::toBool
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter
Definition: StringUtils.cpp:374
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:345
StringUtils::toDoubleSecure
static double toDoubleSecure(const std::string &sData, const double def)
converts a string into the integer value described by it
Definition: StringUtils.cpp:365
StringUtils::to_lower_case
static std::string to_lower_case(std::string str)
Transfers the content to lower case.
Definition: StringUtils.cpp:59
StringUtils::convertUmlaute
static std::string convertUmlaute(std::string str)
Converts german "Umlaute" to their latin-version.
Definition: StringUtils.cpp:87
StringUtils::prune
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:48
StringUtils::endsWith
static bool endsWith(const std::string &str, const std::string suffix)
Checks whether a given string ends with the suffix.
Definition: StringUtils.cpp:180
StringUtils::hexToChar
static unsigned char hexToChar(const std::string &str)
Definition: StringUtils.cpp:260
StringUtils::toTimeString
static std::string toTimeString(int time)
Builds a time string (hh:mm:ss) from the given seconds.
Definition: StringUtils.cpp:154
StringUtils::escapeXML
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.
Definition: StringUtils.cpp:190
UtilExceptions.h
StringUtils::startsWith
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
Definition: StringUtils.cpp:174
StringUtils::hexToInt
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...
Definition: StringUtils.cpp:321
StringUtils::toIntSecure
static int toIntSecure(const std::string &sData, int def)
converts a string into the integer value described by it
Definition: StringUtils.cpp:288
StringUtils::latin1_to_utf8
static std::string latin1_to_utf8(std::string str)
Transfers from Latin 1 (ISO-8859-1) to UTF-8.
Definition: StringUtils.cpp:70
StringUtils
Some static methods for string processing.
Definition: StringUtils.h:39
StringUtils::substituteEnvironment
static std::string substituteEnvironment(std::string str)
Definition: StringUtils.cpp:122
StringUtils::urlEncode
static std::string urlEncode(const std::string &url, const std::string encodeWhich="")
Definition: StringUtils.cpp:206
StringUtils::transcode
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
Definition: StringUtils.h:136
StringUtils::toInt
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
Definition: StringUtils.cpp:278
StringUtils::emptyString
static std::string emptyString
An empty string.
Definition: StringUtils.h:83
StringUtils::toLong
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,...
Definition: StringUtils.cpp:297