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