Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
StringUtils.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 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#include <config.h>
24
25#include <string>
26#include <iostream>
27#include <cstdio>
28#include <cstring>
29#include <regex>
30#ifdef WIN32
31#include <windows.h>
32#else
33#include <unistd.h>
34#endif
38#include "StringUtils.h"
39
40#define KM_PER_MILE 1.609344
41
42
43// ===========================================================================
44// static member definitions
45// ===========================================================================
46std::string StringUtils::emptyString;
47
48
49// ===========================================================================
50// method definitions
51// ===========================================================================
52std::string
53StringUtils::prune(const std::string& str) {
54 const std::string::size_type endpos = str.find_last_not_of(" \t\n\r");
55 if (std::string::npos != endpos) {
56 const int startpos = (int)str.find_first_not_of(" \t\n\r");
57 return str.substr(startpos, endpos - startpos + 1);
58 }
59 return "";
60}
61
62
63std::string
64StringUtils::pruneZeros(const std::string& str, int max) {
65 const std::string::size_type endpos = str.find_last_not_of("0");
66 if (endpos != std::string::npos && str.back() == '0') {
67 std::string res = str.substr(0, MAX2((int)str.size() - max, (int)endpos + 1));
68 return res;
69 }
70 return str;
71}
72
73std::string
74StringUtils::to_lower_case(const std::string& str) {
75 std::string s = str;
76 std::transform(s.begin(), s.end(), s.begin(), [](char c) {
77 return (char)::tolower(c);
78 });
79 return s;
80}
81
82
83std::string
84StringUtils::to_upper_case(const std::string& str) {
85 std::string s = str;
86 std::transform(s.begin(), s.end(), s.begin(), [](char c) {
87 return (char)::toupper(c);
88 });
89 return s;
90}
91
92
93std::string
95 // inspired by http://stackoverflow.com/questions/4059775/convert-iso-8859-1-strings-to-utf-8-in-c-c
96 std::string result;
97 for (const auto& c : str) {
98 const unsigned char uc = (unsigned char)c;
99 if (uc < 128) {
100 result += uc;
101 } else {
102 result += (char)(0xc2 + (uc > 0xbf));
103 result += (char)((uc & 0x3f) + 0x80);
104 }
105 }
106 return result;
107}
108
109
110std::string
112 str = replace(str, "\xE4", "ae");
113 str = replace(str, "\xC4", "Ae");
114 str = replace(str, "\xF6", "oe");
115 str = replace(str, "\xD6", "Oe");
116 str = replace(str, "\xFC", "ue");
117 str = replace(str, "\xDC", "Ue");
118 str = replace(str, "\xDF", "ss");
119 str = replace(str, "\xC9", "E");
120 str = replace(str, "\xE9", "e");
121 str = replace(str, "\xC8", "E");
122 str = replace(str, "\xE8", "e");
123 return str;
124}
125
126
127std::string
128StringUtils::replace(std::string str, const std::string& what, const std::string& by) {
129 std::string::size_type idx = str.find(what);
130 const int what_len = (int)what.length();
131 if (what_len > 0) {
132 const int by_len = (int)by.length();
133 while (idx != std::string::npos) {
134 str = str.replace(idx, what_len, by);
135 idx = str.find(what, idx + by_len);
136 }
137 }
138 return str;
139}
140
141
142std::string
143StringUtils::substituteEnvironment(const std::string& str, const std::chrono::time_point<std::chrono::system_clock>* const timeRef) {
144 std::string s = str;
145 if (timeRef != nullptr) {
146 const std::string::size_type localTimeIndex = str.find("${LOCALTIME}");
147 const std::string::size_type utcIndex = str.find("${UTC}");
148 const bool isUTC = utcIndex != std::string::npos;
149 if (localTimeIndex != std::string::npos || isUTC) {
150 const time_t rawtime = std::chrono::system_clock::to_time_t(*timeRef);
151 char buffer [80];
152 struct tm* timeinfo = isUTC ? gmtime(&rawtime) : localtime(&rawtime);
153 strftime(buffer, 80, "%Y-%m-%d-%H-%M-%S.", timeinfo);
154 auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(*timeRef);
155 auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(*timeRef - seconds);
156 const std::string micro = buffer + toString(microseconds.count());
157 if (isUTC) {
158 s.replace(utcIndex, 6, micro);
159 } else {
160 s.replace(localTimeIndex, 12, micro);
161 }
162 }
163 }
164 const std::string::size_type pidIndex = str.find("${PID}");
165 if (pidIndex != std::string::npos) {
166#ifdef WIN32
167 s.replace(pidIndex, 6, toString(::GetCurrentProcessId()));
168#else
169 s.replace(pidIndex, 6, toString(::getpid()));
170#endif
171 }
172 if (std::getenv("SUMO_LOGO") == nullptr) {
173 s = replace(s, "${SUMO_LOGO}", "${SUMO_HOME}/data/logo/sumo-128x138.png");
174 }
175 const std::string::size_type tildeIndex = str.find("~");
176 if (tildeIndex == 0) {
177 s.replace(0, 1, "${HOME}");
178 }
179 s = replace(s, ",~", ",${HOME}");
180#ifdef WIN32
181 if (std::getenv("HOME") == nullptr) {
182 s = replace(s, "${HOME}", "${USERPROFILE}");
183 }
184#endif
185
186 // Expression for an environment variables, e.g. ${NAME}
187 // Note: - R"(...)" is a raw string literal syntax to simplify a regex declaration
188 // - .+? looks for the shortest match (non-greedy)
189 // - (.+?) defines a "subgroup" which is already stripped of the $ and {, }
190 std::regex envVarExpr(R"(\$\{(.+?)\})");
191
192 // Are there any variables in this string?
193 std::smatch match;
194 std::string strIter = s;
195
196 // Loop over the entire value string and look for variable names
197 while (std::regex_search(strIter, match, envVarExpr)) {
198 std::string varName = match[1];
199
200 // Find the variable in the environment and its value
201 std::string varValue;
202 if (std::getenv(varName.c_str()) != nullptr) {
203 varValue = std::getenv(varName.c_str());
204 }
205
206 // Replace the variable placeholder with its value in the original string
207 s = std::regex_replace(s, std::regex("\\$\\{" + varName + "\\}"), varValue);
208
209 // Continue the loop with the remainder of the string
210 strIter = match.suffix();
211 }
212 return s;
213}
214
215
216std::string
217StringUtils::isoTimeString(const std::chrono::time_point<std::chrono::system_clock>* const timeRef) {
218 const std::chrono::system_clock::time_point now = timeRef == nullptr ? std::chrono::system_clock::now() : *timeRef;
219 const auto now_seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
220 const std::time_t now_c = std::chrono::system_clock::to_time_t(now);
221 const auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now - now_seconds).count();
222 std::tm local_tm = *std::localtime(&now_c);
223
224 // Get the time zone offset
225 std::time_t utc_time = std::time(nullptr);
226 std::tm utc_tm = *std::gmtime(&utc_time);
227 const double offset = std::difftime(std::mktime(&local_tm), std::mktime(&utc_tm)) / 3600.0;
228 const int hours_offset = static_cast<int>(offset);
229 const int minutes_offset = static_cast<int>((offset - hours_offset) * 60);
230
231 // Format the time
232 std::ostringstream oss;
233 char buf[32];
234 std::strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &local_tm);
235 oss << buf << "."
236 << std::setw(6) << std::setfill('0') << std::abs(microseconds)
237 << (hours_offset >= 0 ? "+" : "-")
238 << std::setw(2) << std::setfill('0') << std::abs(hours_offset) << ":"
239 << std::setw(2) << std::setfill('0') << std::abs(minutes_offset);
240 return oss.str();
241}
242
243
244bool
245StringUtils::startsWith(const std::string& str, const std::string prefix) {
246 return str.compare(0, prefix.length(), prefix) == 0;
247}
248
249
250bool
251StringUtils::endsWith(const std::string& str, const std::string suffix) {
252 if (str.length() >= suffix.length()) {
253 return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
254 } else {
255 return false;
256 }
257}
258
259
260std::string
261StringUtils::padFront(const std::string& str, int length, char padding) {
262 return std::string(MAX2(0, length - (int)str.size()), padding) + str;
263}
264
265
266std::string
267StringUtils::escapeXML(const std::string& orig, const bool maskDoubleHyphen) {
268 std::string result = replace(orig, "&", "&amp;");
269 result = replace(result, ">", "&gt;");
270 result = replace(result, "<", "&lt;");
271 result = replace(result, "\"", "&quot;");
272 if (maskDoubleHyphen) {
273 result = replace(result, "--", "&#45;&#45;");
274 }
275 for (char invalid = '\1'; invalid < ' '; invalid++) {
276 result = replace(result, std::string(1, invalid).c_str(), "");
277 }
278 return replace(result, "'", "&apos;");
279}
280
281
282std::string
283StringUtils::escapeShell(const std::string& orig) {
284 return replace(orig, "\"", "\\\"");
285}
286
287
288std::string
289StringUtils::escapeCSV(const std::string& orig, const char separator, const char quote) {
290 const std::string chars{separator, quote};
291 if (orig.find_first_of(chars) == std::string::npos) {
292 return orig;
293 }
294 const std::string quoteStr{quote};
295 return quoteStr + replace(orig, quoteStr, {'\\', quote}) + quoteStr;
296}
297
298
299std::string
300StringUtils::urlEncode(const std::string& toEncode, const std::string encodeWhich) {
301 std::ostringstream out;
302
303 for (int i = 0; i < (int)toEncode.length(); ++i) {
304 const char t = toEncode.at(i);
305
306 if ((encodeWhich != "" && encodeWhich.find(t) == std::string::npos) ||
307 (encodeWhich == "" &&
308 ((t >= 45 && t <= 57) || // hyphen, period, slash, 0-9
309 (t >= 65 && t <= 90) || // A-Z
310 t == 95 || // underscore
311 (t >= 97 && t <= 122) || // a-z
312 t == 126)) // tilde
313 ) {
314 out << toEncode.at(i);
315 } else {
316 out << charToHex(toEncode.at(i));
317 }
318 }
319
320 return out.str();
321}
322
323
324std::string
325StringUtils::urlDecode(const std::string& toDecode) {
326 std::ostringstream out;
327
328 for (int i = 0; i < (int)toDecode.length(); ++i) {
329 if (toDecode.at(i) == '%') {
330 std::string str(toDecode.substr(i + 1, 2));
331 out << hexToChar(str);
332 i += 2;
333 } else {
334 out << toDecode.at(i);
335 }
336 }
337
338 return out.str();
339}
340
341std::string
342StringUtils::charToHex(unsigned char c) {
343 short i = c;
344
345 std::stringstream s;
346
347 s << "%" << std::setw(2) << std::setfill('0') << std::hex << i;
348
349 return s.str();
350}
351
352
353unsigned char
354StringUtils::hexToChar(const std::string& str) {
355 short c = 0;
356 if (!str.empty()) {
357 std::istringstream in(str);
358 in >> std::hex >> c;
359 if (in.fail()) {
360 throw NumberFormatException(str + " could not be interpreted as hex");
361 }
362 }
363 return static_cast<unsigned char>(c);
364}
365
366
367int
368StringUtils::toInt(const std::string& sData) {
369 long long int result = toLong(sData);
370 if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
371 throw NumberFormatException(toString(result) + " int overflow");
372 }
373 return (int)result;
374}
375
376
377bool
378StringUtils::isInt(const std::string& sData) {
379 // first check if can be converted to long int
380 if (isLong(sData)) {
381 const long long int result = toLong(sData);
382 // now check if the result is in the range of an int
383 return ((result <= std::numeric_limits<int>::max()) && (result >= std::numeric_limits<int>::min()));
384 }
385 return false;
386}
387
388
389int
390StringUtils::toIntSecure(const std::string& sData, int def) {
391 if (sData.length() == 0) {
392 return def;
393 }
394 return toInt(sData);
395}
396
397
398long long int
399StringUtils::toLong(const std::string& sData) {
400 const char* const data = sData.c_str();
401 if (data == 0 || data[0] == 0) {
402 throw EmptyData();
403 }
404 char* end;
405 errno = 0;
406#ifdef _MSC_VER
407 long long int ret = _strtoi64(data, &end, 10);
408#else
409 long long int ret = strtoll(data, &end, 10);
410#endif
411 if (errno == ERANGE) {
412 errno = 0;
413 throw NumberFormatException("(long long integer range) " + sData);
414 }
415 if ((int)(end - data) != (int)strlen(data)) {
416 throw NumberFormatException("(long long integer format) " + sData);
417 }
418 return ret;
419}
420
421
422bool
423StringUtils::isLong(const std::string& sData) {
424 const char* const data = sData.c_str();
425 if (data == 0 || data[0] == 0) {
426 return false;
427 }
428 char* end;
429 // reset errno before parsing, to keep errors
430 errno = 0;
431 // continue depending of current plattform
432#ifdef _MSC_VER
433 _strtoi64(data, &end, 10);
434#else
435 strtoll(data, &end, 10);
436#endif
437 // check out of range
438 if (errno == ERANGE) {
439 return false;
440 }
441 // check length of converted data
442 if ((int)(end - data) != (int)strlen(data)) {
443 return false;
444 }
445 return true;
446}
447
448
449int
450StringUtils::hexToInt(const std::string& sData) {
451 if (sData.length() == 0) {
452 throw EmptyData();
453 }
454 size_t idx = 0;
455 int result;
456 try {
457 if (sData[0] == '#') { // for html color codes
458 result = std::stoi(sData.substr(1), &idx, 16);
459 idx++;
460 } else {
461 result = std::stoi(sData, &idx, 16);
462 }
463 } catch (...) {
464 throw NumberFormatException("(hex integer format) " + sData);
465 }
466 if (idx != sData.length()) {
467 throw NumberFormatException("(hex integer format) " + sData);
468 }
469 return result;
470}
471
472
473bool
474StringUtils::isHex(std::string sData) {
475 if (sData.length() == 0) {
476 return false;
477 }
478 // remove the first character (for HTML color codes)
479 if (sData[0] == '#') {
480 sData = sData.substr(1);
481 }
482 const char* sDataPtr = sData.c_str();
483 char* returnPtr;
484 // reset errno
485 errno = 0;
486 // call string to long (size 16) from standard library
487 strtol(sDataPtr, &returnPtr, 16);
488 // check out of range
489 if (errno == ERANGE) {
490 return false;
491 }
492 // check if there was an error converting sDataPtr to double,
493 if (sDataPtr == returnPtr) {
494 return false;
495 }
496 // compare size of start and end points
497 if (static_cast<size_t>(returnPtr - sDataPtr) != sData.size()) {
498 return false;
499 }
500 return true;
501}
502
503
504double
505StringUtils::toDouble(const std::string& sData) {
506 if (sData.size() == 0) {
507 throw EmptyData();
508 }
509 try {
510 size_t idx = 0;
511 const double result = std::stod(sData, &idx);
512 if (idx != sData.size()) {
513 throw NumberFormatException("(double format) " + sData);
514 } else {
515 return result;
516 }
517 } catch (...) {
518 // invalid_argument or out_of_range
519 throw NumberFormatException("(double) " + sData);
520 }
521}
522
523
524bool
525StringUtils::isDouble(const std::string& sData) {
526 if (sData.size() == 0) {
527 return false;
528 }
529 const char* sDataPtr = sData.c_str();
530 char* returnPtr;
531 // reset errno
532 errno = 0;
533 // call string to double from standard library
534 strtod(sDataPtr, &returnPtr);
535 // check out of range
536 if (errno == ERANGE) {
537 return false;
538 }
539 // check if there was an error converting sDataPtr to double,
540 if (sDataPtr == returnPtr) {
541 return false;
542 }
543 // compare size of start and end points
544 if (static_cast<size_t>(returnPtr - sDataPtr) != sData.size()) {
545 return false;
546 }
547 return true;
548}
549
550
551double
552StringUtils::toDoubleSecure(const std::string& sData, const double def) {
553 if (sData.length() == 0) {
554 return def;
555 }
556 return toDouble(sData);
557}
558
559
560double
561StringUtils::toDoubleAllowTime(const std::string& sData) {
562 if (sData.find(":") == std::string::npos) {
563 return toDouble(sData);
564 } else if (isTime(sData)) {
565 return STEPS2TIME(string2time(sData));
566 }
567 throw NumberFormatException("(double) " + sData);
568}
569
570bool
571StringUtils::toBool(const std::string& sData) {
572 if (sData.length() == 0) {
573 throw EmptyData();
574 }
575 const std::string s = to_lower_case(sData);
576 if (s == "1" || s == "yes" || s == "true" || s == "on" || s == "x" || s == "t") {
577 return true;
578 }
579 if (s == "0" || s == "no" || s == "false" || s == "off" || s == "-" || s == "f") {
580 return false;
581 }
582 throw BoolFormatException(s);
583}
584
585
586bool
587StringUtils::isBool(const std::string& sData) {
588 if (sData.length() == 0) {
589 return false;
590 }
591 const std::string s = to_lower_case(sData);
592 // check true values
593 if (s == "1" || s == "yes" || s == "true" || s == "on" || s == "x" || s == "t") {
594 return true;
595 }
596 // check false values
597 if (s == "0" || s == "no" || s == "false" || s == "off" || s == "-" || s == "f") {
598 return true;
599 }
600 // no valid true or false values
601 return false;
602}
603
604
606StringUtils::toVersion(const std::string& sData) {
607 std::vector<std::string> parts = StringTokenizer(sData, ".").getVector();
608 return MMVersion(toInt(parts.front()), toDouble(parts.back()));
609}
610
611
612double
613StringUtils::parseDist(const std::string& sData) {
614 if (sData.size() == 0) {
615 throw EmptyData();
616 }
617 try {
618 size_t idx = 0;
619 const double result = std::stod(sData, &idx);
620 if (idx != sData.size()) {
621 const std::string unit = prune(sData.substr(idx));
622 if (unit == "m" || unit == "metre" || unit == "meter" || unit == "metres" || unit == "meters") {
623 return result;
624 }
625 if (unit == "km" || unit == "kilometre" || unit == "kilometer" || unit == "kilometres" || unit == "kilometers") {
626 return result * 1000.;
627 }
628 if (unit == "mi" || unit == "mile" || unit == "miles") {
629 return result * 1000. * KM_PER_MILE;
630 }
631 if (unit == "nmi") {
632 return result * 1852.;
633 }
634 if (unit == "ft" || unit == "foot" || unit == "feet") {
635 return result * 12. * 0.0254;
636 }
637 if (unit == "\"" || unit == "in" || unit == "inch" || unit == "inches") {
638 return result * 0.0254;
639 }
640 if (unit[0] == '\'') {
641 double inches = 12 * result;
642 if (unit.length() > 1) {
643 inches += std::stod(unit.substr(1), &idx);
644 if (unit.substr(idx) == "\"") {
645 return inches * 0.0254;
646 }
647 }
648 }
649 throw NumberFormatException("(distance format) " + sData);
650 } else {
651 return result;
652 }
653 } catch (...) {
654 // invalid_argument or out_of_range
655 throw NumberFormatException("(double) " + sData);
656 }
657}
658
659
660double
661StringUtils::parseSpeed(const std::string& sData, const bool defaultKmph) {
662 if (sData.size() == 0) {
663 throw EmptyData();
664 }
665 try {
666 size_t idx = 0;
667 const double result = std::stod(sData, &idx);
668 if (idx != sData.size()) {
669 const std::string unit = prune(sData.substr(idx));
670 if (unit == "km/h" || unit == "kph" || unit == "kmh" || unit == "kmph") {
671 return result / 3.6;
672 }
673 if (unit == "m/s") {
674 return result;
675 }
676 if (unit == "mph") {
677 return result * KM_PER_MILE / 3.6;
678 }
679 if (unit == "knots") {
680 return result * 1.852 / 3.6;
681 }
682 throw NumberFormatException("(speed format) " + sData);
683 } else {
684 return defaultKmph ? result / 3.6 : result;
685 }
686 } catch (...) {
687 // invalid_argument or out_of_range
688 throw NumberFormatException("(double) " + sData);
689 }
690}
691
692
693
694std::string
695StringUtils::trim_left(const std::string s, const std::string& t) {
696 std::string result = s;
697 result.erase(0, s.find_first_not_of(t));
698 return result;
699}
700
701std::string
702StringUtils::trim_right(const std::string s, const std::string& t) {
703 std::string result = s;
704 result.erase(s.find_last_not_of(t) + 1);
705 return result;
706}
707
708std::string
709StringUtils::trim(const std::string s, const std::string& t) {
710 return trim_right(trim_left(s, t), t);
711}
712
713
714std::string
715StringUtils::wrapText(const std::string s, int width) {
716 std::vector<std::string> parts = StringTokenizer(s).getVector();
717 std::string result;
718 std::string line;
719 bool firstLine = true;
720 bool firstWord = true;
721 for (std::string p : parts) {
722 if ((int)(line.size() + p.size()) < width || firstWord) {
723 if (firstWord) {
724 firstWord = false;
725 } else {
726 line += " ";
727 }
728 line += p;
729 } else {
730 if (firstLine) {
731 firstLine = false;
732 } else {
733 result += "\n";
734 }
735 result += line;
736 line.clear();
737 line += p;
738 }
739 }
740 if (line.size() > 0) {
741 if (firstLine) {
742 firstLine = false;
743 } else {
744 result += "\n";
745 }
746 result += line;
747 }
748 return result;
749}
750
751
752std::string
753StringUtils::adjustDecimalValue(double value, int precision) {
754 // obtain value in string format with 20 decimals precision
755 auto valueStr = toString(value, precision);
756 // now clear all zeros
757 while (valueStr.size() > 1) {
758 if (valueStr.back() == '0') {
759 valueStr.pop_back();
760 } else if (valueStr.back() == '.') {
761 valueStr.pop_back();
762 return valueStr;
763 } else {
764 return valueStr;
765 }
766 }
767 return valueStr;
768}
769
770
771/****************************************************************************/
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition SUMOTime.cpp:47
bool isTime(const std::string &r)
check if the given string is a valid time
Definition SUMOTime.cpp:70
#define STEPS2TIME(x)
Definition SUMOTime.h:58
std::pair< int, double > MMVersion
(M)ajor/(M)inor version for written networks and default version for loading
Definition StdDefs.h:71
T MAX2(T a, T b)
Definition StdDefs.h:86
#define KM_PER_MILE
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
std::vector< std::string > getVector()
return vector of strings
static std::string pruneZeros(const std::string &str, int max)
Removes trailing zeros (at most 'max')
static std::string urlEncode(const std::string &url, const std::string encodeWhich="")
encode url (stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/)
static bool isDouble(const std::string &sData)
check if the given sData can be conveted to double
static MMVersion toVersion(const std::string &sData)
parse a (network) version string
static bool isBool(const std::string &sData)
check if the given value can be converted to bool
static std::string to_upper_case(const std::string &str)
Transfers the content to upper case.
static std::string charToHex(unsigned char c)
char to hexadecimal
static std::string urlDecode(const std::string &encoded)
decode url (stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/)
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 trim(const std::string s, const std::string &t=" \t\n")
remove leading and trailing whitespace
static std::string to_lower_case(const std::string &str)
Transfers the content to lower case.
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 escapeShell(const std::string &orig)
Escape special characters with backslash.
static std::string replace(std::string str, const std::string &what, const std::string &by)
Replaces all occurrences of the second string by the third string within the first string.
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 bool isHex(std::string sData)
check if the given string can be converted to hex
static std::string latin1_to_utf8(std::string str)
Transfers from Latin 1 (ISO-8859-1) to UTF-8.
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
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.
static double parseDist(const std::string &sData)
parse a distance, length or width value with a unit
static std::string adjustDecimalValue(double value, int precision)
write with maximum precision if needed but remove trailing zeros
static unsigned char hexToChar(const std::string &str)
hexadecimal to char
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
static std::string wrapText(const std::string s, int width)
remove leading and trailing whitespace
static double parseSpeed(const std::string &sData, const bool defaultKmph=true)
parse a speed value with a unit
static double toDoubleAllowTime(const std::string &sData)
converts a string into a double value. Values in human-readable-time format (see string2time) are per...
static std::string emptyString
An empty string.
static bool endsWith(const std::string &str, const std::string suffix)
Checks whether a given string ends with the suffix.
static std::string substituteEnvironment(const std::string &str, const std::chrono::time_point< std::chrono::system_clock > *const timeRef=nullptr)
Replaces an environment variable with its value (similar to bash); syntax for a variable is ${NAME}.
static bool isLong(const std::string &sData)
Check if the given sData can be converted to long.
static int toIntSecure(const std::string &sData, int def)
converts a string into the integer value described by it
static std::string isoTimeString(const std::chrono::time_point< std::chrono::system_clock > *const timeRef=nullptr)
Returns an ISO8601 formatted time string with microsecond precision.
static std::string escapeCSV(const std::string &orig, const char separator, const char quote='"')
Adds quotes to strings containing the separator or the quote char and escapes the quote char with a b...
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 isInt(const std::string &sData)
check if the given sData can be converted to int
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter