dune-vtk 2.12
Loading...
Searching...
No Matches
string.hh
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cctype>
5#include <locale>
6#include <sstream>
7#include <string>
8
9namespace Dune::Vtk
10{
13 {
14 for (auto& c : input)
15 c = char(::toupper(int(c)));
16 return input;
17 }
18
21 {
22 for (auto& c : input)
23 c = char(::tolower(int(c)));
24 return input;
25 }
26
29 {
30 auto it = std::find_if(str.begin(), str.end(), [](char ch)
31 {
32 return !std::isspace<char>(ch, std::locale::classic());
33 });
34 str.erase(str.begin() , it);
35 return str;
36 }
37
40 {
41 auto it = std::find_if(str.rbegin(), str.rend(), [](char ch)
42 {
43 return !std::isspace<char>(ch, std::locale::classic());
44 });
45 str.erase(it.base(), str.end());
46 return str;
47 }
48
51 {
52 return ltrim(rtrim(str));
53 }
54
57 {
58 auto s = str;
59 return trim(s);
60 }
61
62
63 template <class InputIter, class T, class Func>
64 void split(InputIter first, InputIter end, T const& t, Func f)
65 {
66 if (first == end)
67 return;
68
69 while (true) {
70 InputIter found = std::find(first, end, t);
71 f(first, found);
72 if (found == end)
73 break;
74 first = ++found;
75 }
76 }
77
78 template <class InputIter, class SeparatorIter, class Func>
79 void split(InputIter first, InputIter end, SeparatorIter s_first, SeparatorIter s_end, Func f)
80 {
81 if (first == end)
82 return;
83
84 while (true) {
85 InputIter found = std::find_first_of(first, end, s_first, s_end);
86 f(first, found);
87 if (found == end)
88 break;
89 first = ++found;
90 }
91 }
92
94 inline void replaceAll(std::string& str, std::string const& from, std::string const& to)
95 {
96 if (from.empty())
97 return;
98 std::size_t start_pos = 0;
99 while ((start_pos = str.find(from, start_pos)) != std::string::npos)
100 {
101 str.replace(start_pos, from.length(), to);
102 start_pos += to.length();
103 }
104 }
105
106
108 template <class InputIter>
109 std::string join (InputIter first, InputIter end, std::string sep = " ")
110 {
111 if (first == end)
112 return "";
113
115 os << *first++;
116 while (first != end)
117 os << sep << *first++;
118 return os.str();
119 }
120
121} // end namespace Dune::Vtk
Definition writer.hh:16
std::string & ltrim(std::string &str)
trim a string from the left
Definition string.hh:28
std::string to_upper(std::string input)
convert all characters in a string to upper case
Definition string.hh:12
std::string & trim(std::string &str)
trim a string from both sides
Definition string.hh:50
std::string trim_copy(std::string const &str)
trim a (copy of the) string from both sides
Definition string.hh:56
void replaceAll(std::string &str, std::string const &from, std::string const &to)
Replace all occurrences of substring from with to in source str.
Definition string.hh:94
std::string to_lower(std::string input)
convert all characters in a string to upper case
Definition string.hh:20
void split(InputIter first, InputIter end, T const &t, Func f)
Definition string.hh:64
std::string join(InputIter first, InputIter end, std::string sep=" ")
Join all entries in the range [first,end) into a single string separated by sep
Definition string.hh:109
std::string & rtrim(std::string &str)
trim a string from the right
Definition string.hh:39
iterator end()
T begin(T... args)
T empty(T... args)
T end(T... args)
T erase(T... args)
T find_first_of(T... args)
T find_if(T... args)
T rbegin(T... args)
T rend(T... args)
T replace(T... args)
T length(T... args)
T str(T... args)