PLaSK library

Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#ifndef PLASK__STRING_H
15#define PLASK__STRING_H
16
21#include <string>
22#include <tuple>
23#include <vector>
24
25#include <boost/algorithm/string/split.hpp>
26#include <boost/algorithm/string/trim.hpp>
27#include <boost/tokenizer.hpp>
28#include <boost/units/detail/utility.hpp>
29
30#include <plask/config.hpp> //for PLASK_API
31
32 namespace plask {
33
37 struct PLASK_API Printable {
38
43 virtual void print(std::ostream& out) const = 0;
44
45 virtual ~Printable();
46
53 friend inline std::ostream& operator<<(std::ostream& out, const Printable& to_print) {
54 to_print.print(out);
55 return out;
56 }
57
62 std::string str() const;
63};
64
72 template <typename Iter>
73std::ostream& print_seq(std::ostream& out, Iter begin, Iter end, const char* sep = ", ") {
74 if (begin == end) return out;
75 out << str(*begin);
76 while (++begin != end) { out << sep << str(*begin); }
77 return out;
78}
79
87 PLASK_API std::pair<std::string, std::string> splitString2(const std::string& to_split, char splitter);
88
96 template <typename Pred>
97std::string filterChars(const std::string& str, Pred pred) {
98 std::string result;
99 for (auto c: str) if (pred(c)) result += c;
100 return result;
101}
102
110 template <typename CharReplacer>
111std::string replaceChars(const std::string& str, CharReplacer repl) {
112 std::string result;
113 result.reserve(str.size());
114 for (auto c: str) result += repl(c);
115 return result;
116}
117
123 PLASK_API std::string removedChars(const std::string& str, const std::string& chars_to_remove);
124
132 template<typename RangeT, typename PredicateT, typename SequenceSequenceT = std::vector<std::string> >
133 SequenceSequenceT splitAndTrimPred(RangeT & input, PredicateT pred, boost::algorithm::token_compress_mode_type eCompress = boost::algorithm::token_compress_off) {
134 SequenceSequenceT result;
135 boost::algorithm::split(result, input, pred, eCompress);
136 for (auto& r: result) boost::algorithm::trim(r);
137 return result;
138}
139
140 typedef boost::tokenizer< boost::escaped_list_separator<char> > split_esc_tokenizer;
141
150 PLASK_API split_esc_tokenizer splitEscIterator(const std::string& str, char splitter, char quote_char = '\'', char esc_char = '\\');
151
160 PLASK_API std::vector<std::string> splitEsc(const std::string& str, char splitter, char quote_char = '\'', char esc_char = '\\');
161
167 PLASK_API bool isCid(const char* potential_id);
168
169
171 template <typename T>
172std::string type_name() {
173 std::string demangled = boost::units::detail::demangle(typeid(T).name());
174 size_t s = demangled.rfind(':');
175 if (s == std::string::npos) s = 0; else ++s;
176 return demangled.substr(s, demangled.find('<')-s);
177}
178
179} // namespace plask
180
181#endif // STRING_H