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-2022 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 <xercesc/util/XMLString.hpp>
28 
29 
30 // ===========================================================================
31 // class definitions
32 // ===========================================================================
37 class StringUtils {
38 public:
40  static std::string prune(const std::string& str);
41 
43  static std::string to_lower_case(std::string str);
44 
46  static std::string latin1_to_utf8(std::string str);
47 
49  static std::string convertUmlaute(std::string str);
50 
53  static std::string replace(std::string str, const char* what,
54  const char* by);
55 
58  static std::string substituteEnvironment(std::string str);
59 
61  static std::string toTimeString(int time);
62 
64  static bool startsWith(const std::string& str, const std::string prefix);
65 
67  static bool endsWith(const std::string& str, const std::string suffix);
68 
70  static std::string padFront(const std::string& str, int length, char padding);
71 
81  static std::string escapeXML(const std::string& orig, const bool maskDoubleHyphen = false);
82 
84  static std::string emptyString;
85 
86  // the following methods stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/
87 
88  static std::string urlEncode(const std::string& url, const std::string encodeWhich = "");
89  static std::string urlDecode(const std::string& encoded);
90 
91  static std::string charToHex(unsigned char c);
92  static unsigned char hexToChar(const std::string& str);
93 
98  static int toInt(const std::string& sData);
99 
102  static int toIntSecure(const std::string& sData, int def);
103 
108  static long long int toLong(const std::string& sData);
109 
114  static int hexToInt(const std::string& sData);
115 
120  static double toDouble(const std::string& sData);
121 
124  static double toDoubleSecure(const std::string& sData, const double def);
125 
132  static bool toBool(const std::string& sData);
133 
137  static inline std::string transcode(const XMLCh* const data) {
138  return transcode(data, (int)XERCES_CPP_NAMESPACE::XMLString::stringLen(data));
139  }
140 
144  static std::string transcode(const XMLCh* const data, int length);
145 
147  static std::string transcodeFromLocal(const std::string& localString);
148 
150  static std::string transcodeToLocal(const std::string& utf8String);
151 
153  static std::string trim_left(const std::string s, const std::string& t = " \t\n");
154 
156  static std::string trim_right(const std::string s, const std::string& t = " \t\n");
157 
159  static std::string trim(const std::string s, const std::string& t = " \t\n");
160 
162  static void resetTranscoder();
163 private:
164  static XERCES_CPP_NAMESPACE::XMLLCPTranscoder* myLCPTranscoder;
165 
166 
167 };
Some static methods for string processing.
Definition: StringUtils.h:37
static std::string urlEncode(const std::string &url, const std::string encodeWhich="")
static std::string charToHex(unsigned char c)
static std::string urlDecode(const std::string &encoded)
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 replace(std::string str, const char *what, const char *by)
static std::string trim(const std::string s, const std::string &t=" \t\n")
remove leading and trailing whitespace
static void resetTranscoder()
must be called when shutting down the xml subsystem
static XERCES_CPP_NAMESPACE::XMLLCPTranscoder * myLCPTranscoder
Definition: StringUtils.h:164
static std::string trim_right(const std::string s, const std::string &t=" \t\n")
remove trailing whitespace from string
static std::string trim_left(const std::string s, const std::string &t=" \t\n")
remove leading whitespace from string
static std::string substituteEnvironment(std::string str)
static std::string toTimeString(int time)
Builds a time string (hh:mm:ss) from the given seconds.
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 to_lower_case(std::string str)
Transfers the content to lower case.
Definition: StringUtils.cpp:59
static std::string latin1_to_utf8(std::string str)
Transfers from Latin 1 (ISO-8859-1) to UTF-8.
Definition: StringUtils.cpp:70
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:48
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.
Definition: StringUtils.cpp:87
static unsigned char hexToChar(const std::string &str)
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
static std::string emptyString
An empty string.
Definition: StringUtils.h:84
static bool endsWith(const std::string &str, const std::string suffix)
Checks whether a given string ends with the suffix.
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:137
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