13 #ifndef __STOUT_STRINGS_HPP__
14 #define __STOUT_STRINGS_HPP__
40 inline std::string
remove(
41 const std::string& from,
42 const std::string& substring,
45 std::string result = from;
48 if (from.find(substring) == 0) {
49 result = from.substr(substring.size());
52 if (from.rfind(substring) == from.size() - substring.size()) {
53 result = from.substr(0, from.size() - substring.size());
57 while ((index = result.find(substring)) != std::string::npos) {
58 result = result.erase(index, substring.size());
67 const std::string& from,
75 start = from.find_first_not_of(chars);
76 end = from.find_last_not_of(chars);
78 start = from.find_first_not_of(chars);
80 end = from.find_last_not_of(chars);
84 if (start == std::string::npos) {
90 size_t length = std::string::npos;
93 if (end.
isSome() && end.
get() != std::string::npos) {
97 return from.substr(start, length);
104 const std::string& from,
105 const std::string& chars)
113 const std::string& s,
114 const std::string& from,
115 const std::string& to)
117 std::string result = s;
124 while ((index = result.find(from, index)) != std::string::npos) {
125 result.replace(index, from.length(), to);
126 index += to.length();
139 const std::string& s,
140 const std::string& delims,
143 if (maxTokens.isSome() && maxTokens.get() == 0) {
147 std::vector<std::string> tokens;
151 size_t nonDelim = s.find_first_not_of(delims, offset);
153 if (nonDelim == std::string::npos) {
157 size_t delim = s.find_first_of(delims, nonDelim);
161 if (delim == std::string::npos ||
162 (maxTokens.isSome() && tokens.size() == maxTokens.get() - 1)) {
163 tokens.push_back(s.substr(nonDelim));
167 tokens.push_back(s.substr(nonDelim, delim - nonDelim));
183 inline std::vector<std::string>
split(
184 const std::string& s,
185 const std::string& delims,
188 if (maxTokens.isSome() && maxTokens.get() == 0) {
192 std::vector<std::string> tokens;
196 size_t next = s.find_first_of(delims, offset);
200 if (next == std::string::npos ||
201 (maxTokens.isSome() && tokens.size() == maxTokens.get() - 1)) {
202 tokens.push_back(s.substr(offset));
206 tokens.push_back(s.substr(offset, next - offset));
222 inline std::map<std::string, std::vector<std::string>>
pairs(
223 const std::string& s,
224 const std::string& delims1,
225 const std::string& delims2)
227 std::map<std::string, std::vector<std::string>> result;
229 const std::vector<std::string> tokens =
tokenize(s, delims1);
230 foreach (
const std::string& token, tokens) {
231 const std::vector<std::string>
pairs =
tokenize(token, delims2);
232 if (pairs.size() == 2) {
233 result[pairs[0]].push_back(pairs[1]);
244 std::stringstream& stream,
245 const std::string& value)
253 std::stringstream& stream,
262 std::stringstream& stream,
270 template <
typename T>
272 std::stringstream& stream,
275 stream << ::stringify(std::forward<T>(value));
280 template <
typename T>
282 std::stringstream& stream,
283 const std::string& separator,
286 return append(stream, std::forward<T>(tail));
290 template <
typename THead,
typename... TTail>
292 std::stringstream& stream,
293 const std::string& separator,
297 append(stream, std::forward<THead>(head)) << separator;
305 template <
typename... T>
307 std::stringstream& stream,
308 const std::string& separator,
320 template <
typename THead1,
typename THead2,
typename... TTail>
322 const std::string& separator,
327 std::stringstream stream;
331 std::forward<THead1>(head1),
332 std::forward<THead2>(head2),
333 std::forward<TTail>(tail)...);
339 inline std::string
join(
const std::string& seperator,
const std::string& s) {
345 template <
typename Iterable>
346 inline std::string
join(
const std::string& separator,
const Iterable& i)
349 typename Iterable::const_iterator iterator = i.begin();
350 while (iterator != i.end()) {
352 if (++iterator != i.end()) {
361 const std::string& s,
362 const char openBracket,
363 const char closeBracket)
366 for (
size_t i = 0; i < s.length(); i++) {
367 if (s[i] == openBracket) {
369 }
else if (s[i] == closeBracket) {
382 return s.size() >= prefix.size() &&
383 std::equal(prefix.begin(), prefix.end(), s.begin());
389 return !s.empty() && s.front() == c;
393 inline bool endsWith(
const std::string& s,
const std::string& suffix)
395 return s.size() >= suffix.size() &&
396 std::equal(suffix.rbegin(), suffix.rend(), s.rbegin());
402 return !s.empty() && s.back() == c;
406 inline bool contains(
const std::string& s,
const std::string& substr)
408 return s.find(substr) != std::string::npos;
412 inline std::string
lower(
const std::string& s)
414 std::string result = s;
415 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
420 inline std::string
upper(
const std::string& s)
422 std::string result = s;
423 std::transform(result.begin(), result.end(), result.begin(), ::toupper);
429 #endif // __STOUT_STRINGS_HPP__
bool endsWith(const std::string &s, const std::string &suffix)
Definition: strings.hpp:393
Try< Nothing > equal(const SlaveInfo &previous, const SlaveInfo ¤t)
std::stringstream & join(std::stringstream &stream, const std::string &separator, T &&...args)
Definition: strings.hpp:306
std::stringstream & append(std::stringstream &stream, const std::string &value)
Definition: strings.hpp:243
constexpr const char * prefix
Definition: os.hpp:94
Try< Nothing > start(const std::string &name)
Starts the slice with the given name (via 'systemctl start <name>').
std::map< std::string, std::vector< std::string > > pairs(const std::string &s, const std::string &delims1, const std::string &delims2)
Definition: strings.hpp:222
bool isSome() const
Definition: option.hpp:115
std::vector< std::string > tokenize(const std::string &s, const std::string &delims, const Option< size_t > &maxTokens=None())
Definition: strings.hpp:138
Result< int > index(const std::string &link)
bool contains(const std::string &s, const std::string &substr)
Definition: strings.hpp:406
Definition: strings.hpp:35
std::stringstream & join(std::stringstream &stream, const std::string &separator, T &&tail)
Definition: strings.hpp:281
const std::string WHITESPACE
Definition: strings.hpp:29
const T & get() const &
Definition: option.hpp:118
std::string replace(const std::string &s, const std::string &from, const std::string &to)
Definition: strings.hpp:112
process::Future< Nothing > transform(process::Owned< Reader< T >> &&reader, const std::function< std::string(const T &)> &func, process::http::Pipe::Writer writer)
This is a helper function that reads records from a Reader, applies a transformation to the records a...
Definition: recordio.hpp:112
std::string upper(const std::string &s)
Definition: strings.hpp:420
Mode
Definition: strings.hpp:32
Try< mode_t > mode(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:126
std::vector< std::string > split(const std::string &s, const std::string &delims, const Option< size_t > &maxTokens=None())
Definition: strings.hpp:183
std::string stringify(int flags)
bool startsWith(const std::string &s, const std::string &prefix)
Definition: strings.hpp:380
Definition: strings.hpp:36
std::string lower(const std::string &s)
Definition: strings.hpp:412
std::string trim(const std::string &from, Mode mode=ANY, const std::string &chars=WHITESPACE)
Definition: strings.hpp:66
bool checkBracketsMatching(const std::string &s, const char openBracket, const char closeBracket)
Definition: strings.hpp:360
Definition: strings.hpp:34