1 #pragma once
2 
3 #include <algorithm>
4 #include <cctype>
5 #include <locale>
6 #include <sstream>
7 #include <string>
8 
9 namespace Dune
10 {
11   namespace Vtk
12   {
13     /// convert all characters in a string to upper case
to_upper(std::string input)14     inline std::string to_upper(std::string input)
15     {
16       for (auto& c : input)
17         c = toupper(c);
18       return input;
19     }
20 
21     /// convert all characters in a string to upper case
to_lower(std::string input)22     inline std::string to_lower(std::string input)
23     {
24       for (auto& c : input)
25         c = tolower(c);
26       return input;
27     }
28 
29     /// trim a string from the left
ltrim(std::string & str)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 
40     /// trim a string from the right
rtrim(std::string & str)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 
51     /// trim a string from both sides
trim(std::string & str)52     inline std::string& trim(std::string& str)
53     {
54       return ltrim(rtrim(str));
55     }
56 
57     /// trim a (copy of the) string from both sides
trim_copy(std::string const & str)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>
split(InputIter first,InputIter end,T const & t,Func f)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>
split(InputIter first,InputIter end,SeparatorIter s_first,SeparatorIter s_end,Func f)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 
95     /// Replace all occurences of substring `from` with `to` in source `str`.
replaceAll(std::string & str,std::string const & from,std::string const & to)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>
join(InputIter first,InputIter end,std::string sep=" ")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
124