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