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 <cstdlib>
29#include <cstring>
30#include <regex>
31#ifdef WIN32
32#define NOMINMAX
33#include <windows.h>
34#undef NOMINMAX
35#else
36#include <unistd.h>
37#endif
41#include "StringUtils.h"
42
43#define KM_PER_MILE 1.609344
44
45
46// ===========================================================================
47// static member definitions
48// ===========================================================================
49std::string StringUtils::emptyString;
50
51
52// ===========================================================================
53// method definitions
54// ===========================================================================
55std::string
56StringUtils::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
66std::string
67StringUtils::pruneZeros(const std::string& str, int max) {
68 const std::string::size_type endpos = str.find_last_not_of("0");
69 if (endpos != std::string::npos && str.back() == '0') {
70 std::string res = str.substr(0, MAX2((int)str.size() - max, (int)endpos + 1));
71 return res;
72 }
73 return str;
74}
75
76std::string
77StringUtils::to_lower_case(const std::string& str) {
78 std::string s = str;
79 std::transform(s.begin(), s.end(), s.begin(), [](char c) {
80 return (char)::tolower(c);
81 });
82 return s;
83}
84
85
86std::string
87StringUtils::to_upper_case(const std::string& str) {
88 std::string s = str;
89 std::transform(s.begin(), s.end(), s.begin(), [](char c) {
90 return (char)::toupper(c);
91 });
92 return s;
93}
94
95
96std::string
98 // inspired by http://stackoverflow.com/questions/4059775/convert-iso-8859-1-strings-to-utf-8-in-c-c
99 std::string result;
100 for (const auto& c : str) {
101 const unsigned char uc = (unsigned char)c;
102 if (uc < 128) {
103 result += uc;
104 } else {
105 result += (char)(0xc2 + (uc > 0xbf));
106 result += (char)((uc & 0x3f) + 0x80);
107 }
108 }
109 return result;
110}
111
112
113std::string
115 str = replace(str, "\xE4", "ae");
116 str = replace(str, "\xC4", "Ae");
117 str = replace(str, "\xF6", "oe");
118 str = replace(str, "\xD6", "Oe");
119 str = replace(str, "\xFC", "ue");
120 str = replace(str, "\xDC", "Ue");
121 str = replace(str, "\xDF", "ss");
122 str = replace(str, "\xC9", "E");
123 str = replace(str, "\xE9", "e");
124 str = replace(str, "\xC8", "E");
125 str = replace(str, "\xE8", "e");
126 return str;
127}
128
129
130std::string
131StringUtils::replace(std::string str, const std::string& what, const std::string& by) {
132 std::string::size_type idx = str.find(what);
133 const int what_len = (int)what.length();
134 if (what_len > 0) {
135 const int by_len = (int)by.length();
136 while (idx != std::string::npos) {
137 str = str.replace(idx, what_len, by);
138 idx = str.find(what, idx + by_len);
139 }
140 }
141 return str;
142}
143
144
145std::string
146StringUtils::substituteEnvironment(const std::string& str, const std::chrono::time_point<std::chrono::system_clock>* const timeRef) {
147 std::string s = str;
148 if (timeRef != nullptr) {
149 const std::string::size_type localTimeIndex = str.find("${LOCALTIME}");
150 const std::string::size_type utcIndex = str.find("${UTC}");
151 const bool isUTC = utcIndex != std::string::npos;
152 if (localTimeIndex != std::string::npos || isUTC) {
153 const time_t rawtime = std::chrono::system_clock::to_time_t(*timeRef);
154 char buffer [80];
155 struct tm* timeinfo = isUTC ? gmtime(&rawtime) : localtime(&rawtime);
156 strftime(buffer, 80, "%Y-%m-%d-%H-%M-%S.", timeinfo);
157 auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(*timeRef);
158 auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(*timeRef - seconds);
159 const std::string micro = buffer + toString(microseconds.count());
160 if (isUTC) {
161 s.replace(utcIndex, 6, micro);
162 } else {
163 s.replace(localTimeIndex, 12, micro);
164 }
165 }
166 }
167 const std::string::size_type pidIndex = str.find("${PID}");
168 if (pidIndex != std::string::npos) {
169#ifdef WIN32
170 s.replace(pidIndex, 6, toString(::GetCurrentProcessId()));
171#else
172 s.replace(pidIndex, 6, toString(::getpid()));
173#endif
174 }
175 if (std::getenv("SUMO_LOGO") == nullptr) {
176 s = replace(s, "${SUMO_LOGO}", "${SUMO_HOME}/data/logo/sumo-128x138.png");
177 }
178 const std::string::size_type tildeIndex = str.find("~");
179 if (tildeIndex == 0) {
180 s.replace(0, 1, "${HOME}");
181 }
182 s = replace(s, ",~", ",${HOME}");
183#ifdef WIN32
184 if (std::getenv("HOME") == nullptr) {
185 s = replace(s, "${HOME}", "${USERPROFILE}");
186 }
187#endif
188
189 // Expression for an environment variables, e.g. ${NAME}
190 // Note: - R"(...)" is a raw string literal syntax to simplify a regex declaration
191 // - .+? looks for the shortest match (non-greedy)
192 // - (.+?) defines a "subgroup" which is already stripped of the $ and {, }
193 std::regex envVarExpr(R"(\$\{(.+?)\})");
194
195 // Are there any variables in this string?
196 std::smatch match;
197 std::string strIter = s;
198
199 // Loop over the entire value string and look for variable names
200 while (std::regex_search(strIter, match, envVarExpr)) {
201 std::string varName = match[1];
202
203 // Find the variable in the environment and its value
204 std::string varValue;
205 if (std::getenv(varName.c_str()) != nullptr) {
206 varValue = std::getenv(varName.c_str());
207 }
208
209 // Replace the variable placeholder with its value in the original string
210 s = std::regex_replace(s, std::regex("\\$\\{" + varName + "\\}"), varValue);
211
212 // Continue the loop with the remainder of the string
213 strIter = match.suffix();
214 }
215 return s;
216}
217
218
219std::string
220StringUtils::isoTimeString(const std::chrono::time_point<std::chrono::system_clock>* const timeRef) {
221 std::chrono::system_clock::time_point now;
222 if (timeRef != nullptr) {
223 now = *timeRef;
224 } else {
225 now = std::chrono::system_clock::now();
226 // Support reproducible builds: if SOURCE_DATE_EPOCH is set (as done by
227 // dpkg-buildpackage and other packaging tools, see
228 // https://reproducible-builds.org/specs/source-date-epoch/), use it
229 // instead of the real current time, so that timestamps embedded in
230 // build-time generated output (e.g. --save-template) do not vary
231 // between otherwise identical builds.
232 const char* const sourceDateEpoch = std::getenv("SOURCE_DATE_EPOCH");
233 if (sourceDateEpoch != nullptr) {
234 now = std::chrono::system_clock::from_time_t(static_cast<std::time_t>(std::strtoll(sourceDateEpoch, nullptr, 10)));
235 }
236 }
237 const auto now_seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
238 const std::time_t now_c = std::chrono::system_clock::to_time_t(now);
239 const auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now - now_seconds).count();
240 std::tm local_tm = *std::localtime(&now_c);
241
242 // Get the time zone offset. Reuse now_c (not a fresh time(nullptr) call)
243 // so this stays deterministic for the same reasons as above: a separate
244 // "real now" read here would reintroduce non-reproducibility even with
245 // SOURCE_DATE_EPOCH honored above.
246 std::tm utc_tm = *std::gmtime(&now_c);
247 const double offset = std::difftime(std::mktime(&local_tm), std::mktime(&utc_tm)) / 3600.0;
248 const int hours_offset = static_cast<int>(offset);
249 const int minutes_offset = static_cast<int>((offset - hours_offset) * 60);
250
251 // Format the time
252 std::ostringstream oss;
253 char buf[32];
254 std::strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &local_tm);
255 oss << buf << "."
256 << std::setw(6) << std::setfill('0') << std::abs(microseconds)
257 << (hours_offset >= 0 ? "+" : "-")
258 << std::setw(2) << std::setfill('0') << std::abs(hours_offset) << ":"
259 << std::setw(2) << std::setfill('0') << std::abs(minutes_offset);
260 return oss.str();
261}
262
263
264bool
265StringUtils::startsWith(const std::string& str, const std::string prefix) {
266 return str.compare(0, prefix.length(), prefix) == 0;
267}
268
269
270bool
271StringUtils::endsWith(const std::string& str, const std::string suffix) {
272 if (str.length() >= suffix.length()) {
273 return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
274 } else {
275 return false;
276 }
277}
278
279
280std::string
281StringUtils::padFront(const std::string& str, int length, char padding) {
282 return std::string(MAX2(0, length - (int)str.size()), padding) + str;
283}
284
285
286std::string
287StringUtils::escapeXML(const std::string& orig, const bool maskDoubleHyphen) {
288 std::string result = replace(orig, "&", "&amp;");
289 result = replace(result, ">", "&gt;");
290 result = replace(result, "<", "&lt;");
291 result = replace(result, "\"", "&quot;");
292 if (maskDoubleHyphen) {
293 result = replace(result, "--", "&#45;&#45;");
294 }
295 for (char invalid = '\1'; invalid < ' '; invalid++) {
296 result = replace(result, std::string(1, invalid).c_str(), "");
297 }
298 return replace(result, "'", "&apos;");
299}
300
301
302std::string
303StringUtils::escapeShell(const std::string& orig) {
304 std::string result = replace(orig, "\"", "\\\"");
305 return result;
306}
307
308
309std::string
310StringUtils::urlEncode(const std::string& toEncode, const std::string encodeWhich) {
311 std::ostringstream out;
312
313 for (int i = 0; i < (int)toEncode.length(); ++i) {
314 const char t = toEncode.at(i);
315
316 if ((encodeWhich != "" && encodeWhich.find(t) == std::string::npos) ||
317 (encodeWhich == "" &&
318 ((t >= 45 && t <= 57) || // hyphen, period, slash, 0-9
319 (t >= 65 && t <= 90) || // A-Z
320 t == 95 || // underscore
321 (t >= 97 && t <= 122) || // a-z
322 t == 126)) // tilde
323 ) {
324 out << toEncode.at(i);
325 } else {
326 out << charToHex(toEncode.at(i));
327 }
328 }
329
330 return out.str();
331}
332
333
334std::string
335StringUtils::urlDecode(const std::string& toDecode) {
336 std::ostringstream out;
337
338 for (int i = 0; i < (int)toDecode.length(); ++i) {
339 if (toDecode.at(i) == '%') {
340 std::string str(toDecode.substr(i + 1, 2));
341 out << hexToChar(str);
342 i += 2;
343 } else {
344 out << toDecode.at(i);
345 }
346 }
347
348 return out.str();
349}
350
351std::string
352StringUtils::charToHex(unsigned char c) {
353 short i = c;
354
355 std::stringstream s;
356
357 s << "%" << std::setw(2) << std::setfill('0') << std::hex << i;
358
359 return s.str();
360}
361
362
363unsigned char
364StringUtils::hexToChar(const std::string& str) {
365 short c = 0;
366 if (!str.empty()) {
367 std::istringstream in(str);
368 in >> std::hex >> c;
369 if (in.fail()) {
370 throw NumberFormatException(str + " could not be interpreted as hex");
371 }
372 }
373 return static_cast<unsigned char>(c);
374}
375
376
377int
378StringUtils::toInt(const std::string& sData) {
379 long long int result = toLong(sData);
380 if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
381 throw NumberFormatException(toString(result) + " int overflow");
382 }
383 return (int)result;
384}
385
386
387bool
388StringUtils::isInt(const std::string& sData) {
389 // first check if can be converted to long int
390 if (isLong(sData)) {
391 const long long int result = toLong(sData);
392 // now check if the result is in the range of an int
393 return ((result <= std::numeric_limits<int>::max()) && (result >= std::numeric_limits<int>::min()));
394 }
395 return false;
396}
397
398
399int
400StringUtils::toIntSecure(const std::string& sData, int def) {
401 if (sData.length() == 0) {
402 return def;
403 }
404 return toInt(sData);
405}
406
407
408long long int
409StringUtils::toLong(const std::string& sData) {
410 const char* const data = sData.c_str();
411 if (data == 0 || data[0] == 0) {
412 throw EmptyData();
413 }
414 char* end;
415 errno = 0;
416#ifdef _MSC_VER
417 long long int ret = _strtoi64(data, &end, 10);
418#else
419 long long int ret = strtoll(data, &end, 10);
420#endif
421 if (errno == ERANGE) {
422 errno = 0;
423 throw NumberFormatException("(long long integer range) " + sData);
424 }
425 if ((int)(end - data) != (int)strlen(data)) {
426 throw NumberFormatException("(long long integer format) " + sData);
427 }
428 return ret;
429}
430
431
432bool
433StringUtils::isLong(const std::string& sData) {
434 const char* const data = sData.c_str();
435 if (data == 0 || data[0] == 0) {
436 return false;
437 }
438 char* end;
439 // reset errno before parsing, to keep errors
440 errno = 0;
441 // continue depending of current plattform
442#ifdef _MSC_VER
443 _strtoi64(data, &end, 10);
444#else
445 strtoll(data, &end, 10);
446#endif
447 // check out of range
448 if (errno == ERANGE) {
449 return false;
450 }
451 // check length of converted data
452 if ((int)(end - data) != (int)strlen(data)) {
453 return false;
454 }
455 return true;
456}
457
458
459int
460StringUtils::hexToInt(const std::string& sData) {
461 if (sData.length() == 0) {
462 throw EmptyData();
463 }
464 size_t idx = 0;
465 int result;
466 try {
467 if (sData[0] == '#') { // for html color codes
468 result = std::stoi(sData.substr(1), &idx, 16);
469 idx++;
470 } else {
471 result = std::stoi(sData, &idx, 16);
472 }
473 } catch (...) {
474 throw NumberFormatException("(hex integer format) " + sData);
475 }
476 if (idx != sData.length()) {
477 throw NumberFormatException("(hex integer format) " + sData);
478 }
479 return result;
480}
481
482
483bool
484StringUtils::isHex(std::string sData) {
485 if (sData.length() == 0) {
486 return false;
487 }
488 // remove the first character (for HTML color codes)
489 if (sData[0] == '#') {
490 sData = sData.substr(1);
491 }
492 const char* sDataPtr = sData.c_str();
493 char* returnPtr;
494 // reset errno
495 errno = 0;
496 // call string to long (size 16) from standard library
497 strtol(sDataPtr, &returnPtr, 16);
498 // check out of range
499 if (errno == ERANGE) {
500 return false;
501 }
502 // check if there was an error converting sDataPtr to double,
503 if (sDataPtr == returnPtr) {
504 return false;
505 }
506 // compare size of start and end points
507 if (static_cast<size_t>(returnPtr - sDataPtr) != sData.size()) {
508 return false;
509 }
510 return true;
511}
512
513
514double
515StringUtils::toDouble(const std::string& sData) {
516 if (sData.size() == 0) {
517 throw EmptyData();
518 }
519 try {
520 size_t idx = 0;
521 const double result = std::stod(sData, &idx);
522 if (idx != sData.size()) {
523 throw NumberFormatException("(double format) " + sData);
524 } else {
525 return result;
526 }
527 } catch (...) {
528 // invalid_argument or out_of_range
529 throw NumberFormatException("(double) " + sData);
530 }
531}
532
533
534bool
535StringUtils::isDouble(const std::string& sData) {
536 if (sData.size() == 0) {
537 return false;
538 }
539 const char* sDataPtr = sData.c_str();
540 char* returnPtr;
541 // reset errno
542 errno = 0;
543 // call string to double from standard library
544 strtod(sDataPtr, &returnPtr);
545 // check out of range
546 if (errno == ERANGE) {
547 return false;
548 }
549 // check if there was an error converting sDataPtr to double,
550 if (sDataPtr == returnPtr) {
551 return false;
552 }
553 // compare size of start and end points
554 if (static_cast<size_t>(returnPtr - sDataPtr) != sData.size()) {
555 return false;
556 }
557 return true;
558}
559
560
561double
562StringUtils::toDoubleSecure(const std::string& sData, const double def) {
563 if (sData.length() == 0) {
564 return def;
565 }
566 return toDouble(sData);
567}
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
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/****************************************************************************/
const std::string invalid_return< std::string >::value
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:49
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 std::string emptyString
An empty string.
Definition StringUtils.h:96
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 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