2021-09-28 10:25:55 -04:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iterator>
|
2021-09-29 10:25:35 -04:00
|
|
|
#include <utility>
|
2021-09-30 13:15:44 -04:00
|
|
|
#include <regex>
|
2021-09-28 10:25:55 -04:00
|
|
|
|
|
|
|
std::string
|
2021-09-30 14:26:45 -04:00
|
|
|
implode(const char *const delim, std::vector<std::string> & x)
|
2021-09-28 10:25:55 -04:00
|
|
|
{
|
|
|
|
switch (x.size())
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return std::string("");
|
|
|
|
case 1:
|
|
|
|
return x[0];
|
|
|
|
default:
|
|
|
|
std::ostringstream os;
|
|
|
|
std::copy(x.begin(), x.end() - 1,
|
|
|
|
std::ostream_iterator<std::string>(os, delim));
|
|
|
|
os << *x.rbegin();
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 10:25:35 -04:00
|
|
|
std::vector<std::string>
|
|
|
|
explode(char delim, std::string const & s)
|
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
std::istringstream iss(s);
|
|
|
|
|
|
|
|
for (std::string token; std::getline(iss, token, delim); )
|
|
|
|
{
|
|
|
|
result.push_back(std::move(token));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-30 13:15:44 -04:00
|
|
|
std::string
|
2021-09-30 14:26:45 -04:00
|
|
|
preg_replace(std::string & pattern, const char *replacement, std::string & subject)
|
2021-09-30 13:15:44 -04:00
|
|
|
{
|
|
|
|
return std::regex_replace(subject, std::regex(pattern), replacement);
|
|
|
|
}
|
|
|
|
|
2021-09-28 10:25:55 -04:00
|
|
|
#if 0
|
|
|
|
#include <iostream>
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2021-09-30 14:26:45 -04:00
|
|
|
#if 1
|
2021-09-28 10:25:55 -04:00
|
|
|
std::vector<std::string> x;
|
|
|
|
|
|
|
|
for (int i = 2; i < argc; i++) {
|
|
|
|
x.push_back(argv[i]);
|
|
|
|
}
|
|
|
|
std::string res = implode(argv[1], x);
|
|
|
|
std::cout << res << "\n";
|
2021-09-29 10:25:35 -04:00
|
|
|
#endif
|
2021-09-30 14:26:45 -04:00
|
|
|
#if 1
|
2021-09-29 10:25:35 -04:00
|
|
|
auto v = explode(' ', "hello world foo bar");
|
|
|
|
|
|
|
|
for (auto e : v)
|
|
|
|
{
|
|
|
|
std::cout << e << "\n";
|
|
|
|
}
|
2021-09-30 13:15:44 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string str = "April 15, 2003";
|
|
|
|
std::string pattern = "(\\w+) (\\d+), (\\d+)";
|
|
|
|
const char *replacement = "$1,1,$3";
|
|
|
|
std::cout << preg_replace(pattern, replacement, str);
|
|
|
|
|
2021-09-29 10:25:35 -04:00
|
|
|
|
2021-09-28 10:25:55 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|