SUMO - Simulation of Urban MObility
StringUtils.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 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 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <string>
27 #include <iostream>
28 #include <cstdio>
31 #include <utils/common/ToString.h>
32 #include "StringUtils.h"
33 
34 
35 // ===========================================================================
36 // static member definitions
37 // ===========================================================================
38 std::string StringUtils::emptyString;
39 
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
44 std::string
45 StringUtils::prune(const std::string& str) {
46  const std::string::size_type endpos = str.find_last_not_of(" \t\n\r");
47  if (std::string::npos != endpos) {
48  const int startpos = (int)str.find_first_not_of(" \t\n\r");
49  return str.substr(startpos, endpos - startpos + 1);
50  }
51  return "";
52 }
53 
54 
55 std::string
56 StringUtils::to_lower_case(std::string str) {
57  for (int i = 0; i < (int)str.length(); i++) {
58  if (str[i] >= 'A' && str[i] <= 'Z') {
59  str[i] = str[i] + 'a' - 'A';
60  }
61  }
62  return str;
63 }
64 
65 
66 std::string
67 StringUtils::latin1_to_utf8(std::string str) {
68  // inspired by http://stackoverflow.com/questions/4059775/convert-iso-8859-1-strings-to-utf-8-in-c-c
69  std::string result;
70  for (int i = 0; i < (int)str.length(); i++) {
71  const unsigned char c = str[i];
72  if (c < 128) {
73  result += c;
74  } else {
75  result += (char)(0xc2 + (c > 0xbf));
76  result += (char)((c & 0x3f) + 0x80);
77  }
78  }
79  return result;
80 }
81 
82 
83 std::string
84 StringUtils::convertUmlaute(std::string str) {
85  str = replace(str, "\xE4", "ae");
86  str = replace(str, "\xC4", "Ae");
87  str = replace(str, "\xF6", "oe");
88  str = replace(str, "\xD6", "Oe");
89  str = replace(str, "\xFC", "ue");
90  str = replace(str, "\xDC", "Ue");
91  str = replace(str, "\xDF", "ss");
92  str = replace(str, "\xC9", "E");
93  str = replace(str, "\xE9", "e");
94  str = replace(str, "\xC8", "E");
95  str = replace(str, "\xE8", "e");
96  return str;
97 }
98 
99 
100 
101 std::string
102 StringUtils::replace(std::string str, const char* what,
103  const char* by) {
104  const std::string what_tmp(what);
105  const std::string by_tmp(by);
106  std::string::size_type idx = str.find(what);
107  const int what_len = (int)what_tmp.length();
108  if (what_len > 0) {
109  const int by_len = (int)by_tmp.length();
110  while (idx != std::string::npos) {
111  str = str.replace(idx, what_len, by);
112  idx = str.find(what, idx + by_len);
113  }
114  }
115  return str;
116 }
117 
118 
119 std::string
121  std::ostringstream oss;
122  if (time < 0) {
123  oss << "-";
124  time = -time;
125  }
126  char buffer[10];
127  sprintf(buffer, "%02i:", (time / 3600));
128  oss << buffer;
129  time = time % 3600;
130  sprintf(buffer, "%02i:", (time / 60));
131  oss << buffer;
132  time = time % 60;
133  sprintf(buffer, "%02i", time);
134  oss << buffer;
135  return oss.str();
136 }
137 
138 
139 bool
140 StringUtils::startsWith(const std::string& str, const std::string prefix) {
141  return str.compare(0, prefix.length(), prefix) == 0;
142 }
143 
144 
145 bool
146 StringUtils::endsWith(const std::string& str, const std::string suffix) {
147  if (str.length() >= suffix.length()) {
148  return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
149  } else {
150  return false;
151  }
152 }
153 
154 
155 std::string
156 StringUtils::escapeXML(const std::string& orig, const bool maskDoubleHyphen) {
157  std::string result = replace(orig, "&", "&amp;");
158  result = replace(result, ">", "&gt;");
159  result = replace(result, "<", "&lt;");
160  result = replace(result, "\"", "&quot;");
161  if (maskDoubleHyphen) {
162  result = replace(result, "--", "&#45;&#45;");
163  }
164  for (char invalid = '\1'; invalid < ' '; invalid++) {
165  result = replace(result, std::string(1, invalid).c_str(), "");
166  }
167  return replace(result, "'", "&apos;");
168 }
169 
170 
171 std::string
172 StringUtils::urlEncode(const std::string& toEncode, const std::string encodeWhich) {
173  std::ostringstream out;
174 
175  for (int i = 0; i < (int)toEncode.length(); ++i) {
176  const char t = toEncode.at(i);
177 
178  if ((encodeWhich != "" && encodeWhich.find(t) == std::string::npos) ||
179  (encodeWhich == "" &&
180  ((t >= 45 && t <= 57) || // hyphen, period, slash, 0-9
181  (t >= 65 && t <= 90) || // A-Z
182  t == 95 || // underscore
183  (t >= 97 && t <= 122) || // a-z
184  t == 126)) // tilde
185  ) {
186  out << toEncode.at(i);
187  } else {
188  out << charToHex(toEncode.at(i));
189  }
190  }
191 
192  return out.str();
193 }
194 
195 std::string
196 StringUtils::urlDecode(const std::string& toDecode) {
197  std::ostringstream out;
198 
199  for (int i = 0; i < (int)toDecode.length(); ++i) {
200  if (toDecode.at(i) == '%') {
201  std::string str(toDecode.substr(i + 1, 2));
202  out << hexToChar(str);
203  i += 2;
204  } else {
205  out << toDecode.at(i);
206  }
207  }
208 
209  return out.str();
210 }
211 
212 std::string
213 StringUtils::charToHex(unsigned char c) {
214  short i = c;
215 
216  std::stringstream s;
217 
218  s << "%" << std::setw(2) << std::setfill('0') << std::hex << i;
219 
220  return s.str();
221 }
222 
223 unsigned char
224 StringUtils::hexToChar(const std::string& str) {
225  short c = 0;
226 
227  if (!str.empty()) {
228  std::istringstream in(str);
229 
230  in >> std::hex >> c;
231 
232  if (in.fail()) {
233  throw std::runtime_error("stream decode failure");
234  }
235  }
236 
237  return static_cast<unsigned char>(c);
238 }
239 
240 
241 /****************************************************************************/
static unsigned char hexToChar(const std::string &str)
static bool endsWith(const std::string &str, const std::string suffix)
Checks whether a given string ends with the suffix.
static std::string toTimeString(int time)
Builds a time string (hh:mm:ss) from the given seconds.
static std::string latin1_to_utf8(std::string str)
Transfers from Latin 1 (ISO-8859-1) to UTF-8.
Definition: StringUtils.cpp:67
static std::string urlEncode(const std::string &url, const std::string encodeWhich="")
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.
static std::string convertUmlaute(std::string str)
Converts german "Umlaute" to their latin-version.
Definition: StringUtils.cpp:84
static std::string emptyString
An empty string.
Definition: StringUtils.h:78
static std::string replace(std::string str, const char *what, const char *by)
static std::string to_lower_case(std::string str)
Transfers the content to lower case.
Definition: StringUtils.cpp:56
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:45
static std::string urlDecode(const std::string &encoded)
static std::string charToHex(unsigned char c)