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