dune-vtk 2.8
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
10{
11 namespace Vtk
12 {
14 inline std::string to_upper(std::string input)
15 {
16 for (auto& c : input)
17 c = toupper(c);
18 return input;
19 }
20
22 inline std::string to_lower(std::string input)
23 {
24 for (auto& c : input)
25 c = tolower(c);
26 return input;
27 }
28
30 inline std::string& ltrim(std::string& str)
31 {
32 auto it = std::find_if(str.begin(), str.end(), [](char ch)
33 {
34 return !std::isspace<char>(ch, std::locale::classic());
35 });
36 str.erase(str.begin() , it);
37 return str;
38 }
39
41 inline std::string& rtrim(std::string& str)
42 {
43 auto it = std::find_if(str.rbegin(), str.rend(), [](char ch)
44 {
45 return !std::isspace<char>(ch, std::locale::classic());
46 });
47 str.erase(it.base(), str.end());
48 return str;
49 }
50
52 inline std::string& trim(std::string& str)
53 {
54 return ltrim(rtrim(str));
55 }
56
58 inline std::string trim_copy(std::string const& str)
59 {
60 auto s = str;
61 return trim(s);
62 }
63
64
65 template <class InputIter, class T, class Func>
66 void split(InputIter first, InputIter end, T const& t, Func f)
67 {
68 if (first == end)
69 return;
70
71 while (true) {
72 InputIter found = std::find(first, end, t);
73 f(first, found);
74 if (found == end)
75 break;
76 first = ++found;
77 }
78 }
79
80 template <class InputIter, class SeparatorIter, class Func>
81 void split(InputIter first, InputIter end, SeparatorIter s_first, SeparatorIter s_end, Func f)
82 {
83 if (first == end)
84 return;
85
86 while (true) {
87 InputIter found = std::find_first_of(first, end, s_first, s_end);
88 f(first, found);
89 if (found == end)
90 break;
91 first = ++found;
92 }
93 }
94
96 inline void replaceAll(std::string& str, std::string const& from, std::string const& to)
97 {
98 if (from.empty())
99 return;
100 std::size_t start_pos = 0;
101 while ((start_pos = str.find(from, start_pos)) != std::string::npos)
102 {
103 str.replace(start_pos, from.length(), to);
104 start_pos += to.length();
105 }
106 }
107
108
109 template <class InputIter>
110 std::string join (InputIter first, InputIter end, std::string sep = " ")
111 {
112 if (first == end)
113 return "";
114
115 std::ostringstream os;
116 os << *first++;
117 while (first != end)
118 os << sep << *first++;
119 return os.str();
120 }
121
122 } // end namespace Vtk
123} // end namspace Dune
Definition: writer.hh:13
std::string & ltrim(std::string &str)
trim a string from the left
Definition: string.hh:30
std::string to_upper(std::string input)
convert all characters in a string to upper case
Definition: string.hh:14
std::string & trim(std::string &str)
trim a string from both sides
Definition: string.hh:52
std::string trim_copy(std::string const &str)
trim a (copy of the) string from both sides
Definition: string.hh:58
void replaceAll(std::string &str, std::string const &from, std::string const &to)
Replace all occurences of substring from with to in source str.
Definition: string.hh:96
std::string to_lower(std::string input)
convert all characters in a string to upper case
Definition: string.hh:22
void split(InputIter first, InputIter end, T const &t, Func f)
Definition: string.hh:66
std::string join(InputIter first, InputIter end, std::string sep=" ")
Definition: string.hh:110
std::string & rtrim(std::string &str)
trim a string from the right
Definition: string.hh:41