13 #ifndef __STOUT_STRINGS_HPP__ 14 #define __STOUT_STRINGS_HPP__ 41 inline std::string
remove(
42 const std::string& from,
43 const std::string& substring,
46 std::string result = from;
49 if (from.find(substring) == 0) {
50 result = from.substr(substring.size());
52 }
else if (mode ==
SUFFIX) {
53 if (from.rfind(substring) == from.size() - substring.size()) {
54 result = from.substr(0, from.size() - substring.size());
58 while ((index = result.find(substring)) != std::string::npos) {
59 result = result.erase(index, substring.size());
68 const std::string& from,
70 const std::string& chars = WHITESPACE)
76 start = from.find_first_not_of(chars);
77 end = from.find_last_not_of(chars);
78 }
else if (mode ==
PREFIX) {
79 start = from.find_first_not_of(chars);
80 }
else if (mode ==
SUFFIX) {
81 end = from.find_last_not_of(chars);
85 if (start == std::string::npos) {
91 size_t length = std::string::npos;
94 if (end.
isSome() && end.
get() != std::string::npos) {
98 return from.substr(start, length);
105 const std::string& from,
106 const std::string& chars)
114 const std::string& s,
115 const std::string& from,
116 const std::string& to)
118 std::string result = s;
125 while ((index = result.find(from, index)) != std::string::npos) {
126 result.replace(index, from.length(), to);
127 index += to.length();
140 const std::string& s,
141 const std::string& delims,
144 if (maxTokens.isSome() && maxTokens.get() == 0) {
148 std::vector<std::string> tokens;
152 size_t nonDelim = s.find_first_not_of(delims, offset);
154 if (nonDelim == std::string::npos) {
158 size_t delim = s.find_first_of(delims, nonDelim);
162 if (delim == std::string::npos ||
163 (maxTokens.isSome() && tokens.size() == maxTokens.get() - 1)) {
164 tokens.push_back(s.substr(nonDelim));
168 tokens.push_back(s.substr(nonDelim, delim - nonDelim));
184 inline std::vector<std::string>
split(
185 const std::string& s,
186 const std::string& delims,
189 if (maxTokens.isSome() && maxTokens.get() == 0) {
193 std::vector<std::string> tokens;
197 size_t next = s.find_first_of(delims, offset);
201 if (next == std::string::npos ||
202 (maxTokens.isSome() && tokens.size() == maxTokens.get() - 1)) {
203 tokens.push_back(s.substr(offset));
207 tokens.push_back(s.substr(offset, next - offset));
223 inline std::map<std::string, std::vector<std::string>>
pairs(
224 const std::string& s,
225 const std::string& delims1,
226 const std::string& delims2)
228 std::map<std::string, std::vector<std::string>> result;
230 const std::vector<std::string> tokens =
tokenize(s, delims1);
231 foreach (
const std::string& token, tokens) {
232 const std::vector<std::string>
pairs =
tokenize(token, delims2);
233 if (pairs.size() == 2) {
234 result[pairs[0]].push_back(pairs[1]);
245 std::stringstream& stream,
246 const std::string& value)
254 std::stringstream& stream,
263 std::stringstream& stream,
271 template <
typename T>
273 std::stringstream& stream,
276 stream << ::stringify(std::forward<T>(value));
281 template <
typename T>
283 std::stringstream& stream,
284 const std::string& separator,
287 return append(stream, std::forward<T>(tail));
291 template <
typename THead,
typename... TTail>
293 std::stringstream& stream,
294 const std::string& separator,
298 append(stream, std::forward<THead>(head)) << separator;
306 template <
typename... T>
308 std::stringstream& stream,
309 const std::string& separator,
321 template <
typename THead1,
typename THead2,
typename... TTail>
323 const std::string& separator,
328 std::stringstream stream;
332 std::forward<THead1>(head1),
333 std::forward<THead2>(head2),
334 std::forward<TTail>(tail)...);
340 inline std::string
join(
const std::string& seperator,
const std::string& s) {
346 template <
typename Iterable>
347 inline std::string
join(
const std::string& separator,
const Iterable& i)
350 typename Iterable::const_iterator iterator = i.begin();
351 while (iterator != i.end()) {
353 if (++iterator != i.end()) {
362 const std::string& s,
363 const char openBracket,
364 const char closeBracket)
367 for (
size_t i = 0; i < s.length(); i++) {
368 if (s[i] == openBracket) {
370 }
else if (s[i] == closeBracket) {
383 return s.size() >= prefix.size() &&
384 std::equal(prefix.begin(), prefix.end(), s.begin());
390 size_t len = ::strnlen(prefix, s.size() + 1);
391 return s.size() >= len &&
398 return !s.empty() && s.front() == c;
402 inline bool endsWith(
const std::string& s,
const std::string& suffix)
404 return s.size() >= suffix.size() &&
405 std::equal(suffix.rbegin(), suffix.rend(), s.rbegin());
409 inline bool endsWidth(
const std::string& s,
const char* suffix)
411 size_t len = ::strnlen(suffix, s.size() + 1);
412 return s.size() >= len &&
419 return !s.empty() && s.back() == c;
423 inline bool contains(
const std::string& s,
const std::string& substr)
425 return s.find(substr) != std::string::npos;
429 inline std::string
lower(
const std::string& s)
431 std::string result = s;
432 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
437 inline std::string
upper(
const std::string& s)
439 std::string result = s;
440 std::transform(result.begin(), result.end(), result.begin(), ::toupper);
446 #endif // __STOUT_STRINGS_HPP__ bool endsWith(const std::string &s, const std::string &suffix)
Definition: strings.hpp:402
Try< Nothing > equal(const SlaveInfo &previous, const SlaveInfo ¤t)
std::stringstream & append(std::stringstream &stream, const std::string &value)
Definition: strings.hpp:244
Definition: format.hpp:45
constexpr const char * prefix
Definition: os.hpp:96
bool endsWidth(const std::string &s, const char *suffix)
Definition: strings.hpp:409
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:223
bool isSome() const
Definition: option.hpp:116
std::vector< std::string > tokenize(const std::string &s, const std::string &delims, const Option< size_t > &maxTokens=None())
Definition: strings.hpp:139
Result< int > index(const std::string &link)
bool contains(const std::string &s, const std::string &substr)
Definition: strings.hpp:423
Definition: strings.hpp:36
std::stringstream & join(std::stringstream &stream, const std::string &separator, T &&tail)
Definition: strings.hpp:282
const std::string WHITESPACE
Definition: strings.hpp:30
const T & get() const &
Definition: option.hpp:119
std::string replace(const std::string &s, const std::string &from, const std::string &to)
Definition: strings.hpp:113
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:437
Definition: attributes.hpp:24
Mode
Definition: strings.hpp:33
Try< mode_t > mode(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:168
std::vector< std::string > split(const std::string &s, const std::string &delims, const Option< size_t > &maxTokens=None())
Definition: strings.hpp:184
std::string stringify(int flags)
bool startsWith(const std::string &s, const std::string &prefix)
Definition: strings.hpp:381
Definition: strings.hpp:37
std::string lower(const std::string &s)
Definition: strings.hpp:429
std::string trim(const std::string &from, Mode mode=ANY, const std::string &chars=WHITESPACE)
Definition: strings.hpp:67
bool checkBracketsMatching(const std::string &s, const char openBracket, const char closeBracket)
Definition: strings.hpp:361
Definition: strings.hpp:35