VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
StringUtil.cpp
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34#include "StringUtil.h"
35
36#include <string.h>
37#include <regex>
38#include <sstream>
39
40std::string StringUtil::findAndReplace(const std::string &source, const std::string &find, const std::string &replace)
41{
42 std::string output = source;
43 size_t j;
44 for (; (j = output.find(find)) != std::string::npos;)
45 output.replace(j, find.length(), replace);
46 return output;
47}
48
49std::string StringUtil::makeUpper(const std::string &instring)
50{
51 std::string output = instring;
52 transform(output.begin(), output.end(), output.begin(), ::toupper);
53 return output;
54}
55
56std::string StringUtil::makeLower(const std::string &instring)
57{
58 std::string output = instring;
59 transform(output.begin(), output.end(), output.begin(), ::tolower);
60 return output;
61}
62
63bool StringUtil::contains(const std::string &source, const char *find) { return (0 != strstr(source.c_str(), find)); }
64
65std::string StringUtil::pad(const std::string &input, char pad, int length)
66{
67 std::string outstring = input;
68 for (int i = (int)outstring.length(); i < length; ++i)
69 outstring += pad;
70 return outstring;
71}
72
73std::string StringUtil::trim(const std::string &input, const std::string &trim /*= std::string(" \t\n")*/)
74{
75 if (input.size() == 0)
76 return input;
77 std::string temp = "";
78 std::string::size_type begpos = input.find_first_not_of(trim);
79 if (begpos == std::string::npos) {
80 return temp;
81 } else {
82 std::string::size_type endpos = input.find_last_not_of(trim);
83 temp = input.substr(begpos, endpos - begpos + 1);
84 }
85 return temp;
86}
87
88int StringUtil::toInt(const std::string &input) { return std::stoi(input); }
89
90float StringUtil::toFloat(const std::string &input) { return std::stof(input); }
91
92double StringUtil::toDouble(const std::string &input) { return std::stod(input); }
93
94bool StringUtil::toBool(const std::string &input)
95{
96 bool b{ false };
97 std::string trimmedInput = trim(input);
98 if (!toBool(b, trimmedInput, std::boolalpha))
99 throw "StringUtils::toBool() - Not a bool: " + trimmedInput;
100 return b;
101}
102
103bool StringUtil::toBool(bool &t, const std::string &input, std::ios_base &(*f)(std::ios_base &))
104{
105 std::istringstream iss(input);
106 return !(iss >> f >> t).fail();
107}
108
109std::vector<std::string> split(const std::string &str, char delim = ' ')
110{
111 std::stringstream ss(str);
112 std::string token;
113 std::vector<std::string> list;
114 while (std::getline(ss, token, delim)) {
115 list.push_back(token);
116 }
117 return list;
118}
119
120std::vector<std::string> StringUtil::split(const std::string &input, const std::string &delim /*= " "*/)
121{
122 std::stringstream ss;
123 ss << "[" << delim << "]";
124
125 std::regex re(ss.str());
126 std::sregex_token_iterator first{ input.begin(), input.end(), re, -1 },
127 last; // the '-1' is what makes the regex split (-1 := what was not matched)
128 std::vector<std::string> tokens{ first, last };
129 tokens.erase(std::remove_if(tokens.begin(), tokens.end(), [](std::string &token) { return token.empty(); }),
130 tokens.end());
131
132 return tokens;
133}
134
135std::vector<int> StringUtil::toIntVector(const std::string &input)
136{
137 std::vector<int> v;
138 std::vector<std::string> inputEntries;
139 inputEntries = split(input, " \n\t");
140 for (std::string entry : inputEntries)
141 if (entry != "")
142 v.push_back(toInt(entry));
143 return v;
144}
145
146std::vector<unsigned int> StringUtil::toUintVector(const std::string &input)
147{
148 std::vector<unsigned int> v;
149 std::vector<std::string> inputEntries;
150 inputEntries = split(input, " \n\t");
151 for (std::string entry : inputEntries)
152 if (entry != "")
153 v.push_back(toInt(entry));
154 return v;
155}
156
157std::vector<bool> StringUtil::toBoolVector(const std::string &input)
158{
159 std::vector<bool> v;
160 std::vector<std::string> inputEntries;
161 inputEntries = split(input, " \n\t");
162 for (std::string entry : inputEntries) {
163 bool b{ false };
164 std::string trimmedInput = trim(input);
165 if (toBool(b, trimmedInput, std::noboolalpha))
166 v.push_back(b);
167 }
168 return v;
169}
170
171std::vector<std::string> StringUtil::toStringVector(const std::string &input) { return split(input, " \n\t"); }
172
173std::vector<double> StringUtil::toDoubleVector(const std::string &input)
174{
175 std::vector<double> v;
176 std::vector<std::string> inputEntries;
177 inputEntries = split(input, " \n\t");
178 for (std::string entry : inputEntries)
179 if (entry != "")
180 v.push_back(toDouble(entry));
181 return v;
182}
183
184template <typename T>
185std::string StringUtil::toString(const T &t)
186{
187 std::ostringstream stream;
188 stream << t;
189 return stream.str();
190}
191
192template std::string StringUtil::toString<int>(const int &t);
193
194bool StringUtil::endsWith(const std::string &input, const std::string &end)
195{
196 if (input.length() >= end.length()) {
197 return (0 == input.compare(input.length() - end.length(), end.length(), end));
198 } else {
199 return false;
200 }
201}
static std::string trim(const std::string &input, const std::string &trim=std::string(" \t\n"))
static std::vector< double > toDoubleVector(const std::string &s)
static std::vector< int > toIntVector(const std::string &s)
static std::string makeUpper(const std::string &instring)
static std::vector< unsigned int > toUintVector(const std::string &s)
static std::string makeLower(const std::string &instring)
std::vector< std::string > split(const std::string &str, char delim=' ')
static std::vector< std::string > split(const std::string &input, const std::string &delim=" ")
static bool endsWith(const std::string &input, const std::string &end)
static std::string pad(const std::string &input, char pad, int length)
static int toInt(const std::string &input)
static bool toBool(const std::string &input)
static std::string findAndReplace(const std::string &source, const std::string &find, const std::string &replace)
static double toDouble(const std::string &input)
static std::vector< bool > toBoolVector(const std::string &s)
static bool contains(const std::string &source, const char *find)
static float toFloat(const std::string &input)
static std::string toString(const T &t)
static std::vector< std::string > toStringVector(const std::string &s)
std::shared_ptr< T > SPtr