dune-vtk 2.12
Loading...
Searching...
No Matches
string.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3#pragma once
4
5#include <algorithm>
6#include <cctype>
7#include <locale>
8#include <sstream>
9#include <string>
10
11namespace Dune::Vtk
12{
15 {
16 for (auto& c : input)
17 c = char(::toupper(int(c)));
18 return input;
19 }
20
23 {
24 for (auto& c : input)
25 c = char(::tolower(int(c)));
26 return input;
27 }
28
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
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
53 {
54 return ltrim(rtrim(str));
55 }
56
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>
82 {
83 if (first == end)
84 return;
85
86 while (true) {
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;
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
110 template <class InputIter>
112 {
113 if (first == end)
114 return "";
115
117 os << *first++;
118 while (first != end)
119 os << sep << *first++;
120 return os.str();
121 }
122
123} // end namespace Dune::Vtk
Definition datacollector.hh:19
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 occurrences 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=" ")
Join all entries in the range [first,end) into a single string separated by sep
Definition string.hh:111
std::string & rtrim(std::string &str)
trim a string from the right
Definition string.hh:41
iterator end()
Implementation of DataCollector for cells, with continuous data.
Definition celltypedatacollector.hh:20
T begin(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)